math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) returns True if a and b are close enough to count as equal, solving the classic floating-point pitfall where 0.1 + 0.2 == 0.3 is False.
Added in Python 3.5 (PEP 485), it tests abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol) using keyword-only tolerances defaulting to 1e-09 relative and 0.0 absolute.
math.isclose() is a function in Python's standard math module: import it with import math and call math.isclose(...), or import the name directly with from math import isclose. Added in Python 3.5.
Syntax
Import math and pass two numbers, plus optional keyword-only tolerances.
python
import math
ok = math.isclose(a, b)
near_zero = math.isclose(a, 0.0, abs_tol=1e-12)
Call
What it does
math.isclose(0.1 + 0.2, 0.3)
Returns True.
math.isclose(1e-12, 0.0)
Returns False (default abs_tol is 0.0).
math.isclose(1e-12, 0.0, abs_tol=1e-09)
Returns True.
math.isclose(float("nan"), float("nan"))
Returns False.
Because default abs_tol is 0.0, comparing any nonzero tiny number against 0.0 returns False unless you pass an explicit abs_tol=.
Function signatures
python
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
# added in Python 3.5; rel_tol and abs_tol are keyword-only
0.1 + 0.2 == 0.3: False
isclose : True
near 0 default : False
near 0 abs_tol : True
How It Works
Exact == fails on binary sums. Because 0.1 + 0.2 evaluates to 0.30000000000000004, strict == returns False.
Default 1e-09 relative check passes.math.isclose() allows 9 digits of relative slack and returns True.
Comparisons to 0.0 need abs_tol. When one side is 0.0, relative tolerance scales against abs(residual) itself (1e-9 * 1.22e-16), so pass abs_tol=1e-12.
Example 2 — Set Custom Relative and Absolute Tolerances for Tests
Apply a 5% relative margin on measurements and a 1-cent absolute margin on currency.
python
import math
print("within 5% (104 vs 100):", math.isclose(100.0, 104.0, rel_tol=0.05))
print("within 5% (106 vs 100):", math.isclose(100.0, 106.0, rel_tol=0.05))
print("within 1 cent:", math.isclose(19.991, 19.999, abs_tol=0.01))
Output
within 5% (104 vs 100): True
within 5% (106 vs 100): False
within 1 cent: True
How It Works
Relative margin scales with magnitude. Five percent of 104.0 is 5.2, so 100.0 and 104.0 pass while 106.0 fails.
Absolute margin stays constant. Setting abs_tol=0.01 compares raw distance independently of price size.
Largest tolerance wins. Passing both checks whichever tolerance gives the wider window.
Example 3 — Check Infinity, NaN, and Negative Tolerance Errors
Inspect special float rules and catch ValueError on negative tolerances.
python
import math
inf = float("inf")
nan = float("nan")
print(math.isclose(inf, inf), math.isclose(inf, -inf))
print(math.isclose(nan, nan))
try:
math.isclose(1.0, 1.0, rel_tol=-0.1)
except ValueError:
print("tolerances must be non-negative")
Python 3.5. For complex numbers, cmath.isclose() was added alongside it.
Because default abs_tol is 0.0 and rel_tol * max(|a|, |b|) equals 1e-09 * 1e-12, which is far smaller than 1e-12. Pass abs_tol=1e-09 when one operand may be zero.
Yes, math.isclose(a, b) always equals math.isclose(b, a).
rel_tol=1e-09 (roughly 9 decimal digits of precision) and abs_tol=0.0.
To avoid mix-ups with positional numbers; calling math.isclose(1.0, 1.0, 1e-5) raises TypeError.