# How many iterations of Taylor series for n correct decimal digits

I'm using Taylor series to estimate trigonometric functions. So I need to know exactly how many iterations of Taylor series (say for sine) are needed for n decimal digits precision?
(I'm writing a calculator program)
Thanks in advance

• That depends on the function. Look into the error. – Git Gud Oct 10 '13 at 13:11
• Can you explain a bit? Particularly for sine function. (Simple please, I'm a first year student at university studying computer science) – SepehrM Oct 10 '13 at 13:16
• I don't know how to give you a simple answer, to be honest. Hopefully someone better versed at this can find a way to explain it to you. – Git Gud Oct 10 '13 at 13:18
• It also very dependent on the computing environment. Although $\sin(x)$ has an alternating Taylor series which is theroretically convergent for all real $x$, you will actually have numerical convergence only for small absolute values, for larger $x$ there must be cancellation, eg. for $x=42$ the largest term is about $10^{17}$, but $|\sin(x)|\le 1$! Therefore you will not get any correct digit with IEEE double! – gammatester Oct 10 '13 at 13:41
• @gammatester, What's best algorithm for estimating sine? – SepehrM Oct 10 '13 at 13:45

## 1 Answer

The answer depends heavily on the distance between the point where you are taking the Taylor series (presumably at$~0$) and the value$~x$ for which you want to compute $\sin x$. The larger $|x|$, the worse the performance of the Taylor series, and the more terms are needed for a reasonable approximation. You may of course use the periodicity of the sine function to limit $|x|$ (supposing $x$ is real) before invoking your approximation.

• So there is no formula for number of iteration needed knowing x and the desired precision? – SepehrM Oct 10 '13 at 13:19
• You can work out a formula for the number of iterations needed given the coefficients of the Taylor series itself, the value of $|x|$, and the desired precision. In the practical range of application, evaluated terms of the Taylor series tend to $0$ at least as fast as a geometric progression, which means that the error is proportional to the first term not taken into account. If the geometric progression converges rapidly (ratio much less than$~1$), the size of that first omitted term itself is a fair estimate. – Marc van Leeuwen Oct 10 '13 at 13:25