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
08-10-2018, 07:06 PM
Post: #42
RE: (12C) Decimal to Fraction
(08-10-2018 05:02 PM)Thomas Klemm Wrote:  In both cases we use that \(\frac{n1}{d1} < x < \frac{n2}{d2}\).

Fine. This saves four lines to that GTO 66 becomes GTO 62.
Here is a revised version:

Code:
01 STO 0
02 INTG
03 STO 1
04 1
05 STO 2
06 STO 3
07 RCL 0
08 FRAC
09 x=0?
10 GTO 62
11 Clx
12 STO 4
13 RCL 1
14 RCL 3
15 +
16 STO 5
17 RCL n
18 RCL 2
19 RCL 4
20 +
21 STO 6
22 x≤y?
23 GTO 25
24 GTO 47
25 RCL 5
26 RCL 0
27 RCL 6
28 x
29 x≤y?
30 GTO 36
31 RCL 5
32 STO 1
33 RCL 6
34 STO 2
35 GTO 13
36 -
37 x=0?
38 GTO 44
39 RCL 5
40 STO 3
41 RCL 6
42 STO 4
43 GTO 13
44 RCL 6
45 RCL 5
46 GTO 00
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?
58 GTO 62
59 RCL 4
60 RCL 3
61 GTO 00
62 RCL 2
63 RCL 1
64 GTO 00

Dieter
Find all posts by this user
Quote this message in a reply
08-11-2018, 12:58 AM
Post: #43
RE: (12C) Decimal to Fraction
Code:
    pick_hi = (n2/d2 - x) < (x - n1/d1)
    return (n2, d2) if pick_hi else (n1, d1)

Discovered a pick_hi bug:
If x = midpoint between the Farey pair, it always pick n1/d1, but n2/d2 might be better.

Example: x = midpoint of a Farey Pair

farey(111/308, 20) => 5/14 --> 4/11 is better
farey( 51/130, 15 ) => 5/13 --> 2/5 is better

It is not technically wrong, but we prefer smaller denominator.
Patch below:
Code:
    diff = 2*d1*d2*x - (n1*d2 + n2*d1)  # x vs Farey Pair midpoint
    if diff == 0: diff = d1 - d2        # x is midpoint, pick smaller d
    return (n2, d2) if diff > 0 else (n1, d1)
Find all posts by this user
Quote this message in a reply
08-11-2018, 02:13 AM
Post: #44
RE: (12C) Decimal to Fraction
(08-09-2018 11:13 AM)Thomas Klemm Wrote:  As we know \(\frac{113}{355}\) is closer for a long time. We have to wait until \(\frac{33215}{104348}\) to get closer

Hi, Thomas,

If you meant closer from below (i.e fraction < 1/Pi), above is correct.

However, if closer in absolute term, you don't have to wait that long.
For semiconvergent better than 113/355, solve for k

1/Pi - 113/355 = 2.703e-8 = (106 + 113k)/(333 + 355k) - 1/Pi
k ~ 145.8

If k >= 146, the fraction is closer.
For k = 146, we get 16604/52163, absolute error = 2.697e-8
Find all posts by this user
Quote this message in a reply
08-11-2018, 09:38 AM
Post: #45
RE: (12C) Decimal to Fraction
(08-11-2018 02:13 AM)Albert Chan Wrote:  If you meant closer from below (i.e fraction < 1/Pi), above is correct.

Exactly. I just checked when would it switch from \(\frac{113}{355}\) to the next value:
Code:
(…)
113 355 32876 103283
113 355 32989 103638
113 355 33102 103993
33215 104348 33102 103993

Sorry for my poor wording.

Thanks for clarifying
Thomas
Find all posts by this user
Quote this message in a reply
08-11-2018, 10:05 AM
Post: #46
RE: (12C) Decimal to Fraction
(08-09-2018 10:15 PM)Albert Chan Wrote:  1. hi = lo + 1 instead of 1/0 (infinity), thus saved 1 iteration

Code:
n2, d2 = n1+1, 1        # hi = lo + 1

(08-10-2018 07:06 PM)Dieter Wrote:  Here is a revised version:
Code:
01 STO 0
02 INTG
03 STO 1
04 1
05 STO 2
06 STO 3
07 RCL 0
08 FRAC
09 x=0?
10 GTO 62
11 Clx
12 STO 4
(…)

Thus we can still safe an iteration using:
Code:
01 STO 0
02 INTG
03 STO 1
04 1
05 STO 2
06 STO 4
07 +
08 STO 3
09 RCL 0
10 FRAC
11 x=0?
12 GTO 62
(…)

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




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