Use CPython's compensated summation for floats and complexes in `sum()`

LI
1mo ago
+229-10

Since 3.12, CPython's sum() adds floats with Neumaier's compensated summation, and 3.14 does the same for complexes.
Monty added the items left to right with +, so ordinary sums came out different from CPython:

Monty mainCPython 3.14
sum([0.1, 0.2, 0.3])0.60000000000000010.6
sum([0.1] * 10)0.99999999999999991.0
sum([1e100, 1.0, -1e100])0.01.0
sum([0.1j] * 10)0.9999999999999999j1j

Summing 3 to 10 random two-decimal prices gave a different total about a third of the time.
None of this is in limitations/.

sum() now follows CPython's builtin_sum_impl.
An int total stays an i64 while the items are ints or bools, and a float or complex total is kept as a compensated sum (CompensatedSum, a port of CPython's cs_add and cs_to_double).
The first item a loop can't take is added with +; a later loop continues if the result has its type, otherwise the rest is added with +.
That keeps the CPython details: a bool start or an int total past 64 bits means plain + for the rest, a float total is rounded when a complex item makes it complex, and a real item only changes the real part of a complex total.
Items are classified with the existing operand() from complex.rs, which converts ints the way PyLong_AsDouble does, OverflowError included.

The int and float loops also no longer go through py_add for every item.

That broke the timing of test_cancelled_queued_feed_run_leaves_session_healthy, which failed on the Python 3.13 job. Its first feed, sum(range(20_000_000)), has to still be running when the queued second feed is cancelled 0.1 s in, and the new loop finishes it in about 0.1 s (about 0.4 s before). The second commit makes that feed spin on the worker's clock for 0.5 s instead.

Testing

  • New asserts in builtin__iter_funcs.py fail on main (assert 0.6000000000000001 == 0.6) and pass on Monty and CPython 3.14.
  • 40,000 generated sum() calls give the same type, value and exception as CPython 3.14.7. They mix floats of every magnitude, inf, nan and -0.0, ints around the i64 limits, big ints, bools, complexes and non-numbers, with every kind of start value. main differed on about 7.5% of them, and nothing main already got right changed.
  • make lint-rs, make lint-py and cargo test -p monty pass. The datatest suite passes on the Monty side, also with memory-model-checks and ref-count-return; the 13 CPython-side cases that fail for me also fail on main in my environment.

#1025 adds a fast path for float lists that adds left to right, so if both are merged that path needs CompensatedSum too.


Summary by cubic

Makes sum() match CPython's float and complex summation: sum([0.1] * 10) now returns 1.0 instead of 0.9999999999999999, and sum([0.1, 0.2, 0.3]) returns 0.6 instead of 0.6000000000000001.

  • Ports CPython's builtin_sum_impl fast loops: int totals stay in i64 while items fit, and float and complex totals use Neumaier's compensated summation.
  • Falls back to plain + exactly where CPython does, preserving -0.0 signs and not turning overflowed totals into nan.
  • The int and float loops no longer route every item through py_add.

Testing

  • New asserts in builtin__iter_funcs.py fail on main and pass on Monty and CPython 3.14.
  • 40,000 generated sum() calls match CPython 3.14.7 for type, value, and exception; main differed on about 7.5% of them.