Python math.isclose() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Python 3.5+

Definition

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)
CallWhat 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

Parameters

ParameterDescription
aRequired first number (int or float).
bRequired second number (int or float).
rel_tolOptional keyword-only non-negative float relative tolerance. Default 1e-09.
abs_tolOptional keyword-only non-negative float minimum absolute tolerance. Default 0.0.

Return Value

Return ValueDescription
boolTrue if a and b lie within either tolerance, else False.

Python Examples

Example 1 — Compare 0.1 + 0.2 With 0.3 and Handle Near-Zero Residuals

See why isclose() rescues 0.1 + 0.2 == 0.3 and why comparisons to 0.0 need abs_tol.

python
import math

print("0.1 + 0.2 == 0.3:", 0.1 + 0.2 == 0.3)
print("isclose         :", math.isclose(0.1 + 0.2, 0.3))

residual = math.sin(math.pi)
print("near 0 default  :", math.isclose(residual, 0.0))
print("near 0 abs_tol  :", math.isclose(residual, 0.0, abs_tol=1e-12))

How It Works

  1. Exact == fails on binary sums. Because 0.1 + 0.2 evaluates to 0.30000000000000004, strict == returns False.
  2. Default 1e-09 relative check passes. math.isclose() allows 9 digits of relative slack and returns True.
  3. 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))

How It Works

  1. Relative margin scales with magnitude. Five percent of 104.0 is 5.2, so 100.0 and 104.0 pass while 106.0 fails.
  2. Absolute margin stays constant. Setting abs_tol=0.01 compares raw distance independently of price size.
  3. 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")

How It Works

  1. Same-sign infinities match. Two positive infinities return True, whereas opposing signs return False.
  2. NaN is never close. Even isclose(nan, nan) evaluates to False.
  3. Negative tolerance rejected. Passing rel_tol < 0 or abs_tol < 0 raises ValueError.

Frequently Asked Questions

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.

Next: math.isfinite()

Return True if x is neither an infinity nor a NaN.

isfinite() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful