標籤:ack eal 常用 lin std recent module truncate 實現
Python標準庫mathmath所提供的數學常量
pi |
數學常量 pi,所屬的變數空間為標準庫math |
e |
數學常量 e,e即自然常數,所屬的變數空間為標準庫math |
math庫中常用的函數三角函數
函數名 |
格式 |
功能 |
sin |
sin(x) |
返回的x弧度 的正弦值 |
cos |
cos(x) |
返回的x弧度 的餘弦值 |
tan |
tan(x) |
返回的x弧度 的正切值 |
asin |
asin(x) |
返回x 的反正弦弧度值 。 |
acos |
acos(x) |
返回x 的反餘弦弧度值 。 |
atan |
atan(x) |
返回x 的反正切弧度值 。 |
atan2 |
atan2(x,y) |
返回給定的 X 及Y 座標值的反正切值。 |
degrees |
degrees(x) |
degrees() 將弧度x轉換為角度。 |
radians |
radians(x) |
返回一個角度 的弧度值 。 |
數值函數
函數名 |
格式 |
功能 |
exp |
exp( x ) |
返回x的指數\(e^x\)。 |
pow |
pow(x,y) |
pow() 方法返回(x的y次方) 的值。 |
sqrt |
sqrt(x) |
sqrt() 方法返回數字x的平方根。 |
hypot |
hypot(x, y) |
hypot() 返回歐幾裡德範數 sqrt(x*x + y*y) 。 |
modf |
modf(x) |
modf() 方法返回x的整數部分 與小數部分 |
fabs |
fabs(x) |
fabs() 方法返回數字x的絕對值 |
log |
log(x) |
log() 方法返回x 的自然對數 。 |
log10 |
log10(x) |
log10()方法返回以10為基數的x對數。 |
copysign |
copysign(x,y) |
返回模為|x| ,符號為sign(y) 的數值 |
factorial |
factorial(x) |
計算x! |
fmod |
fmod(x,y) |
x對y求模 ,實現是以C庫 為底,返回更為精確的浮點數 |
frexp |
frexp(x) |
返回一個二元組,分別是x的指數部分 與尾數部分 (m, e) ,
x == m * 2**e |
fsum |
fsum(x) |
返回一個求和後得到的浮點數 。 |
其他的函數
x 是不是正負無窮大.
x是不是NaN(不是一個數字),
Return x * (2**i). 和frexp()相反.
Return x的整數部分(truncated 截斷)int(x)
Return e**x - 1. x非常小的時候使用,會更精確。
Return log(1+x)以e為底,x近似於0時更準確
(雙曲線方法)Hyperbolic functions
Return the inverse hyperbolic cosine of x.
Return the inverse hyperbolic sine of x.
Return the inverse hyperbolic tangent of x.
Return the hyperbolic cosine of x.
Return the hyperbolic sine of x.
Return the hyperbolic tangent of x.
(特殊方法)Special functions
math.erf(x)
math.erfc(x)
math.gamma(x)
math.lgamma(x)
Python標準庫cmath
標準庫math缺乏對於複數的操作,這時候需要引入另外的一個Python內建的標準庫cmath。
如下:
>>> import math >>> math.sqrt(-1) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: math domain error >>> import cmath >>> cmath.sqrt(-1) 1j
可以看到math.sqrt傳入的參數為負數時,因為操作範圍在實數會報錯;cmath.sqrt操作範圍在複數所以傳入參數為-1返回一個虛數。
cmath擁有與math相同絕大多數函數,只是操作範圍在複數域內;以下仍有幾個函數值得記憶:
equal to math.atan2(x.imag, x.real).
equal to (abs(x), phase(x)).
equal to r * (math.cos(phi) + math.sin(phi)*1j).
Return (x.imag+x.real*j)
Python STL math&cmath