math
— 数学函数
¶
This module is always available. It provides access to the mathematical functions defined by the C standard.
这些函数无法用于复数;使用的同名函数来自
cmath
模块若要求支持复数。支持复数和那些不支持复数的函数之间的区别,是由于大多数用户不想学习理解复数要求的那么多数学知识。接收异常而不是复杂结果允许更早地检测用作参数的意外复数,所以,程序员可以确定它最初是如何生成的及为什么生成。
此模块提供下列函数。除另有明确说明外,所有返回值是浮点数。
math.
ceil
(
x
)
¶
返回上限为 x as a float, the smallest integer value greater than or equal to x .
math.
copysign
(
x
,
y
)
¶
返回
x
with the sign of
y
. On a platform that supports signed zeros,
copysign(1.0, -0.0)
返回
-1.0
.
2.6 版新增。
math.
fabs
(
x
)
¶
返回绝对值的 x .
math.
factorial
(
x
)
¶
返回
x
阶乘。引发
ValueError
if
x
不是整型或为负。
2.6 版新增。
math.
floor
(
x
)
¶
Return the floor of x as a float, the largest integer value less than or equal to x .
math.
fmod
(
x
,
y
)
¶
返回
fmod(x, y)
, as defined by the platform C library. Note that the Python expression
x % y
may not return the same result. The intent of the C standard is that
fmod(x, y)
be exactly (mathematically; to infinite precision) equal to
x - n*y
for some integer
n
such that the result has the same sign as
x
and magnitude less than
abs(y)
。Python 的
x % y
returns a result with the sign of
y
instead, and may not be exactly computable for float arguments. For example,
fmod(-1e-100, 1e100)
is
-1e-100
, but the result of Python’s
-1e-100 % 1e100
is
1e100-1e-100
, which cannot be represented exactly as a float, and rounds to the surprising
1e100
。出于此原因,函数
fmod()
is generally preferred when working with floats, while Python’s
x % y
is preferred when working with integers.
math.
frexp
(
x
)
¶
Return the mantissa and exponent of
x
as the pair
(m, e)
.
m
is a float and
e
is an integer such that
x == m * 2**e
exactly. If
x
为 0,返回
(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
)
¶
返回 iterable (可迭代) 中值的精确浮点和。通过追踪多个中间体部分的和,以避免精度损失:
>>> 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
The algorithm’s accuracy depends on IEEE-754 arithmetic guarantees and the typical case where the rounding mode is half-even. 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.
For further discussion and two alternative approaches, see the ASPN cookbook recipes for accurate floating point summation .
2.6 版新增。
math.
isinf
(
x
)
¶
Check if the float x is positive or negative infinity.
2.6 版新增。
math.
isnan
(
x
)
¶
Check if the float x is a NaN (not a number). For more information on NaNs, see the IEEE 754 standards.
2.6 版新增。
math.
modf
(
x
)
¶
Return the fractional and integer parts of x . Both results carry the sign of x and are floats.
math.
trunc
(
x
)
¶
返回
Real
值
x
truncated to an
Integral
(usually a long integer). Uses the
__trunc__
方法。
2.6 版新增。
注意,
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).
对于
ceil()
,
floor()
,和
modf()
functions, note that
all
floating-point numbers of sufficiently large magnitude are exact integers. 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
abs(x) >= 2**52
necessarily has no fractional bits.
math.
exp
(
x
)
¶
返回
e**x
.
math.
expm1
(
x
)
¶
返回
e**x - 1
. For small floats
x
,减法在
exp(x) - 1
can result in a significant loss of precision; the
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-05
2.7 版新增。
math.
log
(
x
[
,
base
]
)
¶
按 1 自变量,返回自然对数为 x (到基 e ).
按 2 自变量,返回对数为
x
到给定
base
,计算如
log(x)/log(base)
.
Changed in version 2.3: base argument added.
math.
log1p
(
x
)
¶
返回自然对数为 1+x (基 e ). The result is calculated in a way which is accurate for x near zero.
2.6 版新增。
math.
log10
(
x
)
¶
返回 10 基对数为
x
。这通常更精确比
log(x, 10)
.
math.
pow
(
x
,
y
)
¶
返回
x
自乘幂
y
. Exceptional cases follow Annex ‘F’ of the C99 standard as far as possible. In particular,
pow(1.0, x)
and
pow(x, 0.0)
始终返回
1.0
,甚至当
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
.
不像内置
**
运算符,
math.pow()
将其 2 自变量转换为类型
float
。使用
**
或内置
pow()
函数用于计算准确整数幂。
2.6 版改变:
The outcome of
1**nan
and
nan**0
was undefined.
math.
sqrt
(
x
)
¶
返回平方根为 x .
math.
acos
(
x
)
¶
返回 acos (反余弦) 为 x (以弧度为单位)。
math.
asin
(
x
)
¶
返回 asin (反正弦) 为 x (以弧度为单位)。
math.
atan
(
x
)
¶
返回 atan (反正切) 为 x (以弧度为单位)。
math.
atan2
(
y
,
x
)
¶
返回
atan(y / x)
(以弧度为单位)。结果介于
-pi
and
pi
. The vector in the plane from the origin to point
(x, y)
makes this angle with the positive X axis. The point of
atan2()
is that the signs of both inputs are known to it, so it can compute the correct quadrant for the angle. For example,
atan(1)
and
atan2(1, 1)
are both
pi/4
,但
atan2(-1,
-1)
is
-3*pi/4
.
math.
cos
(
x
)
¶
返回 cos (余弦) 为 x 弧度。
math.
hypot
(
x
,
y
)
¶
返回 Euclidean (欧几里德) 范数
sqrt(x*x + y*y)
. This is the length of the vector from the origin to point
(x, y)
.
math.
sin
(
x
)
¶
返回 sin (正弦) 为 x 弧度。
math.
tan
(
x
)
¶
返回 tan (正切) 为 x 弧度。
math.
degrees
(
x
)
¶
转换角度 x 从弧度到度。
math.
radians
(
x
)
¶
转换角度 x 从度到弧度。
math.
acosh
(
x
)
¶
Return the inverse hyperbolic cosine of x .
2.6 版新增。
math.
asinh
(
x
)
¶
Return the inverse hyperbolic sine of x .
2.6 版新增。
math.
atanh
(
x
)
¶
Return the inverse hyperbolic tangent of x .
2.6 版新增。
math.
cosh
(
x
)
¶
Return the hyperbolic cosine of x .
math.
sinh
(
x
)
¶
Return the hyperbolic sine of x .
math.
tanh
(
x
)
¶
Return the hyperbolic tangent of x .
math.
erf
(
x
)
¶
Return the error function at x .
2.7 版新增。
math.
erfc
(
x
)
¶
Return the complementary error function at x .
2.7 版新增。
math.
gamma
(
x
)
¶
Return the Gamma function at x .
2.7 版新增。
math.
lgamma
(
x
)
¶
Return the natural logarithm of the absolute value of the Gamma function at x .
2.7 版新增。
math.
pi
¶
The mathematical constant π = 3.141592…, to available precision.
math.
e
¶
The mathematical constant e = 2.718281…, to available precision.
CPython 实现细节:
The
math
module consists mostly of thin wrappers around the platform C math library functions. Behavior in exceptional cases follows Annex F of the C99 standard where appropriate. 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)
). 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
pow(float('nan'), 0.0)
or
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. Typical behavior is to treat all NaNs as though they were quiet.
2.6 版改变: Behavior in special cases now aims to follow C99 Annex F. In earlier versions of Python the behavior in special cases was loosely specified.
另请参阅
cmath
很多这些函数的复数版本。