Post Reply 
(12C) Decimal to Fraction
08-10-2018, 05:02 PM
Post: #41
RE: (12C) Decimal to Fraction
(08-10-2018 12:28 PM)Dieter Wrote:  nmax is stored in the n-register

That's a clever idea for the HP-12C.

(08-09-2018 10:15 PM)Albert Chan Wrote:  I had revised my Python code after your HP-11C translation. Now:
1. hi = lo + 1 instead of 1/0 (infinity), thus saved 1 iteration
2. Both abs() calls are removed

These are the relevant lines:
Code:
    n1, d1 = int(x), 1
    n2, d2 = n1+1, 1        # hi = lo + 1
    pick_hi = (n2/d2 - x) < (x - n1/d1)
    return (n2, d2) if pick_hi else (n1, d1)

In both cases we use that \(\frac{n1}{d1} < x < \frac{n2}{d2}\).

This allows to remove squaring the differences in:
Code:
47 RCL 0
48 RCL 3
49 RCL 4
50 /
51 -
52 ENTER
53 x
54 RCL 0
55 RCL 1
56 RCL 2
57 /
58 -
59 ENTER
60 x
61 x≤y?

Instead we can just use:
Code:
47 RCL 3
48 RCL 4
49 /
50 RCL 0
51 -
52 RCL 0
53 RCL 1
54 RCL 2
55 /
56 -
57 x≤y?

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 - Dieter - 08-10-2018, 12:28 PM
RE: (12C) Decimal to Fraction - Gamo - 05-07-2019, 01:44 AM
RE: (12C) Decimal to Fraction - Thomas Klemm - 08-10-2018 05:02 PM
RE: (12C) Decimal to Fraction - Dieter - 08-10-2018, 07:06 PM



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