[fpc-pascal] Floating point question
Florian Klämpfl
florian at freepascal.org
Sun Feb 4 19:38:41 CET 2024
Am 04.02.2024 um 18:54 schrieb James Richters:
> I can understand storing the constant in the lowest precision that doesn't cause data loss, thus making thing more efficient, but the actual calculation done by the compiler should be done at maximum precision and only the final result stored in the lowest required precision.
> This calculation is only carried out buy the compiler once, during compilation, not by the executing program.
>
> The calculation should be done completely using extended, and if the result of the entire calculation is a 2, then store it as a byte, if it's 1.23 then store it as a single, and if it's 1.2324241511343 store it as Extended. The problem is the 33/1440 is being stored as a single and IS LOSING DATA, the division should have been detected and therefore the lowest precision that doesn't cause data loss is NOT a single.
>
> In all cases in our example, we should not be getting different values for the same constant. The implementation not the right way of doing it. It's not doing what is required by the statement:
>
> "New behaviour: floating point constants are now considered to be of the lowest precision which doesn't cause data loss"
>
> The "New behaviour" has a DEFINATE bug in it, because we are experiencing data loss.
You are understand the statement wrong: it says nothing about operations/expressions, only constants.
>
> The correct way to implement this is to have the compiler ALWAYS evaluate everything at highest precision, THEN after all computations are complete evaluate the final answer to store in the constant and reduce the precision of only the constant if it's justified. If it was done this way then it would always give the expected result.
This is plainly wrong. Simply because it would mean that we have to carry out also all calculations with variables
always with the highest precision. And this is not how things are supposed to be done in any reasonable programming
language. The legacy x87 fpu doing so causes already enough headache.
More information about the fpc-pascal
mailing list