So, I am writing a C++ program that sum the series expansion of sin(x). But for large values of x my program fails.

$\sin(x)=x-(x^3/3!)+(x^5/5!)-...=$$\sum_{n=0}^n (-1)^n\frac{x^{2n+1}}{(2n+1)!}$

I don't understand, how can we say that the sum converge for all x?

If $\lim_{x\to \infty}$ then the terms in the series blows up towards infinity and the sum diverges. What is it that I don't understand?

  • $\begingroup$ Do a test for converging, for example. $\endgroup$ – mrs Oct 3 '18 at 20:19
  • $\begingroup$ Have you tried the algorithm of Horner. $\endgroup$ – hamam_Abdallah Oct 3 '18 at 20:20
  • $\begingroup$ Even if the terms are large, they still alternate in sign of each other, so the sum wouldn't storm off into infinity $\endgroup$ – WaveX Oct 3 '18 at 20:21
  • $\begingroup$ $\sin$ expansion should converge for any real $x$ $\endgroup$ – Vasya Oct 3 '18 at 20:22
  • $\begingroup$ Yes, I've implemented Horners scheme. But, for some reason the program fails for large x. Does it have to to with float or double datatypes to do? $\endgroup$ – LEARN Oct 3 '18 at 20:24

To avoid an overflow which makes the series diverging, use recursive functions

to compute powers $x^n$ and factorials $n!$.

use the formulas $$x^i=x^{i-1}.x$$ and



Check the Lagrange Remainder of your Taylor series $$R_n = \frac {f^{(n+1)}(\alpha) x^{n+1}}{(n+1)!}$$

Since you have ${(n+1)!}$ dominating $x^{n+1}$, the remainder will approach zero and your series converge.

You can also look at the series as an alternating series whose terms are approaching zero, so it converges.

For larger $x$ values you need more terms to get good approximation.

  • $\begingroup$ This is no computationally stable method for larger $x$ and finite arithmetic, i.e. IEEE double. You cannot simply use more terms because the accumulated sum is already too inaccurate. $\endgroup$ – gammatester Oct 3 '18 at 20:57
  • 1
    $\begingroup$ Mathematically speaking the series converges. Computationally it is not clear for large values of x. We need better algorithms or better computers to verify the convergence numerically. $\endgroup$ – Mohammad Riazi-Kermani Oct 3 '18 at 21:04
  • 2
    $\begingroup$ Numerically it is clear, that the method is unstable. For $x=10$ you will loose about $11$ bits because the largest term is greater that $2^{11}.$ And for $x > 40$ you will loose all bits of a $53$-bit double. $\endgroup$ – gammatester Oct 3 '18 at 21:10
  • $\begingroup$ @gammatester: one way to combat loss of precision is to calculate $x^n/n!$ as a series of multiplications: $\frac{x}{n} \cdot \frac{x}{n-1} ...$ The terms are actually not that big so there is no danger of overflow. $\endgroup$ – Vasya Oct 4 '18 at 13:32
  • $\begingroup$ No. Have you tried it? I did and the result is: $\sin 40= 0.745113160479349$ and the computed sum is $1.361642816548455,$ which is obviously totally incorrect (using $138$ terms). It would be possible, if the maximum magnitudes would have very few bits, but their significands are full bit, due to the division by $n, n-1, \dots$ The values are: for $n=38,$ decimal value $-370420141783066.62$, hex $\mathtt{C2F50E528F7F81AA}$ and $n=40, 361385504178601.56,$ hex $\mathtt{42F48AD9E9A81A99}.$ Possible overflow is no problem for this small $x$, the error comes from catastrophic cancellation. $\endgroup$ – gammatester Oct 4 '18 at 13:55

The ratio of consecutive terms is $-\frac{x^2}{2(n+1)(2n+3)}$, which $\to 0$ as $n\to\infty$ regardless of the value of $x$, so the series always converges. I'm not sure why you're having trouble programming it, but multiplying the latest term by the required ratio may help. You might also find it helpful to use a series for small $|x|$, while recursing down the size of $x$ using identities such as $\sin 3x=\sin x(3-4\sin^2 x)$. (You're probably experienced enough programming for this tip to be unnecessary, but just in case: cash $\sin x$ in the above so you don't have to compute it twice, or you'll increase the algorithm's computational complexity.)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.