HP Forums
(12c Platinum) Fast & Accurate Trigonometric Functions - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (12c Platinum) Fast & Accurate Trigonometric Functions (/thread-17806.html)



(12c Platinum) Fast & Accurate Trigonometric Functions - Gerson W. Barbosa - 12-10-2021 05:12 PM

RPN Program:

001-    2               023-    CHS             045-  g x≤y             067-    /               089-  g GTO 265
002-    STO 6           024-  g x≤y             046-  g GTO 017         068-    +               090-    2         
003-    R↓              025-  g GTO 031         047-    R↓              069-    RCL 8           091-    STO 6
004-    RCL .0          026-    2               048-    xy             070-    ×               092-    R↓
005-    ÷               027-    ×               049-    STO 8           071-    ENTER           093-    RCL .0
006-    xy             028-    xy             050-  g x²              072-  g x²              094-    ÷
007-    STO 4           029-    -               051-    ENTER           073-    4               095-    CHS
008-    xy             030-    ENTER           052-    ENTER           074-    ×               096-    9
009-    ENTER           031-    R↓              053-    ENTER           075-    CHS             097-    0
010-  g x²              032-    ENTER           054-    RCL .2          076-    3               098-    +
011-  g √ᵡ              033-  g x²              055-    ×               077-    +               099-  g GTO 006
012-    3               034-  g √ᵡ              056-    RCL .3          078-    ×               100-    RCL .0
013-    6               035-    ENTER           057-    -               079-    RCL 6           101-    ÷
014-    0               036-    ENTER           058-    ×               080-    2               102-    STO 4
015-  g x≤y             037-    9               059-    RCL .4          081-  g x≤y             103-    x
016-  g GTO 120         038-    0               060-    +               082-  g GTO 087         104-    STO 7
017-    R↓              039-    -               061-    ×               083-    R↓              105-    1
018-    R↓              040-  g x=0             062-    RCL .5          084-  g x=0             106-    STO 6
019-    9               041-  g GTO 047         063-    -               085-  g GTO 114         107-    RCL 4
020-    0               042-    R↓              064-    ×               086-  g GTO 109         108-  g GTO 009
021-  g x≤y             043-    9               065-    RCL .1          087-    R↓              109-    CLx
022-  g GTO 026         044-    0               066-    3               088-    xy             110-    STO 6


