math
Real-number functions and constants. Most functions accept an int or
float and return a float; exceptions are noted below.
Arguments to native functions are positional.
Available API
Trigonometric arguments and results use radians. modf(x) returns
(fractional_part, integer_part), both floats; pass a float to this function.
copysign(x, y) takes the magnitude of x and the sign of y.
import math
assert math.ceil(1.2) == 2
assert type(math.floor(1.8)) is int
assert math.gcd(18, 24) == 6
assert math.fsum([0.1] * 10) == 1.0
assert math.isclose(math.sin(math.pi / 2), 1.0)
assert math.modf(2.5) == (0.5, 2.0)
Differences from CPython
-
fsumaccepts a list, not an arbitrary iterable, and uses compensated summation rather than CPython's full algorithm. -
isclose(a, b)checksabs(a - b) < 1e-9. It has no relative or absolute tolerance keyword arguments; infinities do not compare close by this rule. -
gcdtakes exactly two arguments. -
factorialis limited by signed 64-bit integers. Keepn <= 20for an exact representable result. -
Domain handling follows the implementation's math functions and may produce NaN or infinity instead of CPython exceptions.
For complex values, use cmath. Implementation: math.c.