Computer Arithmetic
For very large x, computing √(x + 1) − √x in floating point suffers catastrophic cancellation. Which rewrite computes it accurately?
- ACompute 1 ÷ (√(x + 1) + √x)
- BCompute √(x + 1 − x)
- CConvert √(x + 1) and √x to double precision and subtract with the same formula
Show answer and explanation
Answer: A. Compute 1 ÷ (√(x + 1) + √x)
√(x + 1) and √x are nearly equal, so subtracting them directly cancels the leading digits. Multiplying numerator and denominator by √(x + 1) + √x gives (x + 1 − x) ÷ (√(x + 1) + √x) = 1 ÷ (√(x + 1) + √x), eliminating the subtraction of close values. √(x + 1 − x) equals 1 and is simply wrong. Switching to double helps, but the same problem returns for larger x, so it is not a fundamental fix.
- A. Correct. The rewrite removes the subtraction and avoids cancellation.
- B. √(x + 1 − x) equals 1, which is not the original expression.
- C. Even in double precision, the problem returns for larger x.