**HP Articles Forum**

[Return to the Index ]

[ Previous | Next ]

**Fast and Accurate Trigonometric Functions on the HP-12C**

*Posted by Gerson Washiski Barbosa on 22 Apr 2005, 2:33 a.m.*

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

Keystrokes |Display | Comments[f][P/R] | | [f]CLEAR[PRGM] |00- | 3 |01- 3 | SIN_COS [/] |02- 10 | [STO][n] |03- 44 11 | [ENTER] |04- 36 | [x] |05- 20 | [ENTER] |06- 36 | [ENTER] |07- 36 | [ENTER] |08- 36 | [RCL]6 |09- 45 6 | [x] |10- 20 | [RCL]5 |11- 45 5 | [+] |12- 40 | [x] |13- 20 | [RCL]4 |14- 45 4 | [+] |15- 40 | [x] |16- 20 | [RCL]0 |17- 45 0 | [+] |18- 40 | [RCL][n] |19- 45 11 | [x] |20- 20 | [ENTER] |21- 36 | [ENTER] |22- 36 | [x] |23- 20 | 4 |24- 4 | [x] |25- 20 | [CHS] |26- 16 | 3 |27- 3 | [+] |28- 40 | [x] |29- 20 | [ENTER] |30- 36 | [ENTER] |31- 36 | [ENTER] |32- 36 | [x] |33- 20 | [CHS] |34- 16 | 1 |35- 1 | [+] |36- 40 | [g][SQRT] |37- 43 21 | [g][GTO]00 |38- 43,33 00 | [ENTER] |39- 36 | ASIN [x] |40- 20 | [1/x] |41- 22 | 1 |42- 1 | [-] |43- 30 | [1/x] |44- 22 | [g][GTO]51 |45- 43,33 51 | [ENTER] |46- 36 | ACOS [x] |47- 20 | [1/x] |48- 22 | 1 |49- 1 | [-] |50- 30 | [g][SQRT] |51- 43 21 | [g][x=0] |52- 43 35 | ATAN [g][GTO]00 |53- 43,33 00 | 1 |54- 1 | [x<>y] |55- 34 | [g][x<=y] |56- 43 34 | [g][GTO]60 |57- 43,33 60 | [1/x] |58- 22 | 9 |59- 9 | 0 |60- 0 | [STO][FV] |61- 44 15 | [RDN] |62- 33 | roll down key (Rv) [1/x] |63- 22 | [STO][n] |64- 44 11 | [ENTER] |65- 36 | [x] |66- 20 | [+] |67- 40 | [g][SQRT] |68- 43 21 | [RCL][n] |69- 45 11 | [-] |70- 30 | [STO][n] |71- 44 11 | [ENTER] |72- 36 | [x] |73- 20 | [ENTER] |74- 36 | [ENTER] |75- 36 | [g][12/] |76- 43 12 | [ENTER] |77- 36 | [RCL]3 |78- 45 3 | [+] |79- 40 | [x] |80- 20 | [RCL]2 |81- 45 2 | [+] |82- 40 | [x] |83- 20 | [RCL]1 |84- 45 1 | [+] |85- 40 | [x] |86- 20 | 1 |87- 1 | [+] |88- 40 | [RCL][n] |89- 45 11 | [x] |90- 20 | 2 |91- 2 | [x] |92- 20 | [RCL]0 |93- 45 0 | [/] |94- 10 | [RCL][FV] |95- 45 15 | [g][x=0] |96- 43 35 | [x<>y] |97- 34 | [x<>y] |98- 34 | [-] |99- 30 | [f][P/R] | |(Listing generated by Tony Hutchin's 12c.exe utility)

-------------------------------------------------------------------------------After entering the program, store the following constants:

R0: 1.745329252E-02 (pi/180, for angle conversions)

R1: -3.333324820E-01 R2: 1.998722722E-01 (a, b and c coefficients for the R3: -1.397054038E-01 arctangent polynomial)

R4: -8.860960894E-07 R5: 1.349561365E-11 (a, b and c coefficients for the R6: -9.716369450E-17 sine polynomial)

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

Here is a state file for Nonpareil so the program can be tried out on the simulator before one decides to key it in:As the state file was saved with no extension, some browsers may add a .txt extension to it. The file extension may be removed or left unchanged (the current version of Nonpareil will accept it just the same).

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

Usage:Enter angles in degrees, 0 <= x <= 90:Trigonometric functions:cos(x): [R/S] sin(x): [R/S] [x<>y] tan(x): [R/S] [/]

Maximum absolute error: about 5E-10 (sin(x)).Inverse trigonometric functions:

Enter arguments within the following ranges:

0 < x < 1 for arcsin(x);

0 < x <= 1 for arccos(x); (*)

1E-50 < x < 1E50 for arctan(x) (**)

Results are given in degrees

(*) cos(x) and tan(x) functions are not accurate near the 90 degree boundary because cos(x) is computed as a function of sin(x) at that same region, in which the 10 digit accuracy of the calculator is not enough to make distinction between different arguments close to that limit. Results given by the sine function, on the other hand match to 9 decimal places those given by the HP-15C, over the range 5.729577E-47 =< x < 90. As a workaround, cos(x) can be calculated as sin(90-x), if cos(x) or tan(x) is needed to full accuracy.

(**) These are the valid ranges, not the ranges where accuracy is granted. Arguments below 1E-05 or above 1E04 for the arctan(x) are subject to rounding errors.

arcsin(x): [g][GTO]39 [R/S] arccos(x): [g][GTO]46 [R/S] arctan(x): [g][GTO]52 [R/S]

The constant stored in R0 (pi/180) may be used to convert angles from degrees to radians and vice-versa.

The key pattern below makes remembering the entry-points addresses much easier:

[9]

[4] [5] [6]

[2] [3]

vertical column at right, keys [3] and [9]: arcsin(x) (think of the sine-axis) middle horizontal row, keys [4] and [6]: arccos(x) (think of the cosine-axis) central remaining keys [5] and [2]: arctan(x)

Maximum relative error: about 4.2E-06 %Some results:(compared with exact results at this precision)0.0001 0.000001745 1.000000000 0.000001745 (0.000001745) (1.000000000) (0.000001745)sin cos tan1.0000 0.017452406 0.999847695 0.017455065 (0.017452406) (0.999847695) (0.017455065)

15.0000 0.258819045 0.965925826 0.267949192 (0.258819045) (0.965925826) (0.267949192)

30.0000 0.500000000 0.866025404 0.577350270 (0.500000000) (0.866025404) (0.577350269)

45.0000 0.707106781 0.707106781 1.000000000 (0.707106781) (0.707106781) (1.000000000)

60.0000 0.866025403 0.500000001 1.732050804 (0.866025404) (0.500000000) (1.732050808)

89.0000 0.999847695 0.017452415 57.28993250 (0.999847695) (0.017452406) (57.28996163)

89.9900 0.999999985 0.000175499 5698.028737 (0.999999985) (0.000174533) (5729.577893)

89.9999 1.000000000 0.000020000 49,999.9999 (1.000000000) (0.000001745) (572,957.7951)

0.0001 0.005729578 89.99427042 0.005729578 (0.005729578) (89.99427042) (0.005729578)asin acos atan0.1000 5.739170147 84.26082985 5.710593018 (5.739170477) (84.26082952) (5.710593137)

0.5000 29.99999978 60.00000022 26.56505044 (30.00000000) (60.00000000) (26.56505118)

0.9999 89.18970794 0.810292057 44.99713700 (89.18970856) (0.810291437) (44.99713507)

1.0000 Error 0 0.000000000 45.00000190 (90.00000000) (0.000000000) (45.00000000)

2.0000 - - 63.43494955 (63.43494882)

50,000 - - 89.99885408 (89.99885408)

100,000 - - 90.00000000 (89.99942704)

Running times:sin, cos: 3.1 seconds atan: 3.6 seconds asin, acos: 4.2 seconds-------------------------------------------------------------------------------

This program does not intend to be either useful or practical, as it depends on constants being stored in all the remaining registers (If the sigma+ key is pressed accidentally, all constants will have to be entered again, just to mention a minor possible trouble). It also uses two financial registers as temporary variables. This should be regarded as a study for later implementation of these functions on the HP-12C Platinum, for example, whose greater memory capacity will allow for some improvements, such as exceptions handling, increase of the accuracy of the arctangent function and built-in constants.

The following expressions have been used:

(i) sin(3x)=sin(x)*(3 - 4 sin^2(x))

(ii) sin(x)=x(1+x^2(a+x^2(b+c*x^2))), (x in radians)

with coefficients a, b and c optimized for the range [0..pi/6]

(as angles are supposed to be entered in degrees, the constants 1, a, b and c are multiplied by successive odd powers of pi/180),

(iii) arctan(x)=2*arctan((sqrt(1/x^2+1)-1/x)), 0 < x =< 1 and

arctan(x)=pi/2-2*arctan((sqrt(x^2+1)-x)), x > 1

(these are divided by the conversion factor pi/180 so that results are given in degrees)

(iv) arctan(x)=x(1+x^2(a+x^2(b+x^2(c+dx^2))))

with a fixed coefficient (d=1/12) and a, b and c optimized for the

range [0..sqrt(2)-1]

(v) sin(x)=sqrt(1-cos(x)^2))

