math
模塊中定義了一些最流行的數學函數。這些包括三角函數、表示函數、對數函數、角度轉換函數等。此外,本模塊中還定義了兩個數學常數。
圓周率是一個眾所周知的數學常數,定義為圓的周長與直徑之比,它的值是 3.141926535973
Example: Getting Pi Value
>>> import math
>>>math.pi
3.141592653589793
math
模塊中定義的另一個眾所周知的數學常數是 e 。它被稱為歐拉數,是自然對數的底數。它的值是 2.718281828459045。
Example: e Value
>>> import math
>>> math.e
2.718281828459045
math
模塊包含計算給定角度的各種三角比值的函數。函數(sin,cos,tan 等。)需要弧度表示的角度作為參數。另一方面,我們用來表示角度的度數。math
模塊提供兩個角度轉換功能:degrees()
和radians()
,將角度從角度轉換為弧度,反之亦然。 例如,以下語句將 30 度的角度轉換為弧度,然後再轉換回來(注意:π弧度相當於 180 度)。
Example: Math Radians and Degrees
>>> import math
>>> math.radians(30)
0.5235987755982988
>>> math.degrees(math.pi/6)
29.999999999999996
以下語句顯示了 30 度角(0.5235987755982988 弧度)的sin, cos and tan
比率:
Example: sin, cos, tan Calculation
>>> import math
>>> math.sin(0.5235987755982988)
0.49999999999999994
>>> math.cos(0.5235987755982988)
0.8660254037844387
>>> math.tan(0.5235987755982988)
0.5773502691896257
大家可能還記得sin(30)=0.5
、 、cos(30)=32
(也就是0.8660254037844387
)和tan(30)= 13
(也就是 0。5773502691896257
)。
math.log()
math.log()
方法返回給定數字的自然對數。自然對數以e
為基數計算。
Example: log
>>> import math
>>>math.log(10)
2.302585092994046
math.log10()
math.log10()
方法返回給定數字的以 10 為底的對數。它被稱為標準對數。
Example: log10
>>> import math
>>>math.log10(10)
1.0
math.exp()
math.exp()
方法將 e 提升到給定數的冪後返回一個浮點數。 換句話說,exp(x)
給e**x
。
Example: Exponent
>>> import math
>>>math.exp(10)
22026.465794806718
這可以通過指數運算符來驗證。
Example: Exponent Operator **
>>> import math
>>>math.e**10
22026.465794806703
math.pow()
math.pow()
方法接收兩個浮點參數,將第一個參數提升到第二個參數,並返回結果。換句話說,冪(4,4)相當於 4**4。
Example: Power
>>> import math
>>> math.pow(2,4)
16.0
>>> 2**4
16
math.sqrt()
math.sqrt()
方法返回給定數字的平方根。
Example: Square Root
>>> import math
>>> math.sqrt(100)
10.0
>>> math.sqrt(3)
1.7320508075688772
以下兩個函數稱為表示函數。 ceil() 函數將給定的數字近似為最小的整數,大於或等於給定的浮點數。 floor()
函數返回小於或等於給定數的最大整數。
Example: Ceil and Floor
>>> import math
>>> math.ceil(4.5867)
5
>>> math.floor(4.5687)
4
在 Python 文檔上了解更多math
模塊。***
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/246088.html