Skip to content

Use compensated summation in sum() like CPython - #9237

Open
jseop-lim wants to merge 1 commit into
RustPython:mainfrom
jseop-lim:sum-compensated
Open

jseop-lim wants to merge 1 commit into
RustPython:mainfrom
jseop-lim:sum-compensated

Conversation

@jseop-lim

@jseop-lim jseop-lim commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

sum() added items one at a time, so every float addition kept its rounding error. CPython's builtin_sum_impl() keeps a compensation term for an exact float or complex accumulator, and since 3.14 it also folds int items into it. test_sum_accuracy covers this but is cpython_only, so it never runs here. CPython treats the algorithm as an implementation detail, though the author hoped other implementations would follow.

AS-IS

>>> sum([0.1] * 10)
0.9999999999999999
>>> sum(b'/%\x97!\x17', 8.962552828027274)
299.96255282802724
>>> sum([2j, 1.0, 1e100, 1.0, -1e100])
2j

TO-BE

>>> sum([0.1] * 10)
1.0
>>> sum(b'/%\x97!\x17', 8.962552828027274)
299.9625528280273
>>> sum([2j, 1.0, 1e100, 1.0, -1e100])
(2+2j)

(matching CPython)

Changes

  • Port the three fast paths of builtin_sum_impl() to builtins.sum. An exact int accumulator sums in an isize until it overflows or meets another type, then an exact float or complex accumulator keeps a Neumaier compensation term. Each path falls back to vm._add at the first item it does not handle. The int path is needed because the default start=0 reaches the float path only through it.
  • Add extra_tests/snippets/builtin_sum.py. Its inputs differ from test_sum_accuracy so 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, plus threading, compared with CPython 3.14.4.

  • 31 hand-written cases cover the hand-over between paths, int, float and complex subclasses with __radd__, isize overflow, inf, nan, signed zeros, an int too large for a float, and Fraction and Decimal items. 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" in extra_tests: 72 pass. The new snippet fails on the unpatched build and passes under CPython 3.14.4.
  • cargo fmt --check clean, cargo clippy -p rustpython-vm --all-targets reports nothing, and pre-commit run --files passes 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

  • Bug Fixes
    • Improved sum accuracy for floating-point and complex-number values, reducing loss of precision during accumulation.
    • Preserved special numeric results, including infinity and negative zero.
    • Ensured mixed numeric inputs continue to fall back to general addition behavior.
    • Improved error handling when numeric conversions cannot be completed.
  • Tests
    • Added regression coverage for precision, complex-number accumulation, mixed numeric types, infinity, and signed-zero behavior.

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
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-20T03:25:01.931980Z 3e53174 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: RustPython/RustPython/.coderabbit.yml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 80b57168-a23f-4364-a6e1-cf9e7433ba70
📥 Commits

Reviewing files that changed from the base of the PR and between 398dd70 and 3e53174.

📒 Files selected for processing: 2
  • crates/vm/src/stdlib/builtins.rs
  • extra_tests/snippets/builtin_sum.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

📝 Walkthrough

The builtin sum now uses integer, compensated float, and compensated complex accumulation paths. It falls back to generic addition for unsupported values and propagates bigint-to-float conversion errors. Regression tests cover mixed types, infinite totals, and signed zero.

Changes: Builtin sum

Layer / File(s) Summary
Sum accumulation and regression coverage
crates/vm/src/stdlib/builtins.rs, extra_tests/snippets/builtin_sum.py
sum adds checked integer accumulation and compensated float and complex accumulation. Unsupported values resume generic addition. Tests cover Fraction inputs, fallback addition, infinite totals, and signed-zero preservation.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: youknowone

Merge Risk: ⚪ Minimal · up to 3e531

The builtin sum change aligns float and complex summation accuracy with CPython and has regression tests. No merge-blocking issue was identified.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: adding compensated summation to sum() based on CPython.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread crates/vm/src/stdlib/builtins.rs
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

@jseop-lim

Copy link
Copy Markdown
Contributor Author

P1 Badge 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

test_sum_accuracy is @support.cpython_only, so it is skipped here and none of the compensation cases run on RustPython. That's why they went into a snippet instead. The other way is to drop cpython_only from that test in Lib/test/test_builtin.py (with a # TODO: RUSTPYTHON note) and delete the snippet, but that means editing the CPython copy, which I understood we try to avoid. Which would you prefer? Either way, I can remove the inf and signed-zero lines that overlap with test_sum.

@jseop-lim

Copy link
Copy Markdown
Contributor Author

CI failure is unrelated: 7 test_socket.BluetoothTest errors (bind(): bad family), which run for the first time on the new ubuntu runner image (20261012 → 20261015) because AF_BLUETOOTH sockets can now be created. An unrelated dependabot PR fails the same way (run).

@youknowone

Copy link
Copy Markdown
Member

@jseop-lim Thank you for both the patch and socket diagnosis!

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.

2 participants