From: Frederick Cheung Date: 2008-01-09T18:15:11+09:00 Subject: Re: Precision of Float On 8 Jan 2008, at 21:17, Gary Wright wrote: > > On Jan 8, 2008, at 3:50 PM, Kornelius Kalnbach wrote: > >> Gary Wright write: >>> You're getting confused because Float#to_s rounds. >> ...while to_i and to_int are just chopping off the integer part, >> like #floor, I guess? >> >> irb(main):020:0> "%0.15f" % (8.2 * 100) >> => "819.999999999999886" >> >> floats are weird things. > > I think a lot of the mystery comes from the implicit base2/base10 > conversions that most programmers don't take into consideration. With a 64bit IEEE float you get around 15 significant decimal figures, here 18 are being printed so it's not surprising the last few are junk. THere also the issue that in the same way that 1/3 doesn't terminate if you write it out in decimal, 0.1 doesn't if you write it out in binary (See also http://docs.sun.com/source/806-3568/ncg_goldberg.html 'What every computer scientist should know about floating point arithmetic') Fred