[Everything below implies a fixed binary format of IEEE 754 with some form of rounding to the nearest, as a rounding mode.]
Since the inverse (calculated as 1/x) is the basic arithmetic operation, it 1is precisely representable, and arithmetic operations are guaranteed to be correctly rounded according to the standard, the mutual result is guaranteed to be within the 0.5units of the last place of the true value. (This applies to any of the basic arithmetic operations specified in the standard.)
The reciprocal reciprocal of the value is xnot guaranteed, usually equal x. A quick example with binary code IEEE 754:
>>> x = 1.8
>>> 1.0 / (1.0 / x)
1.7999999999999998
>>> x == 1.0 / (1.0 / x)
False
However, if overflow and underflow are excluded and what xis finite, nonzero and exactly representable, the following results are true:
The value 1.0 / (1.0 / x)will differ from xno more than 1 unit in the last place.
x ( , [1.0, 2.0), ) sqrt(2), roundtrip: 1.0 / (1.0 / x) == x.
: , x , x , [1.0, 2.0). , x , ( ). , IEEE 754, IEEE 754.
1 / x , , y (, ) 64 float 1 / x. :
y float 1 / x, y 1/x [0.5, 1.0], 2^-53, |y - 1/x| <= 2^-54. , :
: |y - 1/x| < 2^-54. |y - 1/x| 2^-54, 1/x ( y 2^-54 ). x, 1/x , , .
if x < sqrt(2) then 1 / x > x / 2, ( ), y >= x / 2, x / y <= 2.
x - 1/y = (y - 1/x) x/y, |y - 1/x| x/y ( , x < sqrt(2)) |x - 1/y| < 2^-53. , x 1/y, 1/y x, . 2.
x < 2, x / y < 4, |x - 1/y| < 2^-52. 1/y 1 ulp x, 1.
sqrt(2): Python [1.0, 2.0) , . , , sqrt(2), .
>>> import random
>>> samples = [random.uniform(1.0, 2.0) for _ in range(10**6)]
>>> bad = [x for x in samples if 1.0 / (1.0 / x) != x]
>>> len(bad)
171279
>>> min(bad)
1.4150519879892107
>>> import math
>>> math.sqrt(2)
1.4142135623730951
, 1 ulp, ( binary64, [1.0, 2.0), 1 2 ^ -52):
>>> samples = [random.uniform(1.0, 2.0) for _ in range(10**6)]
>>> max(abs(x - 1.0 / (1.0 / x)) for x in samples)
2.220446049250313e-16
>>> 2**-52
2.220446049250313e-16
64- IEEE 754, , :
>>> x = 1.3e308
>>> x_roundtrip = 1.0 / (1.0 / x)
>>> x.hex()
'0x1.72409614c1e6ap+1023'
>>> x_roundtrip.hex()
'0x1.72409614c1e6cp+1023'
x_roundtrip , 1 / x , x.
: IEEE 754-2008 , , , , float sqrt(10) , ( , ) . - , , |x - 1/y| < 1/2 10^(1-p) : , 1 + 16 10^(2p-1) ( , , , , ).
>>> from decimal import Decimal, getcontext
>>> import random
>>> getcontext().prec = 6
>>> samples = [+Decimal(random.uniform(1.0, 10.0)) for _ in range(10**6)]
>>> bad = [x for x in samples if 1 / (1 / x) != x]
>>> min(bad)
Decimal('3.16782')