Skip to content

Use approximate float comparison for JSON test records#200

Merged
JerBouma merged 2 commits intoJerBouma:mainfrom
wavebyrd:fix-float-precision
Mar 13, 2026
Merged

Use approximate float comparison for JSON test records#200
JerBouma merged 2 commits intoJerBouma:mainfrom
wavebyrd:fix-float-precision

Conversation

@wavebyrd
Copy link
Copy Markdown

Fixes #196.

Python's shortest-repr float formatting varies across versions (and platforms), which causes the test recorder to detect spurious changes in JSON fixture files. For example, a value recorded as 0.011710000000000002 may serialize as 0.01171 in a newer environment -- both represent the same IEEE 754 double, just with different string representations.

What changed:

In tests/conftest.py, the Record class now falls back to math.isclose (relative tolerance 1e-9) when comparing JSON record files whose string representations differ. The exact-match fast path is tried first, so there is zero overhead when strings already match. Non-JSON records (CSV, txt) are unaffected.

How it works:

  • _values_approx_equal recursively walks two JSON-parsed structures, using math.isclose for float-to-float pairs and strict equality for everything else (ints, strings, bools, nulls, structure/length).
  • _json_strings_approx_equal wraps the above with json.loads and catches decode errors (falling back to exact comparison).
  • record_changed checks the file extension and only uses the approximate path for .json records.

No recorded fixtures were changed. No new dependencies.

Carson Jones and others added 2 commits March 12, 2026 23:09
Python's shortest-repr float formatting varies across versions,
causing recorder mismatches on values like 0.011710000000000002
vs 0.01171.  For JSON record files, fall back to math.isclose
when the exact strings differ, using a relative tolerance of 1e-9.

Fixes JerBouma#196
@JerBouma JerBouma merged commit 2c20396 into JerBouma:main Mar 13, 2026
1 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Recorder mismatch: JSON float string representation differs in performance tests

2 participants