Python math.radians()方法

Pythonmath.radians() 方法是将一个角度从度数转换为弧度的有效方法。请注意,1 degree = π/180 radians

Pythonmath.radians() 方法的语法

math.radians(deg)

参数

deg – 任何必须转换为弧度的角度,单位为度。

返回值

该方法返回一个代表弧度的角度的浮点值。

例1:在Python中使用math.radians() 方法

import math
x=70
value=math.radians(x)
print(f"The conversion of {x} degrees into radians is {value}.")
x=-109
value=math.radians(x)
print(f"The conversion of {x} degrees into radians is {value}.")
x=math.pi
value=math.radians(x)
print(f"The conversion of {x} degrees into radians is {value}.")
x=math.inf
value=math.radians(x)
print(f"The conversion of {x} degrees into radians is {value}.")
x=0
value=math.radians(x)
print(f"The conversion of {x} degrees into radians is {value}.")

输出:

The conversion of 70 degrees into radians is 1.2217304763960306.
The conversion of -109 degrees into radians is -1.9024088846738192.
The conversion of 3.141592653589793 degrees into radians is 0.05483113556160755.
The conversion of inf degrees into radians is inf.
The conversion of 0 degrees into radians is 0.0.

注意,这些值可以是正的,也可以是负的。这些方法用于与几何学有关的数学计算,在天文计算中也有一定的应用。

例2:使用math.radians() 方法时TypeError

import math
##entering a string
x="Hi"
value=math.radians(x)
print(f"The conversion of {x} degrees into radians is {value}.")
##entering a list
x=[1,2,3]
value=math.radians(x)
print(f"The conversion of {x} degrees into radians is {value}.")
##entering complex numbers
x=1+5j
value=math.radians(x)
print(f"The conversion of {x} degrees into radians is {value}.")

输出:

Traceback (most recent call last):
  File "main.py", line 7, in <module>
    value=math.radians(x)
TypeError: must be real number, not str
Traceback (most recent call last):
  File "main.py", line 15, in <module>
    value=math.radians(x)
TypeError: must be real number, not list
Traceback (most recent call last):
  File "main.py", line 22, in <module>
    value=math.radians(x)
TypeError: can't convert complex to float

上面的代码片断显示了使用math.radians() 方法时可能出现的所有语法错误。

例3:math.radians() 方法及其反例

import math
x=70
value=math.radians(x)
print(f"The conversion of {x} degrees into radians is {value}.")
deg=math.degrees(value)
print(f"The conversion of {value} radians into degrees is {deg}.")

输出:

The conversion of 70 degrees into radians is 1.2217304763960306.
The conversion of 1.2217304763960306 radians into degrees is 70.0.

角度可以是一个整数或浮点值。