From: Mike Stok Date: 2011-12-16T21:25:35+09:00 Subject: Re: Rounding error? It you're interested in some reading about floating point then http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html is worth looking at. Mike On 2011-12-16, at 7:20 AM, Peter Vandenabeele wrote: > 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 -- Mike Stok http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.