 The Museum of HP Calculators

HP Articles Forum

Fast and Accurate Trigonometric Functions on the HP-17BII

Posted by Gerson W. Barbosa on 12 Jan 2007, 5:17 p.m.

Each of the following sets of equations below constitutes a complete trigonometric functions implementation for the HP-17BII Financial Calculator. The choice for a particular set will depend on the user's preference. Some sets of equations allow for fast solving time, but with input ranges limited to -90 to 90 degrees for the sine function and 0 to 180 degrees for the cosine function (however all valid input ranges are always allowed for the inverse functions). When the inverse trigonometric functions are missing in the equation set they still may be solved iteratively. The answers are always accurate to at least eleven significant figures all through the specified input ranges.

The table below may be of help when choosing the set of equations:

```-------------------------------------------------------------------------------------------------------------------------
Equation    SIN, TAN         COS       ASIN, ACOS       ATAN              Equation     SIN, COS     TAN       ASIN
set     input range    input range  input range   input range         Verification   Solving    Solving   Solving
(degrees)      (degrees)                                       time (s)     time (s)   time (s)  time (s)
-------------------------------------------------------------------------------------------------------------------------
(1)     -90 to 90       0 to 180                                        3.2;  6.9      0.8        1.4       6  (**)
(2)     -90 to 90       0 to 180                                        3.0;  6.1      1.5        2.8      15  (**)
-9.99999999999E499 to
(3)  -999999999999 to 999999999999   -1 to 1   9.99999999999E499        6.4; 15.2      2.1        4.7      18  (**)
* (all)       * (all)
(4)  -999999999999 to 999999999999                                      6.7; 15.6      2.1        4.6      3.1
(5)  -999999999999 to 999999999999                                      6.7; 13.2      1.1        2.1      2.5
-------------------------------------------------------------------------------------------------------------------------
(*) Best performance obtained when providing good initial estimates
(**) Typical (random initial guesses)
```

A good balance between verification and solving times is obtained with the equation set number 1. Despite the longer solving times, especially for the inverse functions, some users might prefer the equation set number 2 because it is more compact. The equation set number 3 should be an option when full range is required. The benefits of the fast solving time in the equations sets number 4 and 5 are annihilated by their long verification times.

