<h2>Python将弧度转换为度或度转换为弧度:</h2>
<p><strong>弧度是什么?它能解决什么问题?:</strong></p>
<p>弧度和度数是两个独立的度量单位,帮助人们表达和传达方向上的精确变化。维基百科对弧度与度数的关系有着很强的直觉:</p>
<p><a href="https://en.wikipedia.org/wiki/Radian" rel="noreferrer">https://en.wikipedia.org/wiki/Radian</a></p>
<p><a href="https://i.stack.imgur.com/xsAa3.png" rel="noreferrer"><img src="https://i.stack.imgur.com/xsAa3.png" alt="Conversion from radians to degrees"/></a></p>
<p><strong>使用库计算弧度的Python示例:</strong></p>
<pre><code>>>> 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
</code></pre>
<p><strong>使用库计算弧度的Python示例:</strong></p>
<pre><code>>>> 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
</code></pre>
<p>来源:<a href="https://docs.python.org/3/library/math.html#angular-conversion" rel="noreferrer">https://docs.python.org/3/library/math.html#angular-conversion</a></p>
<p><strong>数学符号:</strong></p>
<p><a href="https://i.stack.imgur.com/ogMhv.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ogMhv.png" alt="Mathematical notation of degrees and radians"/></a></p>
<h2>可以在不使用库的情况下进行度数/弧度转换:</h2>
<p>如果你滚动你自己的度/弧度转换器,你必须写你自己的代码来处理边缘情况。</p>
<p>这里的错误很容易犯,而且会像它伤害1999年火星轨道飞行器的开发者一样伤害他们,因为这里的非直觉边缘案例,他们投入了1.25亿美元将它撞向火星。</p>
<p><strong>让我们撞毁轨道飞行器,把我们自己的弧度旋转到度:</strong></p>
<p>无效的弧度作为输入返回垃圾输出。</p>
<pre><code>>>> 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
</code></pre>
<p><strong>角度到弧度:</strong></p>
<pre><code>>>> 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
</code></pre>
<p><strong>用度数和弧度表示多个旋转</strong></p>
<p>单次旋转的有效弧度值在0到2*pi之间。单个旋转度值介于0和360之间。但是,如果要表示多个旋转,则有效的弧度和度数值介于0和无穷大之间。</p>
<pre><code>>>> 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
</code></pre>
<p><strong>折叠多个旋转:</strong></p>
<p>通过对一个旋转的值进行修改,可以将多个度/弧度旋转折叠为一个旋转。对于360度的度数,对于弧度,模数为2*pi。</p>
<pre><code>>>> 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.
</code></pre>
<p><strong>保护</strong></p>
<p>汗学院有一些很好的内容来巩固三角和角数学的直觉:<a href="https://www.khanacademy.org/math/algebra2/trig-functions/intro-to-radians-alg2/v/introduction-to-radians" rel="noreferrer">https://www.khanacademy.org/math/algebra2/trig-functions/intro-to-radians-alg2/v/introduction-to-radians</a></p>