[fpc-pascal] Floating point question

Michael Van Canneyt michael at freepascal.org
Mon Feb 19 22:36:08 CET 2024



On Sun, 18 Feb 2024, James Richters via fpc-pascal wrote:

>> And if you have set the precision, then the calculation will be identical to the calculation when you use a variable of the same type (if not, it's indeed a bug).
>
> This is what I have been trying to point out.  Math with identical casting with variables and constants are not the same.
> Maybe if I try with a simpler example:
>
> program Const_Vs_Var;
>
> Const
>   A_const = Byte(1);
>   B_const = Single(3.5);
> Var
>   A_Var : Byte;
>   B_Var : Single;
>   Const_Ans1, Var_Ans1 : Extended;
>
> Begin
>   A_Var := A_Const;
>   B_Var := B_Const;
>
>   Const_Ans1 := Extended(Byte(A_Const)/Single(B_Const));
>   Var_Ans1   := Extended(Byte(A_Var)/Single(B_Var));
>
>   WRITELN ( ' Const_Ans1 = ', Const_Ans1);
>   WRITELN ( '   Var_Ans1 = ',   Var_Ans1);
> End.
>
> Const_Ans1 =  2.85714298486709594727E-0001
>   Var_Ans1 =  2.85714285714285714282E-0001
>
> Windows 10 Calculator shows the answer to be
> 0.28571428571428571428571428571429  Which matches up with the way variables have done this math, not the way constants have done it.

I would not put too much trust in Windows calculator, since there you have
no control over the precision at all.

It seems to be a windows-specific problem. Here is the result of your
program when executed on Linux:

  Const_Ans1 =  2.85714298486709594727E-0001
    Var_Ans1 =  2.85714298486709594727E-0001

As you can see, the result is identifical.

As for the explanation, I will have to leave that to the compiler developers.

Michael.


More information about the fpc-pascal mailing list