Avoid Loss of significance error I want to compute $f(x)$to avoid loss of significance$$f(x)=\frac{1-\cos x}{x^2}$$
One way is writing Taylor series approximation for $\cos x$ about $x=0$. what about this approach:
$$f(x)=\frac{2\sin^2(\frac x2)}{x^2}$$
Is this ok ?
 A: Yes, this is okay as the sine close to zero is computed as $xh(x^2)$, so that computing $\frac{\sin x}{x}$ is (almost) as exact as the implementation of $h(x^2)$. Plotting shows that indeed the original formula has large errors for $x\approx 10^{-8}$.

The difference plot to the 8th degree Taylor polynomial

shows that both alternative formulas have errors in the magnitude of the machine precision.
A: Better than Taylor series would be the $[2n,2n]$ Padé approximants. Foe example, for $n=2$ you will have
$$f(x)=\frac{1-\cos (x)}{x^2}\sim\frac {65520-3780 x^2+59 x^4 } {131040+3360 x^2+34 x^4 } $$ Compared to a Taylor expansion, the difference is
$$\text{Padé- Taylor}=\frac{127 }{87178291200}x^{10}+O\left(x^{12}\right)$$
For example, for $x=10^{-6}$, the difference between the exact and approximated values is $1.46\times 10^{-69}$.
Using the next approximant $(n=3)$,it would be
$$f(x)=\frac{1-\cos (x)}{x^2}\sim\frac {5491886400-346666320 x^2+7038360 x^4-45469 x^6} {24(457657200+9249240 x^2+86030 x^4+389 x^6 )} $$ Compared to a Taylor expansion, the difference is
$$\text{Padé- Taylor}=\frac{11321 }{438437062103040000}x^{14}+O\left(x^{16}\right)$$
For example, for $x=10^{-6}$, the difference between the exact and approximated values is $2.58\times 10^{-98}$.
A: You are already mindful of rounding errors, but that is not the only issue here.

*

*In IEEE floating point arithmetic, $0/0$ evaluates as NaN (not a number). You need a conditional statement to ensure that $f(0)$ is evaluated as $\frac{1}{2}$.

*In IEEE double precision, the largest positive number is $(2-2^{-52})\cdot 2^{1023}$, so $x^2$ will certainly overflow to infinity when $|x| \ge 2^{512}$ and the default rounding mode is active. If you evaluate $f$ as you suggested, i.e., $$f(x) = 2 \frac{\sin^2\left(\frac{x}{2}\right)}{x^2},$$ and $x^2$ overflows, then the computed result of $f(x)$ is zero. You avoid this issue by evaluating $f$ as $$f(x) = 2 \left(\frac{\sin\left(\frac{x}{2}\right)}{x}\right)^2.$$

*Be mindful of approximations that are more accurate than the rounding error associated with their evaluation.

In general, we seek to compute a target value $T$. We most then choose an approximation $A$ as well as an algorithm for evaluating $A$. In general, the computed value $\hat{A}$ of $A$ will be different from $A$. The total error is $$T-\hat{A} = (T - A) + (A - \hat{A}).$$ Ideally, the two contributions, i.e., the approximation error $T-A$ and the rounding error $A - \hat{A}$ should have the same size and the total error should be less than the user's tolerance $\tau$. Suppose $|T-\hat{A}| < \tau$.

*

*If $|T-A| \ll |A-\hat{A}|$, then we are guilty of mathematical overkill and are probably wasting CPU cycles because very accurate approximations tend to be more costly to evaluate than less accurate approximations.

*If $|A-\hat{A}| \ll |T-A|$, then we are guilty of algorithmic overkill and we may be able to find a faster, less accurate algorithm. It may well be enough to execute the same basic algorithm in single precision instead of double precision.

A: when you say "compute" I assume you mean without using standardised functions, so how would you then continue from this $\sin^2$ term for which the series will not be as pleasant. In terms of using the first series we know that:
$$1-\cos(x)=\sum_{n=1}^\infty\frac{(-1)^{n+1}}{(2n)!}x^{2n}$$
and so:
$$f(x)=\sum_{n=1}^\infty\frac{(-1)^{n+1}}{(2n)!}x^{2(n-1)}$$
However if you mean to take values where $x$ is very small I think the second method is good as approximations will show the value at $x=0$
A: Yes, your formulation is perfect for the job.  While the Taylor series approach is also good, using the half-angle sine is easier to program and equally accurate.
As a further exercise, apply the Taylor series expansion
$\sin(x/2)=(x/2)-(x/2)^3/3!+(x/2)^5/5!-...$
and square the results through the $x^6$ term.  Compare this result with what the direct Taylor series expansion for $1-\cos x$ would give.
