math — Mathematical functions


This module provides access to common mathematical functions and constants, including those defined by the C standard.

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. 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.

Floating point arithmetic

ceil(x)

Ceiling of x, the smallest integer greater than or equal to 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

fmax(x, y)

Maximum of two floating-point values

fmin(x, y)

Minimum of two floating-point values

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

isnormal(x)

Check if x is a normal number

issubnormal(x)

Check if x is a subnormal number

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

signbit(x)

Check if x is a negative number

ulp(x)

Value of the least significant bit of x

Power, exponential and logarithmic functions

cbrt(x)

Cube root of x

exp(x)

e raised to the power x

exp2(x)

2 raised to the power x

expm1(x)

e raised to the power x, minus 1

log(x, base)

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

log1p(x)

Natural logarithm of 1+x (base e)

log2(x)

Base-2 logarithm of x

log10(x)

Base-10 logarithm of x

pow(x, y)

x raised to the power y

sqrt(x)

Square root of x