math --- 數(shù)學(xué)函數(shù)?


該模塊提供了對C標(biāo)準(zhǔn)定義的數(shù)學(xué)函數(shù)的訪問。

這些函數(shù)不適用于復(fù)數(shù);如果你需要計(jì)算復(fù)數(shù),請使用 cmath 模塊中的同名函數(shù)。將支持計(jì)算復(fù)數(shù)的函數(shù)區(qū)分開的目的,來自于大多數(shù)開發(fā)者并不愿意像數(shù)學(xué)家一樣需要學(xué)習(xí)復(fù)數(shù)的概念。得到一個(gè)異常而不是一個(gè)復(fù)數(shù)結(jié)果使得開發(fā)者能夠更早地監(jiān)測到傳遞給這些函數(shù)的參數(shù)中包含復(fù)數(shù),進(jìn)而調(diào)查其產(chǎn)生的原因。

該模塊提供了以下函數(shù)。除非另有明確說明,否則所有返回值均為浮點(diǎn)數(shù)。

數(shù)論與表示函數(shù)?

math.ceil(x)?

Return the ceiling of x, the smallest integer greater than or equal to x. If x is not a float, delegates to x.__ceil__, which should return an Integral value.

math.comb(n, k)?

返回不重復(fù)且無順序地從 n 項(xiàng)中選擇 k 項(xiàng)的方式總數(shù)。

當(dāng) k <= n 時(shí)取值為 n! / (k! * (n - k)!);當(dāng) k > n 時(shí)取值為零。

也稱為二項(xiàng)式系數(shù),因?yàn)樗葍r(jià)于表達(dá)式 (1 + x) ** n 的多項(xiàng)式展開中第 k 項(xiàng)的系數(shù)。

如果任一參數(shù)不為整數(shù)則會引發(fā) TypeError。 如果任一參數(shù)為負(fù)數(shù)則會引發(fā) ValueError。

3.8 新版功能.

math.copysign(x, y)?

返回一個(gè)基于 x 的絕對值和 y 的符號的浮點(diǎn)數(shù)。在支持帶符號零的平臺上,copysign(1.0, -0.0) 返回 -1.0.

math.fabs(x)?

返回 x 的絕對值。

math.factorial(n)?

Return n factorial as an integer. Raises ValueError if n is not integral or is negative.

3.9 版后已移除: 接受具有整數(shù)值的浮點(diǎn)數(shù) (例如 5.0) 的行為已被棄用。

math.floor(x)?

Return the floor of x, the largest integer less than or equal to x. If x is not a float, delegates to x.__floor__, which should return an Integral value.

math.fmod(x, y)?

返回 fmod(x, y) ,由平臺C庫定義。請注意,Python表達(dá)式 x % y 可能不會返回相同的結(jié)果。C標(biāo)準(zhǔn)的目的是 fmod(x, y) 完全(數(shù)學(xué)上;到無限精度)等于 x - n*y 對于某個(gè)整數(shù) n ,使得結(jié)果具有 與 x 相同的符號和小于 abs(y) 的幅度。Python的 x % y 返回帶有 y 符號的結(jié)果,并且可能不能完全計(jì)算浮點(diǎn)參數(shù)。 例如, fmod(-1e-100, 1e100)-1e-100 ,但Python的 -1e-100 % 1e100 的結(jié)果是 1e100-1e-100 ,它不能完全表示為浮點(diǎn)數(shù),并且取整為令人驚訝的 1e100 。 出于這個(gè)原因,函數(shù) fmod() 在使用浮點(diǎn)數(shù)時(shí)通常是首選,而Python的 x % y 在使用整數(shù)時(shí)是首選。

math.frexp(x)?

(m, e) 對的形式返回 x 的尾數(shù)和指數(shù)。 m 是一個(gè)浮點(diǎn)數(shù), e 是一個(gè)整數(shù),正好是 x == m * 2**e 。 如果 x 為零,則返回 (0.0, 0) ,否則返回 0.5 <= abs(m) < 1 。這用于以可移植方式“分離”浮點(diǎn)數(shù)的內(nèi)部表示。

math.fsum(iterable)?

返回迭代中的精確浮點(diǎn)值。通過跟蹤多個(gè)中間部分和來避免精度損失:

>>>
>>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
0.9999999999999999
>>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
1.0

