Python 中的 degrees() 和 radians()


數學中的角度測量使用這兩個測量單位:度和弧度。它們經常用於涉及角度的數學計算中,並且需要從一個值轉換為另一個值。在 Python 中,我們可以使用 Python 函式來實現這些轉換。

degrees() 函式

此函式以弧度值作為引數,並返回度數的等效值。返回值是一個浮點數。

示例

 即時演示

import math
# Printing degree equivalent of radians.
print("1 Radians in Degrees : ",math.degrees(1))
print("20 Radians in Degrees : ",math.degrees(20))
print("180 Radians in Degrees : ",math.degrees(180))

輸出

執行以上程式碼將得到以下結果−

1 Radians in Degrees : 57.29577951308232
20 Radians in Degrees : 1145.9155902616465
180 Radians in Degrees : 10313.240312354817

radians() 函式

此函式以角度值作為引數,並返回弧度數的等效值。返回值是一個浮點數。

示例

 即時演示

import math
# Printing radian equivalent of degrees.
print("1 degrees in radian : ",math.radians(1))
print("60 degrees in radian : ",math.radians(60))
print("180 degrees in radian : ",math.radians(180))

輸出

執行以上程式碼將得到以下結果−

1 degrees in radian : 0.017453292519943295
60 degrees in radian : 1.0471975511965976
180 degrees in radian : 3.141592653589793

使用 numpy 的角度和弧度()

numpy 包還擁有可以直接將角度轉換為弧度並反之亦然的內建函式。這些函式名為 deg2rad 和 rad2deg。

示例

 即時演示

import numpy as np
# Printing radian equivalent of degrees.
print("1 degrees to radian : ",np.deg2rad(1))
print("1 radian to degree : ",np.rad2deg(1))

輸出

執行以上程式碼將得到以下結果^−

1 degrees to radian : 0.017453292519943295
1 radian to degree : 57.29577951308232

更新於:2019-08-23

677 views

開啟您的職業生涯

完成課程並獲得認證

立即開始
廣告
© . All rights reserved.