在python.org数学库中,我只能找到math.cos(x)cos/sin/tan/acos/asin/atan.这将以弧度返回答案.我怎样才能获得学位答案?
这是我的代码:
import math
x = math.cos(1)
y = x * 180 / math.pi
print y
30.9570417874
Run Code Online (Sandbox Code Playgroud)
我的计算器在deg上给了我:
cos(1)
0.9998476...
Run Code Online (Sandbox Code Playgroud)
Mar*_*som 128
Python在math包中包含两个函数; radians将度数转换为弧度,degrees并将弧度转换为度数.
要匹配计算器的输出,您需要:
>>> math.cos(math.radians(1))
0.9998476951563913
Run Code Online (Sandbox Code Playgroud)
请注意,所有trig函数都在角度和三角形两边的比率之间进行转换.cos,sin和tan以弧度为角度作为输入并返回比率; acos,asin和atan将比率作为输入并以弧度返回角度.您只能转换角度,而不是比率.
Eri*_*ski 28
Radians是什么,它解决了什么问题?:
弧度和度数是两个独立的度量单位,可以帮助人们表达和传达方向的精确变化.维基百科对于他们的信息图表有一些很好的直觉,关于如何相对于度数定义一个Radian:
https://en.wikipedia.org/wiki/Radian
使用库计算弧度度的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
Run Code Online (Sandbox Code Playgroud)
使用库从度数计算弧度的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
Run Code Online (Sandbox Code Playgroud)
资料来源:https://docs.python.org/3/library/math.html#angular-conversion
数学符号:
如果你滚动自己的度/弧度转换器,你必须编写自己的代码来处理边缘情况.
这里的错误很容易造成伤害,就像它伤害了1999年火星轨道器的开发者一样,因为这里的非直观边缘情况,他们沉没了1.25亿美元将其撞到火星上.
让这个轨道飞行器崩溃并将我们自己的Radians滚动到Degrees:
无效弧度作为输入返回垃圾输出.
>>> 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
Run Code Online (Sandbox Code Playgroud)
度数到弧度:
>>> 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
Run Code Online (Sandbox Code Playgroud)
用度和弧度表示多次旋转
单旋转有效弧度值介于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
Run Code Online (Sandbox Code Playgroud)
折叠多次旋转:
您可以通过对一个旋转的值进行修改,将多度/弧度旋转折叠为单个旋转.对于你用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.
Run Code Online (Sandbox Code Playgroud)
专家提示
汗学院有一些很好的内容来巩固关于三角学和角度数学的直觉:https://www.khanacademy.org/math/algebra2/trig-functions/intro-to-radians-alg2/v/introduction-to-radians
Abh*_*jit 18
您可以使用简单地将弧度结果转换为度数
math.degrees并适当舍入到所需的小数位
例如
>>> round(math.degrees(math.asin(0.5)),2)
30.0
>>>
Run Code Online (Sandbox Code Playgroud)
弧度也可以使用 NumPy 转换为度数rad2deg()。像这样:
>>> import numpy as np
>>> print(np.rad2deg(1))
57.29577951308232
Run Code Online (Sandbox Code Playgroud)
如果需要对结果进行四舍五入,则使用 NumPy 的round(). 在下面的示例中,我四舍五入到小数点后 6 位。
>>> print(np.round(np.rad2deg(1), 6)
57.29578
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
190178 次 |
| 最近记录: |