Cosine of Degree Values in NumPy

Manav Narula Jan 30, 2023

The numpy module has a variety of functions available to perform different mathematical operations. We can calculate trigonometric ratios using functions like `numpy.sin()`, `numpy.cos()` and `numpy.tan()` to find the sine, cosine and tangent values respectively.

For example,

``````import numpy as np

print(np.cos(0.5))
``````

Output:

``````0.8775825618903728
``````

It is worth noting that angles by default are accepted in radians and not in degrees in the above example. In this tutorial, we will learn how to calculate these trigonometric functions when the angle in degrees. For this, we will convert the angle from degree to radian within the trigonometric function.

For our examples, we will work with the `cos()` function, which returns the cosine values, but these methods will work the same for all the ratios.

Use the `numpy.radians()` Function to Use Trigonometric Functions With Angles in Degrees

The numpy module can perform conversions of angles from radians to degree and vice-versa. To convert an angle from radian to degree, we can use the `radians()` function from this module.

We convert the required angle to radian and then pass it to the trigonometric function.

For example,

``````import numpy as np

``````

Output:

``````0.7071067811865476
``````

Alternatively, we can use the `deg2rad()` function from this module, which works the same but has a more descriptive name.

Use the `math.radians` Function to Use Trigonometric Functions With Angles in Degrees

The `math.radians()` function can also be used. It converts the angle from degree to radian values.

The following code shows how:

``````import math
import numpy as np

``````

Output:

``````0.7071067811865476
``````

Note that the math module also has inbuilt functions to find trigonometric ratios. The main advantage of using the numpy module is that functions from this module can calculate the ratios for values in an array also.

Author: Manav Narula

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.