# Math Help - Programming the sin function

1. ## Programming the sin function

If I were to write my own sine function, in some programming language, how would I do it?

I know if given an angle, you can simply substitute that into a right triangle with hypotenuse 1 and determine the triangle. But to get to return the result, I keep coming back to using the sine function, which is what I'm trying to program.

So I'm just curious as to how the sine function works to a computer.

Thanks everyone.

2. Originally Posted by lucius
If I were to write my own sine function, in some programming language, how would I do it?

I know if given an angle, you can simply substitute that into a right triangle with hypotenuse 1 and determine the triangle. But to get to return the result, I keep coming back to using the sine function, which is what I'm trying to program.

So I'm just curious as to how the sine function works to a computer.

Thanks everyone.
First because $\sin(x)$ is periodic with period $2\pi$ and has other symmetries we need only calculate it for $x \in [0,\pi/2]$

Then:

$\sin(x)= x(1-x^2/6)+\varepsilon \ \ \ \ \ (1)$

where

$|\varepsilon|\le |x|^5/120\ \ \ \ \ \ \ \ (2)$

Also:

$\sin(x)=2\sin^2(x/2)-1 \ \ \ \ \ \ (3)$

So if $x$ is small enough so that the error in $(2)$ is acceptable then use $(1)$. If the error is too big use $(3)$ and $\sin (x/2^n)$ in $(1)$ with $n$ choosen to give acceptable error.

In practice a better polynomial approximation is used as are neater ways of doing the calculations in general.

3. You can also use a Taylor Series Approximation, which requires some degree of iteration to get a good approximation. The formula for sine is: Summation from n = 0 to infinity of (-1^n)/(2n+1)! * x^(2n+1).

4. Hello, lucius!

. . . So I'm just curious as to how the sine function works to a computer.

I would use the infinite series for $\sin x$, where $x$ is in radians.

. . $\sin x \;=\;x - \frac{x^3}{3!} + \frac{x^5}{5!} + \frac{x^7}{7!} - \frac{x^9}{9!} - \frac{x^{11}}{11!} +\; \hdots$

5. Originally Posted by macosxnerd101
You can also use a Taylor Series Approximation, which requires some degree of iteration to get a good approximation. The formula for sine is: Summation from n = 0 to infinity of (-1^n)/(2n+1)! * x^(2n+1).
Originally Posted by Soroban
Hello, lucius!

I would use the infinite series for $\sin x$, where $x$ is in radians.

. . $\sin x \;=\;x - \frac{x^3}{3!} + \frac{x^5}{5!} + \frac{x^7}{7!} - \frac{x^9}{9!} - \frac{x^{11}}{11!} +\; \hdots$

In my earlier post you will note that I used a Taylor/McLaurin series truncated at the $x^3$ term for my small angle approximation, and provided a bound for the error. Almost universally a long Taylor series is never used. The general approach is to use a polynomial approximation for small angles and then the properties of the trig functions to reduce the required angle to something in the valid range of the approximation.

The fact that I used a Taylor approximation is of course misleading, in practice an optimal polynomial for the required angle range is used.

Note in the approximation that I used the maximum error $<10^{-12}$ when $|x|<0.01$ radian.