From: Peter Vandenabeele Date: 2011-12-16T21:20:08+09:00 Subject: Re: Rounding error? --00235446ff3c0f057904b4349f3c Content-Type: text/plain; charset=UTF-8 On Fri, Dec 16, 2011 at 1:09 PM, Vimal R. wrote: > Hi, > > I don't face this problem. > > When I follow the same code in my machine, I get proper result for > 4.9 x 5.9 = 28.91 > 3.9 x 3.9 = 15.21 > You most probably _do_ face the problem, but don't see it because the _presentation_ of the result masks the small remainder errors. And what's worse, the result might be dependent on the position of the moon (sorry, joke, but the result will differ, based on the exact Ruby version, CPU etc. that is used when the code is ran). On ruby 1.9.3 peterv@e6500:~$ irb ruby-1.9.3-p0 :001 > 4.9 * 5.9 => 28.910000000000004 ruby-1.9.3-p0 :002 > "%35.30f"%(4.9 * 5.9) => " 28.910000000000003694822225952521" On ruby 1.8.7 peterv@e6500:~$ rvm use 1.8.7-p330 Using /home/peterv/.rvm/gems/ruby-1.8.7-p330 peterv@e6500:~$ irb no such file to load -- wirble ruby-1.8.7-p330 :001 > 4.9 * 5.9 => 28.91 ruby-1.8.7-p330 :002 > "%35.30f"%(4.9 * 5.9) => " 28.910000000000003694822225952521" HTH, Peter -- Peter Vandenabeele http://twitter.com/peter_v http://rails.vandenabeele.com --00235446ff3c0f057904b4349f3c--