[Math] Avoid catastrophic cancellation? but I can’t see any

catastrophic-cancellationfloating pointMATLAB

enter image description here


As you can see here, the question is about part b. By using Matlab, the answer to the part a is -2.4, but by using "format long" to compute directly, the answer is -2.401923018799901, which I don't think the cancellation is catastrophic.

Bty, I tried to put rewrite it to 1/x – sqrt(1/x^2 + 5/x), while the answer is -2.4 as well.

I am also not confident with my rewriting, please show me if you have any better rewritings.

Thanks a lot.

Best Answer

One way to rewrite the expression is to multiply by the conjugate:

$$\frac{1 - \sqrt{1 + 5x}}{x} \cdot \frac{1 + \sqrt{1 + 5x}}{1 + \sqrt{1 + 5x}} = \frac{1 - (1 + 5x)}{x(1 + \sqrt{1 + 5x})} = -\frac{5}{1 + \sqrt{1 + 5x}}$$

For this second expression, when $x \sim 0$ the denominator is $\sim 2$, and you get a number close to $-5/2$. In the original expression when $x \sim 0$ both the numerator and denominator are close to zero so you could get problems.

But it doesn't actually seem like you got any problems in your computation, so...?

Related Question