Python 中的 degrees() 和 radians()
数学中的角度测量使用这两个测量单位:度和弧度。它们经常用于涉及角度的数学计算中,并且需要从一个值转换为另一个值。在 Python 中,我们可以使用 Python 函数来实现这些转换。
degrees() 函数
此函数以弧度值作为参数,并返回度数的等效值。返回值是一个浮点数。
示例
import math # Printing degree equivalent of radians. print("1 Radians in Degrees : ",math.degrees(1)) print("20 Radians in Degrees : ",math.degrees(20)) print("180 Radians in Degrees : ",math.degrees(180))
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
输出
运行以上代码将得到以下结果−
1 Radians in Degrees : 57.29577951308232 20 Radians in Degrees : 1145.9155902616465 180 Radians in Degrees : 10313.240312354817
radians() 函数
此函数以角度值作为参数,并返回弧度数的等效值。返回值是一个浮点数。
示例
import math # Printing radian equivalent of degrees. print("1 degrees in radian : ",math.radians(1)) print("60 degrees in radian : ",math.radians(60)) print("180 degrees in radian : ",math.radians(180))
输出
运行以上代码将得到以下结果−
1 degrees in radian : 0.017453292519943295 60 degrees in radian : 1.0471975511965976 180 degrees in radian : 3.141592653589793
使用 numpy 的角度和弧度()
numpy 包还拥有可以直接将角度转换为弧度并反之亦然的内置函数。这些函数名为 deg2rad 和 rad2deg。
示例
import numpy as np # Printing radian equivalent of degrees. print("1 degrees to radian : ",np.deg2rad(1)) print("1 radian to degree : ",np.rad2deg(1))
输出
运行以上代码将得到以下结果^−
1 degrees to radian : 0.017453292519943295 1 radian to degree : 57.29577951308232
广告