# HP Forums

Full Version: Question about trig functions approximation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Hi All,

In your opinion, among the sin, cos, and tan functions (as you get any two trig functions from the value of the third trig function), which one is easier to calculate using polynomial approximations or special approximation? If you are referring to a special approximation can you please state it or offer a link to that information?

Thanks,

Namir

Quote:
Hi All,

In your opinion, among the sin, cos, and tan functions (as you get any two trig functions from the value of the third trig function), which one is easier to calculate using polynomial approximations or special approximation? If you are referring to a special approximation can you please state it or offer a link to that information?

Thanks,

Namir

Namir,

I tend to lean towards sine. However, I find the convergence for the Taylor Series for sine to be super slow. The approximations listed are stated to be good for eight-digit accuracy for the interval [0 to pi/2].

From "Scientific Analysis on the Pocket Calculator" by Jon M. Smith (published 1975):

```Sine and Cosine:  error < 2 x 10^-9 with 0 <= x <= pi/2
sin x = x*(1 + x^2*(a2 + x^2*(a4 + x^2*(a6 + x^2*(a8 + a10*x^2)))))
a2  = -0.16666 66664
a4  =  0.00833 33315
a6  = -0.00019 84090
a8  =  0.00000 27526
a10 = -0.00000 00239
cos x = 1 + x^2*(a2 + x^2*(a4 + x^2*(a6 + x^2*(a8 + a10*x^2)))))
a2  = -0.49999 99963
a4  =  0.04166 66418
a6  = -0.00138 88397
a8  =  0.00002 47609
a10 = -0.00000 26050
```
The "compacted" approximations are said to give three-digit accuracy for [0, pi/2].

```Sine and Cosine:  Error = 2 * 10^-4 and 2 * 10^-9, respectively, for 0 <= x <= pi/2
sin x = x * (1 + x^2*(a2 + a4*x^2))
a2 = -0.16605
a4 =  0.00761
cos x = 1 + x^2*(a2 + a4*x^2))
a2 = -0.49670
a4 =  0.03705
```

There was also a thread of calculating trig functions with the HP 12C, that was a while ago.

Hope this helps,

Eddie

Thanks Eddie for the nice info!!!

Once I get sin(x) I can then calculate cos(x) and tan(x) as:

cos(x) = sqrt(1 - sin(x)^2)

And,

tan(x) = sin(x)/cos(x)

Edited: 9 Jan 2013, 1:59 p.m.

You can easily set up your own polynomial approximation with exactly taylored errors using standard tools like Excel and a few minutes time. I tried something similiar to the sine approximation you mentioned and came up with a result with 10-digit accuracy:

```sin x = x*(1 + x^2*(a2 + x^2*(a4 + x^2*(a6 + x^2*(a8 + a10*x^2)))))

a2  = -0,16666 66660
a4  =  0,00833 33303
a6  = -0,00019 84078 2
a8  =  0,00000 27521 57
a10 = -0,00000 00238 29
```
Max. absolute error in [0; pi/2] is approx. +/- 2 E-11

Max. relative error in [0; pi/2] is less than +5 / -2 E-11

A rational approximation should perform even better.

Dieter

Quote:
cos(x) = sqrt(1 - sin(x)^2)
For cos(x), why not just use the same sin function, adding 90° to the input first?

Quote:
The "compacted" approximations are said to give three-digit accuracy for [0, pi/2].
Years ago when I was figuring out the functions to do in 16-bit scaled-integer in embedded systems where I didn't want the overhead of floating-point, I found it was better to calculate only 0-45° and get the rest of the circle from there, since when doing only a few terms of the taylor series with tweaked coeffients, the error jumped way up after 45°.

Quote:
cos(x) = sqrt(1 - sin(x)^2)

Careful with this one. As sin(x) -> 1, cos(x) -> 0 and you're going to lose precision very quickly. A fused multiply add or using sqrt( (1-sin(x))(1+sin(x))) will assist somewhat.

Likewise cos(x) = sin(x+90 degrees) is also risky, although likely less so since cos(x) approaches 1 quadratically as x -> 0.

The 34S uses a straight Taylor series expansion (after a high precision modulo reduction step) and calculates both sin and cos at the same time. It would converge faster if I reduced the argument into the range 0 .. pi/4 or even -pi/4 .. pi/4 but this doesn't seem necessary. See sincosTaylor() in decn.c for the implementation.

- Pauli

Hi, Namir:

Quote:
In your opinion, among the sin, cos, and tan functions (as you get any two trig functions from the value of the third trig function), which one is easier to calculate using polynomial approximations or special approximation?

Let's see:

• Using polynomial approximations, sin(x) and cos(x) are the same and the easiest, while tan(x) is slightly worse because tan(x) has poles, which polynomial approximations cannot cope with. Even if the pole is well outside the approximation's intended range after range reduction it still negatively affects accuracy.

• Using rational approximations (the ratio of two polynomials) all three of sin(x), cos(x), tan(x) are about the same, as rational approximations can adequately cope with the rather well-behaved tan(x) poles so accuracy won't be affected.
That being so, I'd compute sin(x) using some suitable range reduction and the corresponding odd-powers minimax polynomial for that range, then derive cos(x) and tan(x) from it.

Some allegedly useful references (PDF documents):

Happy New Year 2013 and

Best regards from V.

```
```

The 0º - 45º approach I used for a >32 bit package. The hard part was strictly increasing and strictly decreasing -- and testing for that.