```---------------------------------------------

1 - SHORT EQUATIONS (MINIMAX POLYNOMIALS):
SIN=L(Y:X*(5.81776417331E-3+L(X2:SQ(X))*(-3.2818376137E-8+G(
X2)*(5.55391606E-14+G(X2)*(2.0935E-26*G(X2)-4.47566E-20)))))*
(3-4*SQ(G(Y)))
COS=L(Y:(90-X)*(5.81776417331E-3+L(X2:SQ(90-X))*(
-3.2818376137E-8+G(X2)*(5.55391606E-14+G(X2)*(2.0935E-26*G(X2
)-4.47566E-20)))))*(3-4*SQ(G(Y)))
TAN=(L(Y:X*(5.81776417331E-3+L(X2:SQ(X))*(-3.2818376137E-8+G(
X2)*(5.55391606E-14+G(X2)*(2.0935E-26*G(X2)-4.47566E-20)))))*
(3-4*SQ(G(Y))))/(L(Y:(90-ABS(X))*(5.81776417331E-3+L(X2:SQ(90
-ABS(X)))*(-3.2818376137E-8+G(X2)*(5.55391606E-14+G(X2)*(
2.0935E-26*G(X2)-4.47566E-20)))))*(3-4*SQ(G(Y))))
---------------------------------------------

2 - SHORT EQUATIONS (TAYLOR'S SERIES):
SIN=L(Y:SIGMA(N:0:5:1:(-1)^N*(X*PI/540)^(2*N+1)/FACT(2*N+1)))
*(3-4*SQ(G(Y)))
COS=L(Y:SIGMA(N:0:5:1:(-1)^N*((90-X)*PI/540)^(2*N+1)/FACT(2*
N+1)))*(3-4*SQ(G(Y)))
TAN=(L(Y:SIGMA(N:0:5:1:(-1)^N*(X*PI/540)^(2*N+1)/FACT(2*N+1))
)*(3-4*SQ(G(Y))))/(L(Y:SIGMA(N:0:5:1:(-1)^N*((90-ABS(X))*PI/
540)^(2*N+1)/FACT(2*N+1)))*(3-4*SQ(G(Y))))
---------------------------------------------

3 - IF-LESS FULL-RANGE EQUATIONS (TAYLOR'S SERIES):
SIN=0*L(X:(-1)^(L(Q:IP(L(X:MOD(X:360))/90))+FACT(G(Q)))*((180
*(G(Q)-IP(G(Q)/2)))-X))+L(Y:SIGMA(N:0:5:1:(-1)^N*(X*PI/540)^(
2*N+1)/FACT(2*N+1)))*(3-4*SQ(G(Y)))
COS=0*L(X:(-1)^(L(Q:IP(L(X:MOD(90-X:360))/90))+FACT(G(Q)))*((
180*(G(Q)-IP(G(Q)/2)))-X))+L(Y:SIGMA(N:0:5:1:(-1)^N*(X*PI/540
)^(2*N+1)/FACT(2*N+1)))*(3-4*SQ(G(Y)))
TAN=(0*L(X:(-1)^(L(Q:IP(L(X:MOD(L(T:X):360))/90))+FACT(G(Q)))
*((180*(G(Q)-IP(G(Q)/2)))-X))+L(Y:SIGMA(N:0:5:1:(-1)^N*(X*PI/
540)^(2*N+1)/FACT(2*N+1)))*(3-4*SQ(G(Y))))/(0*L(X:(-1)^(L(Q:
IP(L(X:MOD((90-G(T)):360))/90))+FACT(G(Q)))*((180*(G(Q)-IP(G(
Q)/2)))-X))+L(Y:SIGMA(N:0:5:1:(-1)^N*(X*PI/540)^(2*N+1)/FACT(
2*N+1)))*(3-4*SQ(G(Y))))
---------------------------------------------

4 - FULL-RANGE EQUATIONS (TAYLOR'S SERIES):
SIN=0*L(X:IF(L(X:MOD(X:360))<90:X:IF(X<270:180-X:X-360)))+L(
Y:SIGMA(N:0:5:1:(-1)^N*(X*PI/540)^(2*N+1)/FACT(2*N+1)))*(3-4*
SQ(G(Y)))
COS=0*L(X:IF(L(X:MOD(90-X:360))<90:X:IF(X<270:180-X:X-360)))+
L(Y:SIGMA(N:0:5:1:(-1)^N*(X*PI/540)^(2*N+1)/FACT(2*N+1)))*(3-
4*SQ(G(Y)))
TAN=(0*L(X:IF(L(X:MOD(X:360))<90:X*L(T:1):IF(X<270:(180-X)*
SQ(L(T:-1)):(X-360)*L(T:1))))+L(Y:SIGMA(N:0:5:1:(-1)^N*(X*PI/
540)^(2*N+1)/FACT(2*N+1)))*(3-4*SQ(G(Y))))/(0*L(X:IF(ABS(L(X:
(90-X)*G(T)))<90:X:(180-ABS(X))*SGN(X)))+L(Y:SIGMA(N:0:5:1:(-
1)^N*(X*PI/540)^(2*N+1)/FACT(2*N+1)))*(3-4*SQ(G(Y))))
ASIN=0*(L(S1:SGN(X))+L(X:ABS(X))+L(X:IF(X<>1:L(F:1)*X/SQRT(1-
SQ(X)):0*(L(F:2))+X))+L(X:IF(X<1:L(S2:1)*X+L(K1:0):0*(L(K1:PI
/2)+L(S2:-1))+1/X))+L(X:IF(X>2-SQRT(3):0*L(K2:PI/6)+(X*SQRT(3
)-1)/(X+SQRT(3)):X+L(K2:0))))+(G(K1)+G(S2)*(SIGMA(N:0:11:1:(-
1)^N*X^(2*N+1)/(2*N+1))+G(K2)))*G(F)*G(S1)*180/PI
ACOS=0*(L(X:IF(X<>-1:L(F:1)*SQRT((1-X)/(1+X)):0*(L(F:-2))+X))
+L(S1:SGN(X))+L(X:ABS(X))+L(X:IF(X<1:L(S2:1)*X+L(K1:0):0*(L(
K1:PI/2)+L(S2:-1))+1/X))+L(X:IF(X>2-SQRT(3):0*L(K2:PI/6)+(X*
SQRT(3)-1)/(X+SQRT(3)):X+L(K2:0))))+(G(K1)+G(S2)*(SIGMA(N:0:
11:1:(-1)^N*X^(2*N+1)/(2*N+1))+G(K2)))*G(F)*G(S1)*360/PI
ATAN=0*(L(S1:SGN(X))+L(X:ABS(X))+L(X:IF(X<1:L(S2:1)*X+L(K1:0)
:0*(L(K1:PI/2)+L(S2:-1))+1/X))+L(X:IF(X>2-SQRT(3):0*L(K2:PI/6
)+(X*SQRT(3)-1)/(X+SQRT(3)):X+L(K2:0))))+(G(K1)+G(S2)*(SIGMA(
N:0:11:1:(-1)^N*X^(2*N+1)/(2*N+1))+G(K2)))*G(S1)*180/PI
----------------------------------------------

5 - FULL-RANGE EQUATIONS (MINIMAX POLYNOMIALS):
SIN=L(Y:L(X:IF(L(X:MOD(X:360))<90:X:IF(X<270:180-X:X-360)))*(
5.81776417331E-3+L(X2:SQ(X))*(-3.2818376137E-8+G(X2)*(
5.55391606E-14+G(X2)*(2.0935E-26*G(X2)-4.47566E-20)))))*(3-4*
SQ(G(Y)))
COS=L(Y:L(X:IF(L(X:MOD(90-X:360))<90:X:IF(X<270:180-X:X-360))
)*(5.81776417331E-3+L(X2:SQ(X))*(-3.2818376137E-8+G(X2)*(
5.55391606E-14+G(X2)*(2.0935E-26*G(X2)-4.47566E-20)))))*(3-4*
SQ(G(Y)))
TAN=(L(Y:L(X:IF(L(X:MOD(X:360))<90:X*L(T:1):IF(X<270:(180-X)*
SQ(L(T:-1)):(X-360)*L(T:1))))*(5.81776417331E-3+L(X2:SQ(X))*(
-3.2818376137E-8+G(X2)*(5.55391606E-14+G(X2)*(2.0935E-26*G(X2
)-4.47566E-20)))))*(3-4*SQ(G(Y))))/(L(Y:L(X:IF(ABS(L(X:(90-X)
*G(T)))<90:X:(180-ABS(X))*SGN(X)))*(5.81776417331E-3+L(X2:SQ(
X))*(-3.2818376137E-8+G(X2)*(5.55391606E-14+G(X2)*(2.0935E-26
*G(X2)-4.47566E-20)))))*(3-4*SQ(G(Y))))
ASIN=0*(L(S1:SGN(X))+L(X:ABS(X))+L(X:IF(X<>1:L(F:1)*X/SQRT(1-
SQ(X)):0*(L(F:2))+X))+L(X:IF(X<1:L(S2:1)*X+L(K1:0):0*(L(K1:PI
/2)+L(S2:-1))+1/X))+L(X:IF(X>2-SQRT(3):0*L(K2:PI/6)+(X*SQRT(3
)-1)/(X+SQRT(3)):X+L(K2:0))))+(G(K1)+G(S2)*(X*(1+L(X2:SQ(X))*
(-0.33333333333+G(X2)*(0.19999999631+G(X2)*(-0.1428565387+G(
X2)*(0.1110748114+G(X2)*(0.0641264*G(X2)-0.08991517))))))+G(
K2)))*G(F)*G(S1)*180/PI
ACOS=0*(L(X:IF(X<>-1:L(F:1)*SQRT((1-X)/(1+X)):0*(L(F:-2))+X))
+L(S1:SGN(X))+L(X:ABS(X))+L(X:IF(X<1:L(S2:1)*X+L(K1:0):0*(L(
K1:PI/2)+L(S2:-1))+1/X))+L(X:IF(X>2-SQRT(3):0*L(K2:PI/6)+(X*
SQRT(3)-1)/(X+SQRT(3)):X+L(K2:0))))+(G(K1)+G(S2)*(X*(1+L(
X2:SQ(X))*(-0.33333333333+G(X2)*(0.19999999631+G(
X2)*(-0.1428565387+G(X2)*(0.1110748114+G(X2)*(0.0641264*G(X2)
-0.08991517))))))+G(K2)))*G(F)*G(S1)*360/PI
ATAN=0*(L(S1:SGN(X))+L(X:ABS(X))+L(X:IF(X<1:L(S2:1)*X+L(K1:0)
:0*(L(K1:PI/2)+L(S2:-1))+1/X))+L(X:IF(X>2-SQRT(3):0*L(K2:PI/
6)+(X*SQRT(3)-1)/(X+SQRT(3)):X+L(K2:0))))+(G(K1)+G(S2)*(
X*(1+L(X2:SQ(X))*(-0.33333333333+G(X2)*(0.19999999631+G(X2)*(
-0.1428565387+G(X2)*(0.1110748114+G(X2)*(0.0641264*G(X2)-
0.08991517))))))+G(K2)))*G(S1)*180/PI
---------------------------------------------
Note: SIGMA in the equations above is the summation character ([ALPHA] [WXYZ] [OTHER] [MORE])
```

