Brent Roman wrote: > Floats are imprecise by design. They are intended to represent measured > quantities, not countable objects. Any real valued measurement is likely to > have more error in its underlying physics than the 8-byte binary floating > point format Ruby uses to approximate that value. I've been forced to argue this point many times because Ruby gives the illusion that Floats "aren't necessarily" precise due to the "prettying up" of to_s results. So when JRuby presented the real IEEE floating point result, we had to "fix" it... > A variant of the float to string conversion that tries to preserve all the > binary information would certainly be useful, but not as the default to_s > method. That is, unless you think that: > > irb> 2.1 - 3.0 > -0.8999999999999999 > > would not be "surprising"! (well, at least annoying) We had to "fix" this sort of case in JRuby because our to_s was returning repeating the repeating decimal versions of many Floats. I found it annoying that many tests in Rails and RubySpec and elsewhere expected floating point math to produce a specific accurate or integral result in most cases. While I appreciate that returning -0.9 for the above "looks" nicer, it also helps reinforce the myth that floating-point results are 100% accurate. The bugs JRuby had (and in some cases still has) representing floating point math results the same way as MRI have often led to broken code as a result...such as a certain piece of code in Rails that multiplied a float and an integer and compared it with another float with no margin for error. Pretending Floats are accurate leads to bugs. - Charlie