A radian is a unit of angels in mathematics. One radian is the angle subtended from the center of a circle that intercepts an arc equal in length to the circle’s radius.
Converting an angel from radian to degree is one of the most used conversions when working with a circle or any shape. So, in this tutorial, we will see the numpy degrees() function and how we can use it to convert to a degree from radian.
How to Convert Radian to Degree in Python
To convert an angle from radian to degree in Python, use the np.degrees() method. The np.degrees() function accepts a value in radians and returns that value in the degree.
The np.degrees() method takes up to two parameters and returns an array of the same size as the input array.
numpy.degrees(arr[, out]) = ufunc ‘degrees’)
The degrees() function takes up to two main parameters:
- arr: This is the array whose elements are converted in degrees.
- out: A position the result will be stored in. If given, the shape to which the inputs broadcast must be in. If the freshly-allocated array is returned unless received or None. The tuple (possible as a keyword argument only) must have a length equal to the outputs.
The degrees() function returns an array of the same size as the input array, containing a degree value inplace of a radian value, but the return value will be in float data type.
Converting angles from radians to a degree using degrees()
See the following code.
#Program to show the working of degrees() import numpy as np import math #Storing value of pi in x x = math.pi #declaring array arr = [0, x / 4, x / 3, x / 2, x] #Printing array print(arr) #Now we will convert radian values to degree arr1 = np.degrees(arr) #Printing degree values print("New array is:") print(arr1)
[0, 0.7853981633974483, 1.0471975511965976, 1.5707963267948966, 3.141592653589793] New array is: [ 0. 45. 60. 90. 180.]
In this program, we have stored the pi value (approx 3.14) in a variable x; we have stored the array’s values by using the value of x. As we know, one radian is 180/pi degrees. So that’s why we have divided the value of x to print different degrees.
So after declaring the array, we converted all radian values into degrees and printed them.
Program to find the value of 3rd angle of a triangle
See the following code.
#Given values of two angles of a triangle #We have to find the value of the 3rd angle of the triangle import numpy as np import math #Storing value of pi in x x = math.pi #declaring array arr = [x / 4, x / 2] #Printing array print(arr) #Now we will convert radian values to degree arr1 = np.degrees(arr) #Printing degree values print("New array is:") print(arr1) sum_two = np.sum(arr1) #Printing value of the third angle print("The third angle is: ", 180 - sum_two)
[0.7853981633974483, 1.5707963267948966] New array is: [45. 90.] The third angle is : 45.0
In this example, this program will calculate the third angle value of a rectangle when the other two values are provided. This is because we have assigned radian values in the array and then printed the array with radian values.
Then we converted them into degrees. And calculated the sum of these two degrees.
As we know, the total angle value of a rectangle is 180 degrees; we have subtracted the total sum from 180 and printed the value of the 3rd angle.
That is it for converting a numpy radians to a degree.