math — 数学函数


此模块提供对 C 标准定义数学函数的访问。

这些函数无法用于复数;使用的同名函数来自 cmath 模块若要求支持复数。支持复数和那些不支持复数的函数之间的区别,是由于大多数用户不想学习理解复数要求的那么多数学知识。接收异常而不是复杂结果允许更早地检测用作参数的意外复数,所以,程序员可以确定它最初是如何生成的及为什么生成。

此模块提供下列函数。除另有明确说明外,所有返回值是浮点数。

Number-theoretic functions

comb(n, k)

Number of ways to choose k items from n items without repetition and without order

factorial(n)

n factorial

gcd(*integers)

Greatest common divisor of the integer arguments

isqrt(n)

Integer square root of a nonnegative integer n

lcm(*integers)

Least common multiple of the integer arguments

perm(n, k)

Number of ways to choose k items from n items without repetition and with order

Floating point arithmetic

ceil(x)

Ceiling of x ,最小整数 >= x

fabs(x)

Absolute value of x

floor(x)

Floor of x , the largest integer less than or equal to x

fma(x, y, z)

Fused multiply-add operation: (x * y) + z

fmod(x, y)

Remainder of division x / y

modf(x)

Fractional and integer parts of x

remainder(x, y)

Remainder of x with respect to y

trunc(x)

Integer part of x

Floating point manipulation functions

copysign(x, y)

Magnitude (absolute value) of x with the sign of y

frexp(x)

Mantissa and exponent of x

isclose(a, b, rel_tol, abs_tol)

Check if the values a and b are close to each other

isfinite(x)

Check if x is neither an infinity nor a NaN

isinf(x)

Check if x is a positive or negative infinity

isnan(x)

Check if x is a NaN (not a number)

ldexp(x, i)

x * (2**i) , inverse of function frexp()

nextafter(x, y, steps)

Floating-point value steps steps after x towards y

ulp(x)

Value of the least significant bit of x

Power, exponential and logarithmic functions

cbrt(x)

Cube root of x

exp(x)

e 自乘幂 x

exp2(x)

2 自乘幂 x

expm1(x)

e 自乘幂 x , minus 1

log(x, base)

Logarithm of x to the given base ( e by default)

log1p(x)

Natural logarithm of 1+x (基 e )

log2(x)

Base-2 logarithm of x

log10(x)

Base-10 logarithm of x

pow(x, y)

x 自乘幂 y

sqrt(x)

Square root of x

Summation and product functions

dist(p, q)

Euclidean distance between two points p and q given as an iterable of coordinates

fsum(iterable)

Sum of values in the input iterable

hypot(*coordinates)

Euclidean norm of an iterable of coordinates

prod(iterable, start)

Product of elements in the input iterable 采用 start

sumprod(p, q)

Sum of products from two iterables p and q

角度转换

degrees(x)

转换角度 x from radians to degrees

radians(x)

转换角度 x from degrees to radians

三角函数

acos(x)

Arc cosine of x

asin(x)

Arc sine of x

atan(x)

Arc tangent of x

atan2(y, x)

atan(y / x)

cos(x)

Cosine of x

sin(x)

Sine of x

tan(x)

Tangent of x

双曲函数

acosh(x)

Inverse hyperbolic cosine of x

asinh(x)

Inverse hyperbolic sine of x

atanh(x)

Inverse hyperbolic tangent of x

cosh(x)

Hyperbolic cosine of x

sinh(x)

Hyperbolic sine of x

tanh(x)

Hyperbolic tangent of x

特殊函数

erf(x)

Error function at x

erfc(x)

Complementary error function at x

gamma(x)

伽玛函数 at x

lgamma(x)

Natural logarithm of the absolute value of the 伽玛函数 at x

常量

pi

π = 3.141592…

e

e = 2.718281…

tau

τ = 2 π = 6.283185…

inf

Positive infinity

nan

“Not a number” (NaN)

Number-theoretic functions

math. comb ( n , k )

Return the number of ways to choose k items from n items without repetition and without order.

评估为 n! / (k! * (n - k)!) k <= n 和评估为 0 当 k > n .

Also called the binomial coefficient because it is equivalent to the coefficient of k-th term in polynomial expansion of (1 + x)ⁿ .

引发 TypeError 若任一自变量不是整数。引发 ValueError 若任一自变量为负。

Added in version 3.8.