1

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 ?

Etemon
  • 6,437
  • If you use series expansion, there is way to know a priori the number of terms to be added for a given accuracy. If this is of any interest for you, just tell. – Claude Leibovici Dec 31 '20 at 12:05

5 Answers5

3

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}$.

enter image description here

The difference plot to the 8th degree Taylor polynomial

enter image description here

shows that both alternative formulas have errors in the magnitude of the machine precision.

Lutz Lehmann
  • 126,666
2

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}$.

  • Would you add the error terms for the Pade approximations? Near zero, their accuracy will be destroyed by a single rounding error, so it is relevant to know how accurate they are when, say, $|x| < \frac{\pi}{6}$, such that $|\cos(x)| > \frac{1}{2}$ and subtractive cancellation is an issue. – Carl Christian Dec 31 '20 at 15:32
  • @CarlChristian. With no-care implementations, yes, we can face this kind of trouble. Here, as ofter, I compared with Taylor series. For me, a key paper is https://uknowledge.uky.edu/cgi/viewcontent.cgi?referer=https://www.google.com/&httpsredir=1&article=1016&context=math_facpub – Claude Leibovici Jan 01 '21 at 03:24
  • It does not matter if we manage to represent the chosen approximation exactly or not. Ultimately, the result has to be rounded to working precision. An approximation that is much more accurate than working precision is overkill. It is reasonable to assume that OP is working in IEEE double precision (DP) and OP has to cover the entire representable range. This can be done with the approximation suggested by OP. The suggested Pade approximations are much more accurate then DP near $x=0$, but much less accurate than DP near $x=\pi/3$, where subtractive cancellation ceases to be a problem. – Carl Christian Jan 01 '21 at 22:23
2

You are already mindful of rounding errors, but that is not the only issue here.

  1. 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}$.
  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.$$
  3. 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$.

  1. 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.
  2. 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.
Carl Christian
  • 12,583
  • 1
  • 14
  • 37
1

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$

Henry Lee
  • 12,215
1

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.

Moo
  • 11,311
Oscar Lanzi
  • 39,403