Your problem is that long double
on your platform has insufficient precision to store the exact value 0.99999999999999999999. This means that the value of that must be converted to a representable value (this conversion happens during translation of your program, not at runtime).
This conversion can generate either the nearest representable value, or the next greater or smaller representable value. The choice is implementation-defined, so your implementation should document which it is using. It seems that your implementation uses x87-style 80bit long double
, and is rounding to the nearest value, resulting in a value of 1.0 stored in x
.
With the assumed format for long double
(with 64 mantissa bits), the highest representable number less than 1.0 is, in hexadecimal:
0x0.ffffffffffffffff
The number exactly halfway between this value and the next higher representable number (1.0) is:
0x0.ffffffffffffffff8
Your very long constant 0.9999999999999999999728949456878623891498136799780 is equal to:
0x0.ffffffffffffffff7fffffffffffffffffffffffa1eb2f0b64cf31c113a8ec...
which should obviously be rounded down if rounding to nearest, but you appear to have reached some limit of the floating point representation your compiler is using, or a rounding bug.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…