Python:将弧度转换为度

2024-05-08 23:33:40 发布

您现在位置:Python中文网/ 问答频道 /正文

在python.org数学库中,我只能使用cos/sin/tan/acos/asin/atan找到math.cos(x)。这将以弧度返回答案。我怎样才能得到答案呢?

这是我的代码:

import math

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

我的计算器,在度上,给我:

cos(1)
0.9998476...

Tags: 答案代码orgimportpi数学mathsin
3条回答

Python将弧度转换为度或度转换为弧度:

弧度是什么?它能解决什么问题?:

弧度和度数是两个独立的度量单位,帮助人们表达和传达方向上的精确变化。维基百科对弧度与度数的关系有着很强的直觉:

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

可以在不使用库的情况下进行度数/弧度转换:

如果你滚动你自己的度/弧度转换器,你必须写你自己的代码来处理边缘情况。

这里的错误很容易犯,而且会像它伤害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 end point 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.

保护

汗学院有一些很好的内容来巩固三角和角数学的直觉:https://www.khanacademy.org/math/algebra2/trig-functions/intro-to-radians-alg2/v/introduction-to-radians

您只需使用

math.degrees并适当舍入到所需的小数位

例如

>>> round(math.degrees(math.asin(0.5)),2)
30.0
>>> 

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

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

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

注意,所有的三角函数都是在三角形的一个角度和两条边的比率之间转换的。cos、sin和tan以弧度为单位输入一个角度并返回比率;acos、as in和atan以弧度为单位输入一个比率并返回一个角度。你只转换角度,而不是比例。

相关问题 更多 >

    热门问题