111-    xy             152-    0               193-  g GTO 196         234-    +               275-  g GTO 000
112-    RCL 4           153-    ×               194-    1/x             235-    ×
113-  g GTO 095         154-    RCL .0          195-    9               236-    RCL .8
114-    xy             155-    ×               196-    0               237-    -
115-    RCL 4           156-  g GTO 265         197-    RCL .1          238-    ×               optional steps:
116-    xy             157-    xy             198-    ×               239-    RCL .9
117-    ÷               158-    STO 4           199-    STO 6           240-    +               276-    1.745329252
118-    RCL 7           159-    xy             200-    xy             241-    ×               287-    EEX
119-  g GTO 266         160-    2               201-    2               242-    3               288-    2
120-    xy             161-    STO 7           202-    ENTER           243-    1/x             289-    CHS
121-    EEX             162-    R↓              203-    3               244-    -               290-    STO .1
122-    1               163-    1               204-  g √ᵡ              245-    ×               291-    2.1
123-    2               164-    xy             205-    STO 9           246-    1               294-    EEX
124-  g x≤y             165-    +               206-    -               247-    +               295-    26
125-  g GTO 146         166-  g x=0             207-    xy             248-    RCL 9           297-    CHS
126-    R↓              167-  g GTO 268         208-  g x≤y             249-    ×               298-    STO .2
127-    xy             168-    1               209-  g GTO 221         250-    RCL 8           299-    4.47569
128-    ÷               169-  g LSTx            210-    ENTER           251-    +               306-    EEX
129-  g LSTx            170-    -               211-    ENTER           252-    RCL 6           307-    20
130-    xy             171-    1               212-    RCL 9           253-  g x=0             309-    CHS
131-  g INTG            172-  g LSTx            213-    ×               254-    xy             310-    STO .3
132-    ×               173-    +               214-    1               255-    xy             311-    5.55391606
133-  g x≤y             174-    ÷               215-    -               256-    -               321-    EEX
134-    CHS             175-  g √ᵡ              216-    xy             257-    RCL 5           322-    14
135-    +               176-  g GTO 183         217-    RCL 9           258-    ×               324-    CHS
136-  g GTO 019         177-    xy             218-    +               259-    RCL 7           325-    STO .4
137-    xy             178-    STO 4           219-    ÷               260-    ×               326-    3.281837614
138-    STO 4           179-    xy             220-    3               261-    RCL .1          337-    EEX
139-    xy             180-    1               221-    0               262-    ÷               338-    8
140-    ENTER           181-    STO 7           222-    RCL .1          263-    RCL .0          339-    CHS
141-  g x²              182-    xy             223-    ×               264-    ×               340-    STO .5
142-    1               183-    ENTER           224-    STO 8           265-    RCL 4           341-    .0784
143-    -               184-  g x²              225-    xy             266-    xy             346-    CHS
144-  g x=0             185-  g √ᵡ              226-    STO 9           267-  g GTO 000         347-    STO .6
145-  g GTO 150         186-  g x=0             227-  g x²              268-    RCL 7           348-    .110351
146-    CHS             187-  g GTO 265         228-    ENTER           269-  g GTO 151         355-    STO .7
147-  g √ᵡ              188-    ÷               229-    ENTER           270-    1               356-    .142838
148-    ÷               189-    STO 5           230-    ENTER           271-  g GTO 273         363-    STO .8
149-  g GTO 180         190-    1               231-    RCL .6          272-    RCL .1          364-    .199999839
150-    xy             191-  g LSTx            232-    ×               273-    STO .0          374-    STO .9
151-    9               192-  g x≤y             233-    RCL .7          274-    R↓              375-  g GTO 270



Usage:

        g GTO 276 R/S            if the optional steps have been keyed in this will load all the
                                 constants needed by the program into registers R.0 through R.9,
                                 otherwise store them manually:

        R.0:  1                  (DEG)

        R.1:  1.745329252E-02    (pi/180)

        R.2:          2.1E-26    
        R.3:      4.47569E-20   
        R.4:   5.55391606E-14    
        R.5:  3.281837614E-08    (sine polynomial coefficients)

        R.6:          -0.0784   
        R.7:         0.110351    
        R.8:         0.142838    
        R.9:      0.199999839    (arctangent polynomial coefficients)


  SIN:  R/S
  COS:  g GTO 090 R/S     
  TAN:  g GTO 100 R/S            
 ASIN:  g GTO 137 R/S      
 ACOS:  g GTO 157 R/S        
 ATAN:  g GTO 177 R/S 

 DEG:   g GTO 270 R/S
 RAD:   g GTO 272 R/S
GRAD:   .9 1/x STO .0


Input ranges:

 SIN, COS, TAN: |x| <  1E12 (degrees)

    ASIN, ACOS: |x| ≤ 1

          ATAN: -1E-50 < x < 1E50  



Examples:


First set the display to FIX 9 and DEG mode,  if not already set:

f 9
g GTO 270 R/S



1) asin(acos(atan(tan(cos(sin(9))))))  

Keystrokes                Display

9                       9.
R/S                     0.156434465
g GTO 090 R/S           0.999996273
g GTO 100 R/S           0.017455000
g GTO 177 R/S           0.999996273
g GTO 157 R/S           0.156434568
g GTO 137 R/S           9.000005947

This is a standard test created by Mike Sebastian to check trigonometric algorithms in calculators (Calculator Forensics).

2) sin²(30) + cos²(30):

Notice that the stack register X is always preserved, so there's no need to store intermediate results in simple
chain calculations (one-level only), as in this example.  


Keystrokes                Display

30 R/S                  0.500000000
g x²                    0.250000000
30 g GTO 090 R/S        0.866025404
g x²                    0.750000000
+                       1.000000000



3) sin(-1234567.8) + acos(-0.77)

Keystrokes                Display