該算法的準(zhǔn)確性取決于IEEE-754算術(shù)保證和舍入模式為半偶的典型情況。在某些非Windows版本中,底層C庫使用擴(kuò)展精度添加,并且有時(shí)可能會使中間和加倍,導(dǎo)致它在最低有效位中關(guān)閉。

有關(guān)待進(jìn)一步討論和兩種替代方法,參見 ASPN cookbook recipes for accurate floating point summation。

math.gcd(*integers)?

返回給定的整數(shù)參數(shù)的最大公約數(shù)。 如果有一個(gè)參數(shù)非零,則返回值將是能同時(shí)整除所有參數(shù)的最大正整數(shù)。 如果所有參數(shù)為零,則返回值為 0。 不帶參數(shù)的 gcd() 返回 0。

3.5 新版功能.

在 3.9 版更改: 添加了對任意數(shù)量的參數(shù)的支持。 之前的版本只支持兩個(gè)參數(shù)。

math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)?

ab 的值比較接近則返回 True,否則返回 False。

根據(jù)給定的絕對和相對容差確定兩個(gè)值是否被認(rèn)為是接近的。

rel_tol 是相對容差 —— 它是 ab 之間允許的最大差值,相對于 ab 的較大絕對值。例如,要設(shè)置5%的容差,請傳遞 rel_tol=0.05 。默認(rèn)容差為 1e-09,確保兩個(gè)值在大約9位十進(jìn)制數(shù)字內(nèi)相同。 rel_tol 必須大于零。

abs_tol 是最小絕對容差 —— 對于接近零的比較很有用。 abs_tol 必須至少為零。

如果沒有錯(cuò)誤發(fā)生,結(jié)果將是: abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

IEEE 754特殊值 NaN , inf-inf 將根據(jù)IEEE規(guī)則處理。具體來說, NaN 不被認(rèn)為接近任何其他值,包括 NaN 。 inf-inf 只被認(rèn)為接近自己。

3.5 新版功能.

參見

PEP 485 —— 用于測試近似相等的函數(shù)

math.isfinite(x)?

如果 x 既不是無窮大也不是NaN,則返回 True ,否則返回 False 。 (注意 0.0 被認(rèn)為 有限的。)

3.2 新版功能.

math.isinf(x)?

如果 x 是正或負(fù)無窮大,則返回 True ,否則返回 False

math.isnan(x)?

如果 x 是 NaN(不是數(shù)字),則返回 True ,否則返回 False

math.isqrt(n)?

返回非負(fù)整數(shù) n 的整數(shù)平方根。 這就是對 n 的實(shí)際平方根向下取整,或者相當(dāng)于使得 a2 ≤ n 的最大整數(shù) a。

對于某些應(yīng)用來說,可以更適合取值為使得 n ≤ a2 的最小整數(shù) a ,或者換句話說就是 n 的實(shí)際平方根向上取整。 對于正數(shù) n,這可以使用 a = 1 + isqrt(n - 1) 來計(jì)算。

3.8 新版功能.

math.lcm(*integers)?

返回給定的整數(shù)參數(shù)的最小公倍數(shù)。 如果所有參數(shù)均非零,則返回值將是為所有參數(shù)的整數(shù)倍的最小正整數(shù)。 如果參數(shù)之一為零,則返回值為 0。 不帶參數(shù)的 lcm() 返回 1。

3.9 新版功能.

math.ldexp(x, i)?

返回 x * (2**i) 。 這基本上是函數(shù) frexp() 的反函數(shù)。

math.modf(x)?

返回 x 的小數(shù)和整數(shù)部分。兩個(gè)結(jié)果都帶有 x 的符號并且是浮點(diǎn)數(shù)。

math.nextafter(x, y)?

返回 x 趨向于 y 的最接近的浮點(diǎn)數(shù)值。

如果 x 等于 y 則返回 y。

示例:

  • math.nextafter(x, math.inf) 的方向朝上:趨向于正無窮。

  • math.nextafter(x, -math.inf) 的方向朝下:趨向于負(fù)無窮。

  • math.nextafter(x, 0.0) 趨向于零。

  • math.nextafter(x, math.copysign(math.inf, x)) 趨向于零的反方向。

另請參閱 math.ulp()。

3.9 新版功能.

math.perm(n, k=None)?

返回不重復(fù)且有順序地從 n 項(xiàng)中選擇 k 項(xiàng)的方式總數(shù)。

