如何使用 Python 将弧度转换为度数?
- 2025-03-07 08:59:00
- admin 原创
- 63
问题描述:
在math
模块中,我只能找到 cos/sin/tan/acos/asin/atan。它们分别以弧度为单位接受参数并返回答案。我怎样才能使用度数来代替?
这是我的代码:
import math
x = math.cos(1)
y = x * 180 / math.pi
print(y)
30.9570417874
我的计算器,在 deg 上,给出的结果为:
cos(1)
0.9998476...
解决方案 1:
Python 包中包含两个函数math
;radians
将度数转换为弧度,以及degrees
将弧度转换为度数。
为了匹配计算器的输出,您需要:
>>> math.cos(math.radians(1))
0.9998476951563913
请注意,所有三角函数都在角度和三角形两边的比率之间进行转换。cos、sin 和 tan 以弧度为单位输入角度并返回比率;acos、asin 和 atan 以比率为单位输入角度并返回弧度。您只能转换角度,而不能转换比率。
解决方案 2:
弧度是什么?它解决什么问题?:
弧度和度是两个独立的测量单位,可以帮助人们表达和传达方向的精确变化。维基百科通过图表直观地说明了弧度相对于度的对应关系:
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
使用库根据角度计算弧度的 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
数学符号:
您无需 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
可汗学院复习三角学、单位圆和角数学,使用正弦、余弦和正切来描述旋转和旋转变化。https ://www.khanacademy.org/math/algebra2/x2ec2f6f830c9fb89 :trig/x2ec2f6f830c9fb89:unit-circle/v/unit-circle-definition-of-trig-functions-1
解决方案 3:
您可以简单地将弧度结果转换为度数,方法是使用math.degrees()
并适当四舍五入到所需的小数位。
例如:
>>> round(math.degrees(math.asin(0.5)), 2)
30.0
解决方案 4:
也可以使用 NumPy 的 将弧度转换为度数rad2deg()
。像这样:
>>> import numpy as np
>>> print(np.rad2deg(1))
57.29577951308232
如果需要对结果进行四舍五入,则使用 NumPy 的round()
。在下面的示例中,我将其四舍五入到小数点后 6 位。
>>> print(np.round(np.rad2deg(1), 6)
57.29578
解决方案 5:
我还喜欢定义自己的函数,这些函数以度数而不是弧度为单位接受和返回参数。我确信有些大写字母纯粹的人不喜欢我的名字,但我只是对我的自定义函数使用大写首字母。定义和测试代码如下。
#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)
不存在。
解决方案 6:
-fix- 因为您想将弧度更改为度,所以实际上是 rad=deg math.pi /180 而不是 deg180/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)
扫码咨询,免费领取项目管理大礼包!