Post Reply 
Arctangent function program [HP-42S, Free42]
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):
    a = b = 2 / x
    p = 1
    q = w = 0
    for _ in range(n):
        a, b = b, -(a + 2 * b)
        q = 1 / (a + q)
        p, w = w * q, (a * w + p) * q
    r = 1 + w**2
    return 2*w/r, (1 - w**2)/r

This is the corresponding program for the HP-42S:
Code:
00 { 64-Byte Prgm } ; x
01 2                ; 2     x
02 X<>Y             ; x     2
03 ÷                ; x/2
04 STO 00           ; a = x/2
05 STO 01           ; b = a
06 1                ; 1
07 STO 02           ; p = 1
08 CLX              ; 0
09 STO 03           ; q = 0
10 STO 04           ; w = 0
11 8                ; 8
12 STO 05           ; n = 8
13▸LBL 00           ; for loop
14 RCL 00           ; a
15 RCL 01           ; b     a
16 STO 00           ; a = b
17 2                ; 2     b       a
18 ×                ; 2b    a
19 +                ; a+2b
20 +/-              ; -(a+2b)
21 STO 01           ; b = -(a+2b)
22 RCL 02           ; p
23 RCL 04           ; w     p
24 STO 02           ; p = w
25 RCL 00           ; a     w       p
26 ×                ; a*w   p
27 +                ; a*w+p
28 STO 04           ; w = a*w+p
29 RCL 00           ; a
30 RCL 03           ; q      a
31 +                ; a+q
32 1/X              ; 1/(a+q)
33 STO 03           ; q = 1/(a+q)
34 STO× 02          ; p = w*q
35 STO× 04          ; w = (a*w+p)*q
36 DSE 05           ; n = n-1
37 GTO 00           ; repeat
38 RCL 04           ; t = tan(x/2)
39 ENTER            ; t     t
40 +                ; 2t
41 LASTX            ; t     2t
42 ENTER            ; t     t       2t
43 ×                ; t^2   2t
44 1                ; 1     t^2     2t
45 X<>Y             ; t^2   1       2t
46 -                ; 1-t^2 2t
47 1                ; 1     1-t^2   2t
48 LASTX            ; t^2   1       1-t^2   2t
49 +                ; 1+t^2 1-t^2   2t      2t
50 ÷                ; cos(x)=(1+t^2)/(1-t^2)  2t
51 X<>Y             ; 2t    cos(x)
52 LASTX            ; 1+t^2 2t      cos(x)
53 ÷                ; sin(x)=2t/(1+t^2)  cos(x)
54 X<>Y             ; cos(x) sin(x)
55 END              ; return

But it looks like we don't gain much with this approach.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Arctangent function program [HP-42S, Free42] - Thomas Klemm - 09-21-2024 01:43 PM



User(s) browsing this thread: 3 Guest(s)