2

I have 2 numbers, x and y, that are known and are represented exactly as floating point numbers. I want to know if z = x - y is always exact or if rounding errors can occur. For simple examples it's obvious:

x = 0.75 = (1 + 0.5) * 2^-1
y = 0.5 = 1 * 2^-1
z = x - y = 0.25 = 0.5 * 2^-1 = 1 * 2^-2

But what if I have x and y such that all significant digits are used and they have the same exponent? My intuition tells me the result should be exact, but I would like to see some kind of proof for this. Is it different if the result is negative?

Lundis
  • 371
  • 1
  • 2
  • 13
  • Your question title and description do not match. In particular - if the exponent is the same, then subtraction should be exact (no proof - maybe there can be exist some border cases, when this is not true); if exponent is not the same, then generally not. – Arvo Nov 05 '14 at 13:12
  • 2
    If exponent and sign are the same, the subtraction will be exact. If the signs are different the magnitudes are added, and there can be a carry from the most significant bit position. A one in the least significant bit position would result in rounding and an inexact result. – Patricia Shanahan Nov 05 '14 at 15:02
  • @Arvo: I assume you mean that my "simple example" and the title doesn't match? Good point though, I will edit the example. – Lundis Nov 05 '14 at 17:43

1 Answers1

8

I am assuming that you want the two numbers to have the same sign. If not, the answer is "yes"; consider (-1) - nextafter(1, infinity), which works out to -2 in floating-point arithmetic with round-to-even.

Under this assumption, the answer is "no." This is (almost) a special case of Sterbenz's theorem: If x and y are floating-point numbers of opposite signs such that |y|/2 <= x <= 2|y|, then x + y is exactly-representable as a floating-point number.

I say "almost" because your statement also works for zero and subnormal numbers.

tmyklebu
  • 13,915
  • 3
  • 28
  • 57