[fpc-pascal] Floating point question

Bart bartjunk64 at gmail.com
Sun Jan 28 11:16:13 CET 2024


On Sun, Jan 28, 2024 at 10:21 AM Bernd Oppolzer via fpc-pascal
<fpc-pascal at lists.freepascal.org> wrote:


> The problem now is:
>
> the printout of my value suggest an accuracy which in fact is not there,

Which is because I was too lazy to cater for that.
Notice the :20:20 in the writeln statement: I tell the compiler how
many digits to use there and it just does as I tell it to.

Floating point calculations will always have rounding errors.
Notice that when a calculation can be off by x percent and you do y
calculations then the end result may be off by x*y percent (worst case
scenario).
If you need infinite precision, there are libraries providing that,
they're just not provided with fpc.
E.g. the (old) Windows calculator gives
8427.0229166666666666666666666667 as the result.

E.g you can do the whole calculation using fractions and then convert
the end result to floating point.
This will give:
8427.0229166666668 using Double (64-bit)
8427.02291666666666625 using Extended (80-bit)
These are the same as in my previous example, indicating that most of
the "inaccuracy" is in the 11/480 part.

Conclusion:
It is **not** a bug, it as expected.

B.t.w. I tested with Delphi 7 and there the accuracy is even one digit
less precise than in fpc (it goes 1 digit earlier "off").

-- 
Bart


More information about the fpc-pascal mailing list