From: William James Date: 2005-11-28T08:42:29+09:00 Subject: Re: floating point subtraction question John W. Kennedy wrote: > Nicholas Rahn wrote: > > Hi, > > > > 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. > > > > Can anyone shed some light on this? > > > > Thanks for your help, > > Nick > > > > ps: > > % ruby -v > > ruby 1.8.3 (2005-06-23) [i486-linux] > > This has nothing to do with Ruby, itself, but is a basic characteristic > of nearly all languages and nearly all computers. > > Remember how you can't make 1/3 a decimal fraction, because it's > 0.3333333333333 -- carried out to infinity, so that if you add 1/3 + 1/3 > + 1/3, you get 0.9999999 -- carried out to infinity, instead of 1.0? > Well, computers (almost always) use binary fractions, and you can't make > 1/10 a binary fraction, because it comes out as 0.000110011001100110011 > -- carried out to infinity, and so you get the same kind of problem. > > If you are doing science or engineering, it doesn't matter, because > things in the real world don't come out as neat decimal fractions, > anyway. But you do want to avoid checking for equality. Instead of > > x==y > > look for > > abs(x-y) > where epsilon is some really small number that serves for your purposes > as "practically equal". > > (IBM mainframes have hardware to handle decimal fractions directly, and > a very few languages -- COBOL, RPG, PL/I, and Ada '95 come to mind -- > have the extra features to make use of it. Some other languages, Java, > for example, include libraries to help you do the "calculate in cents" > trick. Ruby, however, does not at present include either facility.) BASIC on the 8-bit Atari used binary-coded decimal.