弧度是数学中的一个天使单位。一个弧度的定义是:从圆心出发,截取长度与圆的半径相等的弧线所减去的角度。在处理圆或任何形状时,将弧度转换为度是最常用的转换方法之一。因此,在本教程中,我们将看到numpy degrees()函数,以及如何使用它从弧度转换为度数。
如何在Python中从弧度转换为度数
要在Python中把角度从弧度转换为度数,请使用Numpy degrees()方法。Numpy方法中的degrees()将一个角度从弧度转换为度数。因此,你需要提供任何弧度的值,degrees()函数返回该值的度数。
Numpy degrees()
Numpy degrees()是一个数学函数,用于将Python中的角度从弧度转换为度数。np.degrees()方法最多接受两个参数,并返回一个与输入数组相同大小的数组。
语法
numpy.degrees(arr[, out]) = ufunc ‘degrees’)
参数
degrees()函数最多需要两个主要参数。
返回值
degrees()函数返回一个与输入数组大小相同的数组,包含度数值以代替弧度值,但返回值将是浮点数数据类型。
使用degrees()将角度从弧度转换为度数
请看下面的代码:
#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.]
解释
在这个程序中,我们将π值(约3.14)存储在一个变量x中;我们使用x的值来存储数组的值。因此,这就是为什么我们将x的值划分为不同的度数。
所以在声明数组后,我们将所有弧度值转换成度数,然后打印出来。
寻找三角形第三角的程序
请看下面的代码:
#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
解释
在这个例子中,当提供其他两个角度值时,这个程序将计算出一个矩形的第三个角度值。我们在数组中分配了弧度值,然后打印了弧度值的数组。
然后我们将它们转换为度数。并计算这两个度数的总和。
我们知道,矩形的总角值是180度;我们用180减去总和,然后打印出第三个角的值。
这个np.degree()教程就到此为止。