Let us assume that x
and y
are both positive.
Let S
be the double-precision sum x + y
.
There are two cases:
if
x
≤y
, thenS - y
is exact by Sterbenz's lemma. It follows that the addition(S - y) + y
is exact (it produces exactlyS
, which is a double-precision number). Therefore, you can pickS - y
forx_new
. Not onlyy + x_new
is exact, but it produces the same resultS
asy + x
.if
x
>y
, then depending on the number of bits set in the significand ofy
, you may have a problem. If the last bit in the significand ofy
is set, for instance, then no numberz
in a binade after the binade ofy
can have the property thatz + y
is exact.
This answer is vaguely related to that answer.