Mathematical Functions in Python
Mathematical Functions in Python
This module provides access to the mathematical functions defined by the C standard.
math.comb(n, k): Return the number of ways to choose k items from n items without repetition and
without order.
math.copysign(x, y): Return a float with the magnitude (absolute value) of x but the sign of y. On
platforms that support signed zeros, copysign(1.0, -0.0) returns -1.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): Return fmod(x, y), as defined by the platform C library. Note that the Python
expression x % y may not return the same result.
math.frexp(x): Y 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.
math.fsum(iterable): Return an accurate floating point sum of values in the iterable. Avoids loss of
precision by tracking multiple intermediate partial sums:
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. gcd() without arguments returns
0.
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0): Return True if the values a and b are close to each
other and False otherwise.
math.isfinite(x): Return True if x is neither an infinity nor a NaN, and False otherwise. (Note that 0.0
is considered finite.)
math.isqrt(n): Return the integer square root of the nonnegative integer n. This is the floor of the
exact square root of n, or equivalently the greatest integer a such that a² ≤ n.
math.lcm(*integers): Return the least common multiple of the specified integer arguments.
math.ldexp(x, i): Return x * (2**i). This is essentially the inverse of function frexp().
math.modf(x): Return the fractional and integer parts of x. Both results carry the sign of x and are
floats.
math.perm(n, k=None): Return the number of ways to choose k items from n items without repetition
and with order.
math.prod(iterable, *, start=1): Calculate the product of all the elements in the input iterable. The
default start value for the product is 1.
math.ulp(x): Return the value of the least significant bit of the float x:
math.exp(x): Return e raised to the power x, where e = 2.718281… is the base of natural logarithms.
This is usually more accurate than math.e ** x or pow(math.e, x).
math.log(x[, base]): With one argument, return the natural logarithm of x (to base e). With two
arguments, return the logarithm of x to the given base
math.log1p(x): Return the natural logarithm of 1+x (base e). The result is calculated in a way which is
accurate for x near zero.
math.log2(x): Return the base-2 logarithm of x. This is usually more accurate than log(x, 2).
math.log10(x): Return the base-10 logarithm of x. This is usually more accurate than log(x, 10).
Trigonometric functions:
math.acos(x): Return the arc cosine of x, in radians. The result is between 0 and pi.
math.asin(x): Return the arc sine of x, in radians. The result is between -pi/2 and pi/2.
math.atan(x): Return the arc tangent of x, in radians. The result is between -pi/2 and pi/2.
math.dist(p, q): Return the Euclidean distance between two points p and q, each given as a sequence
(or iterable) of coordinates. The two points must have the same dimension.
Angular conversion
Special functions
def phi(x): 'Cumulative distribution function for the standard normal distribution' return (1.0 + erf(x
/ sqrt(2.0))) / 2.0
math.erfc(x): Return the complementary error function at x. The complementary error function is
defined as 1.0 - erf(x). It is used for large values of x where a subtraction from one would cause a loss
of significance.
math.lgamma(x): Return the natural logarithm of the absolute value of the Gamma function at x.
Constants
math.inf: A floating-point positive infinity. (For negative infinity, use -math.inf.) Equivalent to the
output of float('inf').
math.nan: A floating-point “not a number” (NaN) value. Equivalent to the output of float('nan')