From: Alex Young Date: 2007-11-27T18:38:27+09:00 Subject: Re: Bizarre Floating point errors in Ruby? Serious bug? space.ship.traveller@gmail.com wrote: >> Float#to_s is doing the formatting. If you want it like python, do it >> like this: >> >> class Float >> def to_s >> "%.12f" % self >> end >> end >> >> (-140.14 * 100) >> # => -14013.999999999998 >> >> Regards, >> Jordan > > Thanks for this useful information. After further investigation, > Python also has the same problem with its pretty printing: > >>>> print (-140.14 * 100) > -14014.0 > > Well, actually this is the correct answer.... i.e. the result we are > looking for if we were using real math. But, it is not the correct > way to round 14013.999999, which is the actual value that we get with > floating point math... so I guess it is hard to pick what is the ideal > behaviour... both have there pros and cons > > Rounding 9.9 to 10.0 is like rounding 99 to 100, but we consider that > only 1 significant figure is important in 100, ie 1xx. So, we are left > with 10.0 with 1SF, but .0 conveys the idea of 1DP correctness, which > is definitely not correct. > > I'd need to consult a mathematician, but I still don't think the Ruby > behaviour is correct, mathematically. I'll post here again once I have > more information about the mathematical correctness of this issue. #to_i doesn't round, it truncates. If you want to round, use #round. -- Alex