Python Glossary Русский

Python 3.5+mathterm

math.isclose

comparison

Checks whether two numbers are approximately equal, within a tolerance.

Syntax

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

The default tolerance is relative — it is computed as a fraction of the larger operand — so comparing against zero never succeeds: math.isclose(1e-12, 0) is False no matter what rel_tol you pass. Whenever one side can be zero or tiny, set abs_tol explicitly. Available since Python 3.5; NaN is never close to anything, itself included, and complex numbers need cmath.isclose.

Examples

import math
print(math.isclose(0.1+0.2, 0.3))  # → True
print(math.isclose(1.0, 1.0000001))  # → False (rel_tol по умолчанию 1e-09, а разница 1e-07)
print(math.isclose(1.0, 1.001))  # → False
print(math.isclose(1.0, 1.001, rel_tol=0.01))  # → True
print(math.isclose(0.0, 0.0))  # → True
print(math.isclose(1e-10, 0, abs_tol=1e-9))  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy