Pytest approx: Comparing Floats With rel, abs and Collections
Pytest approx compares floating-point results within a tolerance, so 0.1 + 0.2 equals 0.3 in your tests the way it does in maths. We tested its default tolerances right at their boundaries, rel and abs, lists and dicts, NaN and infinity, NumPy arrays and the near-zero trap, and compared it with math.isclose and numpy.allclose, which disagree on the same numbers.
- Pytest approx passes when values are within a relative tolerance of 1e-6 or an absolute tolerance of 1e-12, whichever is larger.
- 1,000,000.9 matched 1,000,000 but 1,000,001.1 did not, exactly at the one-in-a-million boundary.
- Near zero, only the tiny absolute tolerance applies: 1e-11 did not match 0 until we passed
abs=1e-9. numpy.allclosecalled 1e-11 and 0 equal, while approx andmath.isclosedid not.
Floating-point numbers are stored in binary, so most decimal fractions are approximations: 0.1 + 0.2 is 0.30000000000000004. Any test that checks a calculated float with == is fragile, failing on tiny rounding differences between platforms, library versions or the order of operations. Pytest approx solves this by comparing within a tolerance, with readable syntax and clear failure messages.
All tests ran with pytest 9.2.0 on Python 3.13.6; the NumPy checks used NumPy 2.5.5. Every assertion below passed unless marked as a deliberate failure.
Why == fails for floats
def test_plain_equality_fails():
assert 0.1 + 0.2 != 0.3 # passes: they really are different
Neither 0.1 nor 0.2 has an exact binary representation, and the tiny errors add up. Comparing with == asks whether the bits are identical, which is almost never what a test means. A test of a tax calculation, a statistics function or a physics formula should ask whether the answer is close enough.
Basic use
import pytest
def test_approx_default():
assert 0.1 + 0.2 == pytest.approx(0.3)
Wrap the expected value in pytest.approx and compare with == (or !=). It reads like the maths and works inside any assertion. Integers work too: 3 == pytest.approx(3.0000001) passed.
Default tolerances, tested at the boundary
def test_rel_default_boundary():
assert 1_000_000 + 0.9 == pytest.approx(1_000_000) # within 1e-6 relative
assert 1_000_000 + 1.1 != pytest.approx(1_000_000) # just outside
By default, approx uses a relative tolerance of 1e-6 (one part in a million) of the expected value. For 1,000,000, the allowed difference is 1.0, so 0.9 away passed and 1.1 away failed. Relative tolerance scales with the size of the number, which is usually what you want: a millimetre matters for a bolt, not for the distance to the Moon.
The near-zero trap
def test_near_zero():
assert 1e-13 == pytest.approx(0)
assert 1e-11 != pytest.approx(0)
assert 1e-11 == pytest.approx(0, abs=1e-9)
A relative tolerance of anything times zero is zero, so for an expected value of 0 only the default absolute tolerance of 1e-12 applies. 1e-13 passed, but 1e-11, which is tiny by any practical measure, did not. Results that should be zero often come out as 1e-10 or 1e-16 after floating-point arithmetic, such as math.sin(math.pi) or a balance of debits and credits. When the expected value is or might be zero, pass an explicit abs that matches your domain.
Setting rel and abs
def test_rel_abs():
assert 101 == pytest.approx(100, rel=0.02) # within 2 percent
assert 103 != pytest.approx(100, rel=0.02)
assert 100.4 == pytest.approx(100, abs=0.5) # within 0.5 either way
rel is a fraction of the expected value and abs is a fixed amount. The combinations behave differently, which we confirmed: passing only abs switches the relative tolerance off completely (1,000,000.9 failed against approx(1_000_000, abs=0.5) even though it is within one part in a million), passing only rel keeps the default absolute tolerance of 1e-12, and passing both lets a value through when it is within either tolerance. Negative or NaN tolerances raise ValueError, according to pytest’s own test suite. Choose rel for quantities that vary in size (prices, distances, durations) and abs for values with a known resolution (sensor readings to 0.1 degree, probabilities near zero).
Lists, tuples and dicts
def test_collections():
assert [0.1 + 0.2, 0.2 + 0.4] == pytest.approx([0.3, 0.6])
assert {"a": 0.1 + 0.2, "b": 1.0} == pytest.approx({"a": 0.3, "b": 1.0})
assert (1/3, 2/3) == pytest.approx((0.3333333, 0.6666667))
approx compares sequences element by element and dictionaries key by key, applying the same tolerance to every number. Lengths and keys must match exactly. This keeps tests of functions that return several floats, like statistics summaries or coordinates, to a single readable assertion. Combine it with parametrized fixtures from our pytest fixtures guide to check many cases at once.
NaN and infinity
def test_nan():
assert math.nan != pytest.approx(math.nan)
assert math.nan == pytest.approx(math.nan, nan_ok=True)
def test_inf():
assert math.inf == pytest.approx(math.inf)
NaN (“not a number”) is never equal to anything, including itself, and approx follows that rule unless you pass nan_ok=True, useful when a function is supposed to return NaN for missing data. Infinity equals only infinity of the same sign; no tolerance makes a finite number match it.
NumPy arrays
a = np.array([0.1, 0.2]) * 3 # [0.30000000000000004, 0.6000000000000001]
a == pytest.approx([0.3, 0.6]) # True
np.allclose(a, [0.3, 0.6]) # True
approx understands NumPy arrays and compares them element-wise, so a single assert covers a whole array. In our pytest venv NumPy was not installed and that test was skipped with pytest.importorskip("numpy"), a handy way to make optional-dependency tests safe; we ran the NumPy checks with a separate interpreter that had NumPy 2.5.5.
pytest.approx vs math.isclose vs numpy.allclose
math.isclose(1e-11, 0) # False (rel 1e-9, abs 0)
1e-11 == pytest.approx(0) # False (rel 1e-6, abs 1e-12)
np.allclose([1e-11], [0]) # True (rtol 1e-5, atol 1e-8)
| Function | Default relative | Default absolute |
|---|---|---|
pytest.approx |
1e-6 | 1e-12 |
math.isclose |
1e-9 | 0 |
numpy.allclose / isclose |
1e-5 | 1e-8 |
The three standard tools gave different answers for the same pair of numbers because their defaults differ by orders of magnitude. math.isclose has no absolute tolerance at all by default, so nothing is close to zero; NumPy is the most forgiving. When a team mixes them, the same comparison can pass in one test and fail in another. Pick explicit tolerances when it matters, rather than relying on any default.
Reading failure messages
assert [1.0, 2.0, 3.0] == pytest.approx([1.0, 2.1, 3.0]) # deliberate failure
E comparison failed. Mismatched elements: 1 / 3:
E Max absolute difference: 0.10000000000000009
E Max relative difference: 0.050000000000000044
E Index | Obtained | Expected
E 1 | 2.0 | 2.1 ± 2.1e-06
On failure, pytest shows how many elements mismatched, the largest absolute and relative differences and a table of the failing positions with the allowed tolerance (± 2.1e-06, which is 1e-6 of 2.1). That makes it easy to see whether a test is slightly too strict or the code is genuinely wrong.
What approx cannot do
def test_wrong_side():
with pytest.raises(TypeError):
assert pytest.approx(1) > 0.5
approx supports only equality and inequality. Ordering comparisons such as > raise TypeError, which we caught with pytest.raises; for “at least” checks, compare the raw numbers with a margin yourself. approx also does not handle strings, and nested structures beyond simple sequences and mappings may not compare as you expect.
Money is not a float problem
def test_money_wrong():
assert 0.1 * 3 == pytest.approx(0.3) # passes, but hides the real issue
from decimal import Decimal
assert Decimal("0.1") * 3 == Decimal("0.3") # exact
If a test needs approx to check a currency total, the code under test probably should not use floats for money. Decimal or integer cents give exact results and exact assertions. Rounding rules for stored amounts are covered in our SQL ROUND guide.
Choosing tolerances
- Start from the domain: how precise is the input data and how precise does the answer need to be?
- Use abs near zero and rel for values that scale.
- Do not loosen tolerances to silence a failing test without understanding why it failed.
- Keep tolerances in one place, for example a constant or fixture, so they are consistent across a suite.
- Generate inputs with property-based testing to find the cases where your tolerance is too tight, before they become flaky tests on another platform.
Final thoughts
Pytest approx replaces fragile float equality with tolerant, readable assertions. Remember its defaults (1e-6 relative, 1e-12 absolute), set an explicit abs whenever zero is a possible answer, use it directly on lists, dicts and NumPy arrays, and do not assume other tools share its defaults: in our test, three standard functions gave two different answers for the same numbers. For money, fix the data type rather than the assertion.
Frequently asked questions
What is the default tolerance of pytest approx?
A relative tolerance of one millionth of the expected value, or an absolute tolerance of 1e-12, whichever is larger. So for large numbers the relative part dominates, and near zero only the absolute part applies.
How do I compare a float to zero in pytest?
Pass an explicit absolute tolerance, for example pytest.approx(0, abs=1e-9). The default absolute tolerance is only 1e-12, so tiny rounding errors near zero otherwise fail.
Can pytest approx compare lists and dictionaries?
Yes. It compares sequences element by element and dictionaries key by key, applying the same tolerance to each number. Lengths and keys must match exactly.
What is the difference between rel and abs in pytest approx?
rel is a fraction of the expected value, so the allowed error grows with the number. abs is a fixed amount. Passing only abs turns relative tolerance off, while passing both lets a value pass if it is within either one.
Why does pytest approx fail with NaN?
NaN is never equal to anything, including itself. Pass nan_ok=True to pytest.approx if your test expects NaN as a valid result.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.