math
— Mathematical functions数学函数¶
This module provides access to the mathematical functions defined by the C standard.此模块提供对C标准定义的数学函数的访问。
These functions cannot be used with complex numbers; use the functions of the same name from the 这些函数不能用于复数;如果需要支持复数,请使用cmath
module if you require support for complex numbers. cmath
模块中相同名称的函数。The distinction between functions which support complex numbers and those which don’t is made since most users do not want to learn quite as much mathematics as required to understand complex numbers. 由于大多数用户不想学习理解复数所需的那么多数学知识,因此对支持复数的函数和不支持复数的函数进行了区分。Receiving an exception instead of a complex result allows earlier detection of the unexpected complex number used as a parameter, so that the programmer can determine how and why it was generated in the first place.通过接收异常而不是复杂结果,可以更早地检测到用作参数的意外复数,以便程序员可以确定它是如何生成的以及为什么生成的。
The following functions are provided by this module. 此模块提供以下功能。Except when explicitly noted otherwise, all return values are floats.除非另有明确说明,否则所有返回值都是浮点值。
Number-theoretic and representation functions数论和表示函数¶
-
math.
ceil
(x)¶ Return the ceiling of x, the smallest integer greater than or equal to x.返回x的上限,即大于或等于x的最小整数。If x is not a float, delegates to如果x不是浮点,则委托给x.__ceil__
, which should return anIntegral
value.x.__ceil__
,后者应返回一个Integral
值。
-
math.
comb
(n, k)¶ Return the number of ways to choose k items from n items without repetition and without order.返回从n个项目中选择k个项目的方法数,无重复,无顺序。Evaluates to当n! / (k! * (n - k)!)
whenk <= n
and evaluates to zero whenk > n
.k<=n
时计算结果为n! / (k! * (n - k)!)
,当k>n
时计算为零。Also called the binomial coefficient because it is equivalent to the coefficient of k-th term in polynomial expansion of the expression也称为二项式系数,因为它等效于表达式(1 + x) ** n
.(1 + x) ** n
的多项式展开中的第k项系数。Raises如果任一参数不是整数,则引发TypeError
if either of the arguments are not integers.TypeError
。Raises如果任一参数为负,则引发ValueError
if either of the arguments are negative.ValueError
。New in version 3.8.版本3.8中新增。
-
math.
copysign
(x, y)¶ Return a float with the magnitude (absolute value) of x but the sign of y.返回一个大小(绝对值)为x但符号为y的浮点。On platforms that support signed zeros,在支持带符号零的平台上,copysign(1.0, -0.0)
returns -1.0.copysign(1.0, -0.0)
返回-1.0
。
-
math.
fabs
(x)¶ Return the absolute value of x.返回x的绝对值。
-
math.
factorial
(x)¶ Return x factorial as an integer.返回x阶乘作为整数。Raises如果x不是整数或为负,则引发ValueError
if x is not integral or is negative.ValueError
。Deprecated since version 3.9:自版本3.9以来已弃用:Accepting floats with integral values (like不赞成接受具有整数值(如5.0
) is deprecated.5.0
)的浮点。
-
math.
floor
(x)¶ Return the floor of x, the largest integer less than or equal to x.返回x的下限,最大整数小于或等于x。If x is not a float, delegates to如果x不是浮点,则委托给x.__floor__
, which should return anIntegral
value.x.__floor__
,该值应返回一个Integral
值。
-
math.
fmod
(x, y)¶ Return返回由平台C库定义的fmod(x, y)
, as defined by the platform C library.fmod(x, y)
。Note that the Python expression请注意,Python表达式x % y
may not return the same result.x % y
可能不会返回相同的结果。The intent of the C standard is thatC标准的目的是,对于某些整数n,fmod(x, y)
be exactly (mathematically; to infinite precision) equal tox - n*y
for some integer n such that the result has the same sign as x and magnitude less thanabs(y)
.fmod(x, y)
精确(数学上;无限精度)等于x-n*y
,以便结果具有与x相同的符号,且幅值小于abs(y)
。Python’sPython的x % y
returns a result with the sign of y instead, and may not be exactly computable for float arguments.x % y
返回一个符号为y的结果,对于浮点参数可能不完全可计算。For example,例如,fmod(-1e-100, 1e100)
is-1e-100
, but the result of Python’s-1e-100 % 1e100
is1e100-1e-100
, which cannot be represented exactly as a float, and rounds to the surprising1e100
.fmod(-1e-100, 1e100)
是-1e-100
,但Python的-1e-100%1e100
的结果是1e100-1e-100
,它不能精确地表示为浮点,并舍入到令人惊讶的1e100
。For this reason, function因此,在处理浮点时,通常首选函数fmod()
is generally preferred when working with floats, while Python’sx % y
is preferred when working with integers.fmod()
,而在处理整数时,则首选Python的x%y。
-
math.
frexp
(x)¶ Return the mantissa and exponent of x as the pair返回x的尾数和指数作为配对(m, e)
.(m, e)
。m is a float and e is an integer such thatm是一个浮点,e是一个整数,使得x == m * 2**e
exactly.x == m * 2**e
正好。If x is zero, returns如果x为零,则返回(0.0, 0)
, otherwise0.5 <= abs(m) < 1
.(0.0, 0)
,否则0.5 <= abs(m) < 1
。This is used to “pick apart” the internal representation of a float in a portable way.这用于以可移植的方式“分离”浮点的内部表示。
-
math.
fsum
(iterable)¶ Return an accurate floating point sum of values in the iterable.返回iterable中值的精确浮点和。Avoids loss of precision by tracking multiple intermediate partial sums:通过跟踪多个中间部分和避免精度损失:>>> 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.0The algorithm’s accuracy depends on IEEE-754 arithmetic guarantees and the typical case where the rounding mode is half-even.该算法的精度取决于IEEE-754算法保证和舍入模式为半偶数的典型情况。On some non-Windows builds, the underlying C library uses extended precision addition and may occasionally double-round an intermediate sum causing it to be off in its least significant bit.在一些非Windows版本中,底层C库使用扩展精度加法,有时可能会对中间和进行双舍入,导致其在最低有效位处关闭。For further discussion and two alternative approaches, see the ASPN cookbook recipes for accurate floating point summation.有关进一步的讨论和两种替代方法,请参阅ASPN食谱中的精确浮点总和。
-
math.
gcd
(*integers)¶ Return the greatest common divisor of the specified integer arguments.返回指定整数参数的最大公约数。If any of the arguments is nonzero, then the returned value is the largest positive integer that is a divisor of all arguments.如果任何参数为非零,则返回值为最大的正整数,该整数是所有参数的除数。If all arguments are zero, then the returned value is如果所有参数均为零,则返回值为0
.0
。不带参数的gcd()
without arguments returns0
.gcd()
返回0
。New in version 3.5.版本3.5中新增。Changed in version 3.9:版本3.9中更改:Added support for an arbitrary number of arguments.添加了对任意数量参数的支持。Formerly, only two arguments were supported.以前,只支持两个参数。
-
math.
isclose
(a, b, *, rel_tol=1e-09, abs_tol=0.0)¶ Return如果值a和b彼此接近,则返回True
if the values a and b are close to each other andFalse
otherwise.True
,否则返回False
。Whether or not two values are considered close is determined according to given absolute and relative tolerances.根据给定的绝对和相对公差确定两个值是否接近。rel_tol is the relative tolerance – it is the maximum allowed difference between a and b, relative to the larger absolute value of a or b.rel_tol是相对公差:它是相对于a或b的较大绝对值,a和b之间允许的最大差值。For example, to set a tolerance of 5%, pass例如,要将公差设置为5%,通过rel_tol=0.05
.rel_tol=0.05
。The default tolerance is默认公差为1e-09
, which assures that the two values are the same within about 9 decimal digits.1e-09
,确保两个值在大约9位小数内相同。rel_tolmust be greater than zero.必须大于零。abs_tol
is the minimum absolute tolerance – useful for comparisons near zero.是最小绝对公差-用于接近零的比较。abs_tol must be at least zero.abs_tol必须至少为零。If no errors occur, the result will be:如果没有出现错误,结果将是:abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
.abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
。The IEEE 754 special values of将根据IEEE规则处理NaN
,inf
, and-inf
will be handled according to IEEE rules.NaN
、inf
和-inf
的IEEE 754特殊值。Specifically,具体来说,NaN
is not considered close to any other value, includingNaN
.NaN
不被视为接近任何其他值,包括NaN
。inf
and-inf
are only considered close to themselves.inf
和-inf
只被认为与它们自己很接近。New in version 3.5.版本3.5中新增。See also
PEP 485 –
A function for testing approximate equality检验近似等式的函数
-
math.
isfinite
(x)¶ Return如果x既不是无穷大也不是NaN,则返回True
if x is neither an infinity nor a NaN, andFalse
otherwise.True
,否则返回False
。(Note that(注意,0.0
is considered finite.)0.0
被认为是有限的。)New in version 3.2.版本3.2中新增。
-
math.
isinf
(x)¶ Return如果x是正无穷大或负无穷大,则返回True
if x is a positive or negative infinity, andFalse
otherwise.True
,否则返回False
。
-
math.
isnan
(x)¶ Return如果x是NaN(不是数字),则返回True
if x is a NaN (not a number), andFalse
otherwise.True
,否则返回False
。
-
math.
isqrt
(n)¶ Return the integer square root of the nonnegative integer n.返回非负整数n的整数平方根。This is the floor of the exact square root of n, or equivalently the greatest integer a such that a² ≤ n.这是n的精确平方根的下限,或者等效于最大整数a,使得a² ≤ n。For some applications, it may be more convenient to have the least integer a such that n ≤ a², or in other words the ceiling of the exact square root of n.对于某些应用程序,使用最小整数a可能更方便,使n ≤ a²,或者换句话说,n的精确平方根的上限。For positive n, this can be computed using对于正n,可以使用a = 1 + isqrt(n - 1)
.a = 1 + isqrt(n - 1)
计算。New in version 3.8.版本3.8中新增。
-
math.
lcm
(*integers)¶ Return the least common multiple of the specified integer arguments.返回指定整数参数的最小公倍数。If all arguments are nonzero, then the returned value is the smallest positive integer that is a multiple of all arguments.如果所有参数均为非零,则返回值为所有参数的倍数的最小正整数。If any of the arguments is zero, then the returned value is如果任何参数为零,则返回值为0
.0
。不带参数的lcm()
without arguments returns1
.lcm()
返回1
。New in version 3.9.版本3.9中新增。
-
math.
ldexp
(x, i)¶ Return返回x * (2**i)
.x * (2**i)
。This is essentially the inverse of function这本质上是函数frexp()
.frexp()
的逆函数。
-
math.
modf
(x)¶ Return the fractional and integer parts of x.返回x的分数和整数部分。Both results carry the sign of x and are floats.这两个结果都带有x的符号,都是浮点数。
-
math.
nextafter
(x, y)¶ Return the next floating-point value after x towards y.返回x后向y的下一个浮点值。If x is equal to y, return y.如果x等于y,则返回y。Examples:示例:math.nextafter(x, math.inf)
goes up: towards positive infinity.向上:朝向正无穷大。math.nextafter(x, -math.inf)
goes down: towards minus infinity.向下:朝向负无穷大。math.nextafter(x, 0.0)
goes towards zero.逐渐接近零。math.nextafter(x, math.copysign(math.inf, x))
goes away from zero.逐渐远离零。
See also
math.ulp()
.New in version 3.9.版本3.9中新增。
-
math.
perm
(n, k=None)¶ Return the number of ways to choose k items from n items without repetition and with order.返回从n个项目中选择k个项目的方法数,无重复且有顺序。Evaluates to当n! / (n - k)!
whenk <= n
and evaluates to zero whenk > n
.k<=n
时计算结果为n! / (n - k)!
,当k>n
时计算为零。If k is not specified or is None, then k defaults to n and the function returns如果k未指定或无,则k默认为n,函数返回n!
.n!
。Raises如果任一参数不是整数,则引发TypeError
if either of the arguments are not integers.TypeError
。Raises如果任一参数为负,则引发ValueError
if either of the arguments are negative.ValueError
。New in version 3.8.版本3.8中新增。
-
math.
prod
(iterable, *, start=1)¶ Calculate the product of all the elements in the input iterable.计算输入iterable中所有元素的乘积。The default start value for the product is乘积的默认start值为1
.1
。When the iterable is empty, return the start value.当iterable为空时,返回起始值。This function is intended specifically for use with numeric values and may reject non-numeric types.此函数专门用于数值,可能会拒绝非数值类型。New in version 3.8.版本3.8中新增。
-
math.
remainder
(x, y)¶ Return the IEEE 754-style remainder of x with respect to y.返回IEEE 754风格的x相对于y的余数。For finite x and finite nonzero y, this is the difference对于有限x和有限非零y,这是差x - n*y
, wheren
is the closest integer to the exact value of the quotientx / y
.x-n*y
,其中n
是最接近商x/y
的精确值的整数。If如果x / y
is exactly halfway between two consecutive integers, the nearest even integer is used forn
.x/y
正好位于两个连续整数的中间,则对n
使用最近的偶数整数。The remainder因此,余数r = remainder(x, y)
thus always satisfiesabs(r) <= 0.5 * abs(y)
.r = remainder(x, y)
始终满足abs(r) <= 0.5 * abs(y)
。Special cases follow IEEE 754: in particular,特殊情况遵循IEEE 754:特别是,对于任何有限x,remainder(x, math.inf)
is x for any finite x, andremainder(x, 0)
andremainder(math.inf, x)
raiseValueError
for any non-NaN x.remainder(x, math.inf)
是x,对于任何非NaN x,remainder(x, 0)
和remainder(math.inf, x)
产生ValueError
。If the result of the remainder operation is zero, that zero will have the same sign as x.如果余数运算的结果为零,则该零将与x具有相同的符号。On platforms using IEEE 754 binary floating-point, the result of this operation is always exactly representable: no rounding error is introduced.在使用IEEE 754二进制浮点的平台上,此操作的结果始终可以精确表示:不会引入舍入误差。New in version 3.7.版本3.7中新增。
-
math.
trunc
(x)¶ Return x with the fractional part removed, leaving the integer part.返回x,去掉小数部分,留下整数部分。This rounds toward 0:这向0舍入:trunc()
is equivalent tofloor()
for positive x, and equivalent toceil()
for negative x.trunc()
相当于正x的floor()
,相当于负x的ceil()
。If x is not a float, delegates to如果x不是浮点数,则委托给x.__trunc__
, which should return anIntegral
value.x.__trunc__
,后者应返回一个Integral
值。
-
math.
ulp
(x)¶ Return the value of the least significant bit of the float x:返回浮点x的最低有效位的值:If x is a NaN (not a number), return x.如果x是NaN(不是数字),则返回x。If x is negative, return如果x为负,则返回ulp(-x)
.ulp(-x)
。If x is a positive infinity, return x.如果x是正无穷大,则返回x。If x is equal to zero, return the smallest positive denormalized representable float (smaller than the minimum positive normalized float,如果x等于零,则返回最小的正非规范化可表示浮点(小于最小正规范化浮点,sys.float_info.min
).sys.float_info.min
)。If x is equal to the largest positive representable float, return the value of the least significant bit of x, such that the first float smaller than x is如果x等于最大的正可表示浮点,则返回x的最低有效位的值,使得小于x的第一个浮点为x - ulp(x)
.x-ulp(x)
。Otherwise (x is a positive finite number), return the value of the least significant bit of x, such that the first float bigger than x is否则(x是正有限数),返回x的最低有效位的值,使得大于x的第一个浮点为x + ulp(x)
.x + ulp(x)
。
ULP stands for “Unit in the Last Place”.ULP代表“最后的单位”。See also另请参见math.nextafter()
andsys.float_info.epsilon
.math.nextafter()
和sys.float_info.epsilon
。New in version 3.9.版本3.9中新增。
Note that 注意,frexp()
and modf()
have a different call/return pattern than their C equivalents: they take a single argument and return a pair of values, rather than returning their second return value through an ‘output parameter’ (there is no such thing in Python).frexp()
和modf()
的调用/返回模式与它们的C等价物不同:它们接受一个参数并返回一对值,而不是通过“输出参数”返回第二个返回值(Python中没有这样的东西)。
For the 对于ceil()
, floor()
, and modf()
functions, note that all floating-point numbers of sufficiently large magnitude are exact integers. ceil()
、floor()
和modf()
函数,请注意,所有大小足够大的浮点数都是精确整数。Python floats typically carry no more than 53 bits of precision (the same as the platform C double type), in which case any float x with Python浮点通常携带不超过53位的精度(与平台C双精度类型相同),在这种情况下,abs(x) >= 2**52
necessarily has no fractional bits.abs(x) >= 2**52
的任何浮点x必然没有分数位。
Power and logarithmic functions幂函数和对数函数¶
-
math.
exp
(x)¶ Return e raised to the power x, where e = 2.718281… is the base of natural logarithms.返回e的幂x,其中e=2.718281是自然对数的基数。This is usually more accurate than这通常比math.e ** x
orpow(math.e, x)
.math.e ** x
或pow(math.e, x)
更准确。
-
math.
expm1
(x)¶ Return e raised to the power x, minus 1.返回e的幂x,减1。Here e is the base of natural logarithms.这里e是自然对数的底。For small floats x, the subtraction in对于小浮点x,exp(x) - 1
can result in a significant loss of precision; theexpm1()
function provides a way to compute this quantity to full precision:exp(x)-1
中的减法可能会导致精度的显著损失;expm1()
函数提供了一种完全精确计算该量的方法:>>> 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-05New in version 3.2.版本3.2中新增。
-
math.
log
(x[, base])¶ With one argument, return the natural logarithm of x (to base e).使用一个参数,返回x的自然对数(以e为底)。With two arguments, return the logarithm of x to the given base, calculated as使用两个参数,将x的对数返回到给定的base,计算为log(x)/log(base)
.log(x)/log(base)
。
-
math.
log1p
(x)¶ Return the natural logarithm of 1+x (base e).返回1+x(以e为底)的自然对数。The result is calculated in a way which is accurate for x near zero.结果的计算方法对于接近零的x是准确的。
-
math.
log2
(x)¶ Return the base-2 logarithm of x.返回x的以2为底的对数。This is usually more accurate than这通常比log(x, 2)
.log(x, 2)
更准确。New in version 3.3.版本3.3中新增。See also
int.bit_length()
returns the number of bits necessary to represent an integer in binary, excluding the sign and leading zeros.返回用二进制表示整数所需的位数,不包括符号和前导零。
-
math.
log10
(x)¶ Return the base-10 logarithm of x.返回x的以10为底的对数。This is usually more accurate than这通常比log(x, 10)
.log(x, 10)
更准确。
-
math.
pow
(x, y)¶ Return返回x
raised to the powery
.x
上升到y
的幂。Exceptional cases follow Annex ‘F’ of the C99 standard as far as possible.例外情况尽可能遵循C99标准的附录F。In particular,特别是,pow(1.0, x)
andpow(x, 0.0)
always return1.0
, even whenx
is a zero or a NaN.pow(1.0, x)
和pow(x, 0.0)
始终返回1.0
,即使x
是零或NaN。If both如果x
andy
are finite,x
is negative, andy
is not an integer thenpow(x, y)
is undefined, and raisesValueError
.x
和y
都是有限的,x
是负的,y
不是整数,那么pow(x, y)
是未定义的,并引起ValueError
。Unlike the built-in与内置的**
operator,math.pow()
converts both its arguments to typefloat
.**
运算符不同,math.pow()
将其两个参数都转换为float类型。Use使用**
or the built-inpow()
function for computing exact integer powers.**
或内置的pow()
函数计算精确的整数幂。
-
math.
sqrt
(x)¶ Return the square root of x.返回x的平方根。
Trigonometric functions三角函数¶
-
math.
acos
(x)¶ Return the arc cosine of x, in radians.返回x的弧余弦,以弧度为单位。The result is between结果在0
andpi
.0
和pi
之间。
-
math.
asin
(x)¶ Return the arc sine of x, in radians.返回x的正弦弧,以弧度为单位。The result is between结果介于-pi/2
andpi/2
.-pi/2
和pi/2
之间。
-
math.
atan
(x)¶ Return the arc tangent of x, in radians.返回x的反正切,以弧度为单位。The result is between结果介于-pi/2
andpi/2
.-pi/2
和pi/2
之间。
-
math.
atan2
(y, x)¶ Return返回atan(y / x)
, in radians.atan(y / x)
,以弧度为单位。The result is between结果介于-pi
andpi
.-pi
和pi
之间。The vector in the plane from the origin to point平面中从原点到(x, y)
makes this angle with the positive X axis.(x, y)
的矢量与正x轴成此角度。The point ofatan2()
is that the signs of both inputs are known to it, so it can compute the correct quadrant for the angle.atan2()
的要点是它知道两个输入的符号,因此可以计算角度的正确象限。For example,例如,atan(1)
andatan2(1, 1)
are bothpi/4
, butatan2(-1, -1)
is-3*pi/4
.atan(1)
和atan2(1, 1)
都是pi/4
,但atan2(-1, -1)
是-3*pi/4
。
-
math.
cos
(x)¶ Return the cosine of x radians.返回x弧度的余弦。
-
math.
dist
(p, q)¶ Return the Euclidean distance between two points p and q, each given as a sequence (or iterable) of coordinates.返回两个点p和q之间的欧几里德距离,每个点以坐标序列(或iterable)的形式给出。The two points must have the same dimension.这两个点必须具有相同的尺寸。Roughly equivalent to:大致相当于:sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))
New in version 3.8.版本3.8中新增。
-
math.
hypot
(*coordinates)¶ Return the Euclidean norm,返回欧几里德范数sqrt(sum(x**2 for x in coordinates))
.sqrt(sum(x**2 for x in coordinates))
。This is the length of the vector from the origin to the point given by the coordinates.这是向量从原点到坐标给定点的长度。For a two dimensional point对于二维点(x, y)
, this is equivalent to computing the hypotenuse of a right triangle using the Pythagorean theorem,sqrt(x*x + y*y)
.(x, y)
,这相当于使用勾股定理sqrt(x*x + y*y)
计算直角三角形的斜边。Changed in version 3.8:版本3.8中更改:Added support for n-dimensional points.增加了对n维点的支持。Formerly, only the two dimensional case was supported.以前,仅支持二维情况。Changed in version 3.10:版本3.10中更改:Improved the algorithm’s accuracy so that the maximum error is under 1 ulp (unit in the last place).提高了算法的准确性,使最大误差低于1 ulp(最后一个单位)。More typically, the result is almost always correctly rounded to within 1/2 ulp.更典型的是,结果几乎总是正确地四舍五入到1/2 ulp以内。
-
math.
sin
(x)¶ Return the sine of x radians.返回x弧度的正弦值。
-
math.
tan
(x)¶ Return the tangent of x radians.返回x弧度的切线。
Angular conversion角度转换¶
-
math.
degrees
(x)¶ Convert angle x from radians to degrees.将角度x从弧度转换为度。
-
math.
radians
(x)¶ Convert angle x from degrees to radians.将角度x从度转换为弧度。
Hyperbolic functions双曲函数¶
Hyperbolic functions双曲函数 are analogs of trigonometric functions that are based on hyperbolas instead of circles.是基于双曲线而非圆的三角函数的类似物。
-
math.
acosh
(x)¶ Return the inverse hyperbolic cosine of x.返回x的反双曲余弦。
-
math.
asinh
(x)¶ Return the inverse hyperbolic sine of x.返回x的反双曲正弦。
-
math.
atanh
(x)¶ Return the inverse hyperbolic tangent of x.返回x的反双曲正切。
-
math.
cosh
(x)¶ Return the hyperbolic cosine of x.返回x的双曲余弦。
-
math.
sinh
(x)¶ Return the hyperbolic sine of x.返回x的双曲正弦。
-
math.
tanh
(x)¶ Return the hyperbolic tangent of x.返回x的双曲正切。
Special functions特殊函数¶
-
math.
erf
(x)¶ Return the error function at x.返回x处的错误函数。Theerf()
function can be used to compute traditional statistical functions such as the cumulative standard normal distribution:erf()
函数可用于计算传统统计函数,例如累积标准正态分布:def phi(x):
'Cumulative distribution function for the standard normal distribution'
return (1.0 + erf(x / sqrt(2.0))) / 2.0New in version 3.2.版本3.2中新增。
-
math.
erfc
(x)¶ Return the complementary error function at x.返回x处的互补误差函数。The complementary error function is defined as互补误差函数定义为1.0 - erf(x)
.1.0-erf(x)
。It is used for large values of x where a subtraction from one would cause a loss of significance.它用于x的大值,其中从1中减去会导致显著性损失。New in version 3.2.版本3.2中新增。
-
math.
gamma
(x)¶ Return the Gamma function at x.返回x处的Gamma函数。New in version 3.2.版本3.2中新增。
-
math.
lgamma
(x)¶ Return the natural logarithm of the absolute value of the Gamma function at x.返回伽玛函数在x处绝对值的自然对数。New in version 3.2.版本3.2中新增。
Constants常数¶
-
math.
pi
¶ The mathematical constant π = 3.141592…, to available precision.数学常数π=3.141592…,达到可用精度。
-
math.
e
¶ The mathematical constant e = 2.718281…, to available precision.数学常数e=2.718281…,达到可用精度。
-
math.
tau
¶ The mathematical constant τ = 6.283185…, to available precision.数学常数τ=6.283185…,达到可用精度。Tau is a circle constant equal to 2π, the ratio of a circle’s circumference to its radius.τ是一个圆常数,等于2π,即圆的周长与其半径的比值。To learn more about Tau, check out Vi Hart’s video Pi is (still) Wrong, and start celebrating Tau day by eating twice as much pie!要了解更多关于陶的信息,请查看维哈特(Vi Hart)的视频Pi is (still) Wrong,并开始通过吃两倍的馅饼来庆祝陶日!New in version 3.6.版本3.6中新增。
-
math.
inf
¶ A floating-point positive infinity.浮点正无穷大。(For negative infinity, use(对于负无穷大,请使用-math.inf
.)-math.inf
。)Equivalent to the output of等效于float('inf')
.float('inf')
的输出。New in version 3.5.版本3.5中新增。
-
math.
nan
¶ A floating-point “not a number” (NaN) value.浮点“非数字”(NaN)值。Equivalent to the output of相当于float('nan')
.float('nan')
的输出。Due to the requirements of the IEEE-754 standard,由于IEEE-754标准的要求,math.nan
andfloat('nan')
are not considered to equal to any other numeric value, including themselves.math.nan
和float('nan')
不被视为等于任何其他数值,包括其本身。To check whether a number is a NaN, use the要检查数字是否为NaN,请使用isnan()
function to test for NaNs instead ofis
or==
.isnan()
函数测试NaN,而不是is
或==
。Example:例子:>>> import math
>>> math.nan == math.nan
False
>>> float('nan') == float('nan')
False
>>> math.isnan(math.nan)
True
>>> math.isnan(float('nan'))
TrueNew in version 3.5.版本3.5中新增。
CPython implementation detail:CPython实施细节: The math
module consists mostly of thin wrappers around the platform C math library functions. math
模块主要由围绕平台C数学库函数的薄型包装器组成。Behavior in exceptional cases follows Annex F of the C99 standard where appropriate. 特殊情况下的行为遵循C99标准附录F(如适用)。The current implementation will raise 当前实现将为无效操作(如ValueError
for invalid operations like sqrt(-1.0)
or log(0.0)
(where C99 Annex F recommends signaling invalid operation or divide-by-zero), and OverflowError
for results that overflow (for example, exp(1000.0)
). sqrt(-1.0)
或log(0.0)
(其中C99附录F建议发送无效操作信号或除以零)引发ValueError
,并为溢出的结果(例如exp(1000.0))引发OverflowError
。A NaN will not be returned from any of the functions above unless one or more of the input arguments was a NaN; in that case, most functions will return a NaN, but (again following C99 Annex F) there are some exceptions to this rule, for example 除非一个或多个输入参数是NaN,否则不会从上述任何函数返回NaN;在这种情况下,大多数函数将返回一个NaN,但(同样在C99附录F之后)该规则也有一些例外,例如pow(float('nan'), 0.0)
or hypot(float('nan'), float('inf'))
.pow(float('nan'), 0.0)
或hypot(float('nan'), float('inf'))
。
Note that Python makes no effort to distinguish signaling NaNs from quiet NaNs, and behavior for signaling NaNs remains unspecified. 注意,Python没有努力区分发信号的NAN和安静的NAN,发信号的NAN的行为仍然没有明确说明。Typical behavior is to treat all NaNs as though they were quiet.典型的行为是对待所有的NaN就像他们很安静一样。
See also
- Module
cmath
Complex number versions of many of these functions.其中许多函数的复数版本。