如何使用Python将弧度转换为角度?

118
math模块中,我只能找到cos/sin/tan/acos/asin/atan这些函数。它们分别以弧度作为参数并返回结果。我该如何使用角度而不是弧度呢?
以下是我的代码:
import math

x = math.cos(1)
y = x * 180 / math.pi
print(y)
30.9570417874

我的计算器,以度为单位,给我的结果是:
cos(1)
0.9998476...

19
你的理解有误,cos 的输入应该是角度而非输出。 - Mark Ransom
@Mark Ransom 这有什么意义吗? - tkbx
4
@lucase.62,Mark 是正确的。cos 函数以角度为输入,在你的例子中是 1。在你的计算器上,这个角度是以度为单位的,在 Python 中,这个角度必须以弧度为单位给出。返回值,在你的例子中是 x,是一个无量纲数。在你的计算器上,你已经计算了 1 度的 cos 值。在你的 Python 示例中,你已经计算了 1 弧度的 cos 值,相当于 57.296 度。 - Stewbob
4
cos 接受一个角度作为输入,输出一个比值。尝试像你在示例中所做的那样将输出转换为度数完全没有意义。相反,你需要将输入的 1 从度数转换为弧度。如果你使用的是 acos,则情况会相反,即输入是比值,输出是弧度。 - Mark Ransom
8个回答

228

Python在math包中包含两个函数;radians将角度转换为弧度,degrees将弧度转换为角度。

为了匹配计算器的输出,您需要:

>>> math.cos(math.radians(1))
0.9998476951563913

请注意所有三角函数都是将一个角度和三角形两边的比率之间进行转换。cos、sin 和 tan 以弧度制的角度为输入并返回比率;acos、asin 和 atan 以比率为输入并返回弧度制的角度。只有角度需要进行转换,而不是比率。


2
这是一个非常棒的回复。你的例子很清晰,但是你的“额外”解释真的很有帮助。我对这个确切的事情很好奇。 - Startec
1
谢谢。作为唯一一个数学技能不好的计算机科学专业人士,这非常有帮助! - Adam Wells
4
如果有人不确定,numpy也有这些函数,可以一次性地将整个数组进行转换。它们也有更明确的名称rad2degdeg2rad - P-Gn

64
什么是弧度,它们解决了什么问题?
弧度和角度是两个不同的测量单位,帮助人们表达和传达方向上的精确变化。维基百科通过信息图形很好地解释了弧度相对于角度的定义。

https://en.wikipedia.org/wiki/Radian

Conversion from radians to degrees

Python使用库计算弧度转角度的示例:
>>> import math
>>> math.degrees(0)                       #0 radians == 0 degrees
0.0
>>> math.degrees(math.pi/2)               #pi/2 radians is 90 degrees
90.0
>>> math.degrees(math.pi)                 #pi radians is 180 degrees
180.0      
>>> math.degrees(math.pi+(math.pi/2))     #pi+pi/2 radians is 270 degrees
270.0 
>>> math.degrees(math.pi+math.pi)         #2*pi radians is 360 degrees
360.0      

Python使用库计算弧度与角度的示例:
>>> import math
>>> math.radians(0)           #0 degrees == 0 radians
0.0
>>> math.radians(90)          #90 degrees is pi/2 radians
1.5707963267948966
>>> math.radians(180)         #180 degrees is pi radians
3.141592653589793
>>> math.radians(270)         #270 degrees is pi+(pi/2) radians
4.71238898038469
>>> math.radians(360)         #360 degrees is 2*pi radians
6.283185307179586

来源: https://docs.python.org/3/library/math.html#angular-conversion

数学符号:

Mathematical notation of degrees and radians

你可以在没有Python库的情况下进行度/弧度转换:
如果你自己编写度/弧度转换器,你必须编写自己的代码来处理边界情况。
在这里犯错误很容易,并且会像1999年火星轨道器的开发人员一样受到伤害,他们因为一个不直观的边界情况而将其撞向火星,造成了1.25亿美元的损失。
>>> 0 * 180.0 / math.pi                         #0 radians is 0 degrees
0.0
>>> (math.pi/2) * 180.0 / math.pi               #pi/2 radians is 90 degrees
90.0
>>> (math.pi) * 180.0 / math.pi                 #pi radians is 180 degrees
180.0
>>> (math.pi+(math.pi/2)) * 180.0 / math.pi     #pi+(pi/2) radians is 270 degrees
270.0
>>> (2 * math.pi) * 180.0 / math.pi             #2*pi radians is 360 degrees
360.0

度数转换为弧度:
>>> 0 * math.pi / 180.0              #0 degrees in radians
0.0
>>> 90 * math.pi / 180.0             #90 degrees in radians
1.5707963267948966
>>> 180 * math.pi / 180.0            #180 degrees in radians
3.141592653589793
>>> 270 * math.pi / 180.0            #270 degrees in radians
4.71238898038469
>>> 360 * math.pi / 180.0            #360 degrees in radians
6.283185307179586

