Repository navigation
Conversation
CPython keeps a Neumaier compensation term while sum() accumulates an exact float or complex, and reaches that path from an exact int start too. RustPython added items one by one, so sum([0.1] * 10) returned 0.9999999999999999 instead of 1.0. Port the int, float and complex fast paths of builtin_sum_impl, each falling back to plain addition at the first item it does not handle. Assisted-by: Claude Code:claude-opus-5-5
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3e53174d62
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| from fractions import Fraction | ||
|
|
||
| # Float and complex accumulators keep a compensation term, as in CPython 3.14 | ||
| assert sum([0.1, 0.2, 0.3]) == 0.6 |
There was a problem hiding this comment.
Reuse the existing sum-accuracy coverage
These compensated float/complex assertions exercise the same paths already covered by Lib/test/test_builtin.py::BuiltinTest.test_sum_accuracy (lines 2261-2270), while the infinity and signed-zero cases are also covered by test_sum (lines 2210-2255). Merely changing operands does not avoid duplicate coverage, so this new snippet conflicts with the repository rule and creates a parallel suite to maintain; reuse the existing Lib/test coverage instead.
AGENTS.md reference: AGENTS.md:L119-L119
Useful? React with 👍 / 👎.
|
|
CI failure is unrelated: 7 |
|
@jseop-lim Thank you for both the patch and socket diagnosis! |
Summary
sum()added items one at a time, so every float addition kept its rounding error. CPython'sbuiltin_sum_impl()keeps a compensation term for an exactfloatorcomplexaccumulator, and since 3.14 it also foldsintitems into it.test_sum_accuracycovers this but iscpython_only, so it never runs here. CPython treats the algorithm as an implementation detail, though the author hoped other implementations would follow.AS-IS
TO-BE
(matching CPython)
Changes
builtin_sum_impl()tobuiltins.sum. An exactintaccumulator sums in anisizeuntil it overflows or meets another type, then an exactfloatorcomplexaccumulator keeps a Neumaier compensation term. Each path falls back tovm._addat the first item it does not handle. Theintpath is needed because the defaultstart=0reaches the float path only through it.extra_tests/snippets/builtin_sum.py. Its inputs differ fromtest_sum_accuracyso the two do not overlap.Test plan
Built and run on macOS aarch64 against upstream/main 398dd70, using the CI feature set
--no-default-features --features stdlib,importlib,stdio,encodings,sqlite,ssl-rustls-aws-lc,host_env,doc,ucd_3_2_0, plusthreading, compared with CPython 3.14.4.int,floatandcomplexsubclasses with__radd__,isizeoverflow, inf, nan, signed zeros, aninttoo large for a float, andFractionandDecimalitems. An unpatched build of that commit differs from CPython on 5 of them and on 614 of 5000 random mixes of ints, floats and complexes. With the patch all of them match.rustpython -m test test_builtin test_math test_statistics test_fractions test_complex test_float test_itertools test_functools: 1262 run, 51 skipped, all pass.pytest -k "rustpython and builtin"inextra_tests: 72 pass. The new snippet fails on the unpatched build and passes under CPython 3.14.4.cargo fmt --checkclean,cargo clippy -p rustpython-vm --all-targetsreports nothing, andpre-commit run --filespasses on both changed files.I used Claude Code (claude-opus-5-5) to draft the analysis, the patch and this description; I reviewed the change and ran every command above myself.
Summary by CodeRabbit
sumaccuracy for floating-point and complex-number values, reducing loss of precision during accumulation.