问题
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.
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/38709206/invertability-of-ieee-754-floating-point-division