3
$\begingroup$

Explain why calculating values of $1 - \cos(x)$ where $x$ near zero using the trigonometric identity $1 - \cos(x) = 2\sin^2\big(\frac{x}{2}\big)$ will result in more accurate results.


Is it because when we calculate $1 - \cos(x)$ for $x$ values near zero results in subtracting two nearly equal numbers and so we loose significant digits, but when we calculate $1-\cos(x)$ using the trigonometric identity $1-\cos(x)=2\sin^2\big(\frac{x}{2}\big)$ we do not subtract two nearly exact numbers?

Why using the identity will be more accurate?

We multiply two near zero numbers and so we will loose in this case significant digits too.

Thanks for any help.

$\endgroup$
4
  • $\begingroup$ Your reasoning is correct. $\endgroup$ – wythagoras Jul 22 '15 at 16:22
  • $\begingroup$ @wythagoras Yes, but when we multiply $sin(x/2)$ with $sin(x/2)$ (Inorder to calculate $sin^2(x/2)$) that are nearly equal to zero numbers, we are loosing some significant digits too. Why using the identity is better? $\endgroup$ – MathNerd Jul 22 '15 at 16:25
  • $\begingroup$ Joriki's answer explains it quite well. $\endgroup$ – wythagoras Jul 22 '15 at 16:30
  • 1
    $\begingroup$ Related to math.stackexchange.com/q/986754/589. $\endgroup$ – lhf Jul 22 '15 at 16:33
6
$\begingroup$

Multiplying numbers near $0$ is not a problem. Given two (machine) numbers $x(1+\delta)$ and $y(1+\epsilon)$, their (machine) product is $xy(1+\epsilon)(1+\delta)\simeq xy(1+\delta+\epsilon)$, so the relative rounding errors just add. If you multiply $n$ numbers like this, the relative rounding error goes like $\sqrt n$. The only problem is that you may exhaust the range of the exponent when the product gets too close to $0$, but that's unavoidable since this is the result you want, and it only happens at extremely small numbers.

By contrast, if you have a machine representation of $\cos x=1-\Delta$ (where $\Delta$ is the real distance to $1$, not a rounding error) as $(1+\epsilon)(1-\Delta)$ and you subtract this from $1$, the result is $1-(1+\epsilon)(1-\Delta)=\Delta-\epsilon+\Delta\epsilon=\Delta(1-\epsilon/\Delta-\epsilon)$. So now your relative error is no longer $\epsilon$ or a small multiple of that, but $\epsilon/\Delta$, which can be much worse if $\Delta$ is, say, $10^{-6}$, a number that's very comfortably accurately represented in the other scenario.

$\endgroup$
0
0
$\begingroup$

As noted, the reasoning of MathNerd is correct. Suppose that $x = a*10^{-6}$ where $a$ is a number of order one known to eight significant digits. Then computing $1 - \cos(x)$ will give a result with no significant digits, while using the trig identity will give a result with seven or eight s.d.

$\endgroup$
4
  • $\begingroup$ This is assuming single-precision floating-point arithmetic. Double-precision numbers have roughly twice that number of siginificant digits. $\endgroup$ – joriki Jul 22 '15 at 16:43
  • $\begingroup$ You are quite right, but if $a$ if only known to eight significant digits, it doesn't matter whether the computer is using single or double precision arithmetic. $\endgroup$ – user255896 Jul 22 '15 at 18:27
  • $\begingroup$ Why's that? If you have $a=1+10^{-8}$, so $x=(1+10^{-8})\cdot10^{-6}$, and you take the cosine, that's $\simeq1-\frac12((1 + 10^{-8})\cdot10^{-6})^2\simeq1-\frac12\cdot10^{-12}\pm\epsilon$, where $\epsilon$ is the machine precision and the $10^{-8}$ contribution is lost anyway, even in double-precision -- it doesn't matter whether you knew $a$ to $8$ digits or more; what matters is that when you now subtract this from $1$ you get $\frac12\cdot10^{-12}\pm\epsilon$, which has no significant digits in single precision and something like $4$ or $5$ significant digits left in double precision. $\endgroup$ – joriki Jul 22 '15 at 18:46
  • $\begingroup$ Exactly, and that is why using the trig identity is far more accurate. $\endgroup$ – user255896 Jul 22 '15 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.