The Museum of HP Calculators

HP Articles Forum

HP-12C Trigonometric Functions

Posted by Viktor Toth on 21 Oct 2001, 9:52 p.m.

By popular demand: a set of two programs that compute all six trigonometric functions (sin, cos, tan, arc sin, arc cos, and arc tan) on the HP-12C.

The first program at address 01 computes the cosine, tangent, and sine of the argument (in radians) and places the result in this order in X, Y, and Z. If all you need is the sine, you can stop at step 24. This program has four shortcomings:

1. For an argument of 0, the program fails, due to its use of the argument's reciprocal value in the iteration. It is possible to correct this deficiency by inserting four lines of code and adjusting line numbers accordingly:

```	01	x=0
02	INTG
03	x=0
04	GTO 29 (line 25 in the original listing)
```

2. For arguments outside the range of -PI/2 < x < PI/2, the sign of the cosine and tangent results may not be correct.

3. For arguments with a large (> PI) absolute value, results are increasingly inaccurate.

4. For arguments near n*PI/2 (n is integer), the accuracy may be less than desirable, due to rounding effects.

The second program has three entry points (marked) for computing the arc cosine, arc tangent, and arc sine of the argument. The result is in radians. This program also has shortcomings:

1. The arc cos is calculated incorrectly for negative arguments

2. Execution time is excessive if the arc sin argument's absolute value is near 1. (A workaround is to compute the arc cos instead, and then subtract the result from PI/2.)

Here are some typical results:

```X       cos X           error           time            arc cos X       error           time
0.01    0.9999500004    < 2e-11          5 s            0.01000000167   < 3e-10          6 s
PI/4    0.7071067810    < 1e-10          8 s            0.7853981635    < 2e-10         37 s
PI/3    0.5000000003    < 1e-9          10 s            1.047197550     < 1e-9          75 s
1.77    0.1978888145    < 6e-10 (sign)  11 s            1.371592654	< 3e-10        430 s
3.14    0.9999987317    < 3e-11 (sign)  15 s            0.001592671386  < 3e-7           5 s
```

So, there's room for improvement.

And now, the programs themselves:

```	01	STO 0
02	STO 1
03	1/x
04	STO 2
05	STO+2
06	RCL 2
07	STO/1
08	Rv
09	STO+2
10	RCL 2
11	CHS
12	STO/1
13	RCL 0
14	RCL 1
15	STO+0
16	CLX
17	RCL 0
18	-
19	x=0
20	GTO 24
21	Rv
22	Rv
23	GTO 05
24	LASTx		X=sin
25	1
26	LASTx
27	ENTER
28	X
29	-
30	SQRT
31	/
32	LASTx
33	R/S		X=cos	Y=tan	Z=sin
=============
34 acos	ENTER
35	X
36	1
37	-
38	CHS
39	SQRT
40	GTO 48
41 atan	ENTER
42	ENTER
43	X
44	1
45	+
46	SQRT
47	/
48 asin	STO 0
49	STO 1
50	ENTER
51	X
52	1
53	STO*1
54	STO*1
55	1
56	+
57	STO/1
58	1
59	+
60	STO/1
61	x-y
62	STO*1
63	x-y
64	RCL 0
65	RCL 1
66	STO+0
67	CLX
68	RCL 0
69	-
70	x=0
71	GTO 74
72	Rv
73	GTO 53
74	LASTx
75	GTO 00
```