當(dāng) k <= n 時(shí)取值為 n! / (n - k)!;當(dāng) k > n 時(shí)取值為零。

如果 k 未指定或?yàn)?None,則 k 默認(rèn)值為 n 并且函數(shù)將返回 n!。

如果任一參數(shù)不為整數(shù)則會引發(fā) TypeError。 如果任一參數(shù)為負(fù)數(shù)則會引發(fā) ValueError。

3.8 新版功能.

math.prod(iterable, *, start=1)?

計(jì)算輸入的 iterable 中所有元素的積。 積的默認(rèn) start 值為 1

當(dāng)可迭代對象為空時(shí),返回起始值。 此函數(shù)特別針對數(shù)字值使用,并會拒絕非數(shù)字類型。

3.8 新版功能.

math.remainder(x, y)?

返回 IEEE 754 風(fēng)格的 x 相對于 y 的余數(shù)。對于有限 x 和有限非零 y ,這是差異 x - n*y ,其中 n 是與商 x / y 的精確值最接近的整數(shù)。如果 x / y 恰好位于兩個(gè)連續(xù)整數(shù)之間,則將最接近的 偶數(shù) 用作 n 。 余數(shù) r = remainder(x, y) 因此總是滿足 abs(r) <= 0.5 * abs(y)。

特殊情況遵循IEEE 754:特別是 remainder(x, math.inf) 對于任何有限 x 都是 x ,而 remainder(x, 0)remainder(math.inf, x) 引發(fā) ValueError 適用于任何非NaN的 x 。如果余數(shù)運(yùn)算的結(jié)果為零,則該零將具有與 x 相同的符號。

在使用IEEE 754二進(jìn)制浮點(diǎn)的平臺上,此操作的結(jié)果始終可以完全表示:不會引入舍入錯(cuò)誤。

3.7 新版功能.

math.trunc(x)?

Return x with the fractional part removed, leaving the integer part. This rounds toward 0: trunc() is equivalent to floor() for positive x, and equivalent to ceil() for negative x. If x is not a float, delegates to x.__trunc__, which should return an Integral value.

math.ulp(x)?

返回浮點(diǎn)數(shù) x 的最小有效比特位的值:

  • 如果 x 是 NaN (非數(shù)字),則返回 x

  • 如果 x 為負(fù)數(shù),則返回 ulp(-x)。

  • 如果 x 為正數(shù),則返回 x。

  • 如果 x 等于零,則返回 去正規(guī)化的 可表示最小正浮點(diǎn)數(shù) (小于 正規(guī)化的 最小正浮點(diǎn)數(shù) sys.float_info.min)。

  • 如果 x 等于可表示最大正浮點(diǎn)數(shù),則返回 x 的最低有效比特位的值,使得小于 x 的第一個(gè)浮點(diǎn)數(shù)為 x - ulp(x)

  • 在其他情況下 (x 是一個(gè)有限的正數(shù)),則返回 x 的最低有效比特位的值,使得大于 x 的第一個(gè)浮點(diǎn)數(shù)為 x + ulp(x)。

ULP 即 "Unit in the Last Place" 的縮寫。

另請參閱 math.nextafter()sys.float_info.epsilon。

3.9 新版功能.

注意 frexp()modf() 具有與它們的C等價(jià)函數(shù)不同的調(diào)用/返回模式:它們采用單個(gè)參數(shù)并返回一對值,而不是通過 '輸出形參' 返回它們的第二個(gè)返回參數(shù)(Python中沒有這樣的東西)。

對于 ceil()floor()modf() 函數(shù),請注意 所有 足夠大的浮點(diǎn)數(shù)都是精確整數(shù)。Python浮點(diǎn)數(shù)通常不超過53位的精度(與平臺C double類型相同),在這種情況下,任何浮點(diǎn) xabs(x) >= 2**52 必然沒有小數(shù)位。

冪函數(shù)與對數(shù)函數(shù)?

math.cbrt(x)?

Return the cube root of x.

3.11 新版功能.

math.exp(x)?

返回 ex 冪,其中 e = 2.718281... 是自然對數(shù)的基數(shù)。這通常比 math.e ** xpow(math.e, x) 更精確。

math.exp2(x)?

Return 2 raised to the power x.

3.11 新版功能.

math.expm1(x)?

