Post Reply 
(12C) Decimal to Fraction
08-09-2018, 06:38 PM
Post: #38
RE: (12C) Decimal to Fraction
(08-09-2018 03:39 PM)Albert Chan Wrote:  I removed \(0 < x < 1\) restriction of farey fraction.

Very nice! This is my attempt to translate your program for the HP-11C:
Code:
*LBL A
 STO 1       ; limit x
 R↓          ; x
 STO 0       ; x
 INT         ; ⌊x⌋
 STO 2       ; lo_n
 1           ; 1 lo_n
 STO 3       ; lo_d=1
 STO 4       ; hi_n=1
 R↓          ; lo_n
 RCL 0       ; x lo_n
 x=y         ; x = lo_n ?
 GTO 4       ; return lo_n lo_d
 Clx         ; 0
 STO 5       ; hi_d=0
*LBL 0       ; while
 RCL 2       ; lo_n
 RCL 4       ; hi_n lo_n
 +           ; lo_n+hi_n
 STO 6       ; n=lo_n+hi_n
 RCL 1       ; limit
 RCL 3       ; lo_d limit
 RCL 5       ; hi_d lo_d limit
 +           ; lo_d+hi_d limit
 STO 7       ; d=lo_d+hi_d limit
 x>y         ; d > limit ?
 GTO 3       ; break
 RCL 6       ; n
 RCL 0       ; x n
 RCL 7       ; d x n
 *           ; x*d n
 x≤y         ; x*d ≤ n ?
 GTO 1
 RCL 6       ; n
 STO 2       ; lo_n
 RCL 7       ; d
 STO 3       ; lo_d
 GTO 0       ; while
*LBL 1       ; x*d n
 x=y         ; x*d = n ?
 GTO 2       ; return n d
 RCL 6       ; n
 STO 4       ; hi_n
 RCL 7       ; d
 STO 5       ; hi_d
 GTO 0       ; while
*LBL 2       ; return n d
 RCL 6       ; n
 RCL 7       ; d
 RTN
*LBL 3       ; break
 RCL 0       ; x
 RCL 4       ; hi_n x
 RCL 5       ; hi_d hi_n x
 /           ; hi_n/hi_d x
 -           ; x-hi_n/hi_d
 ABS         ; ∆_hi=|x-hi_n/hi_d|
 RCL 0       ; x ∆_hi
 RCL 2       ; lo_n x ∆_hi
 RCL 3       ; lo_d lo_n x ∆_hi
 /           ; lo_n/lo_d x ∆_hi
 -           ; x-lo_n/lo_d ∆_hi
 ABS         ; ∆_lo=|x-lo_n/lo_d| ∆_hi
 x≤y         ; ∆_lo ≤ ∆_hi ?
 GTO 4       ; return lo_n lo_d
 RCL 4       ; hi_n
 RCL 5       ; hi_d hi_n
 RTN
*LBL 4
 RCL 2       ; lo_n
 RCL 3       ; lo_d lo_n
 RTN

Quote:
Code:
>>> farey(-0.10322, 1e8)  # negatives ok
(-5161, 50000)

That's not implemented yet. So I'm leaving this as an exercise for the dear reader.

Kind regards
Thomas
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(12C) Decimal to Fraction - Gamo - 08-06-2018, 12:33 PM
RE: (12C) Decimal to Fraction - Dieter - 08-06-2018, 08:44 PM
RE: (12C) Decimal to Fraction - Dieter - 08-07-2018, 07:29 AM
RE: (12C) Decimal to Fraction - Dieter - 08-07-2018, 01:02 PM
RE: (12C) Decimal to Fraction - Dieter - 08-07-2018, 11:38 AM
RE: (12C) Decimal to Fraction - Gamo - 08-07-2018, 09:29 AM
RE: (12C) Decimal to Fraction - Joe Horn - 08-07-2018, 06:28 PM
RE: (12C) Decimal to Fraction - Dieter - 08-07-2018, 07:10 PM
RE: (12C) Decimal to Fraction - Joe Horn - 08-08-2018, 12:28 AM
RE: (12C) Decimal to Fraction - Dieter - 08-08-2018, 07:04 PM
RE: (12C) Decimal to Fraction - Dieter - 08-09-2018, 09:27 AM
RE: (12C) Decimal to Fraction - Dieter - 08-08-2018, 09:44 PM
RE: (12C) Decimal to Fraction - Gamo - 08-09-2018, 12:51 AM
RE: (12C) Decimal to Fraction - Dieter - 08-09-2018, 07:46 AM
RE: (12C) Decimal to Fraction - Dieter - 08-09-2018, 08:09 AM
RE: (12C) Decimal to Fraction - Gamo - 08-09-2018, 09:16 AM
RE: (12C) Decimal to Fraction - Dieter - 08-09-2018, 10:30 AM
RE: (12C) Decimal to Fraction - Thomas Klemm - 08-09-2018 06:38 PM
RE: (12C) Decimal to Fraction - Dieter - 08-10-2018, 12:28 PM
RE: (12C) Decimal to Fraction - Gamo - 05-07-2019, 01:44 AM
RE: (12C) Decimal to Fraction - Dieter - 08-10-2018, 07:06 PM



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