[fpc-pascal] Floating point question

James Richters james.richters at productionautomation.net
Fri Feb 16 17:39:20 CET 2024


>But the reduced precision should not come unexpectedly simply because the
compiler attaches type attributes to constants (which can't be easily 
>explained), and then the outcome of simple decimal arithmetic is incorrect.

How are you disagreeing? This is EXACTLY what I am saying. My math with
variables is ALWAYS correct, my math with Constants is not coming out the
same and can't be easily explained.  I guess I should have re-posted the
example because it's not as it seems,  this is a counterexample I had
provided to show the  incorrect math indeed could force an INCREASE in
Precision, but even if I don't cast the variables you still get the same
unexpected results.. this is just to demonstrate that the problem is not the
reduction and assignment itself, but it's the way calculations are done by
the compiler being wrong. 

The math with constants is what's wrong, not the reduction in precision.
Look closely at the following example.  I am ALWAYS adding an integer to a
byte divided by a Single... The reduction in precision has been removed from
this example because I explicitly cast what I wanted.

This proves that the math with constants is what the problem is, not that
the constants themselves were reduced in precision.

The results of Const_Ans1 MUST Equal Var_Ans1 EXACTLY or all kinds of
problems will arise. 

I have no reason to expect that the results of Var_Ans1 and Const_Ans1 would
be different, yet they are.   THIS is the Problem, and that's pretty much
exactly what you just said, so I believe we are in agreement.

The problem isn't that the reduction in precision assigned a type attribute
to the constant, it's that the math with that type is being done wrong,
because I can do the math with the EXACT same typed variables and it comes
out correctly.  I'm trying to show where the real problem is, (not very
successfully)

Math with Variables in the executing program, a byte / single can very well
be extended. 
With the compiler math a byte / single is FORCED to be a single, which is
incorrect.
Dividing by a single does NOT imply the result should be a single.

Before the changes in v2.2, this was NEVER an issue, because all the
constants were full precision, so this could never possible come up as there
would be no math with a single in it.

My point is, no matter what is wrong, the result of math with constants must
always be exactly the same as math with variables otherwise no one can
figure out what the heck is going on. 

James


program Const_Vs_Var;

Const
   A_const = Integer(8427);
   B_const = Byte(33);
   C_const = Single(1440.5);
   Win_Calc = 16854.045817424505380076362374176;
   Const_Ans = 16854.045817424505380076362374176 / (8427 + 33 / 1440.5); Var
   A_Var : Integer;
   B_Var : Byte;
   C_Var : Single;
   Const_Ans1, Var_Ans1 : Extended;

Begin
   A_Var := A_Const;
   B_Var := B_Const;
   C_Var := C_Const;

   Var_Ans1   := Win_Calc / (A_Var+B_Var/C_Var);
   Const_Ans1 := Win_Calc / (A_Const+B_Const/C_Const);

   WRITELN ( '  Const_Ans = ',  Const_Ans:20:20);
   WRITELN ( ' Const_Ans1 = ', Const_Ans1:20:20);
   WRITELN ( '   Var_Ans1 = ',   Var_Ans1:20:20);
End.

The result is:
  Const_Ans = 2.00000010627116630224
 Const_Ans1 = 2.00000010627116630224
   Var_Ans1 = 2.00000000000000000000


When I do:
   Var_Ans1   := Win_Calc / (A_Var+B_Var/C_Var);
   Const_Ans1 := Win_Calc / (A_Const+B_Const/C_Const);





-----Original Message-----
From: fpc-pascal <fpc-pascal-bounces at lists.freepascal.org> On Behalf Of
Bernd Oppolzer via fpc-pascal
Sent: Friday, February 16, 2024 10:48 AM
To: James Richters via fpc-pascal <fpc-pascal at lists.freepascal.org>
Cc: Bernd Oppolzer <bernd.oppolzer at t-online.de>
Subject: Re: [fpc-pascal] Floating point question


Am 16.02.2024 um 15:57 schrieb James Richters via fpc-pascal:
>> So you are saying when constant propagation is on, an expression should
have a different result than with constant propagation off?
> The result of math when using constants MUST be the same as the result of
identical math using variables.
>
> There should never be a difference if I did my formula with hard coded
constants vs variables.
>
>    Const_Ans = 2.00000010627116630224
>   Const_Ans1 = 2.00000010627116630224
>     Var_Ans1 = 2.00000000000000000000
>
> This should not be happening.
>
> James

See my other post;

if the developer explicitly wants reduced precision, then this is what
happens.
But the reduced precision should not come unexpectedly simply because the
compiler attaches type attributes to constants (which can't be easily
explained), and then the outcome of simple decimal arithmetic is incorrect.

So I have to disagree, sorry.

_______________________________________________
fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal



More information about the fpc-pascal mailing list