From: Robert Klemme Date: 2007-11-27T17:58:15+09:00 Subject: Re: Bizarre Floating point errors in Ruby? Serious bug? 2007/11/27, space.ship.traveller@gmail.com : > > > 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. You can save yourself the effort. No computational machine that uses float math can guarantee to be "mathematical correct" in all situations. The reason is fairly simple: machines have limited resources to represent numbers while in math there are a lot of real numbers around that cannot be represented with finite resources with pi and e only being the most famous ones. q.e.d. In your case however BigDecimal is sufficient: $ irb -r bigdecimal irb(main):001:0> a=BigDecimal.new '-140.14' => # irb(main):002:0> a*100 => # irb(main):003:0> (a*100).to_i => -14014 irb(main):004:0> (a*100).to_f => -14014.0 Kind regards robert -- use.inject do |as, often| as.you_can - without end