From: Mike Stok Date: 2006-03-31T09:04:55+09:00 Subject: Re: Very interesting Float comparison! On 30-Mar-06, at 6:58 PM, Ryan Allen wrote: > Hi Guys (and Gals!) > > This is my first post on the list, so Hello! > > Something very strange is happening (as far as I can tell) with a > float comparison; here is the code in question: > > ("55.59".to_f / 100) == 0.5559 # returns false > > However: > > "55.59".to_f / 100 # returns Float 0.5559 > 0.5559 # returns Float 0.5559 > 0.5559 == 0.5559 # returns true > > And more!: > > ("55.5".to_f / 100) == 0.555 # returns true > ("55.53".to_f / 100) == 0.5553 # returns true > ("55.56".to_f / 100) == 0.5556 # returns true > ("55.58".to_f / 100) == 0.5558 # returns true > ("55.59".to_f / 100) == 0.5559 # returns false !! > ("44.59".to_f / 100) == 0.4449 # returns true !! > ("44.59".to_f / 100) == 0.4449 # returns false !!!! > > It doesn't seem to like .59's... What did .59 ever do? > > I picked this up while writing a unit test, I've run this code in > irb and I'm not sure what's going on... > > Can anyone shed some light on this? > > I'm using ruby 1.8.4 (2005-12-24) [powerpc-darwin8.5.0] > > Cheers, > Ryan. You might want to look at http://docs.sun.com/source/806-3568/ ncg_goldberg.html and http://support.microsoft.com/kb/q69333/ ratdog:~ mike$ irb irb(main):001:0> '%.40f' % 0.5559 => "0.5558999999999999497291014449729118496180" irb(main):002:0> '%.40f' % (55.59 / 100) => "0.5559000000000000607514039074885658919811" irb(main):003:0> '%.40f' % ("55.59".to_f / 100) => "0.5559000000000000607514039074885658919811" Mike -- Mike Stok http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.