1234567.8 CHS          -1,234,567.8
R/S                    -0.790155012
.77 CHS g GTO 157 R/S   140.3538889  
+                       139.5637338


4) tan(-6.2 rad)/asin(0.15 rad)

Set the angular mode to RADIANS:

g GTO 272 R/S

Keystrokes                Display

6.2 CHS g GTO 100 R/S   0.083377715
0.15 g GTO 137 R/S      0.150568273
÷                       0.553753545

Go back to DEG mode:

g GTO 270 R/S           0.553753545     


Notice that the first example results do not quite match the ones you would get on the classic scientific Voyagers. As a
matter of fact, the final result is even closer to 9: 9.00000594679 (which is rounded to 10 digits to fit in the display).
Unlike the classic Voyagers, the full 12-digit mantissa is not rounded to the number of digits in the display. Since the
HP 12c Platinum has abandoned the old HP rounding philosophy, it does not make much sense to implement this feature,
but we can use the remaining programming space to replicate the original HP rounding method, just for testing purposes:

1) Change the line 267 to g GTO 376:

g GTO 266
f P/R
g GTO 376
f P/R


2) Key in these lines:

376-    ENTER
377-  g x² 
378-  g √ᵡ  
379-  g x=0
380-  g GTO 000
381-  g LN
382-    1
383-    0
384-  g LN
385-    ÷
386-  g INTG
387-    1
388-    -
389-    1
390-    0
391-    x
392-    yᵡ 
393-    ÷
394-  g LSTx
395-    x
396-  f RND
397-    x
398-    ×
399-  g GTO 000


This will round the answers to the number of the decimal places of the display plus one. 

Let us repeat the first example:

1) asin(acos(atan(tan(cos(sin(9)))))), assuming FIX 9 and DEG mode 

Keystrokes                Display

9                       9.
R/S                     0.156434465
g GTO 090 R/S           0.999996273
g GTO 100 R/S           0.017455000
g GTO 177 R/S           0.999996272
g GTO 157 R/S           0.156441660
g GTO 137 R/S           9.000417403

Now all 10-digit results exactly match those of the HP-15C and other HP 10-digit calculators.

Don't forget to revert to the original program by editing the line 267:

g GTO 266
f P/R
g GTO 000
f P/R


Formulae:

\(\sin x =r\left ( 3-4r^{2} \right )\)

where

\(r = x\left (a_{1}+x^{2}\left (a_{2}+x^{2}\left (a_{3}+x^{2}\left (a_{4}+a_{5}x^{2}\right )\right )\right) \right )\)

and where

a₁ = 5.81776417333E-03
a₂ =-3.281837614E-08
a₃ = 5.55391606E-14
a₄ =-4.47569E-20
a₅ = 2.1E-26


x in degrees; |x| ≤ 90 degrees; Max Abs Error < 1.46E-12


\(\arctan x =x\left ( b_{1} + x^{2}\left ( b_{2}+x^{2}\left (b_{3}+x^{2}\left ( b_{4}+x^{2}\left ( b_{5}+b_{6} x^{2}\right ) \right )\right ) \right )\right )\)

where

b₁ = 1
b₂ =-0.333333333333
b₃ = 0.199999839
b₄ =-0.142838
b₅ = 0.110351 
b₆ =-0.0784


|x| ≤ \(2-\sqrt{3}\); Max Abs Error < 7.85E-12; results in radians


\(\arctan x =\frac{x}{|x|}\left ( \frac{\pi }{6}+\arctan \frac{|x|\sqrt{3}-1}{|x|+\sqrt{3}} \right )\)

\(2-\sqrt{3}\) < |x| ≤ 1


\(\arctan x =\frac{x}{|x|}\left ( \frac{\pi }{2}-\arctan \frac{1}{|x|} \right )\)

|x| > 1


\(\cos x =\sin \left ( \frac{\pi }{2} - x \right )\)


\(\tan x=\frac{\sin x}{\cos x}\)


\(\arcsin x = |x|\arctan \frac{x}{\sqrt{1-x^{2}}}\)

|x| < 1


\(\arccos x =2\arctan \sqrt{\frac{1-x}{1+x}}\)

-1 < x ≤ 1