Invertability of IEEE 754 floating-point division
Asked Answered
R

2

18

What is the invertability of the IEEE 754 floating-point division? I mean is it guaranteed by the standard that if double y = 1.0 / x then x == 1.0 / y, i.e. x can be restored precisely bit by bit?

The cases when y is infinity or NaN are obvious exceptions.

Rome answered 1/8, 2016 at 22:44 Comment(6)
There are obvious cases where it can't, such as infinity and indefinite, and possibly denormalized numbers too. But it's a good question for the rest.Trogon
It seems like this would work just fine for zero and infinity...Kezer
By simple counter example one can show that an IEEE-754 compliant floating-point reciprocal can not be reverted in this fashion. For example, using rounding mode to-nearest-or-even, with binary32: x=0x1.fffffep-1: 1.0f/x=0x1.000002p+0 1.0f/(1.0f/x)=0x1.fffffcp-1 and with binary64: x=0x1.fffffffffffffp-1: 1.0f/x=0x1.0000000000001p+0 1.0f/(1.0f/x)=0x1.ffffffffffffep-1Giliana
Is a poor man's counterexample accepted? Any modern CPU fails that for x = 100000, and I'm pretty sure they're IEEE754 complaint...Novelty
What's guaranteed is that the operation happens in infinite precision, then the result is coarced into the destination value. If you start introducing rounding errors in the coarce step, doing the operation again (even in infinite precision) won't lead to the original result. Also, now you've got a number of inputs that become identical when you invert, and that all should lead to different outputs when you invert again, and that defeats your argument.Novelty
That also acceptable :-) The details are (round to nearest-or-even): x= 0x1.86a0000000000p+16: 1.0f/x= 0x1.4f8b588e368f1p-17 1.0f/(1.0f/x)= 0x1.869ffffffffffp+16Giliana
G
17

Yes, there are IEEE 754 double-precision(*) values x that are such x != 1.0 / (1.0 / x).

It is easy to build an example of a normal value with this property by hand: the one that's written 0x1.fffffffffffffp0 in C99's hexadecimal notation for floating-point values is such that 1.0 / (1.0 / 0x1.fffffffffffffp0) == 0x1.ffffffffffffep0. It was natural to expect 0x1.fffffffffffffp0 to be a counter-example because 1.0 / 0x1.fffffffffffffp0 falls at the beginning of a binade, where floating-point numbers are less dense, so a larger relative error had to happen on the innermost division. More precisely, 1.0 / 0x1.fffffffffffffp0 falls just above the midpoint between 0.5 and its double-precision successor, so that 1.0 / 0x1.fffffffffffffp0 is rounded up to the successor of 0.5, with a large relative error.

In decimal %.16e format, 0x1.fffffffffffffp0 is 1.9999999999999998e+00 and 0x1.ffffffffffffep0 is 1.9999999999999996e+00.

(*) there is no reason for the inverse function to have the property in the question for any of the IEEE 754 format

Gamete answered 2/8, 2016 at 7:46 Comment(1)
A fun and easily proved fact here is that (assuming overflow and underflow are avoided, an IEEE 754 binary format, round-half-to-even, etc.), any float x whose fraction is in the range [1.0, sqrt(2)] will have the property that 1.0 / (1.0 / x) == x.Mayan
A
-3

Obviously not. 1/10 has no representation. You get an approximation instead. Inverting that will not give you 10.

Edit: there are a large number of these. Any inverse that requires more than 53 bits will be one of them.

There is an easy test. In C you could test 1.0/(1.0/10.0) against 10.0 and you will discover they aren't equal.

Andonis answered 2/8, 2016 at 0:36 Comment(4)
What isn't true? That 0.1 cannot be represented or that inverting what is no 0.1 will not yield 10? How would a device know that (1/10)-eps is supposed to be 1/10 and not the approximation ?Andonis
So what will inverting 1/10 give?Helbon
inverting 0.1 (which can't be represented in binary. see grouper.ieee.org/groups/754/faq.html#binary-decimal ) . It should be equal to 1/(0.1+e) where e is the representation error input That it is not is due to post processing. So the test I outlined works/not works depending on implementation and depending on the situation sometimes right/sometimes wrong. (see also: exploringbinary.com/… )Andonis
Sorry, flawed assumption. 1/X may be approximated, but so will (1/X) * X . Now the question is, do these approximations always cancel. A single counter-example would suffice. As plasmacel shows, 1/10 is not that counter-example.Chaoan

© 2022 - 2024 — McMap. All rights reserved.