8000 fix: float sums give the same digits on Python 3.11 and 3.12+ by rafatahmed · Pull Request #341 · rafatahmed/Flahax · GitHub
[go: up one dir, main page]

Skip to content

fix: float sums give the same digits on Python 3.11 and 3.12+ - #341

Merged
rafatahmed merged 2 commits into
mainfrom
fix/sum-across-python-versions
Nov 27, 2026
Merged

rafatahmed merged 2 commits into
mainfrom
fix/sum-across-python-versions

Conversation

@rafatahmed
Copy link
Copy Markdown
Owner

Closes #334

Python 3.12 made the built-in sum compensated (Neumaier) for floats; 3.11 adds left to right, so the same inputs gave results that differ in the last digits. The package now sums through flahax.float_sum:

  • On 3.12 and later it is the built-in sum (same object), so nothing changes there.
  • On 3.11 it is a pure-Python copy of the 3.12 algorithm, so 3.11 now gives the 3.12+ digits.
  • All 152 sum( calls in 39 modules of src/flahax use it; a test fails if a bare sum( is added again.

This is option 3 on the issue, not math.fsum: fsum would change the digits on every version and slow the solver's hot loop.

Evidence

  • Digests of recommend (three target sets), aqueous solves of the four PHREEQC fixtures and the delivery example: before, 3.11.17 differed from 3.12.11, 3.13.15 and 3.14.0; after, all four are identical, and the 3.12+ digests did not change.
  • The copy was checked against the real built-in sum on 300,000 random cases (floats, ints, bools, inf, nan, -0.0, mixed, different starts): 0 mismatches on 3.12.11 and 3.13.15.
  • 215 test modules pass on 3.13; 79 tests of nine numeric modules pass on 3.11.17. The heavy modules run in CI.
  • New tests/test_float_sum.py pins 23 vectors taken from the 3.13 built-in, on every Python.
  • Records in outputs/test-runs/2026-11-27-xver-fix/ (local).

Known limits

  • 3.11 is slower. The pure-Python sum costs about 3x per call; an aqueous solve goes from 3.1 to 5.2 ms (+68%) on 3.11. 3.12 and later are unchanged. I tried a tighter version; it is no faster.
  • 3.14 differs from 3.12/3.13 for ints that follow a float (3.14 also compensates those). Not touched here; none of the workflows I compared hit it (all four interpreters give identical digests).
  • float_sum repeats the integer handling of 3.12/3.13 for ints below 263; CPython itself uses the platform long there (32 bit on Windows), which only matters for ints above 231 added after a float.

@rafatahmed rafatahmed added the bug Something isn't working label Nov 27, 2026
@rafatahmed rafatahmed self-assigned this Nov 27, 2026
@codecov
codecov Bot commented Nov 27, 2026
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (e741d1e) to head (db43056).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #341   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          148       149    +1     
  Lines        17515     17588   +73     
  Branches      3550      3561   +11     
=========================================
+ Hits         17515     17588   +73     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rafatahmed
rafatahmed marked this pull request as ready for review November 27, 2026 01:22
@rafatahmed
rafatahmed merged commit 3fd4117 into main Nov 27, 2026
29 checks passed
@rafatahmed
rafatahmed deleted the fix/sum-across-python-versions branch November 27, 2026 01:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Results differ in the last digits between Python 3.11 and 3.12+ (the built-in sum changed)

1 participant

0