From: gwtmp01@... Date: 2005-11-28T08:06:42+09:00 Subject: Re: floating point subtraction question On Nov 27, 2005, at 5:41 PM, Nicholas Rahn wrote: > I'm a bit confused with some simple floating point arithmetic: > > % ruby -e 'puts "#{(1.2 - 1.0) == 0.2}"' > false > > % ruby -e 'puts "#{(1.5 - 1.0) == 0.5}"' > true > > I'm sure there's a perfectly logical reason for this, but i think i've > been staring at it for too long to see what it is. The short answer is that decimal floating point literals can not accurately represent the binary floating point values so you get all sorts of rounding errors. Take a look at http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html In general it is a mistake to use == to compare floating point values because of this mismatch. An alternate approach is to ask if the difference between two values is less than some small delta: instead of a == b use abs(a - b) < delta for some suitable (small, near zero) value of delta For all the gory details read "What Every Computer Scientist Should Know About Floating-Point" http://docs.sun.com/source/806-3568/ncg_goldberg.html