[fpc-pascal] Floating point question

Florian Klämpfl florian at freepascal.org
Sat Feb 17 20:18:02 CET 2024



> Am 16.02.2024 um 15:34 schrieb Bernd Oppolzer via fpc-pascal <fpc-pascal at lists.freepascal.org>:
> 
> Am 16.02.2024 um 08:32 schrieb Florian Klämpfl via fpc-pascal:
>> Am 16.02.2024 um 08:23 schrieb Ern Aldo via fpc-pascal <fpc-pascal at lists.freepascal.org> <mailto:fpc-pascal at lists.freepascal.org>:
>>> 
>>>  Compile-time math needs to be as correct as possible. RUN-time math can worry about performance.
>> So you are saying when constant propagation is on, an expression should have a different result than with constant propagation off?
> I don't know exactly, what you mean by constant propagation.
> 
> But IMO, given this (sort of fictive) Pascal code snippet:
> 
> 
> const Xconst : single = 1440.0; 
> 
> var y1, y2 : real; 
> 
> y1 := 33.0 / 1440.0; 
> 
> y2 :=  33.0 / Xconst;
> 
> the division in the first assignment (to y1) should be done at maximum precision, that is, 
> both constants should be converted by the compiler to the maximum available precision and 
> the division should be done (best at compile time) using this precision. 
> 
Constant folding is an optimization technique, so the first expression could be also evaluated at run time in case of a simple compiler (constant folding is not something which is mandatory) which means that we have to use always full precision (what full means depends on the host and target platform thought) for real operations. So either: always full precision with the result all operations get bloated or some approach to assign a precision to real constants.

It gets even more hairy if more advanced optimization techniques are involved:

Consider

var
   y1,y2 : single;

 y1 := 1440.0
 y2 := 33.0 / y1;

When constant propagation and constant folding are on (both are optimizations), y2 can be calculated at compile time and everything reduced to one assignment to y2. So with your proposal the value of y2 would differ depending on the optimization level.
> in the second case, if the compiler supports constants of the reduced type (which I believe it does, 
> 
> no matter how the syntax is), I find it acceptable if the computation is done using single precision, 
> because that's what the developer calls for.
> 
> So probably the answer to your question is: yes.
> 
> Kind regards
> 
> Bernd
> 
> 
> 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20240217/36a4d932/attachment-0001.htm>


More information about the fpc-pascal mailing list