[Newbies] Float comparsion

Bert Freudenberg bert at freudenbergs.de
Fri Feb 27 12:33:54 UTC 2009


On 27.02.2009, at 13:22, Camillo Bruni wrote:

> Hy all,
> I recently tried to compare two Float which apparently should be  
> (nearly) the same.
> I have to following excerpt:
>
> 	a := (Float halfPi / 2) tan
> 	a = 1.0
>
> The comparison evaluates to false, although it should be 1.0.
> How do I compare Floats so I get useful results?
>
> I'm using Squeak 3.8.18beta3U VM and the
> Squeak3.10.2-7179-basic.image on Mac OS X 10.5 with an Intel Core 2  
> Duo.


Don't they teach you anything in Bern? ;-)

Floats are approximations. You can't meaningfully compare them for  
equality. One way is to use

	(a - b) abs < epsilon

- Bert -




More information about the Beginners mailing list