返回 ex 次冪,減1。這里 e 是自然對數(shù)的基數(shù)。對于小浮點(diǎn)數(shù) x , exp(x) - 1 中的減法可能導(dǎo)致 significant loss of precision; expm1() 函數(shù)提供了一種將此數(shù)量計(jì)算為全精度的方法:

>>>
>>> from math import exp, expm1
>>> exp(1e-5) - 1  # gives result accurate to 11 places
1.0000050000069649e-05
>>> expm1(1e-5)    # result accurate to full precision
1.0000050000166668e-05

3.2 新版功能.

math.log(x[, base])?

使用一個(gè)參數(shù),返回 x 的自然對數(shù)(底為 e )。

使用兩個(gè)參數(shù),返回給定的 base 的對數(shù) x ,計(jì)算為 log(x)/log(base) 。

math.log1p(x)?

返回 1+x 的自然對數(shù)(以 e 為底)。 以對于接近零的 x 精確的方式計(jì)算結(jié)果。

math.log2(x)?

返回 x 以2為底的對數(shù)。這通常比 log(x, 2) 更準(zhǔn)確。

3.3 新版功能.

參見

int.bit_length() 返回表示二進(jìn)制整數(shù)所需的位數(shù),不包括符號和前導(dǎo)零。

math.log10(x)?

返回 x 底為10的對數(shù)。這通常比 log(x, 10) 更準(zhǔn)確。

math.pow(x, y)?

Return x raised to the power y. Exceptional cases follow the IEEE 754 standard as far as possible. In particular, pow(1.0, x) and pow(x, 0.0) always return 1.0, even when x is a zero or a NaN. If both x and y are finite, x is negative, and y is not an integer then pow(x, y) is undefined, and raises ValueError.

與內(nèi)置的 ** 運(yùn)算符不同, math.pow() 將其參數(shù)轉(zhuǎn)換為 float 類型。使用 ** 或內(nèi)置的 pow() 函數(shù)來計(jì)算精確的整數(shù)冪。

在 3.11 版更改: The special cases pow(0.0, -inf) and pow(-0.0, -inf) were changed to return inf instead of raising ValueError, for consistency with IEEE 754.

math.sqrt(x)?

返回 x 的平方根。

三角函數(shù)?

math.acos(x)?

返回以弧度為單位的 x 的反余弦值。 結(jié)果范圍在 0pi 之間。

math.asin(x)?

返回以弧度為單位的 x 的反正弦值。 結(jié)果范圍在 -pi/2pi/2 之間。

math.atan(x)?

返回以弧度為單位的 x 的反正切值。 結(jié)果范圍在 -pi/2pi/2 之間。.

math.atan2(y, x)?

以弧度為單位返回 atan(y / x) 。結(jié)果是在 -pipi 之間。從原點(diǎn)到點(diǎn) (x, y) 的平面矢量使該角度與正X軸成正比。 atan2() 的點(diǎn)的兩個(gè)輸入的符號都是已知的,因此它可以計(jì)算角度的正確象限。 例如, atan(1)atan2(1, 1) 都是 pi/4 ,但 atan2(-1, -1)-3*pi/4 。

math.cos(x)?

返回 x 弧度的余弦值。

math.dist(p, q)?

返回 pq 兩點(diǎn)之間的歐幾里得距離,以一個(gè)坐標(biāo)序列(或可迭代對象)的形式給出。 兩個(gè)點(diǎn)必須具有相同的維度。

大致相當(dāng)于:

sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))

3.8 新版功能.

math.hypot(*coordinates)?

返回歐幾里得范數(shù),sqrt(sum(x**2 for x in coordinates))。 這是從原點(diǎn)到坐標(biāo)給定點(diǎn)的向量長度。

對于一個(gè)二維點(diǎn) (x, y),這等價(jià)于使用畢達(dá)哥拉斯定義 sqrt(x*x + y*y) 計(jì)算一個(gè)直角三角形的斜邊。

在 3.8 版更改: 添加了對 n 維點(diǎn)的支持。 之前的版本只支持二維點(diǎn)。

在 3.10 版更改: 改進(jìn)了算法的精確性,使得最大誤差在 1 ulp (最后一位的單位數(shù)值) 以下。 更為常見的情況是,結(jié)果幾乎總是能正確地舍入到 1/2 ulp 范圍之內(nèi)。

math.sin(x)?

返回 x 弧度的正弦值。

math.tan(x)?

