HP Forums

Full Version: Trigonometric Functions (HP-17BII)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Here is a new set of trigonometric functions equations for the HP-17BII. These are based upon approximations for both sine and cosine involving a few Taylor series terms and the correspondent hyperbolic functions, as discussed here last week.

SIN=(1-2*IP((MOD(X:360)/180)))*(2*SIGMA(K:1:21:4:(L(A:MOD(X:180)*PI/180))^K/FACT(K))-(EXP(G(A))-EXP(-G(A)))/2)

COS=(1-2*IP((MOD(X+90:360)/180)))*(2*SIGMA(K:1:21:4:(L(A:MOD(X+90:180)*PI/180))^K/FACT(K))-(EXP(G(A))-EXP(-G(A)))/2)

TAN=(4*SIGMA(K:1:21:4:(L(A:MOD(X:180)*PI/180))^K/FACT(K))-EXP(G(A))+EXP(-G(A)))/
(4*SIGMA(K:0:20:4:(L(A:MOD(X:180)*PI/180))^K/FACT(K))-EXP(G(A))-EXP(-G(A)))

The equations are short enough to be quickly keyed into the calculator without mistakes. Arguments in degrees up to +/- 999 999 999 999 are accepted for SIN and TAN (The range is slightly shorter for COS because it is computed as SIN shifted 90 degrees).
The maximum absolute error for SIN should be in theory 3.5*10-13 (2*pi^25/25! -- that is close to what is obtained on the HP-200LX solver as can be seen in the third plot), but in practice the HP-17BII, a 12-digit calculator (four less than the HP-200LX solver), can deliver results accurate to 10 or 11 results only. When working next to its accuracy level, the HP-200LX loses about two digits of accuracy (second plot).

The running times are very good: about 1.1 second for SIN and COS and 2.2 seconds for TAN. Of course the inverse functions can also be calculated, but this will take longer and will depend on the initial guess.

Plots of SIN, COS and TAN on the HP-200LX:


The second plot shows the difference of the SIN equation in relation to the built-in SIN function (in degrees mode), when seven terms of the Taylor series are used (...SIGMA(K:1:25:4:...).

For arguments and results in radians, the conversion factor pi/180 should be used. Alternatively, the following set of equations (arguments and results in radians, but input range limited to [-pi..pi]) can be used:

SIN=2*SIGMA(K:1:21:4:X^K/FACT(K))-(EXP(X)-EXP(-X))/2
COS=2*SIGMA(K:0:20:4:X^K/FACT(K))-(EXP(X)+EXP(-X))/2
TAN=(4*SIGMA(K:1:21:4:X^K/FACT(K))-EXP(X)+EXP(-X))/(4*SIGMA(K:0:20:4:X^K/FACT(K))-EXP(X)-EXP(-X))
This latter set should work on all HP-17BII versions. I don't have any HP-17BII+ handy to test the first set of equations. Perhaps a few changes have to me made, because of issues with L() and G() on the newest models.

The cos-cosh and sin-sinh series calculate the cos and sin values, respectively, with good accuracy. They are rivaled (as web searches recently reminded me) by their CORDIC versions for cos and sin. The CORDIC algorithms use pre-calculated tables of values to replace the rich list of polynomial coefficients used by summation series.

The moral of the proverbial story is ... if you need to calculate cos and sin (and other trig functions and their inverses) you need to work with sizable arrays of coefficients or tables. In other words, high-precision results come with a serious calculation effort. Using polynomials with 2 to 4 coefficients will generally not give you high precision results. You will get results that are good for a few decimal places. These limited results were acceptable in the days when the slide rule and log tables were kings and ruled the computational landscape! The advent of electronic calculators dethroned these vintage tools and made us expect high precision results.

Namir

Namir,

Here is a very good reference, in case you are not aware of it yet:

http://www.research.scea.com/research/pdfs/RGREENfastermath_GDC02.pdf

http://www.research.scea.com/gdc2003/fast-math-functions_p1.pdf

http://www.research.scea.com/gdc2003/fast-math-functions_p2.pdf

BTW, here is the Minimax version of the radians sine equation (input range [-pi..pi]). The polynomial is four orders lower (that it, one term less), evaluated using Horner's Method. Also, there is only one instance of EXP, thus the the evaluation is very fast (about 0.7 seconds).

SIN=X*(2+L(Q:SQ(SQ(X)))*(1/60+G(Q)*(5.511463854E-6+G(Q)*(
3.211803895E-10+5.6307313E-15*G(Q)))))-(L(V:EXP(X))-1/G(V))/2

Regards,

Gerson.

--------------------

SUMMARY

MINIMAX EQUATIONS (RADIANS)

SIN=X*(2+L(Q:SQ(SQ(X)))*(1/60+G(Q)*(5.511463854E-6+G(Q)
*(3.211803895E-10+5.6307313E-15*G(Q)))))-(EXP(X)-EXP(-X))/2

COS=(L(V:SQRT(2)*(2+L(Q:SQ(SQ(X)))*(1/192+G(Q)*(
1.93762394E-7+1.01959E-12*G(Q)))-(EXP(X/2)+EXP(-X/2))/2))+1)*
(G(V)-1)

FULL RANGE TAYLOR SERIES EQUATIONS (DEGREES)

SIN=(1-2*IP((MOD(X:360)/180)))*(2*SIGMA(K:1:21:4:(L(A:MOD(X:
180)*PI/180))^K/FACT(K))-(EXP(G(A))-EXP(-G(A)))/2)

COS=(1-2*IP((MOD(X+90:360)/180)))*(2*SIGMA(K:1:21:4:(L(A:MOD(
X+90:180)*PI/180))^K/FACT(K))-(EXP(G(A))-EXP(-G(A)))/2)

TAN=(4*SIGMA(K:1:21:4:(L(A:MOD(X:180)*PI/180))^K/FACT(K))-EXP
(G(A))+EXP(-G(A)))/(4*SIGMA(K:0:20:4:G(A)^K/FACT(K))-EXP(G(A))
-EXP(-G(A)))

FULL RANGE MINIMAX EQUATIONS (DEGREES)

SIN=(1-2*IP((MOD(X:360)/180)))*(L(A:MOD(X:180)*PI/180)*(2+L(
Q:SQ(SQ(G(A))))*(1/60+G(Q)*(5.511463854E-6+G(Q)*(
3.211803895E-10+5.6307313E-15*G(Q)))))-(EXP(G(A))-EXP(-G(A)))
/2)

COS=(1-2*IP((MOD(X+90:360)/180)))*(L(A:MOD(X+90:180)*PI/180)*
(2+L(Q:SQ(SQ(G(A))))*(1/60+G(Q)*(5.511463854E-6+G(Q)*(
3.211803895E-10+5.6307313E-15*G(Q)))))-(EXP(G(A))-EXP(-G(A)))
/2)

TAN=(L(A:MOD(X:180)*PI/180)*(2+L(Q:SQ(SQ(G(A))))*(1/60+G(Q)*(
5.511463854E-6+G(Q)*(3.211803895E-10+5.6307313E-15*G(Q)))))-(
EXP(G(A))-EXP(-G(A)))/2)/((L(V:SQRT(2)*(2+G(Q)*(1/192+G(Q)*(
1.93762394E-7+1.01959E-12*G(Q)))-(EXP(G(A)/2)+EXP(-G(A)/2))/
2))+1)*(G(V)-1))


Edited: 11 Apr 2013, 11:41 a.m.