Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you compare floats safely in Python?

Python
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 25, 2026
4 min read

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.

Table of Contents

  • Why == fails for floats
  • Using math.isclose
  • Comparing against zero needs abs_tol
  • Choosing tolerances
  • Other ways to compare floats
  • When not to use a tolerance
  • Frequently Asked Questions
    • Why not just round both numbers and compare?
    • What tolerance should I use?
    • How do I compare floats with infinity or NaN?
    • Does isclose work with integers and Decimals?
  • Summary

Why == fails for floats

Most decimal fractions are stored as slightly rounded binary values, so results of arithmetic often differ from the number you expect:

python
text

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.

Using math.isclose

math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) has existed since Python 3.5. It returns True when:

text
python
text

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.

Comparing against zero needs abs_tol

A relative tolerance is a fraction of the numbers' size. If one number is zero, the allowed difference is zero too:

python
text

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.

Choosing tolerances

  1. Decide what accuracy the real-world quantity needs, for example 0.001 metres.
  2. Use rel_tol for values that span many magnitudes.
  3. Add abs_tol for values that may be at or near zero.
  4. Keep tolerances larger than the machine epsilon (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).

Other ways to compare floats

Different libraries use different defaults, so you can get different answers for the same numbers.

ToolDefault toleranceNotes
math.iscloserel_tol=1e-09, abs_tol=0.0Symmetric, standard library
unittest assertAlmostEqual7 decimal placesAbsolute difference, rounded
pytest.approxrelative 1e-6, absolute 1e-12Works with lists and dicts too
numpy.isclosertol=1e-05, atol=1e-08Asymmetric: scales by the second argument
cmath.isclosesame as math.iscloseFor complex numbers
python

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.

When not to use a tolerance

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

Frequently Asked Questions

Why not just round both numbers and compare?

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.

What tolerance should I use?

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.

How do I compare floats with infinity or NaN?

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.

Does isclose work with integers and Decimals?

It accepts integers and floats. For Decimal or Fraction values compare directly, since they are exact.

Summary

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.

Share

Similar posts

  • How do you format numbers with f-strings in Python?
  • How do you use f-strings in Python?
  • What are escape sequences in Python strings?
  • Why are strings immutable in Python?
  • How do you check if a string is a number in Python?
  • How do you count occurrences of a substring in Python?
  • How do you check if a string contains a substring in Python?
  • How do you change the case of a string in Python?