Arctangent function program [HP-42S, Free42]
|
09-17-2024, 04:23 AM
(This post was last modified: 09-17-2024 08:12 AM by Gerson W. Barbosa.)
Post: #21
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
(09-17-2024 01:56 AM)Thomas Klemm Wrote: Minor typo in: Thanks! At least the key codes are correct. Anyway, I’ve found out that more than 9 iterations are needed. Apparently no accuracy improvement beyond 11 or 12. Better accuracy should be expected on the 12C Platinum, but I don’t have 30 steps free in any of mine. Typo correction and patch: Code: 002 { 43 33 25 } g GTO 25 Edited to fix a typo |
|||
09-17-2024, 04:32 PM
Post: #22
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
(09-17-2024 04:23 AM)Gerson W. Barbosa Wrote: Anyway, I’ve found out that [for tan(x)] more than 9 iterations are needed. It depends on domain. see https://demonstrations.wolfram.com/Conti...ntFunction If tan argument limited to ±pi/4, 8 iterations already have 16+ digits accuracy. Instead of getting CF result bottom-up, with fixed iterations, we can also do this top-down. see https://www.ams.org/journals/mcom/1952-0...9650-3.pdf, Method III Note: there is a typo in formula, it should be 1+p(i) = 1 / (1 + r(i)*(1+p(i-1))) This is why I use variable p1 = 1+p instead. Code: function my_tan(x) lua> my_tan(pi/4) 1 0.2032910765367568 0.9886892399342051 2 0.011098440980763355 0.9997876809149685 3 0.00021018750072653323 0.999997868415695 4 2.1181106602376684e-06 0.9999999865263552 5 1.341497025916972e-08 0.9999999999413255 6 5.84877225566155e-11 0.9999999999998133 7 1.8641515888296674e-13 0.9999999999999997 8 4.534798657058108e-16 1.0000000000000002 9 8.698065146274166e-19 1.0000000000000002 1.0000000000000002 |
|||
09-17-2024, 10:14 PM
(This post was last modified: 09-18-2024 12:42 AM by Albert Chan.)
Post: #23
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
tan(x) continued fraction formula is good, but sin(x) taylor series is better. (*)
Sine x^(2k+1) / (2k+1)! term shrink very fast! sin(x) argument to within ±pi/4, 7 iterations get 16+ digits accuracy. Even if domain expanded to ±pi/2, only 3 more iterations are needed. Here, I convert sin taylor series to continued fraction, just to check previous post CF code. Euler's continued fraction formula proof is in code comment! We could do taylor term directly, t = t * -a , bypass continued fraction calculations. Code: function my_sin(x) lua> my_sin(pi/4) 1 -0.08074551218828074 0.7046526512091675 2 0.002490394570192714 0.7071430457793603 3 -3.657620418217711e-05 0.7071064695751781 4 3.1336168903781285e-07 0.7071067829368671 5 -1.7572476734434049e-09 0.7071067811796194 6 6.948453273886762e-12 0.7071067811865679 7 -2.0410263396642417e-14 0.7071067811865475 8 4.628704628834906e-17 0.7071067811865475 0.7071067811865475 lua> _ / sqrt(1 - _*_) -- = tan(pi/4) 0.9999999999999999 (*) Correction, tan vs sin convergence rate probably too close to call. But tan(x) has bigger range, getting sin/cos from it should be more accurate. Bonus, we can map all trig. functions with Tangent half-angle formula, without square root. |
|||
09-17-2024, 11:58 PM
(This post was last modified: 09-18-2024 12:00 AM by Thomas Klemm.)
Post: #24
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
(09-17-2024 04:32 PM)Albert Chan Wrote: Instead of getting CF result bottom-up, with fixed iterations, we can also do this top-down. Example We can use the program in Convergents of a Continued Fraction to calculate: \( \tan(0.1) = [0; 10, -30, 50, -70, 90, -110, …] \) 0 1 0 10 XEQ 00 0.1 -30 R/S 0.1003344481605351170568561872909699 50 R/S 0.1003346720214190093708165997322624 -70 R/S 0.1003346720854403773884482176487636 90 R/S 0.100334672085450544030807774009714 -110 R/S 0.100334672085450545058008197616473 0.1 TAN 0.1003346720854505450580800457811115 |
|||
09-19-2024, 04:25 AM
Post: #25
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
This program for HP-42S calculates \(\sin(x)\) and \(\cos(x)\) based on \(\tan(\frac{x}{2})\):
Code: 00 { 63-Byte Prgm } Hitting the [÷] button calculates \(\tan(x)\). Example 1 R/S y: 0.8414709848078965065362209309116153 x: 0.54030230586813971758203414358187 ÷ x: 1.557407724654902229769750316481308 |
|||
09-19-2024, 02:34 PM
Post: #26
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
(09-19-2024 04:25 AM)Thomas Klemm Wrote: Example With the patched HP-12C program above I get 1 ENTER 47 R/S ; RAD mode R/S -> HP-12C: Z: 1.557407724 Y: 0.5403023061 X: 0.8414709848 HP-12C Platinum: Z: 1.557407725 Y: 0.5403023059 X: 0.8414709848 I had made some tests using the tangent half-angle formula in the second half of the first quadrant. That would need six instead of eleven iterations, but I don’t think it’s worth the additional steps since current 12C calculators are extremely fast. The program loses accuracy near critical points like 90° and 180°, so it’s meant for everyday trigs only. Anyway, when maximum accuracy is needed this program should be used instead (HP-12C Platinum only). Because of page width change, my formatting has been completely ruined. That is the same program in the old article forum, except for the one-step reduction to make the ATAN entry-point more consistent with the ones for ACOS and ASIN. |
|||
09-20-2024, 01:43 AM
(This post was last modified: 09-20-2024 02:00 AM by Gerson W. Barbosa.)
Post: #27
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
Updated programs
HP-12C: Code:
Initialization: 360 ENTER 3.141592654 / STO 5 0 (DEG) or 2 (RAD) g GTO 52 R/S Usage: x R/S -> T: TAN(x) Z: TAN(x) Y: COS(x) X: SIN(x) First and second quadrants only. —— HP-15C listing for the JRPN 15C Simulator: Code:
|
|||
09-21-2024, 01:43 PM
Post: #28
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
For the sake of completeness I used only 3 variables \(p\), \(q\) and \(w\), similar to what I've done here.
Here is a Python program for a function that returns both \(\sin(x)\) and \(\cos(x)\): Code: def sc(x, n): This is the corresponding program for the HP-42S: Code: 00 { 64-Byte Prgm } ; x But it looks like we don't gain much with this approach. |
|||
09-21-2024, 05:31 PM
(This post was last modified: 09-21-2024 06:27 PM by Albert Chan.)
Post: #29
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
Slightly improved Thomas code (previous post)
Note: no changes done to tan(x/2) calculations sin(x) * tan(x/2) = 2*sin(x/2)*cos(x/2) * sin(x/2)/cos(x/2) = 2*sin(x/2)^2 = versine(x) = 1 - cos(x) Code: 00 { 63-Byte Prgm } ; x PI 6 / R/S ; x = pi/6 T = 0.267949192431 ; tan(x/2) Z = 0.133974596216 ; versine(x) Y = 0.5 ; sin(x) X = 0.866025403784 ; cos(x) Update: replaced "X↑2" by "Enter ×" (reason, see next post) |
|||
09-21-2024, 06:13 PM
Post: #30
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
(09-21-2024 05:31 PM)Albert Chan Wrote: Slightly improved Thomas code Nice. But please note that the reason for the following lines is that operations like X↑2 or R↑ are missing on the HP-12C: Code: 42 ENTER ; t t 2t Therefore I avoided them to make a translation easier. And with this of course goes all the arithmetic stack operations. What about DSE, you may ask. As often this is left as an exercise to the reader. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)