<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>To simplify the problem further: <br>
</p>
<p>the addition of 12 /24.0 and the subtraction of 0.5 should be
removed, IMO, <br>
because both can be done with floats without loss of precision
(0.5 can be represented exactly in float). <br>
</p>
<p>So the problem can be reproduced IMO with this small Pascal
program: <br>
</p>
<pre>program TESTDBL1 ;
var TT : REAL ;
begin (* HAUPTPROGRAMM *)
TT := 8427 + 33 / 1440.0 ;
WRITELN ( 'tt=' , TT : 20 : 20 ) ;
end (* HAUPTPROGRAMM *) .
</pre>
<p>With my compiler, REAL is always DOUBLE, and the computation is
carried out by a P-Code interpreter<br>
(or call it just-in-time compiler - much like Java), which is
written in C. <br>
</p>
<p>The result is: <br>
</p>
<pre>tt=8427.02291666666678790000</pre>
<p>and it is the same, no matter if I use this simplified
computation or the original</p>
<pre class="moz-quote-pre" wrap="">tt := (8427 - 0.5) + (12 / 24.0) + (33 / 1440.0);
</pre>
<p class="moz-quote-pre" wrap="">My value is between the two other
values: <br>
</p>
<pre class="moz-quote-pre" wrap="">tt=8427.02291666666680000000
tt=8427.02291666666678790000
ee=8427.02291666666666625000</pre>
<p class="moz-quote-pre" wrap="">The problem now is: <br>
</p>
<p class="moz-quote-pre" wrap="">the printout of my value suggest an
accuracy which in fact is not there, because with double, you can
trust <br>
only the first 16 decimal digits ... after that, all is
speculative a.k.a. wrong. That's why FPC IMO rounds at this <br>
place, prints the 8, and then only zeroes. <br>
</p>
<p class="moz-quote-pre" wrap="">The extended format internally has
more hex digits and therefore can reliably show more decimal
digits. <br>
But the last two are wrong, too (the exact value is 66666...
period). <br>
</p>
<p class="moz-quote-pre" wrap="">HTH, <br>
kind regards <br>
<br>
Bernd </p>
<p></p>
<p><br>
<br>
</p>
<div class="moz-cite-prefix">Am 27.01.2024 um 22:53 schrieb Bart via
fpc-pascal:<br>
</div>
<blockquote type="cite"
cite="mid:CAMye31xwru5HgWA91_kBTYymb0dUaDetmDXH_tP+xZ0MBb4Jbw@mail.gmail.com">
<pre class="moz-quote-pre" wrap="">On Sat, Jan 27, 2024 at 6:23 PM Thomas Kurz via fpc-pascal
<a class="moz-txt-link-rfc2396E" href="mailto:fpc-pascal@lists.freepascal.org"><fpc-pascal@lists.freepascal.org></a> wrote:
</pre>
<blockquote type="cite">
<pre class="moz-quote-pre" wrap="">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.
</pre>
</blockquote>
<pre class="moz-quote-pre" wrap="">
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
_______________________________________________
fpc-pascal maillist - <a class="moz-txt-link-abbreviated" href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a>
<a class="moz-txt-link-freetext" href="https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal">https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal</a>
</pre>
</blockquote>
</body>
</html>