Repository navigation
Use compensated summation in sum() like CPython #9237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import math | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
These compensated float/complex assertions exercise the same paths already covered by AGENTS.md reference: AGENTS.md:L119-L119 Useful? React with 👍 / 👎. |
||
| assert sum(b"/%\x97!\x17", 8.962552828027274) == 299.9625528280273 | ||
| assert sum([0.1, 0.2, 0.3], 0j) == 0.6 + 0j | ||
| assert sum([1j, 0.1, 0.2, 0.3]) == 0.6 + 1j | ||
| assert sum([0.1j, 0.2j, 0.3j]) == 0.6j | ||
|
|
||
| # The first item of another type falls back to plain addition | ||
| assert sum([0.5, Fraction(1, 4)]) == 0.75 | ||
| assert sum([0.5j, Fraction(1, 4)]) == 0.25 + 0.5j | ||
|
|
||
| # The compensation never turns an infinite sum into nan or drops a zero's sign | ||
| assert sum([math.inf, 1.0]) == math.inf | ||
| assert repr(sum([-0.0j], -0.0j)) == "(-0-0j)" | ||
Uh oh!
There was an error while loading. Please reload this page.