Reputation: 91
Real Close to the Machine: Floating Point in D
https://dlang.org/articles/d-floating-point.html
says
Useful relations for a floating point type F, where x and y are of type F
...
- x>0 if and only if 1/(1/x) > 0; x<0 if and only if 1/(1/x) < 0.
what is the meaning of this sentence?
Upvotes: 6
Views: 130
Reputation: 2289
In the text you're quoting, we're looking at how the representation is symmetric around 1, and that the rounding doesn't break this. That is, for any number 0 < x < 1
, there's a corresponding number 1 < y < ∞
, such that y = 1/x
and 1/y = x
. That's the first half - the second is simply the same for negative numbers: 0 > x > -1
and -1 > y > -∞
.
It may not be immediately obvious how this can be a problem, but consider x = 3
.
y
must then be 1/3 = 0.333...
. With a limited precision of 3 decimal digits, 1/y
would then be 3.003003003...
. IEEE 754 defines how this should work, and says that the rounding should ensure 1/(1/x)
should be equal to x
, and thus that the result should be 3
, even if there are rounding errors in both 1/x
and 1/y
- they should cancel each other out.
Older floating-point systems weren't as well-behaved as IEEE 754. I'm not sure if any of them weren't symmetric around 1, but that's certainly within the realm of possibility.
Upvotes: 5