Arctangent function program [HP-42S, Free42]
|
09-14-2024, 10:42 AM
(This post was last modified: 09-14-2024 01:31 PM by Gerson W. Barbosa.)
Post: #1
|
|||
|
|||
Arctangent function program [HP-42S, Free42]
As I was evaluating ATAN(1/300) on Free42, I noticed the answer was approximately
1/(300 + 1/(900 + 1/(375 + 3/2800))) = 0.00333332098773662486119811248858... This is equivalent to 1/(300 + 1/(900 + 4/(1500 + 9/2100))) = 0.00333332098773662486119811248858... which suggests that atan(1/x) = 1/(x + 1^2/(3x + 2^2/(5x + 3^2/(7x + ... )))) I thought of writing a program for the HP-12C but I tried it on the HP-42S first, because it’s easier. I might write a 12C version later, but I don’t think it would be short and fast enough. On the HP-42S replace 43 with 14, for arguments in the range ]0..1]. Code:
Example: 1 XEQ “ATN” 4 × -> 3.141592653589793238462643383279503 |
|||
09-14-2024, 12:59 PM
Post: #2
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
What does the "PM" mean in line 09 of the program?
<0|ɸ|0> -Joe- |
|||
09-14-2024, 01:39 PM
Post: #3
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42] | |||
09-14-2024, 01:43 PM
Post: #4
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
(09-14-2024 10:42 AM)Gerson W. Barbosa Wrote: which suggests that From Inverse Tangent: Quote:The inverse tangent has continued fraction representations |
|||
09-14-2024, 02:42 PM
Post: #5
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
(09-14-2024 01:43 PM)Thomas Klemm Wrote:(09-14-2024 10:42 AM)Gerson W. Barbosa Wrote: which suggests that Yes, Thomas. That’s one of the many continued fraction representations for the arctangent(x) function l found when googling for them yesterday. What I have used is a continued fraction for arctangent(1/x). It appeared to me somewhat simpler to program, but I may be wrong. I would have to write programs for a few of those and see how they compare. Cheers, Gerson. |
|||
09-14-2024, 03:00 PM
Post: #6
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
Both formulas are essentially the same: just substitute \(x\) with \(\frac{1}{x}\).
My impression was that you came up with this representation by yourself. So I just wanted to confirm that indeed it is correct. |
|||
09-14-2024, 04:33 PM
Post: #7
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
(09-14-2024 10:42 AM)Gerson W. Barbosa Wrote: atan(1/x) = 1/(x + 1^2/(3x + 2^2/(5x + 3^2/(7x + ... )))) Formula work well if user input = 1/x, is small (within ±1) (05-31-2021 09:51 PM)Albert Chan Wrote: \(\displaystyle\arctan(x) = 2\arctan\left( {x \over \sqrt{1+x^2}+1} \right)\) RHS atan argument is always within ±1 plot(x/(1+sqrt(1+x^2)), x = -10 .. 10) With atan "half-angle" formula, we can extend "ATN" code for all ranges. Code: 00 { 59-Byte Prgm } 1E99 XEQ "ATN" 2 × → 3.141592653589793238462643383279502 |
|||
09-14-2024, 04:41 PM
Post: #8
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
(09-14-2024 03:00 PM)Thomas Klemm Wrote: Both formulas are essentially the same: just substitute \(x\) with \(\frac{1}{x}\). You are right, Thomas. Thank you! A question still remains: would the program take up less steps if implemented according to the original formula? The initial 1/X instruction wouldn’t be necessary anymore, of course, but would that make the program shorter? A more challenging problem – at least for me – would be making the program HP-41 compatible. Recall arithmetic is quite handy! |
|||
09-14-2024, 05:11 PM
Post: #9
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
(09-14-2024 04:41 PM)Gerson W. Barbosa Wrote: A question still remains: would the program take up less steps if implemented according to the original formula? This is what I came up with: Code: 00 { 39-Byte Prgm } |
|||
09-14-2024, 05:37 PM
(This post was last modified: 09-14-2024 05:51 PM by Thomas Klemm.)
Post: #10
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
(09-14-2024 04:41 PM)Gerson W. Barbosa Wrote: A more challenging problem – at least for me – would be making the program HP-41 compatible. Recall arithmetic is quite handy! Without recall arithmetic we'd probably have to use storage arithmetic. This leads to a lot of stack movements. Therefore I suggest to use a register for the loop counter. This gives us enough wiggle room to use more or less the same program: Code: 01▸LBL "ATN" Instead of register 00 we could use register M. |
|||
09-14-2024, 09:29 PM
(This post was last modified: 09-14-2024 09:47 PM by Gerson W. Barbosa.)
Post: #11
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
(09-14-2024 05:37 PM)Thomas Klemm Wrote: Very nice! Here’s my port to the HP-12C: Code:
1 g GTO 00 R/S 4 × -> 3.141592653 ( ~ 17 seconds, almost instantly on the 12C+ ) (Edit to include a few missing prefixes in the listing) |
|||
09-14-2024, 10:37 PM
Post: #12
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
Here's the program for the HP-12C:
Code: 01 { 01 } 1 |
|||
09-14-2024, 10:55 PM
Post: #13
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
And here for the HP-25C:
Code: 01: 31 : ENTER |
|||
09-14-2024, 11:03 PM
Post: #14
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
(09-14-2024 10:37 PM)Thomas Klemm Wrote: Here's the program for the HP-12C: Significantly shorter, but again about 17 seconds on my older 12C. Do we really need 15 iterations? 13 appear to be just fine. Timing down to ~ 14.5 seconds. |
|||
09-14-2024, 11:09 PM
Post: #15
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42] | |||
09-15-2024, 04:06 PM
(This post was last modified: 09-16-2024 10:59 PM by Albert Chan.)
Post: #16
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
From wikipedia Gauss's continued fraction
Wikipedia solve for \(_2 F _1(1,b;c;z) \) CF with a=0, c=c-1, and simplify. Here is another way, with b=1 \( \displaystyle \begin{align} _2 F _1 (a,1;c;z) &= 1 + \frac{a\;z}{c} \; _2F _1(a+1,1;c+1;z) \\ &= \frac{1}{1-a\;z \left( \frac{_2F _1(a+1,1;c+1;z)} {c\; _2F _1(a,1;c;z)}\right)} \\ \\ &= \frac{1}{1\;-}\; \frac{a\,z}{c \;- }\; \frac{(c-a)\,1z}{(c+1) \;- }\; \frac{(c+0)(a+1)\,z}{(c+2) \;- }\; \frac{(c-a+1)\,2z}{(c+3) \;- }\; \frac{(c+1)(a+2)\,z}{(c+4) \;- }\; \frac{(c-a+2)\,3z}{(c+5) \;- }\; ... \end{align}\) \(\displaystyle \begin{align} \arctan(z) &= z\;_2\!F_1 \left(\frac{1}{2}, 1; \frac{3}{2}; -z^2 \right) \\ &= \frac{z}{1\;+}\; \frac{\frac{1}{2}\,z^2}{\frac{3}{2} \;+ }\; \frac{1 \, z^2}{\frac{5}{2} \;+ }\; \frac{\frac{9}{4} \, z^2}{\frac{7}{2}\;+ }\; \frac{4 \, z^2}{\frac{9}{2}\;+ }\; \frac{\frac{25}{4}\, z^2}{\frac{11}{2}\;+ }\; \frac{9\, z^2}{\frac{13}{2} \;+ }\; ... \\ \\ &= \frac{z}{1\;+}\; \frac{z^2}{3 \;+ }\; \frac{2^2 \,z^2}{5 \;+ }\; \frac{3^2 \, z^2}{7\;+ }\; \frac{4^2 \, z^2}{9\;+ }\; \frac{5^2 \, z^2}{11\;+ }\; \frac{6^2 \, z^2}{13\;+ }\; ... \end{align}\) This may also be of interest: Continued Fraction expansion of tan(x) Continued Fraction of tan(n*x) |
|||
09-15-2024, 04:55 PM
(This post was last modified: 09-15-2024 08:37 PM by Gerson W. Barbosa.)
Post: #17
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
(09-14-2024 10:37 PM)Thomas Klemm Wrote: Here's the program for the HP-12C: Since we’re at it, here’s tangent using basically the same nice scheme of yours: Code: 01 { 36 } ENTER 3.141592654 ENTER 4 / g GTO 00 R/S -> 1.000000000 1.5707 g GTO 00 R/S -> 10381.32(037) ( ~ 7 seconds on the good old 12C ) ———- P.S.: Or, if we want all of them on the stack: Code: ... g GTO 00 R/S -> T: TAN(x) Z: TAN(x) Y: COS(x) X: SIN(x) ———- Code:
g GTO 36 R/S -> Z: ATAN(x) Y: ACOS(x) X: ASIN(x) (*) These are left as an exercise to the interested reader. |
|||
09-15-2024, 09:18 PM
Post: #18
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
Instead of using
we could use This makes the program a bit shorter at the cost of an additional register: Code: 01 { 44 01 } STO 1 |
|||
09-16-2024, 05:10 PM
Post: #19
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42]
(09-15-2024 09:18 PM)Thomas Klemm Wrote: This makes the program a bit shorter at the cost of an additional register: No problem! Stack-only is not important for the HP-12C. Size is. Thanks for the improvement! Except for the borrowed code, the following is not size-optimized. But that’s just a test. HP-12C: Code:
Initialization: 3.141592654 ENTER 180 / STO 5 0 (DEG) or 1 (RAD) g GTO 47 R/S Usage: x R/S -> T: TAN(x) Z: TAN(x) Y: COS(x) X: SIN(x) First and second quadrants only. 90 degrees case not handled. —— HP-15C listing the JRPN 15C Simulator: Code:
|
|||
09-17-2024, 01:56 AM
Post: #20
|
|||
|
|||
RE: Arctangent function program [HP-42S, Free42] | |||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 4 Guest(s)