The Museum of HP Calculators

HP Articles Forum

Hyperbolic Approximations for Trigonometric Functions on the HP-12C

Posted by Gerson Washiski Barbosa on 26 Feb 2005, 1:27 p.m.

```----------------------------------------------------------------
COS/SIN: 01 ENTER    		ACOS:	52 ENTER
02 ENTER		        53 *
03 ENTER         		54 1/x
04 68.3393			55 1
11 /				56 -
12 g e^x			57 g SQRT
13 ENTER		ATAN:	58 1
14 1/x				59 x<>y
15 +				60 g x=<y
16 CHS				61 g GTO 65
17 4 				62 1/x
18 +				63 CHS
19 x<>y			64 9
20 ENTER			65 0
21 *				66 x<>y
22 21813			67 g e^x
27 /				68 ENTER
28 g e^x			69 1/x
29 2				70 -
30 *				71 g LSTx
31 /				72 ENTER
32 ROLLDN			73 1/x
33 *				74 +
34 g SQRT			75 g LSTx
35 /				76 g LN
36 x<>y			77 ROLLDN
37 ENTER			78 /
38 *				79 1844.6
39 CHS				85 *
40 1				86 ROLLDN
41 +				87 ROLLDN
42 g SQRT			88 ROLLDN
43 *				89 6
44 x<>y			90 y^x
45 g GTO 00			91 g SQRT
ASIN:	 46 ENTER			92 CHS
47 *    			93 32.2
48 CHS                      	97 +
49 1				98 /
50 +				99 +
51 g SQRT
----------------------------------------------------------------
Usage:
Trigonometric functions:
Enter angles in degrees, -90 =< x <= 90 (*):
R/S => cos(x)
x<>y => sin(x)
x<>y / => tan(x)
Or,
R/S / => tan(x)
(*) (actual limits for signs consistency: +/- 89.99998084)
Average  percent error: 0.05% (0 to 89.98 degrees).
Inverse trigonometric functions:
Enter arguments within the following ranges:
0 =< x < 1        for arcsin(x);
1E-50 < x <= 1       for arccos(x);
0 =< x < 1E100    for arctan(x) (**)
GTO.46 R/S => arcsin(x)
GTO.52 R/S => arccos(x)
GTO.58 R/S => arctan(x)
Notice that the numeric keys cross-pattern below makes remembering the entry points addresses much easier:
|8|
|4|  |5|  |6|
|2|
central key + upper key: arctan(x)
central key + lower key: arccos(x)
left key + right key: arcsin(x)
(**) (though ARCTAN works for negative arguments up to -1, this
should not be taken into account in order to keep all functions in the first quadrant.
Average ARCTAN percent error: 0.02% within the valid range (maximum 0.06% at x=1)
Some examples:
sin          cos          tan
0.0001     0.0000       1.0000       0.0000
(0.0000)     (1.0000)     (0.0000)   (exact value at this precision)
-1.0000    -0.0175       0.9998      -0.0175
(-0.0175)     (0.9994)    (-0.0175)
15.0000     0.2593       0.9658       0.2685
(0.2588)     (0.9659)     (0.2679)
45.0000     0.7077       0.7065       1.0016
(0.7071)     (0.7071)     (1.0000)
89.0000     0.9998       0.0175      57.2041
(0.9998)     (0.0175)    (57.2900)
89.9900     1.0000       1.7450E-4   5730.7974
(1.0000)     (1.7453E-4)  5729.5779
89.9999     1.0000       1.4120E-6   708193.48
(1.0000)     (1.7453E-6)  572957.80
asin         acos         atan
0.0000     0.0000      Error 0       0.0000
(0.0000)    (90.0000)     (0.0000)
0.0001     0.0057      89.9943       0.0057
(0.0057)    (89.9943)     (0.0057)
0.1000     5.7383      84.2617       5.7097
(5.7392)    (84.2608)     (5.7106)
0.9999    89.1899       0.8101      45.0239
(89.1897)     (0.8103)    (44.9971
1.0000    Error 0       0.0000      45.0268
(90.0000)     (0.0000)    (45.0000)
2.0000       -             -        63.4241
(63.4349)
5.0000       -             -        78.6904
(78.6901)
999.9999       -             -        89.9427
(89.9427)
1E6          -             -        89.9999
89.9999
1E7          -             -        90.0000
90.0000
Running times are about 4 to 5.5 seconds, depending on the function (not argument dependable).
--------------------------------------------------------------
Although there are a few excellent programs for computing trigonometric functions in the HP-12C, I decided to
write this one to fit some specific needs: reasonable running time, arguments and results in degrees and accurate enough
for any practical purposes. The running times are not as good as I initially thought they would be by using this approach,
but are reasonable. As of the accuracy, it is comparable to a good slide ruler: about 0.05%.
This program uses all 99 memory steps but leaves all remaining registers free (RO to R6 and the financial
registers), however the stack is not preserved.
The expressions (I) and (II) below are a fair approximation to cosine and arctangent. The dividends are hyperbolic
functions close to their respective trigonometric functions and agreeing exactly at the extremes of their valid ranges.
The divisors were obtained through curve fitting.
(I)     cos(x) ~ (2-cosh(0.83840144x))/exp(0.15049479x^2), x in radians                  ( 0.83840144 = 2 acosh(2)/pi )
(-pi/2 =< x =< pi/2)
or, for programming optimization:
cos(x) ~ (4-(exp(x/68.3393)+1/(exp(x/68.3393))))/(2exp(x^2/21813)), x in degrees,
and
(II)    arctan(x) ~ 1.03125550*tanh(x)/(1.03143767-0.032032226*x^3), results in radians     (1.03125550 = pi/(4*tanh(1)),
(0 =< x =< 1)
or
arctan(x) ~ 1.03125550((exp(x)-1/exp(x))/(exp(x)+1/exp(x)))/(1.03143767-0.032032226x^3), results in radians
or, for programming optimization:
arctan(x) ~ 1844.6((exp(x)-1/exp(x))/(exp(x)+1/exp(x)))/(32.2-(abs(x))^3); (0 =< x < 1)  and
arctan(x) ~ 90+1844.6((exp(-1/x)-1/exp(-1/x))/(exp(-1/x)+1/exp(-1/x)))/(32.2-(abs(-1/x))^3); x>=1, results in
degrees,
The other functions are computed as follows:
sin(x)=sgn(x)*sqrt(1-cos^2)     (Since sgn(x) is computed as sqrt(x^2)/x, sin(x) fails for x=0)
tan(x)=sin(x)/cos(x)
arcsin(x)=arccos(sqrt(1-x^2))   (This does not preserve the sign of the function, but allows it to be
computed sequentially, as an argument to ARCCOS)
arccos(x)=arctan(sqrt(1/x^2-1)) (This causes the loss of the sign, but requires less programming steps).
Plots of (I) and (II) may show to what extent these approximations are reliable.
```

Edited: 7 July 2005, 9:49 p.m.