numpy.radians()Function to Use Trigonometric Functions With Angles in Degrees
math.radiansFunction to Use Trigonometric Functions With Angles in Degrees
The numpy module has a variety of functions available to perform different mathematical operations. We can calculate trigonometric ratios using functions like
numpy.tan() to find the sine, cosine and tangent values respectively.
import numpy as np print(np.cos(0.5))
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.
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.
import numpy as np print(np.cos(np.radians(45)))
Alternatively, we can use the
deg2rad() function from this module, which works the same but has a more descriptive name.
math.radians Function to Use Trigonometric Functions With Angles in Degrees
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 print(np.cos(math.radians(45)))
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.