[fpc-pascal] Floating point question

James Richters james.richters at productionautomation.net
Sun Feb 4 20:15:05 CET 2024


>No need to yell.
Yes, that's true, I apologize, I did not mean to come across that way.  

>This is how reasonable programing languages work. The result type depends
only on the type of the involved variables/expressions. *Never* the variable
it is assigned to.

If it's never defined by the variable it's assigned to, then maximum
precision should be used, because you don't know how it will be used.

I understand that the result depends on the variables and expressions,
The problem with constants used in an expression is that some determination
needs to be made because it's not specified.
Since it's not specified, then I think it should be implied to be the same
as the variable it would be stored in, if that determination cannot be made,
then maximum precision should be used.

In fact sometimes it does use the precision of the variable being assigned
to look at this:

program TESTDBL1 ;

Var
    AA : Extended;
    BB : Extended;
    CC : Extended;
    DD : Extended;
    EE : Extended;
    FF : Extended;
    GG : Double;
    HH : Single;

begin
   AA := 8427;
   BB := 33;
   CC := 1440.0;
   DD := AA+BB/CC;
   EE := 8427+33/1440.0;
   FF := 8427+33/1440;
   GG := 8427+33/1440;
   HH := 8427+33/1440;
   WRITELN ( 'DD =' , DD : 20 : 20 ) ;
   WRITELN ( 'EE =' , EE : 20 : 20 ) ;
   WRITELN ( 'FF =' , FF : 20 : 20 ) ;
   WRITELN ( 'GG =' , GG : 20 : 20 ) ;
   WRITELN ( 'HH =' , HH : 20 : 20 ) ;
end.

DD =8427.02291666666666625000
EE =8427.02246093750000000000
FF =8427.02291666666666625000
GG =8427.02291666666680000000
HH =8427.02246100000000000000

For FF, GG, and HH, I did not put the .0, so it must have made them al
integers... but now the division is carried out in the way that makes sense
for the variable it's being stored in,  it's only when I force the 1440 to
be a float by putting the .0 on that it gets it wrong. 
But if it was supposed to be 1440.1 then I couldn't leave the .1 off and
maybe I still have the issue.... but no.. I  DON'T have it... it's only
getting it wrong if it's 1440.0

Look at THIS:


program TESTDBL1 ;

Var
    AA : Extended;
    BB : Extended;
    CC : Extended;
    DD : Extended;
    EE : Extended;
    FF : Extended;
    GG : Double;
    HH : Single;

begin
   AA := 8427;
   BB := 33;
   CC := 1440.0;
   DD := AA+BB/CC;
   EE := Extended(8427+Extended(33/Extended(1440.1)));
   FF := 8427+33/1440.1;
   GG := 8427+33/1440.1;
   HH := 8427+33/1440.1;
   WRITELN ( 'DD =' , DD : 20 : 20 ) ;
   WRITELN ( 'EE =' , EE : 20 : 20 ) ;
   WRITELN ( 'FF =' , FF : 20 : 20 ) ;
   WRITELN ( 'GG =' , GG : 20 : 20 ) ;
   WRITELN ( 'HH =' , HH : 20 : 20 ) ;
end.

DD =8427.02291666666666625000
EE =8427.02291507534198978000
FF =8427.02291507534198978000
GG =8427.02291507534210000000
HH =8427.02246100000000000000

Just FYI, windows calculator gives 8427.0229150753419901395736407194 so I
expect to get the following for this:
FF =8427.02291507534198978000
GG =8427.02291507534210000000
HH =8427.02246100000000000000
And YES that is what I get.

Things are only broken if I put 1440.0  there is a bug in this condition.

   FF := 8427+33/1440.0;
   GG := 8427+33/1440.0;
   HH := 8427+33/1440.0;

Windows calculator gets: 8,427.0229166666666666666666666667
I expect to get:
FF =8427.02291666666666625000
GG =8427.02291666666680000000
HH =8427.02246100000000000000

But no, I get:
FF =8427.02246093750000000000
GG =8427.02246093750000000000
HH =8427.02246100000000000000


If I leave off the .0 then it's correct:
   FF := 8427+33/1440;
   GG := 8427+33/1440;
   HH := 8427+33/1440;

FF =8427.02291666666666625000
GG =8427.02291666666680000000
HH =8427.02246100000000000000


I feel much better about it all now.. I think it's SUPPOSED to work the way
I expect, but there is a bug if you put something like 1440.0 in your
constant expression.

Sorry again for my earlier tone.

James



More information about the fpc-pascal mailing list