Calculating values of $1 - \cos(x)$ for $x$ near zero using computer arithmetic

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.

• Your reasoning is correct. – wythagoras Jul 22 '15 at 16:22
• @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? – MathNerd Jul 22 '15 at 16:25
• Joriki's answer explains it quite well. – wythagoras Jul 22 '15 at 16:30
• Related to math.stackexchange.com/q/986754/589. – lhf Jul 22 '15 at 16:33

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.
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.
• 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. – user255896 Jul 22 '15 at 18:27
• 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. – joriki Jul 22 '15 at 18:46