Project

General

Profile

Actions

Feature #10298

closed

Array#float_sum (like math.fsum of Python)

Added by t-nissie (Takeshi Nishimatsu) about 10 years ago. Updated about 8 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-dev:48579]

Description

Here, I propose Array#float_sum in array.c (or math.c).
Array#float_sum returns an accurate total summation of Float
elements in an array using the Kahan summation algorithm
http://en.wikipedia.org/wiki/Kahan_summation_algorithm .
This algorithm can significantly reduce the numerical
error in the total obtained by adding a sequence of
finite precision floating point numbers, compared to the
obvious approach. Python already have math.fsum
https://docs.python.org/2/library/math.html#math.fsum .

[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1].float_sum   #=> 1.0
[].float_sum                                                   #=> 0.0
Array.new( 10, 0.1).float_sum    #=>  1.0
Array.new(100, 0.1).float_sum    #=> 10.0
# cf.
Array.new( 10, 0.1).reduce(:+)   #=>  0.9999999999999999
Array.new(100, 0.1).reduce(:+)   #=>  9.99999999999998

The name of method can be fsum, sum_float, etc., though
I propose float_sum.

This Array#float_sum is inspired by Feature #9834 Float#{next_float,prev_float}.


Files

array.float_sum.patch (1.32 KB) array.float_sum.patch t-nissie (Takeshi Nishimatsu), 09/26/2014 09:41 AM

Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #12217: Introducing Enumerable#sum for precision compensated summation and revert r54237Closedmrkn (Kenta Murata)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0