Project

General

Profile

Actions

Bug #22092

closed

`Array#sum` takes slow path, does not perform compensated summation of Float elements when init argument is a Float

Bug #22092: `Array#sum` takes slow path, does not perform compensated summation of Float elements when init argument is a Float

Added by georgeclaghorn (George Claghorn) 12 days ago. Updated 2 days ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 4.0.1 (2026-01-13 revision e04267a14b) +PRISM [arm64-darwin24]
[ruby-core:125612]

Description

% time ruby -e 'puts Array.new(1_000_000_000, 0.1).sum'
100000000.0
ruby -e 'puts Array.new(1_000_000_000, 0.1).sum'  2.67s user 2.02s system 71% cpu 6.577 total

% time ruby -e 'puts Array.new(1_000_000_000, 0.1).sum(0.0)'
99999998.74541782
ruby -e 'puts Array.new(1_000_000_000, 0.1).sum(0.0)'  22.72s user 1.80s system 96% cpu 25.332 total

Both the fast path and compensated summation are undocumented implementation details as far as I know, but this appears to be a possible oversight.

Updated by nobu (Nobuyoshi Nakada) 12 days ago Actions #1

  • Status changed from Open to Closed

Applied in changeset git|be696c9c241840aa366fd7444b2297f1db55d8f3.


[Bug #22092] Improve Array#sum when the initial value is a Float

Updated by nobu (Nobuyoshi Nakada) 12 days ago Actions #2

  • Backport changed from 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN to 3.3: REQUIRED, 3.4: REQUIRED, 4.0: REQUIRED

Updated by etienne (Étienne Barrié) 2 days ago · Edited Actions #3 [ruby-core:125727]

4.0: DONE

Edit: Sorry I tried editing custom properties to note that this was backported to 4.0 but it seems that it's not the way 😅

Updated by byroot (Jean Boussier) 2 days ago Actions #4

  • Backport changed from 3.3: REQUIRED, 3.4: REQUIRED, 4.0: REQUIRED to 3.3: REQUIRED, 3.4: REQUIRED, 4.0: DONE
Actions

Also available in: PDF Atom