Python math.degree()方法

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

语法

math.degrees(rad)

参数

rad 要转换为度的任何弧度的角度。

返回值

此方法的返回类型是一个以度为单位的角度。

例子

正确使用 Pythonmath.degrees() 方法

代码片段:

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

输出:

The conversion of -0.9 radians into degrees is -51.56620156177409.
The conversion of 3.141592653589793 radians into degrees is 180.0.
The conversion of inf radians into degrees is inf.

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

TypeError 当使用Pythonmath.degrees() 方法时

代码摘录:

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

输出:

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

上面的代码片段显示了使用math.degrees 方法可能出现的所有语法错误。

演示math.degrees() 方法和它的反例

代码片段:

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

输出:

The conversion of 7 radians into degrees is 401.07045659157626.
The conversion of 401.07045659157626 degrees into radians is 7.0.

角度可以是一个整数 (int) 或一个浮点值 (float)。