# [music-dsp] fast sin(x) function

Wed Jun 11 16:40:39 EDT 2008

```On 10 Jun 2008, at 15:31, robert bristow-johnson wrote:

> if the OP wants a sin() function for the purpose of generating a
> nice sine wave for sound, then i would also recommend LUT that is
> large enough that linear interpolation would be good enough.

Okay, for now I'm just using sin(x) I'll have a look at using a LUT.

>  if instead the sin() function is used in control/coefficient
> generation, then i would suggest using the finite order series
> below.  with the exception of arctan(), there is no division in any
> of these.  should be fast for a DSP.

thanks for these I'll experiment with them later.  I am finding that
sin(x) functions are cropping up in a variety of places in my work
(coefficients etc) as well as cos(x) so I've been on the lookout for
something fast and accurate but I have been trying to avoid using
lookup tables.. can't say why exactly something just told me to avoid
them in certain situations.  Oh and I am developing for PowerPC and
Intel.

Thanks for all the suggestions.

Stephen.

>
>
>
>> Or, then, use one of the better math libraries, which...do that for
>> you. ;)
>
>
>
>
>       log2(1+x) ~=              1.4425449290 * x
>                        +       -0.7181451002 * x^2
>                        +        0.4575485901 * x^3
>                        +       -0.2779042655 * x^4
>                        +        0.1217970128 * x^5
>                        +       -0.0258411662 * x^6
>
> 0 <= x <= 1     |error| < 2.217e-6   (exact when x=0 and x=1)
>                   (about 1/375th of a cent)
>
>        2^x ~=                   1.0
>                        +        0.6930321187 * x
>                        +        0.2413797743 * x^2
>                        +        0.0520323499 * x^3
>                        +        0.0135557571 * x^4
>
> 0 <= x <= 1     |error|/(2^x) < 3.340e-6  (exact when x=0 and x=1)
>                   (about 1/173rd of a cent)
>
>        sqrt(x) ~=               0.2286139087
>                        +        1.3027308205 * x
>                        +       -0.9125353819 * x^2
>                        +        0.5029902702 * x^3
>                        +       -0.1217996175 * x^4
>
> 1/2 <= x <= 1   |error|/sqrt(x) < 1.076e-5
> (exact when x=1 and nearly exact when x=1/2)
>
>
>  sin((pi/2)*x) ~=               1.5707963268 * x
>                        +       -0.6459640619 * x^3
>                        +        0.0796915849 * x^5
>                        +       -0.0046768800 * x^7
>                        +        0.0001530302 * x^9
>
> -1 <= x <= 1  (i can't find the max error results, but it's small)
>
>
>      arctan(x) ~=  x/f(x^2)
>
>   where   f(u)  =               1.0
>                        +        0.3328895051 * u
>                        +       -0.0846792282 * u^2
>                        +        0.0325223264 * u^3
>                        +       -0.0074930586 * u^4
>
> -1 <= x <= 1   (can't find the error results here either, but it's
> small.)
>
>
> --
>
> r b-j                  rbj at audioimagination.com
>
> "Imagination is more important than knowledge."
>
> --
> dupswapdrop -- the music-dsp mailing list and website:
> subscription info, FAQ, source code archive, list archive, book