Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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


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


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


Bignum is still there, working in almost exactly the same way, it's not exposed to the user anymore.


This is a nice thing about Common Lisp, not just integer promotion to bignums, but ratios too (works in complex, also).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: