Easter Sunday Basic Trigs (HP-12C)



#31

HP-12C


01- 3 21- 1/x
02- / 22- +
03- ENTER 23- 2
04- ENTER 24- /
05- 4 25- -
06- y^x 26- ENTER
07- 12/ 27- ENTER
08- ENTER 28- *
09- ENTER 29- 4
10- * 30- *
11- 1 31- 3
12- 4 32- -
13- 0 33- *
14- / 34- ENTER
15- + 35- ENTER
16- 2 36- *
17- + 37- CHS
18- x<>y 38- 1
19- e^x 39- +
20- ENTER 40- SQRT
41- GTO 00
Usage:

Enter angle in radians ( 0 <= x <= pi )

R/S -> sin(x)

x<>y -> cos(x)

/ -> tan(x)

Examples:

x | sin(x) | cos(x) | tan(x) |
------+-------------+-------------+-------------+
3.14 | 0.001592545 |-0.999998732 |-0.001592547 |
3.02 | 0.121293236 |-0.992616719 |-0.122195439 |
2.50 | 0.598472141 |-0.801143618 |-0.747022290 |
2.00 | 0.909297427 |-0.416146836 |-2.185039869 |
1.65 | 0.996865028 |-0.079120890 |-12.59926467 |
1.58 | 0.999999647 |-0.009203545 |-108.6491891 |
1.57 | 0.999999683 | 0.000796324 | 1255.769712 |
1.50 | 0.997494987 | 0.070737197 | 14.10142097 |
1.00 | 0.841470986 | 0.540302303 | 1.557407735 |
0.50 | 0.479425548 | 0.877582557 | 0.546302504 |
0.05 | 0.049979231 | 0.998750257 | 0.050041770 |
0.00 | 0.000000000 | 1.000000000 | 0.000000000 |

x |sin(x*pi/180)|cos(x*pi/180)|tan(x*pi/180)|
------+-------------+-------------+-------------+
180.0 | 0.000000000 |-1.000000000 | 0.000000000 |
179.0 | 0.017452387 |-0.999847696 |-0.017455045 |
150.0 | 0.499999998 | 0.866025405 |-0.577350265 |
135.0 | 0.707106777 |-0.707106785 |-0.999999989 |
120.0 | 0.866025404 |-0.500000000 |-1.732050807 |
90.1 | 0.999998477 |-0.001745334 |-572.9552528 |
90.0 | 1.000000000 | 0.000000002 | 577350260.0*|
89.9 | 0.999998477 | 0.001745326 | 572.9581364 |
60.0 | 0.866025403 | 0.500000002 | 1.732050799 |
45.0 | 0.707106784 | 0.707106779 | 1.000000006 |
30.0 | 0.499999999 | 0.866025404 | 0.577350268 |
1.0 | 0.017452857 | 0.999847687 | 0.017455515 |

(*) tan(90°) = oo

pi = 3.141592654

Formulas:

cos(x) ~ x^4/12 + (x^4/12)^2/140 - cosh(x) + 2

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

cos(3*x) = cos(x)*(4*(cos(x))^2 - 3)

More details at Mardi Gras Basic Trigs (HP-12C).

HP 12c Platinum


001- 3 020- 1/x
002- / 021- +
003- ENTER 022- 2
004- ENTER 023- /
005- x^2 024- -
006- x^2 025- ENTER
007- 12/ 026- x^2
008- ENTER 027- 4
009- x^2 028- *
010- 1 029- 3
011- 4 030- -
012- 0 031- *
013- / 032- ENTER
014- + 033- x^2
015- 2 034- CHS
016- + 035- 1
017- x<>y 036- +
018- e^x 037- SQRT
019- ENTER 038- GTO 000

Examples:

x | sin(x) | cos(x) | tan(x) |
------+-------------+-------------+-------------+
3.14 | 0.001592628 |-0.999998732 |-0.001592630 |
3.02 | 0.121293239 |-0.992616719 |-0.122195442 |
2.50 | 0.598472143 |-0.801143617 |-0.747022294 |
2.00 | 0.909297427 |-0.416146837 |-2.185039863 |
1.65 | 0.996865028 |-0.079120889 |-12.59926479 |
1.58 | 0.999999646 |-0.009203543 |-108.6492026 |
1.57 | 0.999999683 | 0.000796327 | 1255.765641 |
1.50 | 0.997494987 | 0.070737202 | 14.10141994 |
1.00 | 0.841470985 | 0.540302306 | 1.557407725 |
0.50 | 0.479425539 | 0.877582562 | 0.546302490 |
0.05 | 0.049979169 | 0.998750260 | 0.050041708 |
0.00 | 0.000000000 | 1.000000000 | 0.000000000 |

x |sin(x*pi/180)|cos(x*pi/180)|tan(x*pi/180)|
------+-------------+-------------+-------------+
180.0 | 0.000000000 |-1.000000000 | 0.000000000 |
179.0 | 0.017452383 |-0.999847696 |-0.017455041 |
150.0 | 0.499999997 | 0.866025406 |-0.577350264 |
135.0 | 0.707106780 |-0.707106785 |-1.000000003 |
120.0 | 0.866025404 |-0.500000000 |-1.732050806 |
90.1 | 0.999998477 |-0.001745329 |-572.9571280 |
90.0 | 1.000000000 |-2.684678-10 |-3724840447* |
89.9 | 0.999998477 | 0.001745328 | 572.9572886 |
60.0 | 0.866025404 | 0.500000000 | 1.732050808 |

(*) tan(90°) = oo

Edited by mistake. No changes.


Edited: 1 Apr 2013, 2:48 p.m. after one or more responses were posted


#32

Gerson,

Thank you soooo much for the listing and its algorithm. This has come at a time when I am also studying the algorithms than can be used to calculate the sine function. So I have added your method to the list. The accuracy results of your method are very impressive. So far your algorithm is at the top of the hit parade, so to speak! Using the trig identities for cos(3x) and sin(2x) I can use your method to cover a wide range of angles WHILE STILL maintaining very low errors!

Namir


Edited: 31 Mar 2013, 8:54 a.m.

#33

It seems the presence of the cosh(x) function in your formula for calculating cos(x) is unusual and yet probably renders the formula stable enough to yield good results. My approach has been to find and use polynomials (either regular ones or rational ones like with the Pade approximants) and avoid as much as possible relying on functions like exp(x), ln(x), and so on.

The Pade approximants yield rational polynomials (of a while variety of orders to pick and choose from) that generally offer stable results, especially when the approximated function has a Taylor series with consecutive terms that alternate in signs causing the series to converge very slowly. Such slow convergence also requires a large number of terms to achieve small errors over a significant range of x values.

Edited: 31 Mar 2013, 10:19 a.m.


#34

Hello Namir,

I am glad you have liked this approximation. I find it particularly suitable for the HP-12C, since it has e^x and divide by 12 is available as a single instruction. Even with only the term x^4/12 it is good enough for practical purposes in the range [-pi/4..pi/4]:

plot cos(x) - (x^4/12 - cosh(x) + 2), x=-pi/4..pi/4 (WolframAlpha)

Adding a second term significantly improves the approximation:

plot cos(x) - (x^4/12 + (x^4/12)^2/140 - cosh(x) + 2), x=-pi/4..pi/4 (WolframAlpha)

One additional term would perhaps allow for the evaluation of the function in the range [-pi/2..pi/2] to 10 places without range reduction.

Best regards,

Gerson.

P.S.: plot cos(x) - (x^4/12 + (x^4/12)^2/140 + (x^4/12)^3/138600 - cosh(x) + 2), x=-pi/2..pi/2




Edited: 31 Mar 2013, 1:40 p.m.


#35

Adding the cubic term does improve accuracy a bit, and I welcome that!!!

Thanks for adding the plots!!!!

I have also tested other approximations for sin(x) that I found on the web. You methods puts them all to shame!!

Namir

Edited: 31 Mar 2013, 1:44 p.m.


#36

Thanks, but please keep in mind that this only works when we have ex readily available, otherwise the computation of ex alone would not compensate at all.

Gerson.


#37

I am thinking about expanding cosh(x) into a Taylor series polynomial (or even better yet a Pade approximant rattional polynomials) so that the whole evaluation of cos(x) (and then sin(x)) relies on basic operations only.

Namir

#38

Using the following Pade approximation for cosh(x) works well:

cosh(x) = (39251520 + 18471600 x^2 + 1075032 x^4 + 14615 x^6)/(39251520 - 
1154160 x^2 + 16632 x^4 - 127 x^6)

The calculated cos and sin have a very good accuracy from 0 to 89 degrees. At 90 degrees this (and many other) approximations suffer!

Namir


Edited: 1 Apr 2013, 1:53 p.m.

#39

Quote:
especially when the approximated function has a Taylor series with consecutive terms that alternate in signs causing the series to converge very slowly.

Maybe I do not understand you correctly, but why should an alternating series converge slowly? On the contrary: in alternating series, the Euler transformation can be used for convergence acceleration.

Kind regards

Thomas

Edited: 1 Apr 2013, 9:15 a.m. after one or more responses were posted


#40

Maybe you are correct. I will check it.

Namir

#41

May I suggest the following change:

16-  x<>y
17- e^x
18- ENTER
29- 1/x
20- +
21- 2
22- /
23- 2 ; or LSTx
24- -
25- -

The rationale behind this change is that 2 - cosh(x) < 1 for x # 0, thus we gain one digit. For small values of x this makes a difference:

   x  |    sin(x)   |    cos(x)   |    tan(x)   |
------+-------------+-------------+-------------+
0.05 | 0.049979164 | 0.998750261 | 0.050041703 |

Kind regards

Thomas

Edited: 31 Mar 2013, 4:00 p.m.


#42

Thanks for the improvement!

I've just tried the following on the fast HP-12C+:

01 STO 0
02 0
03 STO 1
04 STO 2
05 RCL 0
06 RCL 1
07 y^x
08 LASTx
09 n!
10 /
11 STO+ 2
12 4
13 STO+ 1
14 2
15 0
16 RCL 1
17 x<=y?
18 GTO 05
19 RCL 2
20 2
21 *
22 RCL 0
23 e^x
24 ENTER
25 1/x
26 +
27 2
28 /
29 -
30 ENTER
31 ENTER
32 *
33 CHS
34 1
35 +
36 SQRT
37 GTO 00

For your example, I get

x | sin(x) | cos(x) | tan(x) |
------+-------------+-------------+-------------+
0.05 | 0.049979164 | 0.998750261 | 0.050041736 |

With 20 in lines 14 and 15 (6 terms of the Taylor expansion) the valid range for cos(x) appears to go from -pi to pi without range reduction (this have to be checked). The hp 12 Platinum might give more accurate results, but I don't know if it will be fast enough. There are many rounding errors involved, but perhaps your method should apply here. I have to leave now, so I'll see this later.

Cheers,

Gerson.

P.S.: There was a typo in line 19 (RCL 1 instead of RCL 2);

It takes about four seconds on the hp 12 Platinum:

   x  |    sin(x)   |    cos(x)   |    tan(x)   |
------+-------------+-------------+-------------+
0.05 |0.04997916917| 0.9987502604|0.05004170837|

Formula:


Edited: 31 Mar 2013, 7:36 p.m.


#43

Ah! I was going to ask you for the equation that generates the polynomial part.

Namir


#44

This is better explained in this thread, message #5:

http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/archv020.cgi?read=179837

For testing purpose, the following should give at least 10 correct digits on the HP-50g, in the range [-pi..pi].

%%HP: T(3)A(R)F(.);
\<< \-> x
\<< '2*\GS(k=0,5,x^(4*k)/(4*k)!)-COSH(x)' EVAL
\>>
\>>

Gerson.

#45

Thomas,

I'll accept your suggestion. Listings and examples below. The results comparisons have been made against the HP-15C.

Cheers,

Gerson.


HP-12C


01- 3 21- 2
02- / 22- /
03- ENTER 23- LSTx
04- ENTER 24- -
05- 4 25- -
06- y^x 26- ENTER
07- 12/ 27- ENTER
08- ENTER 28- *
09- ENTER 29- 4
10- * 30- *
11- 1 31- 3
12- 4 32- -
13- 0 33- *
14- / 34- ENTER
15- + 35- ENTER
16- x<>y 36- *
17- e^x 37- CHS
18- ENTER 38- 1
19- 1/x 39- +
20- + 40- SQRT
41- GTO 00
Usage:

Enter angle in radians ( 0 <= x <= pi )

R/S -> sin(x)

x<>y -> cos(x)

/ -> tan(x)

Examples:

x | sin(x) | cos(x) | tan(x) |
------+------------------+------------------+-------------------+
3.14 | 0.001592545(653) |-0.999998732 |-0.001592547(655) |
3.02 | 0.121293238(55) |-0.992616719(7) |-0.122195441(59) |
2.50 | 0.598472142(4) |-0.801143617(6) |-0.747022293(7) |
2.00 | 0.909297427 |-0.416146836(7) |-2.185039866(3) |
1.65 | 0.996865029 |-0.079120888(9) |-12.59926494(79) |
1.58 | 0.999999647 |-0.009203547(3) |-108.6491585(2036) |
1.57 | 0.999999683 | 0.000796325(7) | 1255.768346(5591) |
1.50 | 0.997494987 | 0.070737197(202) | 14.10142097(1995) |
1.00 | 0.841470986(5) | 0.540302304(6) | 1.557407731(25) |
0.50 | 0.479425542(39) | 0.877582560(2) | 0.546302495(0) |
0.05 | 0.049979164(9) | 0.998750261(0) | 0.050041703(8) |
0.00 | 0.000000000 | 1.000000000 | 0.000000000 |

x | sin(x*pi/180) | cos(x*pi/180) | tan(x*pi/180) |
------+------------------+------------------+-------------------+
180.0 | 0.000002000(0000)|-1.000000000 | 0.000002000(0000) |
179.0 | 0.017452392(406) |-0.999847695 |-0.017455051(65) |
150.0 | 0.499999997(50..)|-0.866025406(4) |-0.577350265(9) |
135.0 | 0.707106778(81) |-0.707106784(1) |-0.999999989(100..)|
120.0 | 0.866025404 |-0.500000000 |-1.732050810(08) |
90.1 | 0.999998477 |-0.001745333(28) |-572.9558211(72134)|
90.0 | 1.000000000 | 0.000000002(0) | 1154700538 ( +oo )|
89.9 | 0.999998477 | 0.001745327(8) | 572.9575675(2134) |
60.0 | 0.866025405(4) | 0.499999999(50..)| 1.732050814(08) |
45.0 | 0.707106781 | 0.707106782(1) | 0.999999999(10..) |
30.0 | 0.500000007(0) | 0.866025400(4) | 0.577350280(69) |
1.0 | 0.017452793(406) | 0.999847688(95) | 0.017455452(065) |

pi = 3.141592654

HP 12c Platinum


001- 3 020- 2
002- / 021- /
003- ENTER 022- LSTx
004- ENTER 023- -
005- x^2 024- -
006- x^2 025- ENTER
007- 12/ 026- x^2
008- ENTER 027- 4
009- x^2 028- *
010- 1 029- 3
011- 4 030- -
012- 0 031- *
013- / 032- ENTER
014- + 033- x^2
015- x<>y 034- CHS
016- e^x 035- 1
017- ENTER 036- +
018- 1/X 037- SQRT
019- + 038- GTO 000

Examples:

x | sin(x) | cos(x) | tan(x) |
------+------------------+------------------+-------------------+
3.14 | 0.001592628(53) |-0.999998732 |-0.001592630(55) |
3.02 | 0.121293239(55) |-0.992616719(7) |-0.122195442(59) |
2.50 | 0.598472143(4) |-0.801143617(6) |-0.747022294(7) |
2.00 | 0.909297427 |-0.416146837 |-2.185039862(3) |
1.65 | 0.996865028(9) |-0.079120889 |-12.59926479 |
1.58 | 0.999999646(7) |-0.009203543 |-108.6492029(36) |
1.57 | 0.999999683 | 0.000796327 | 1255.765628(591) |
1.50 | 0.997494987 | 0.070737202 | 14.10141994(5) |
1.00 | 0.841470985 | 0.540302306 | 1.557407725 |
0.50 | 0.479425539 | 0.877582562 | 0.546302490 |
0.05 | 0.049979169 | 0.998750260 | 0.050041708 |
0.00 | 0.000000000 | 1.000000000 | 0.000000000 |

x | sin(x*pi/180) | cos(x*pi/180) | tan(x*pi/180) |
------+------------------+------------------+-------------------+
180.0 | 0.000000000 |-1.000000000 | 0.000000000 |
179.0 | 0.017452382(406) |-0.999847696(6) |-0.017455041(65) |
150.0 | 0.499999997(50..)|-0.866025406(4) |-0.577350264(9) |
135.0 | 0.707106780(1) |-0.707106782(1) |-0.999999997(10..) |
120.0 | 0.866025404 |-0.500000000 |-1.732050805(8) |
90.1 | 0.999998477 |-0.001745329(8) |-572.9571394(2134) |
90.0 | 1.000000000 |-0.00000000026(+0)|-3849001795 ( +oo )|
89.9 | 0.999998477 | 0.001745328 | 572.9572858(134) |
60.0 | 0.866025404 | 0.500000000 | 1.732050808 |
45.0 | 0.707106781 | 0.707106781 | 1.000000000 |
30.0 | 0.500000000 | 0.866025404 | 0.577350269 |
1.0 | 0.017452408(6) | 0.999847695 | 0.017455067(5) |

pi = 3.141592654

#46

Thank you for this nice program. It also works on the HP-38E and HP-38C which happens to be my favorite calculator, and which was the predeccessor to the HP-12C.


#47

Which do you have, the HP-38E or HP-38C? There is another trigs program on the software library, using another approach (minimax polynomial approximation). It's very inconvenient on the HP-38E however (99 steps and 7 constants in the registers).

