math.isclose
Check that two numbers are close to each other within a tolerance.
Python 3.13
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)- =default value
- *arguments after it by name only
- a
- The first number.int | float
- b
- The second number.int | float
- rel_tol
- The allowed relative difference, 1e-09 by default, which is about 9 matching significant digits.floatOptional
- abs_tol
- The allowed absolute difference, 0.0 by default; needed for comparison with zero.floatOptional
Return value
True if the numbers are close, otherwise False.
Examples
Comparing floats
Python 3.13
import math
print(0.1 + 0.2 == 0.3, math.isclose(0.1 + 0.2, 0.3))False True
Comparing with zero using abs_tol
Python 3.13
import math
print(math.isclose(1e-10, 0), math.isclose(1e-10, 0, abs_tol=1e-9))False True
Details
Floats are stored approximately, so 0.1 + 0.2 == 0.3 gives False. Relative tolerance does not work with zero: any non-zero number is far from zero in relative terms, so set abs_tol for such a comparison.
