# \[Feedback\] Array#float\_sum (like math.fsum of Python)

**URL:** <https://www.ruby-forum.com/t/feedback-array-float-sum-like-math-fsum-of-python/232500>\
**Category:** Development\
**Created:** [October 13, 2014, 8:53am UTC](https://www.ruby-forum.com/t/feedback-array-float-sum-like-math-fsum-of-python/232500 "2014-10-13T08:53:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [October 13, 2014, 8:53am UTC](https://www.ruby-forum.com/t/feedback-array-float-sum-like-math-fsum-of-python/232500/1 "2014-10-13T08:53:40Z")

</div>

Issue #10298 has been updated by Akira T…

Status changed from Open to Feedback

* * *

Feature #10298: Array#float\_sum (like math.fsum of Python)

> **[Feature #10298: Array#float\_sum (like math.fsum of Python) - Ruby master -...](https://bugs.ruby-lang.org/issues/10298#change-49377)**
>
> Redmine

- Author: Takeshi Nishimatsu
- Status: Feedback
- Priority: Low
- Assignee:
- Category: math
- Target version:

* * *

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  
[Kahan summation algorithm - Wikipedia](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  
[9.2. math — Mathematical functions — Python 2.7.18 documentation](https://docs.python.org/2/library/math.html#math.fsum) .

```auto
[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)