(vi) tan(x)=sin(x)/cos(x)

(vii) arcsin(x)=arctan(sqrt(1/(1/(x^2)-1)))

(viii) arccos(x)=arctan(sqrt(1/(x^2)-1))

The expression (iii) was derived from:

(ix) arctan(2a/(1-a^2)=2*arctan(a)

The expressions (i) and (iii) have been used here in order to reduce the usual [0..pi/2] sine range and [0..1] arctangent range to [0..pi/6] and [0..sqrt(2)-1], respectively, so that accuracy over those ranges is achieved with less constants. Just for an idea of the importance of these range reductions, a 7th order minimax polynomial to approximate sin(x) over the range [0..pi/4] yields a maximum absolute error of 2.6e-9 (see reference 1 below). The 7th order polynomial approximation used in the program yields a maximum absolute error of about 9.5e-11. Rather than mathematically determining the minimax polynomial coefficients, they were obtained numerically, by means of curve fitting (polynomial model, 100 to 150 samples), enough for practical purposes. In case the registers are accidentaly cleared, reentering only the angle conversion factor (3.141592654/180) in register 0 will allow for very rough approximations that might be enough, however, for certain applications.

References:1) For some methods to approximate trigonometric functions, refer to the following paper, by Robin Green of Sony Computer Entertainment America:

Faster Math Functions

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

2) Expression (ix) was found at:

*Edited: 3 Aug 2005, 9:47 p.m. *