[fpc-pascal] Why this evaluates on "if" wrong ?

Florian Klaempfl florian at freepascal.org
Sun Oct 28 19:58:41 CET 2007


L schrieb:
>>> If you do on a hand calculator:
>>>
>>> 1/3
>>>
>>> ... you will see:
>>>
>>> 0.33333333333
>>>
>>> If you multiply again with 3, you will see:
>>>
>>> 0.99999999999
>>>
>>> ... and not 1.00000000000.
> 
> 
> On my electronic/digital calculator I see '1'

They calculate usually internally with more digits than they display or
they even might use bcd numbers which makes the program working with fpc
as well:

//
// Hm. This is really weird. I assign 1 to x, which is of double type.
// Then I add 0.4 to the x, then I subtract 0.4 from x.
// So, now I should have 1 in x. But when I compare x with 1, I get
// wrong evaluation of "if x = 1 then" statement !
//
// I tried this on two different Windows Intel machines and it was wrong.
// I tried it also on Mac with PowerPC processor and it was also wrong.
// (Both Delphi & FPC compilers)
//
//                                                 milan at marusinec.sk
//
program
 floating_error_1_4 ;

 uses
   fmtbcd;

{$APPTYPE CONSOLE }

var
 x : TBCD;

BEGIN
 x:=1;
 x:=x + 0.4;
 x:=x - 0.4;

 if x = 1.0 then
  writeln('OK: x = 1 ' )
 else
  writeln('Error: "if x = 1" is evaluated wrong !!!! ' );

END.

> 
> It is a casio fx-300SA
> 
> Maybe $5 digital calculators are smarter than $3000 computers ;)
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
> 




More information about the fpc-pascal mailing list