# Avoid catastrophic cancellation? but I can't see any?

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.

• In "base 10, precision 2 floating point" we don't get $-2.401923018799901$ but rather $-2.35$ (note that all computations should use only 2 significant figures)! Sep 16, 2014 at 22:51
• @Winther Thanks for your comment. When I do this in Matlab, I used digits(2) and then used vpa() to perform "precision 2 floating point". Could you tell me how to do it correctly in Matlab? thanks Sep 16, 2014 at 22:57
• I don't know, but it's easy enough to do by hand: $1+5x = 1.17$ and $\sqrt{1.17} = 1.08167 \to 1.08$ so $1-\sqrt{1+5x} = -0.08$ and finally $(1-\sqrt{1+5x})/x=-0.08/0.034 = -2.35294 \to -2.35$. Sep 16, 2014 at 23:01
• @Winther Thank you, I'll try to figure it out. Really thank you for your calculation by hand. Sep 16, 2014 at 23:03
• btw about significant figures see here. So I think you might need digits(3) in MatLab?! Sep 16, 2014 at 23:03

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...?

I think the right rewriting is

$-\frac{5}{1+\sqrt{1+5x}}$

obtained by multiplication for $1+\sqrt{1+5x}$.

You usually have a cancellation with the sign minus when the result is very close to zero, so it's convenient use a rewriting where it doesn't appear

• @genisage edited Sep 16, 2014 at 22:45

The trick here is to avoid the subtraction cancellation through multiplying numerator and denominator by the conjugate radical:

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

Give that a try.

Here's what happens if you evaluate the original expression in 2 decimal digit floating point precision:

$$x = 0.034$$

$$5x \approx 0.17$$

$$1 + 5x \approx 1.2$$

$$\sqrt{1+5x} \approx 1.1$$

$$1 - \sqrt{1+5x} \approx -0.1 \; \text{ NB: subtractive cancellation }$$

$$\frac{1 - \sqrt{1+5x}}{x} \approx -2.9$$

So there's a significant loss of precision in doing it this way.

• Thank you for your answer and with precision 2, the answer is -2.4 as well, and after part a and part b, the part c of this question is to compute relative errors in previous two parts but in this case, it's meaningless. Are there anything wrong with my performance? Thanks Sep 16, 2014 at 22:53
• @JasonHu: If you compute the intermediate expressions with two significant digits, you will not get -2.4. Perhaps you are computing the expression with high precision intermediate values, then doing one final rounding at the end. That's not what the exercise calls for. Sep 16, 2014 at 23:04
• Do you know how to process this in Matlab? I used vpa() function with digits(2) and I think vpa do the precision 2 for every calculation it contains, not just the final answer. Sep 16, 2014 at 23:21
• Then write them out on separate lines, storing in separate variables if need be, and see if the Answer is the same. Or do it by hand on a sheet of paper, as I just did, and be done with it. Sep 16, 2014 at 23:23
• I did it step by step and you are right, the answer is -2.9. Thanks. Do you know any appropriate function to use in matlab? Sep 16, 2014 at 23:28