用度和弧度表示多次旋转
单次旋转的有效弧度值介于0和2*pi之间。单次旋转的度数值介于0和360之间。然而,如果你想表示多次旋转,有效的弧度和度数值介于0和无穷大之间。
>>> import math
>>> math.radians(360)                 #one complete rotation
6.283185307179586
>>> math.radians(360+360)             #two rotations
12.566370614359172
>>> math.degrees(12.566370614359172)  #math.degrees and math.radians preserve the
720.0                                 #number of rotations

合并多个旋转:
您可以通过对一个旋转值进行取模,将多个度数/弧度旋转合并为一个旋转。对于度数,取模运算为360,对于弧度,取模运算为2*pi。
>>> import math
>>> math.radians(720+90)        #2 whole rotations plus 90 is 14.14 radians
14.137166941154069
>>> math.radians((720+90)%360)  #14.1 radians brings you to 
1.5707963267948966              #the endpoint as 1.57 radians.

>>> math.degrees((2*math.pi)+(math.pi/2))            #one rotation plus a quarter 
450.0                                                #rotation is 450 degrees.
>>> math.degrees(((2*math.pi)+(math.pi/2))%(2*math.pi)) #one rotation plus a quarter
90.0                                                    #rotation brings you to 90.

基本教育关于弧度和角度
使用三角学和旋转表达式进行5分钟的复习,将弧度转换为角度,反之亦然:https://youtu.be/ovLbCvq7FNA?t=31 Khan Academy的三角学复习,单位圆和角度数学,使用正弦、余弦和正切来描述旋转和旋转变化。https://www.khanacademy.org/math/algebra2/x2ec2f6f830c9fb89:trig/x2ec2f6f830c9fb89:unit-circle/v/unit-circle-definition-of-trig-functions-1

21
你可以通过使用math.degrees()将弧度结果简单地转换为度数,并根据需要的小数位数进行适当的四舍五入。
例如:
>>> round(math.degrees(math.asin(0.5)), 2)
30.0

1
为了匹配问题中的示例,请使用“math.cos(math.radians(1))”。 - Mark Ransom
为什么在将弧度传递给math.degrees之前要将其传递给asin?你的做法会导致即使是有效的弧度值也会出现“ValueError:math domain error”。 - Eric Leschinski

4
弧度也可以通过使用numpy转换为度数
print(np.rad2deg(1))
57.29577951308232

如果需要四舍五入(我在下面保留了小数点后6位),那么:
print(np.round(np.rad2deg(1), 6)

57.29578

2

我也喜欢定义自己的函数,使用角度作为参数和返回值,而不是弧度。我知道有些人可能不喜欢我的命名方式,但我只是在自定义函数的第一个字母大写。下面是定义和测试代码。

#Definitions for trig functions using degrees.
def Cos(a):
    return cos(radians(a))
def Sin(a):
    return sin(radians(a))
def Tan(a):
    return tan(radians(a))
def ArcTan(a):
    return degrees(arctan(a))
def ArcSin(a):
    return degrees(arcsin(a))
def ArcCos(a):
    return degrees(arccos(a))

#Testing Code
print(Cos(90))
print(Sin(90))
print(Tan(45))
print(ArcTan(1))
print(ArcSin(1))
print(ArcCos(0))

请注意,我已经使用以下方式将math(或numpy)导入命名空间中:
from math import *

请注意,我的函数在定义它们的命名空间中。例如,
math.Cos(45)

不存在。


1

-修正- 因为你想要从弧度转换为角度,实际上应该是 rad=deg * math.pi /180 而不是 deg*180/math.pi

import math
x=1                # in deg
x = x*math.pi/180  # convert to rad
y = math.cos(x)    # calculate in rad

print y

在一行里可以这样写。
y=math.cos(1*math.pi/180)

1
永远不要在整数除法中除以一个整数(在这种情况下),即使有时候这样做是有用的,也有更明确的写法。 - fuesika

0

我喜欢这种方法,使用sind(x)cosd(x)

import math

def sind(x):
    return math.sin(math.radians(x))

def cosd(x):
    return math.cos(math.radians(x))

0
要在Python中将弧度转换为角度,您可以使用math模块中的degrees()函数。以下是您可以执行此操作的方法:
# Define the angle in radians
angle_in_radians = 1.5708  # Replace this with your radian value

# Use the degrees() function to convert to degrees
angle_in_degrees = math.degrees(angle_in_radians)

# Print the result
print(f"{angle_in_radians} radians is equal to {angle_in_degrees} degrees.")

在这个例子中,我们首先导入了math模块,它提供了Python中的数学函数和常量。然后,我们定义了以弧度为单位的角度(用你的弧度值替换1.5708)。接下来,我们使用math.degrees()函数将弧度值转换为角度。最后,我们打印结果,显示角度的弧度和度数。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接