返回 x 弧度的正切值。

角度轉(zhuǎn)換?

math.degrees(x)?

將角度 x 從弧度轉(zhuǎn)換為度數(shù)。

math.radians(x)?

將角度 x 從度數(shù)轉(zhuǎn)換為弧度。

雙曲函數(shù)?

雙曲函數(shù) 是基于雙曲線而非圓來對三角函數(shù)進(jìn)行模擬。

math.acosh(x)?

返回 x 的反雙曲余弦值。

math.asinh(x)?

返回 x 的反雙曲正弦值。

math.atanh(x)?

返回 x 的反雙曲正切值。

math.cosh(x)?

返回 x 的雙曲余弦值。

math.sinh(x)?

返回 x 的雙曲正弦值。

math.tanh(x)?

返回 x 的雙曲正切值。

特殊函數(shù)?

math.erf(x)?

返回 x 處的 error function

erf() 函數(shù)可用于計(jì)算傳統(tǒng)的統(tǒng)計(jì)函數(shù),如 累積標(biāo)準(zhǔn)正態(tài)分布

def phi(x):
    'Cumulative distribution function for the standard normal distribution'
    return (1.0 + erf(x / sqrt(2.0))) / 2.0

3.2 新版功能.

math.erfc(x)?

返回 x 處的互補(bǔ)誤差函數(shù)。 互補(bǔ)錯(cuò)誤函數(shù) 定義為 1.0 - erf(x)。 它用于 x 的大值,從其中減去一個(gè)會導(dǎo)致 有效位數(shù)損失。

3.2 新版功能.

math.gamma(x)?

返回 x 處的 伽馬函數(shù) 值。

3.2 新版功能.

math.lgamma(x)?

返回Gamma函數(shù)在 x 絕對值的自然對數(shù)。

3.2 新版功能.

常量?

math.pi?

數(shù)學(xué)常數(shù) π = 3.141592...,精確到可用精度。

math.e?

數(shù)學(xué)常數(shù) e = 2.718281...,精確到可用精度。

math.tau?

數(shù)學(xué)常數(shù) τ = 6.283185...,精確到可用精度。Tau 是一個(gè)圓周常數(shù),等于 2π,圓的周長與半徑之比。更多關(guān)于 Tau 的信息可參考 Vi Hart 的視頻 Pi is (still) Wrong。吃兩倍多的派來慶祝 Tau 日 吧!

3.6 新版功能.

math.inf?

浮點(diǎn)正無窮大。 (對于負(fù)無窮大,使用 -math.inf 。)相當(dāng)于 float('inf') 的輸出。

3.5 新版功能.

math.nan?

A floating-point "not a number" (NaN) value. Equivalent to the output of float('nan'). Due to the requirements of the IEEE-754 standard, math.nan and float('nan') are not considered to equal to any other numeric value, including themselves. To check whether a number is a NaN, use the isnan() function to test for NaNs instead of is or ==. Example:

>>>
>>> import math
>>> math.nan == math.nan
False
>>> float('nan') == float('nan')
False
>>> math.isnan(math.nan)
True
>>> math.isnan(float('nan'))
True

在 3.11 版更改: It is now always available.

3.5 新版功能.

CPython implementation detail: math 模塊主要包含圍繞平臺C數(shù)學(xué)庫函數(shù)的簡單包裝器。特殊情況下的行為在適當(dāng)情況下遵循C99標(biāo)準(zhǔn)的附錄F。當(dāng)前的實(shí)現(xiàn)將引發(fā) ValueError 用于無效操作,如 sqrt(-1.0)log(0.0) (其中C99附件F建議發(fā)出無效操作信號或被零除), 和 OverflowError 用于溢出的結(jié)果(例如, exp(1000.0) )。除非一個(gè)或多個(gè)輸入?yún)?shù)是NaN,否則不會從上述任何函數(shù)返回NaN;在這種情況下,大多數(shù)函數(shù)將返回一個(gè)NaN,但是(再次遵循C99附件F)這個(gè)規(guī)則有一些例外,例如 pow(float('nan'), 0.0)hypot(float('nan'), float('inf')) 。

請注意,Python不會將顯式NaN與靜默NaN區(qū)分開來,并且顯式NaN的行為仍未明確。典型的行為是將所有NaN視為靜默的。

參見

cmath 模塊

這里很多函數(shù)的復(fù)數(shù)版本。