0
$\begingroup$
  • $\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?

$\endgroup$
5
  • $\begingroup$ What does "Catasphrohic" mean? $\endgroup$
    – bof
    Nov 7, 2019 at 7:41
  • $\begingroup$ 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$? $\endgroup$
    – quarague
    Nov 7, 2019 at 7:46
  • $\begingroup$ Is your second expression originally $1+\cos(x)$? Else it is rather stable around $x=\pi$. $\endgroup$ Nov 7, 2019 at 10:55
  • $\begingroup$ This answer addresses the root of your problem. $\endgroup$ Nov 7, 2019 at 20:12
  • $\begingroup$ @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 $\endgroup$
    – bobby
    Nov 7, 2019 at 22:09

2 Answers 2

1
$\begingroup$

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.

plot of absolute difference of the two expressions

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

plot of absolute difference of the two expressions

$\endgroup$
0
$\begingroup$

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

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .