The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

Question about trig functions approximation
Message #1 Posted by Namir on 9 Jan 2013, 9:03 a.m.

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

      
Re: Question about trig functions approximation
Message #2 Posted by Eddie W. Shore on 9 Jan 2013, 9:45 a.m.,
in response to message #1 by 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

            
Re: Question about trig functions approximation
Message #3 Posted by Namir on 9 Jan 2013, 1:57 p.m.,
in response to message #2 by Eddie W. Shore

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.

                  
Re: Question about trig functions approximation
Message #4 Posted by Garth Wilson on 9 Jan 2013, 4:46 p.m.,
in response to message #3 by Namir

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?
                  
Re: Question about trig functions approximation
Message #5 Posted by Paul Dale on 9 Jan 2013, 5:37 p.m.,
in response to message #3 by Namir

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

            
Re: Question about trig functions approximation
Message #6 Posted by Dieter on 9 Jan 2013, 3:48 p.m.,
in response to message #2 by Eddie W. Shore

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

            
Re: Question about trig functions approximation
Message #7 Posted by Garth Wilson on 9 Jan 2013, 4:54 p.m.,
in response to message #2 by Eddie W. Shore

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°.
                  
Re: Question about trig functions approximation
Message #8 Posted by htom trites jr on 11 Jan 2013, 10:35 p.m.,
in response to message #7 by Garth Wilson

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

      
Re: Question about trig functions approximation
Message #9 Posted by Valentin Albillo on 10 Jan 2013, 6:49 a.m.,
in response to message #1 by Namir

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.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall