To compare floats safely in Python, use math.isclose(a, b) instead of ==. It treats two numbers as equal when they differ by less than a relative or absolute tolerance, which absorbs the tiny rounding errors that binary floating-point arithmetic produces. For compare floats Python code that checks against zero, you must also pass abs_tol, because a relative tolerance alone never matches zero.
This article explains how math.isclose works, how to choose tolerances, and how the alternatives in unittest, pytest and NumPy differ. It targets Python 3.13 and 3.14.
Most decimal fractions are stored as slightly rounded binary values, so results of arithmetic often differ from the number you expect:
The difference is about one unit in the last place. An equality check treats it as a mismatch, even though the values are as close as doubles allow. The fix is to ask whether the numbers are close enough for your purpose.
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) has existed since Python 3.5. It returns True when:
The default rel_tol of 1e-09 means the values must agree to about nine significant digits. The check is symmetric: isclose(a, b) equals isclose(b, a), because it scales by the larger magnitude.
A relative tolerance is a fraction of the numbers' size. If one number is zero, the allowed difference is zero too:
Whenever the expected value might be zero, set abs_tol to the smallest difference you consider meaningful in your domain's units. There is no universal value; 1e-9 is reasonable for values near 1, but wrong for values measured in nanometres.
rel_tol for values that span many magnitudes.abs_tol for values that may be at or near zero.sys.float_info.epsilon, about 2.2e-16) times the number of operations that contribute error.For ordering checks, such as "is a less than or equal to b", combine both: a < b or math.isclose(a, b).
Different libraries use different defaults, so you can get different answers for the same numbers.
| Tool | Default tolerance | Notes |
|---|---|---|
math.isclose | rel_tol=1e-09, abs_tol=0.0 | Symmetric, standard library |
unittest assertAlmostEqual | 7 decimal places | Absolute difference, rounded |
pytest.approx | relative 1e-6, absolute 1e-12 | Works with lists and dicts too |
numpy.isclose | rtol=1e-05, atol=1e-08 | Asymmetric: scales by the second argument |
cmath.isclose | same as math.isclose | For complex numbers |
In NumPy, np.isclose(a, b) is not symmetric and its default atol=1e-08 quietly makes tiny numbers match each other, which surprises many people. Prefer np.allclose with explicit tolerances in tests.
Tolerances fit measurements and computed results. They are the wrong tool for money or counts. For currency use decimal.Decimal, and for exact ratios use fractions.Fraction; both compare exactly with ==.
You also should not use floats as dictionary keys or in sets when they come from calculations, because hashing depends on exact equality. Round to a fixed number of places first or use integers (for example cents).
Rounding can split two nearly equal values onto different sides of a rounding boundary, for example 0.2499999 and 0.2500001 rounded to one place. A tolerance check does not have this edge problem.
Pick it from the accuracy your problem needs. For double precision a relative tolerance of 1e-9 is a safe default for a handful of operations; for zero use an explicit abs_tol.
math.isclose(math.inf, math.inf) is True, and math.nan is never close to anything, including itself. Use math.isnan() to test for NaN.
It accepts integers and floats. For Decimal or Fraction values compare directly, since they are exact.
Never rely on == for floats that came out of arithmetic. Use math.isclose with a rel_tol that matches your needed accuracy, add abs_tol for comparisons with zero, and remember that unittest, pytest and NumPy each have different defaults. For money or exact ratios, switch to Decimal or Fraction.