Post Reply 
Python to FOCAL Compiler
10-06-2015, 07:11 PM (This post was last modified: 10-06-2015 07:15 PM by Thomas Klemm.)
Post: #6
RE: Python to FOCAL Compiler
(10-06-2015 03:58 PM)Gerson W. Barbosa Wrote:  I don't mean to hijack this thread either, but since you've talked about elliptic integrals and you appear to be in the mood, what about converting the fast algorithm presented by Hugh Steers here to RPN?

Here's a Python program based on Hugh's implementation:

Code:
def ellipse(a, b):
    u = 1
    v = b / a
    s = (1 + v**2)/2
    t = 1
    while 0 == 0:
        m = (u + v)/2
        if m == u:
            break
        w = (u - v)/2
        v = sqrt(u * v)
        u = m
        s = s - t * w**2
        t = t * 2
    return 2 * pi * a * s / u

And that's the generated FOCAL program using 113 bytes:

Code:
LBL "ELLIPSE"
STO 01 ; b
RDN
STO 00 ; a
RDN
1
STO 02 ; u
RDN
RCL 01 ; b
RCL 00 ; a
/
STO 03 ; v
RDN
1
RCL 03 ; v
2
Y↑X
+
2
/
STO 04 ; s
RDN
1
STO 05 ; t
RDN
LBL 00
0
0
X#Y?
GTO 02
RCL 02 ; u
RCL 03 ; v
+
2
/
STO 06 ; m
RDN
RCL 06 ; m
RCL 02 ; u
X#Y?
GTO 01
GTO 03 ; break
GTO 01
LBL 01
RCL 02 ; u
RCL 03 ; v
-
2
/
STO 07 ; w
RDN
RCL 02 ; u
RCL 03 ; v
*
SQRT
STO 03 ; v
RDN
RCL 06 ; m
STO 02 ; u
RDN
RCL 04 ; s
RCL 05 ; t
RCL 07 ; w
2
Y↑X
*
-
STO 04 ; s
RDN
RCL 05 ; t
2
*
STO 05 ; t
RDN
GTO 00
LBL 02
LBL 03
2
PI
*
RCL 00 ; a
*
RCL 04 ; s
*
RCL 02 ; u
/
RTN

But we can easily bring this down to 77 bytes:

Code:
LBL "ELLIPSE"
STO 01 ; b
RDN
STO 00 ; a
1
STO 02 ; u
STO 05 ; t
RCL 01 ; b
RCL 00 ; a
/
STO 03 ; v
X↑2
+
2
/
STO 04 ; s
LBL 00
RCL 02 ; u
RCL 03 ; v
+
2
/
STO 06 ; m
RCL 02 ; u
X=Y?
GTO 03 ; break
RCL 02 ; u
RCL 03 ; v
-
2
/
STO 07 ; w
RCL 02 ; u
RCL 03 ; v
*
SQRT
STO 03 ; v
RCL 06 ; m
STO 02 ; u
RCL 05 ; t
RCL 07 ; w
X↑2
*
ST- 04 ; s
2
ST* 05 ; t
GTO 00
LBL 03
2
PI
*
RCL 00 ; a
*
RCL 04 ; s
*
RCL 02 ; u
/
RTN

Kind regards
Thomas

PS: We can't use True in conditions as there's no corresponding comparison operator in the HP-41C. That's why I used 0 == 0 instead in the while-loop. The generated code could then be removed in the optimized variant of the program:

Code:
0
0
X#Y?
GTO 02
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Python to FOCAL Compiler - Thomas Klemm - 10-05-2015, 10:58 PM
RE: Python to FOCAL Compiler - Sukiari - 10-06-2015, 03:36 AM
RE: Python to FOCAL Compiler - Thomas Klemm - 10-06-2015 07:11 PM
Java to FOCAL Compiler - Thomas Klemm - 10-07-2015, 04:38 PM



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