On this page
math
Prev: marshal | Up: stdlib_module | Next: mimetypes
Rel: numbers, cmath, decimal, fractions, random
Ref: docs.python.org — math
Tags: #public #python3 #stdlib
Import
import math
Overview
C-speed real-number mathematical functions (sqrt, isqrt, floor, ceil, log, exp, sin, cos, comb, perm, gcd, lcm, prod, isclose, isfinite) and constants (pi, e, tau, inf, nan). Use math for scalar floating-point and integer math; use decimal for exact currency, statistics for descriptive stats, and cmath for complex numbers.
Quick Start
import math
assert math.isclose(0.1 + 0.2, 0.3)
assert math.isqrt(26) == 5
assert math.gcd(24, 36) == 12
assert math.comb(5, 2) == 10
assert math.prod([2, 3, 4]) == 24
Implementation Walkthrough
1. Safe Floating-Point Comparison & Validation (isclose, isfinite)
Never compare computed floats with ==; use math.isclose() with explicit relative/absolute tolerances, and guard against NaN/inf with math.isfinite():
import math
def safe_normalize(values: list[float]) -> list[float]:
if not values or not all(math.isfinite(v) for v in values):
raise ValueError("All values must be finite numbers")
total = math.fsum(values) # high-precision summation
if math.isclose(total, 0.0, abs_tol=1e-12):
raise ValueError("Sum is zero")
return [v / total for v in values]
res = safe_normalize([0.1, 0.2, 0.7])
assert math.isclose(math.fsum(res), 1.0)
2. Integer Geometry & Exact Combinatorics
import math
assert math.hypot(3, 4) == 5.0
assert math.dist((0, 0), (3, 4)) == 5.0
assert math.lcm(12, 18) == 36
Gotchas & Boundaries
math.isclose(a, 0.0)usesabs_tol=0.0by default: When comparing against zero, always pass an explicitabs_tol(e.g.,abs_tol=1e-9).- Trigonometric functions expect radians, not degrees: Convert with
math.radians()andmath.degrees(). - Use
math.isqrt(n)for exact integer square roots:int(math.sqrt(n))loses precision for integers above $2^{53}$. math.isnan(x)is required to test forNaN:float("nan") == float("nan")is alwaysFalse.
Common Functions
- math.acos() —
acos(x, /)— Return the arc cosine (measured in radians) of x. - math.acosh() —
acosh(x, /)— Return the inverse hyperbolic cosine of x. - math.asin() —
asin(x, /)— Return the arc sine (measured in radians) of x. - math.asinh() —
asinh(x, /)— Return the inverse hyperbolic sine of x. - math.atan() —
atan(x, /)— Return the arc tangent (measured in radians) of x. - math.atan2() —
atan2(y, x, /)— Return the arc tangent (measured in radians) of y/x. - math.atanh() —
atanh(x, /)— Return the inverse hyperbolic tangent of x. - math.cbrt() —
cbrt(x, /)— Return the cube root of x. - math.ceil() —
ceil(x, /)— Return the ceiling of x as an Integral. - math.comb() —
comb(n, k, /)— Number of ways to choose k items from n items without repetition and without order. - math.copysign() —
copysign(x, y, /)— Return a float with the magnitude (absolute value) of x but the sign of y. - math.cos() —
cos(x, /)— Return the cosine of x (measured in radians). - math.cosh() —
cosh(x, /)— Return the hyperbolic cosine of x. - math.degrees() —
degrees(x, /)— Convert angle x from radians to degrees. - math.dist() —
dist(p, q, /)— Return the Euclidean distance between two points p and q. - math.erf() —
erf(x, /)— Error function at x. - math.erfc() —
erfc(x, /)— Complementary error function at x. - math.exp() —
exp(x, /)— Return e raised to the power of x. - math.exp2() —
exp2(x, /)— Return 2 raised to the power of x. - math.expm1() —
expm1(x, /)— Return exp(x)-1. - math.fabs() —
fabs(x, /)— Return the absolute value of the float x. - math.factorial() —
factorial(n, /)— Find n!. - math.floor() —
floor(x, /)— Return the floor of x as an Integral. - math.fma() —
fma(x, y, z, /)— Fused multiply-add operation. - math.fmod() —
fmod(x, y, /)— Return fmod(x, y), according to platform C. - math.frexp() —
frexp(x, /)— Return the mantissa and exponent of x, as pair (m, e). - math.fsum() —
fsum(seq, /)— Return an accurate floating-point sum of values in the iterable seq. - math.gamma() —
gamma(x, /)— Gamma function at x. - math.gcd() —
gcd(*integers)— Greatest Common Divisor. - math.hypot() —
hypot(*coordinates)— Multidimensional Euclidean distance from the origin to a point. - math.isclose() —
isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)— Determine whether two floating-point numbers are close in value. - math.isfinite() —
isfinite(x, /)— Return True if x is neither an infinity nor a NaN, and False otherwise. - math.isinf() —
isinf(x, /)— Return True if x is a positive or negative infinity, and False otherwise. - math.isnan() —
isnan(x, /)— Return True if x is a NaN (not a number), and False otherwise. - math.isqrt() —
isqrt(n, /)— Return the integer part of the square root of the input.
Constants & Attributes
math.e(float) =2.718281828459045math.inf(float) =infmath.nan(float) =nanmath.pi(float) =3.141592653589793math.tau(float) =6.283185307179586
Linked from
- tuts/python3
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/cmath
- tuts/python3/stdlib_module/decimal
- tuts/python3/stdlib_module/fractions
- tuts/python3/stdlib_module/marshal
- tuts/python3/stdlib_module/math/acos
- tuts/python3/stdlib_module/math/acosh
- tuts/python3/stdlib_module/math/asin
- tuts/python3/stdlib_module/math/asinh
- tuts/python3/stdlib_module/math/atan
- tuts/python3/stdlib_module/math/atan2
- tuts/python3/stdlib_module/math/atanh
- tuts/python3/stdlib_module/math/cbrt
- tuts/python3/stdlib_module/math/ceil
- tuts/python3/stdlib_module/math/comb
- tuts/python3/stdlib_module/math/copysign
- tuts/python3/stdlib_module/math/cos
- tuts/python3/stdlib_module/math/cosh
- tuts/python3/stdlib_module/math/degrees
- tuts/python3/stdlib_module/math/dist
- tuts/python3/stdlib_module/math/erf
- tuts/python3/stdlib_module/math/erfc
- tuts/python3/stdlib_module/math/exp
- tuts/python3/stdlib_module/math/exp2
- tuts/python3/stdlib_module/math/expm1
- tuts/python3/stdlib_module/math/fabs
- tuts/python3/stdlib_module/math/factorial
- tuts/python3/stdlib_module/math/floor
- tuts/python3/stdlib_module/math/fma
- tuts/python3/stdlib_module/math/fmod
- tuts/python3/stdlib_module/math/frexp
- tuts/python3/stdlib_module/math/fsum
- tuts/python3/stdlib_module/math/gamma
- tuts/python3/stdlib_module/math/gcd
- tuts/python3/stdlib_module/math/hypot
- tuts/python3/stdlib_module/math/isclose
- tuts/python3/stdlib_module/math/isfinite
- tuts/python3/stdlib_module/math/isinf
- tuts/python3/stdlib_module/math/isnan
- tuts/python3/stdlib_module/math/isqrt
- tuts/python3/stdlib_module/mimetypes
- tuts/python3/stdlib_module/numbers
- tuts/python3/stdlib_module/random
- tuts/python3/stdlib_module/statistics
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/tutorials/core_fundamentals/primitive_data_types
- tuts/python3/tutorials/popular_application_domains/data_science_and_machine_learning
Related notes
- tuts/python3/stdlib_module/cmath Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/decimal Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/fractions Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/marshal Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/math/acos Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/math/acosh Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/math/asin Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/math/asinh Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/math/atan Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/math/atan2 Linked here; links to this page; shared tags: #stdlib