http://www.hpmuseum.org/software/38ctrig.htm

Regards,

Gerson.


#48

Gerson, I had the C, for 20 great years of heavy use. Died about 10 years ago as has my HP41CV. Fortunately I have a 38C emulator (and many others) that works perfectly.

Thanks,
Bob

#49

Gerson,

How did the cosh(x) term end up in the approximation for cos(x)????

A very curious Namir


#50

Just sum the respective Taylor series together and then isolate cos x:

Regards,

Gerson.

Edited to fix a typo and to correct a mistake in the fourth paragraph, per Thomas Klemm's observation below


Edited: 4 Apr 2013, 1:29 p.m. after one or more responses were posted


#51

Very elegant and very simple!!!

:-)

Namir


#52

That's the way I like my math!

#53

I fixed a missing multiplication by 2 in the fourth line soon after posting, but forgot to do the same on the third line. Sorry for the mistake!

It's been corrected now!

Edited: 4 Apr 2013, 1:42 p.m.

#54

One can use a similar approach for sin(x):

sin(x) = x (2 + y/60 + y^2/181440 + y^3/3113510400 + 
y^4/177843714048000 + y^5/25545471085854720000 +
y^6/7755605021665492992000000 +
y^7/4420880996869850977271808000000) - sinh(x)

Where y = x^4

Namir

Edited: 1 Apr 2013, 4:21 p.m.


#55

Which of course works nicely :-)

#56

Quote:


The factor 2 is missing:

But of course we still get the idea.

Kind regards

Thomas


#57

The last line was right, nonetheless. The previous line has now been corrected, thanks!

Cheers,

Gerson.

#58

The following gives about 10 to 11 significant digits in the range [0..+/-pi], in case anyone wants to try. They should work in other HP-17B models as well.

SIN=2*SIGMA(K:1:21:4:X^K/FACT(K))-(EXP(X)-EXP(-X))/2

COS=2*SIGMA(K:0:20:4:X^K/FACT(K))-(EXP(X)+EXP(-X))/2

TAN=(4*SIGMA(K:1:21:4:X^K/FACT(K))-EXP(X)+EXP(-X))/(4*SIGMA(K:0:20:4:X^K/FACT(K))-EXP(X)-EXP(-X))

X in radians.

#59

Gerson,

Simple, elegant, and accurate (to at least 6 digits). Beautiful algorithm!

Eddie


#60

Thanks, Eddie!

Quote:
accurate (to at least 6 digits)

The next term in the Taylor series, 2*(x)^16/16!, would contribute with only 2e-13, at most (in the extremes of the [-pi..pi] range, when x = pi/3). The lost of accuracy is greater for the sine function (and tangent), because it is computed as sqrt(1 - cos2x). The lack of at least two guard digits is also responsible for many accumulated errors.

Anyway, 6 digits are quite an improvement after almost seven centuries :-)

"In 1342, Levi (ben Gerson) wrote On Sines, Chords and Arcs, which examined trigonometry, in particular proving the sine law for plane triangles and giving five-figure sine tables."

(From Wikipedia)

Cheers,

Gerson.


Possibly Related Threads...
Thread Author Replies Views Last Post
  HP PRIME - Need help for basic program svp. dg1969 4 238 10-08-2013, 04:06 PM
Last Post: dg1969
  Sunday distraction Keith Midson 0 86 08-04-2013, 12:28 AM
Last Post: Keith Midson
  HP 10s+ Easter Egg (Statistics) Eddie W. Shore 1 137 04-11-2013, 04:25 PM
Last Post: Andrew Feist
  "The Basic HP-71" Jeff Davis 3 242 08-30-2012, 12:20 AM
Last Post: Howard Owen
  A Sunday Programming Challenge Allen 61 1,048 03-17-2012, 06:48 PM
Last Post: Allen
  HP's Tired Basic Calculator Line Jeff 4 199 02-10-2012, 01:33 PM
Last Post: Dirk Mehldau
  After the 15c LE, better chance of a basic RPN calc from HP now? nick lidakis 80 1,365 09-18-2011, 08:15 AM
Last Post: snaggs
  HP 12C, 12C Platinum & 15C iOS App Walter Lam 2 185 06-02-2011, 01:25 PM
Last Post: Andrés C. Rodríguez (Argentina)
  BASIC BEEP Howard Owen 5 170 05-12-2011, 05:03 PM
Last Post: Garth Wilson
  Better Late Than Never--VAL() in HP-71B BASIC Namir 25 780 05-08-2011, 11:50 AM
Last Post: Howard Owen

Forum Jump: