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.
 A: 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.
A: 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.
