math — mathematical functions

Python math.isclose for floating-point comparison, plus a few common numeric helpers from the standard-library math module.

モジュールページの本文は現在英語です。

The math module is part of the Python standard library. It provides C-backed mathematical functions on Python floats. It is not a third-party numeric stack such as NumPy, and it is not the statistics module.

Floating-point addition is binary, so 0.1 + 0.2 is not exactly 0.3. Compare inexact results with math.isclose, not with ==, unless you need an exact bit pattern. This page focuses on isclose and a handful of frequent helpers.

Examples were checked against Python 3.13. See the official documentation for the complete API.

Python reference overview →

isclose and floating-point comparison

math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) returns True when the difference is no greater than the larger of the relative and absolute tolerances: abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol).

The default abs_tol is 0.0, so two values near zero must be extremely close relative to their magnitude. Use a non-zero abs_tol when a comparison to 0.0 should succeed for a very small remainder. Following IEEE 754, NaN is not close to anything, including itself, and inf is close only to itself.

math.isclose

math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)

Test whether two floats are close enough under a relative or absolute tolerance. This is a numeric comparison, not an exact equality test.

ParameterDescription
a, bThe values to compare, typically floats.
rel_tolMaximum allowed difference relative to the larger magnitude of a and b. The default is 1e-09.
abs_tolMaximum allowed difference regardless of magnitude. The default is 0.0.

Returns: True if the values are considered close, otherwise False.

  • 0.1 + 0.2 == 0.3 is False, but math.isclose(0.1 + 0.2, 0.3) is True with the default relative tolerance.
  • math.isclose(1e-200, 0.0) is False until you pass a suitable abs_tol, because the default absolute tolerance is 0.0.
  • rel_tol=0.001 treats 1000 and 1001 as close, but not 1000 and 1002, because the allowed difference is 0.001 * 1002.
python
import math

print(0.1 + 0.2 == 0.3, 0.1 + 0.2)
print(math.isclose(0.1 + 0.2, 0.3))
print(math.isclose(1e-200, 0.0), math.isclose(1e-200, 0.0, abs_tol=1e-150))
print(math.isclose(float("nan"), float("nan")))
print(math.isclose(float("inf"), float("inf")))
print(math.isclose(1000, 1001, rel_tol=0.001), math.isclose(1000, 1002, rel_tol=0.001))

Expected output

False 0.30000000000000004
True
False True
False
True
True False

Official documentation →

Selected numeric functions

sqrt, gcd, hypot, and prod are ordinary functions on numbers or iterables of numbers. They operate on Python ints and floats, not on arrays. For sample means and standard deviations, use the statistics module instead.

math.sqrt, math.gcd, math.hypot, math.prod

math.sqrt(x)
math.gcd(*integers)
math.hypot(*coordinates)
math.prod(iterable, *, start=1)

Common scalar helpers: a square root, a greatest common divisor, a Euclidean norm, and a product.

ParameterDescription
xA non-negative number for sqrt. A negative value raises ValueError.
integers / coordinates / iterablegcd takes integers. hypot takes coordinates of a point. prod takes an iterable of numbers and an optional start value.

Returns: sqrt and hypot return floats. gcd returns an int. prod returns the product, with type depending on the inputs and start.

Raises

  • ValueError — sqrt raises ValueError for a negative input. Domain errors in other functions are also reported as ValueError.
  • math.hypot(3, 4) is 5.0, computed in a way that is designed to be more stable than a raw expression such as sqrt(x*x + y*y) for large values.
python
import math

print(math.sqrt(9), math.gcd(24, 18), math.hypot(3, 4), math.prod([2, 3, 4]))

Expected output

3.0 6 5.0 24

Official documentation →