The table below is a demonstration of both the accuracy and ease of use of these equations in the HP-17BII Solver. These results were obtained with the equation set number 1 (assuming the equations are ordered in the list as SIN, COS, TAN).

```-------------------------------------------------------------------------------------------------
Keystrokes                        Display                       Calculation
-------------------------------------------------------------------------------------------------
9 [X] [SIN]                          SIN=0.15643446504                      sin(9)
[EXIT] [v] [CALC] [STO] [X] [COS]    COS=9.99996272745E-1                 cos(sin(9))
[EXIT] [v] [CALC] [STO] [X] [TAN]    TAN=1.74549998555E-2               tan(cos(sin(9)))
[STO] [TAN] [X]       	               X=9.99996272745E-1            atan(tan(cos(sin(9))))
[EXIT] [^] [CALC] [STO] [COS] [X]      X=1.56434385713E-1         acos(atan(tan(cos(sin(9)))))
[EXIT] [^] [CALC] [STO] [SIN] [X]      X=8.99999539825         asin(acos(atan(tan(cos(sin(9))))))
-------------------------------------------------------------------------------------------------
```

This is Mike Sebastian's Forensic Result for that set of equations. Sets 2, 3 and 5 return 9.00000194973, 9.00005188184 and 9.00000229472, respectively.

The fastest results were obtained with the use Minimax Polynomials, as expected. Range reduction has also been important in achieving such accurate results in relatively short time. When Taylor's Series are used solving times increase because fifty per cent more terms are required in the polynomials and the coefficients have to be calculated on the fly. However this allows for more compact equations. For more details refer to Fast and Accurate Trigonometric Functions on the HP-12C Platinum

-------

Note:

In the minimax polynomial equations above, the basic sine equation could be replaced with this one:

```SIN=L(Y:X*(PI/900+L(X2:SQ(X))*(-7.088769242E-9+G(X2)*(
4.3187175E-15-1.2504E-21*G(X2)))))*(SQ(G(Y))*(16*SQ(G(Y))-20)+5)
```

It's slightly shorter and almost as fast (0.9 seconds) and gives answers accurate to eleven digits on the HP-17BII (maximum absolute error: 1.3E-12). The constant 5.81776417331E-3 in the original equations can be replaced with PI/540 at the cost of a few tens of milliseconds in solving time.

Edited: 14 Aug 2007, 6:35 p.m.