# Floating point arithmetic — catastrophic and subtractive cancellation

• $$\sqrt{1+x} - \sqrt{1-x}$$ is a subtractive cancellation when $$x \approx 0$$, and

• $$1 - \cos(x)$$ is a catastrophic cancellation when $$x \approx \pi$$.

I've been trying to read up on it but can't seem to click. They both make very $$\pi$$ like numbers but are different cancellation? How do they differ? Could someone please explain it to me?

• What does "Catasphrohic" mean?
– bof
Nov 7, 2019 at 7:41
• One way to define catastrophic cancellation is to look at power series expansions and check whether some terms cancel out. In both your examples the highest order term of the expansions cancels out. This does not happen in your second example, as $\cos(x)$ is close to $-1$ for $x$ close to $\pi$? Nov 7, 2019 at 7:46
• Is your second expression originally $1+\cos(x)$? Else it is rather stable around $x=\pi$. Nov 7, 2019 at 10:55
• @Dr.LutzLehmann Ohh I misread the question I copied down. It's unstable when $x \approx 0$ and the alternate formula made using the conjugate is unstable when $x \approx \pi$ src: gyazo.com/0981f1544244d75ab8a26f2f0d9313c7 Nov 7, 2019 at 22:09
The terms in the first expression have floating point errors of about $$(1+|x|)\mu$$ where $$\mu$$ is the machine constant, $$1{\rm ulp}$$ or relative precision of the floating point data type. So in the difference you get errors of up to $$2(1+|x|)\mu$$. However, the exact value of the expression is, by binomial expansion, $$\frac{2x}{\sqrt{1+x}+\sqrt{1-x}}$$, which for small $$x$$ is about $$x$$. Thus the relative error close to $$x=0$$ will be large.
The same happens with the second expression, if it were $$1+\cos(x)$$. The floating point error of the sum, which effectively is a subtraction, is bounded by $$(1+|\cos(x)|)\mu\le 2\mu$$, while the equivalent expression $$\frac{\sin^2(x)}{1-\cos(x)}$$ shows that the difference can become arbitrarily small. For $$|x-\pi|<\sqrt{\mu}$$ the original expression will evaluate as zero, giving relative error $$1$$, which is slightly more catastrophic than in the first task where that happens only for $$|x|<\mu$$.
I guess it is because the first difference is of the order $$x$$, while the second one is of the order $$(x-\pi)^2$$ (from Taylor's expansion).