From: Zsban Ambrus Date: 2004-05-13T18:53:20+09:00 Subject: Re: Major Addition Bug? On Thu, May 13, 2004 at 04:15:59AM +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 > > > > > 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. No. I'd rather think this is not the problem. The problem is that you should not compare floating-point numbers with == or != unless you have a good reason to do it. In this case, you should just do this: raise "false" if ((625.91 + 900.00 + 22.00) - 1547.91).abs>1e-12 which just succeeds fine. You don't usually need an infinite precision, and in more complicated cases, you can't even acheive one. (You could use rationals here, because these are only simple arithmetic operations, and you'd get an exact result. > > For a daily life example: How many digits does it take to store > "1/3" exactly? 0.333333... > > > > Hal > >