3 ms·
Ruby seems to handle this out of the box: >> RUBY_VERSION ⇒ "2.7.2" >> data = [1_000_000_000.1, 1.1] * 50_000 ; nil ⇒ nil >> data.sum ⇒ 50000000
by pdkl95 5y ago
Ruby seems to handle this out of the box:
>> RUBY_VERSION
⇒ "2.7.2"
>> data = [1_000_000_000.1, 1.1] * 50_000 ; nil
⇒ nil
>> data.sum
⇒ 50000000060000.0
>> data.sum / data.length
⇒ 500000000.6
>> data.sort.sum / data.length
⇒ 500000000.6
>> data.sort.reverse.sum / data.length
⇒ 500000000.6
I know that ruby will auto-promote an Integer into a Bignum. It seems like ruby will also auto-promote other types? I should research this...
- dmurray 5y agoFWIW, Ruby the language doesn't have Bignum any more, though, just Integer which handles arbitrarily large numbers. Under the hood, there are some optimizations for small integers. https://bugs.ruby-lang.org/issues/12005 https://bugs.ruby-lang.org/issues/12005
- chrisseaton 5y agoBignum is still there, working in almost exactly the same way, it's not exposed to the user anymore.
- aardvark179 5y agoNope. The sum method is implemented in C, and as soon as it sees a float value in the array it switches to Kahan-Babuska summation. We don’t do that in TruffleRuby and so don’t produce quite such an accurate result.
- ska 5y agoThis is a nice thing about Common Lisp, not just integer promotion to bignums, but ratios too (works in complex, also).