Issue #14926 has been updated by shevegen (Robert A. Heiler).


I sort of agree; I would have expected the same result.

Other arrays with numbers output the same result:

    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].reduce(0, &:+) # => 55
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].sum            # => 55

So the result of .sum on infinity seems awkward if .reduce(0, &:+) leads
to Infinity.

I am a bit surprised about infinity though:

    a = [Float::INFINITY].reduce(0, &:+)
    b = [Float::INFINITY]

    puts a
    puts b

    puts (a == b)

    false

I also would have expected the latter to be true. Infinity confuses
me a lot so I will leave others to this issue. :)

----------------------------------------
Bug #14926: Different results between Enumerable#sum and #reduce when Float::INFINITY present
https://bugs.ruby-lang.org/issues/14926#change-73049

* Author: swistak35 (Rafa asocha)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
When `Float::INFINITY` is present in array, `Enumerable#sum` and `Enumerable#reduce(0, &:+)` are giving different results.

```
puts [Float::INFINITY].sum
puts [Float::INFINITY].reduce(0, &:+)
```

results in

```
NaN
Infinity
```

And I would expect both of them to be `Infinity` (meaning: `#sum` is working incorrectly).

Checked on `ruby 2.5.1p57` and `ruby 2.4.3p205`, both have the same bug.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>