Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2019-12

certain divisions in single precision only

8 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Tomas L.

Dear fpc developers,

Let me submit a divison bug:

When a floating point constant which fits into single precision is divided by integer variable, the result is assumed to be a single-precision number.

program div5;
var  five:integer=5;
begin
    writeln(1.0/five);
    writeln(1.0/five-1/five);
end.

versions where problem spotted:
3.0.4 [2019/10/27] for x86_64 on windows
3.0.4+dfsg-18ubuntu2 [2018/08/29] for x86_64

older version with no problem:
fpc 2.6.4 i386-win32 is OK

Thanks

T.

Reply by email

Martok
to Jonas Maebe
Indeed, this is by design. See https://wiki.freepascal.org/User_Changes_2.2.0#Floating_point_constants for how to explicitly specify a different precision.

That was long before the change Tomas discovered.

The gotcha here is about your fix to
<https://bugs.freepascal.org/view.php?id=25121>, which together with the one you linked made runtime and compile time evaluation the same, at the expense of specifically breaking Delphi compat.

Show quoted text 8 lines
From the current Delphi Reference: In Delphi, intermediate results of Single precision floating-point expressions are always implicitly stored as Extended on x86. By default, all x64 arithmetic operations and expressions involving only Single precision floating-point values retain high precision by storing intermediate results as Double precision values.

But since then, FPC sees Single / Int2Single = Single, with no high precision intermediates.
There was either a bug or a thread here about the code that broke and some issues with explicit casts also not helping, but I can't seem to locate either.

Best,

Sebastian

Reply by email

Florian Klämpfl
to Martok
Martok wrote 22 lines
> Indeed, this is by design. See > https://wiki.freepascal.org/User_Changes_2.2.0#Floating_point_constants for > how to explicitly specify a different precision. That was long before the change Tomas discovered. The gotcha here is about your fix to <https://bugs.freepascal.org/view.php?id=25121>, which together with the one you linked made runtime and compile time evaluation the same, at the expense of specifically breaking Delphi compat. From the current Delphi Reference: > In Delphi, intermediate results of Single precision floating-point > expressions are always implicitly stored as Extended on x86. > > By default, all x64 arithmetic operations and expressions involving only > Single precision floating-point values retain high precision by storing > intermediate results as Double precision values. But since then, FPC sees Single / Int2Single = Single, with no high precision intermediates.

As the delphi behavior is very i386/x86-64 specific, FPC deviates from it (not to mention that the non-i386 behavior was implemented years before a non-i386 delphi was available).

Reply by email

Martok
to Florian Klämpfl: As the delphi behavior is very i386/x86-64 specific, FPC deviates from it (not to mention…
Florian Klämpfl wrote
As the delphi behavior is very i386/x86-64 specific, FPC deviates from it (not to mention that the non-i386 behavior was implemented years before a non-i386 delphi was available).

Jup, that's what I meant with "made runtime and compile time evaluation the same". Without that change, cross and native compilers would have different constant evaluation, which is obviously not good either.

Best,
Sebastian

Reply by email

Marco van de Voort
to Jonas Maebe: Actually, back then I thought I fixed the (dynamic) behaviour at least in a…

Op 2019-12-08 om 18:46 schreef Jonas Maebe:

Show quoted text 6 lines
-i386 delphi was available). Actually, back then I thought I fixed the (dynamic) behaviour at least in a Delphi-compatible way: https://bugs.freepascal.org/view.php?id=7179 I don't remember anymore on what test this assumption was based though.

Afaik Delphi x64 doesn't do single math at all, and converts each load/store from single to double, and only operates on doubles

Reply by email

Florian Klämpfl
to Marco van de Voort
Marco van de Voort wrote 11 lines
Op 2019-12-08 om 18:46 schreef Jonas Maebe: > -i386 delphi was available). > Actually, back then I thought I fixed the (dynamic) behaviour at least > in a Delphi-compatible way: https://bugs.freepascal.org/view.php?id=7179 > > I don't remember anymore on what test this assumption was based though. > Afaik Delphi x64 doesn't do single math at all, and converts each load/store from single to double, and only operates on doubles

Yes. It basically mimics the i386 behavior: doing all operations with the highest available precision.

Reply by email