[fpc-pascal] Floating point question
Bart
bartjunk64 at gmail.com
Sat Jan 27 22:53:21 CET 2024
On Sat, Jan 27, 2024 at 6:23 PM Thomas Kurz via fpc-pascal
<fpc-pascal at lists.freepascal.org> wrote:
> Hmmm... I don't think I can understand that. If the precision of "double" were that bad, it wouldn't be possible to store dates up to a precision of milliseconds in a TDateTime. I have a discrepancy of 40 seconds here.
Consider the following simplified program:
====
var
tt: double;
ee: extended;
begin
tt := (8427 - Double(0.5)) + (12/ Double(24.0)) +
(33/Double(1440.0)) + (0/Double(86400.0));
ee := (8427 - Extended(0.5)) + (12/ Extended(24.0)) +
(33/Extended(1440.0)) + (0/Extended(86400.0));
writeln('tt=',tt:20:20);
writeln('ee=',ee:20:20);
end.
===
Now see what it outputs:
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>fpc test.pas
Free Pascal Compiler version 3.2.2 [2021/05/15] for i386
...
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
tt=8427.02291666666680000000
ee=8427.02291666666666625000
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>fpc -Px86_64 test.pas
Free Pascal Compiler version 3.2.2 [2021/05/15] for x86_64
..
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
tt=8427.02291666666680000000
ee=8427.02291666666680000000
On Win64 both values are the same, because there Extended = Double.
On Win32 the Extended version is a bit closer to the exact solution:
8427 - 1/2 + 1/2 + 33/1440 = 8427 + 11/480
Simple as that.
Bart
More information about the fpc-pascal
mailing list