[fpc-pascal] Floating point question

Thomas Kurz fpc.2021 at t-net.ruhr
Sat Jan 27 13:40:07 CET 2024


Hello,

I'm facing some strange floating-point issues and cannot find what's actually wrong. OS is win64, but I'm even getting different results when targeting to win32.

Please consider the following program:

program test1;

{$mode objfpc}

function maketime (ayear, amonth, aday, ahour, aminute, asecond: integer): double;
  VAR
    y: Int64 = 0;
    m: Int64 = 0;
    d: Int64 = 0;
    f: Int64 = 0;
    y2000: Int64 = 0;
    ut: double;
    tt: double;

  begin
    y := ayear;
    m := amonth;
    d := aday;
    f := (14 - m) DIV 12;

    y2000 := (
        (d - 365972956)
        + (1461 * (y + 1000000 - f)) DIV 4
        + (367 * (m - 2 + 12 * f)) DIV 12
        - (3 * ((y + 1000100 - f) DIV 100)) DIV 4
    );

    assert (y2000 = 8427);

    ut := (y2000 - 0.5) + (AHour / 24.0) + (AMinute / 1440.0) + (ASecond / 86400.0);
    tt := (8427 - 0.5) + (12/ 24.0) + (33/1440.0) + (0/86400.0);
    writeln ((ut - tt) * 86400.0);
    assert (ut = tt);
    result := ut;
  end;

var t: double; diff: double;

begin
  t := maketime (2023, 1, 27, 12, 33, 0);
  t := (8427 - 0.5) + (12/ 24.0) + (33/1440.0) + (0/86400.0);
  diff := (t - 8427.022916666670) * 86400.0;
  writeln (t);
  writeln (diff);
end.



My problems are:

1. The "writeln" in line 32 correctly prints "0.0000" when (cross-) compiling to win64, but "39.375" when compiling to win32 (with ppc386).

2. According to both C, Sqlite and Excel, the value of "t" in line 41 should be "8427.02291666667". FPC gives me a difference of approximately 39/86400.

Could someone point me to what I'm doing wrong, please. I have a feeling to miss the forest for the trees.

Thanks,
Thomas



More information about the fpc-pascal mailing list