From: gaojun@...
Date: 2021-03-19T01:01:11+00:00
Subject: [ruby-core:102923] [Ruby master Bug#17731] Integer downflow with	inject/reduce

Issue #17731 has been updated by gaojun (Jun Gao).


Some more interesting samples:
``` ruby
3.0.0 :001 > [0,-2305843009213693953,-2305843009213693953].inject(:+)
 => 13835058055282163710 
3.0.0 :002 > [-2305843009213693953,-2305843009213693953].inject(:+)
 => -4611686018427387906 
3.0.0 :003 > [-2305843009213693953,-2305843009213693953,0].inject(:+)
 => -4611686018427387906 
```


----------------------------------------
Bug #17731: Integer downflow with inject/reduce
https://bugs.ruby-lang.org/issues/17731#change-90987

* Author: gaojun (Jun Gao)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
Test case:
[-2305843009213693953,-2305843009213693953,-2305843009213693953].inject(:+)
or
([-2**61-1] * 3).inject(:+)

Expected Result:
-6917529027641081859

Actual Result:
11529215046068469757

Some clues:
* Downflow seems not happen if there are only two elements in Array
* The sample above works fine in https://try.ruby-lang.org/
* [-2305843009213693953,-2305843009213693953,-2305843009213693953].sum  can get the correct result
* ([-2**61] * 3).inject(:+) can get the correct result

Affected Versions:
* ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]
* ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]
* ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]




-- 
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>