Bug 17324 – Floating point 1/(1/x) > 0 if x > 0 not generally true
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-04-13T22:15:14Z
Last change time
2022-07-15T10:55:07Z
Keywords
pull
Assigned to
No Owner
Creator
Bolpat
Comments
Comment #0 by qs.il.paperinik — 2017-04-13T22:15:14Z
In https://dlang.org/d-floating-point.html it is claimed:
"[...] in order to preserve identities such as: if x>0, then 1/(1/x) > 0."
But this is wrong, the identity doesn't hold anyway! Compile the following with DMD v2.074.0.
void main()
{
import std.math : nextafter;
double x = nextafter(0.0, 1.0); // produce smallest denormal number > 0.
assert(x > 0);
double y = 1/x;
assert(y > 0);
assert(1/y > 0); // fails
assert(1/(1/x) > 0); // passes due to wrong optimuzation
}
Comment #1 by clugdbug — 2017-05-09T10:21:04Z
Right, it's a bit sloppy, I'm treating +0.0 as "> 0".
The wording should actually be something like:
identities such as: x and 1/(1/x) always have the same sign bit.
(The point is that you can take the reciprocal without destroying the sign bit).
> assert(1/y > 0); // fails
That's because y is +infinity, so the result is +0.0.
> assert(1/(1/x) > 0); // passes due to wrong optimuzation
That's not a problem, the compiler was able to avoid creating a spurious NaN. It's allowed to use arbitrary intermediate precision.
Comment #2 by dlang-bot — 2022-07-15T10:32:46Z
@RazvanN7 created dlang/dlang.org pull request #3342 "Fix Issue 17324 - Floating point 1/(1/x) > 0 if x > 0 not generally true" fixing this issue:
- Fix Issue 17324 - Floating point 1/(1/x) > 0 if x > 0 not generally true
https://github.com/dlang/dlang.org/pull/3342
Comment #3 by dlang-bot — 2022-07-15T10:55:07Z
dlang/dlang.org pull request #3342 "Fix Issue 17324 - Floating point 1/(1/x) > 0 if x > 0 not generally true" was merged into master:
- dc1295bbe6111c0e21155252695d59923f93cceb by Razvan Nitu:
Fix Issue 17324 - Floating point 1/(1/x) > 0 if x > 0 not generally true
https://github.com/dlang/dlang.org/pull/3342