From: Xavier Date: 2004-05-13T08:03:53+09:00 Subject: Re: Major Addition Bug? On Thu, 13 May 2004 04:15:59 +0900, Hal Fulton wrote: > Sean O'Dell wrote: >> On Wednesday 12 May 2004 09:53, ts wrote: >> >>>>>>>>"S" == Sean O'Dell writes: >>> >>>S> raise "false" if ((625.91 + 900.00 + 22.00) != 1547.91) >>> >>> Well, this is well known >> >> >> Well known? I had no idea this bug existed until I wrote a line of code that >> double-checked the arithmetic from a different app. I have code all over >> doing simple arithmetic like this. Ruby can't add 3 simple numbers? I don't >> mean to be disproportionately negative, but that shakes my faith more than a >> little. It's just simple arithmetic. How old is this bug? When is it >> getting fixed? > > Offhand I'd say this bug dates back to the 1940s, and isn't > getting fixed any time soon. > > Seriously, I think it's a matter of trying to store an infinite > number of digits in a finite number of bits. > > For a daily life example: How many digits does it take to store > "1/3" exactly? 0.333333... > > > > Hal Exactly. Has anyone ever tried to write 1.1 in binary? Tell me when you've finished writing it ;-) Use BigNums which are probably an implementation (I haven't looked into Ruby's) of BCD or similar. BCD (Binary Coded Decimal) has been in use for ages. It uses one byte per decimal digit. A less wasteful form is called packed BCD and uses a nibble (4 bits) to store a decimal digit. BCD is older than punch cards btw. Another way out using floats is to redefine Float::== $ irb irb(main):001:0> class Float irb(main):002:1> def ==(other) irb(main):003:2> (self-other).abs<1E-7 irb(main):004:2> end irb(main):005:1> end => nil irb(main):006:0> (625.91 + 900.00 + 22.00) == 1547.91 => true Hth