The formula \((1-\cos(x))/x^2\) is prone to loss of significance when \(x\) is close to \(0\text{.}\) Rewrite it in a mathematically equivalent way which avoids this issue. Compare the performance of both formulas in Matlab with \(x = 10^{-9}\text{.}\)
Solution.
The issue is that \(\cos x\) is close to \(1\) and we subtract it from \(1\text{.}\) The identity
\begin{equation*}
(1-\cos x)(1+\cos x) = 1-\cos^2 x = \sin^2 x
\end{equation*}
can be used to avoid this issue. According to it,
\begin{equation*}
\frac{1-\cos(x)}{x^2} = \frac{\sin^2(x)}{x^2(1+\cos x)}
\end{equation*}
There is no loss of significance in the formula on the right. Compare in Matlab:
f = @(x) (1-cos(x))/x^2; g = @(x) sin(x)^2/(x^2*(1+cos(x))); disp(f(1e-9)) disp(g(1e-9))
The first formula produces \(0\text{,}\) the second produces \(0.5\text{.}\) The second value is correct. Indeed,
\begin{equation*}
\frac{1-\cos(x)}{x^2} \to \frac12 \quad \text{as } x\to 0
\end{equation*}
