(12C) Decimal to Fraction - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: (12C) Decimal to Fraction (/thread-11178.html) |
RE: (12C) Decimal to Fraction - Thomas Klemm - 08-10-2018 05:02 PM (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: These are the relevant lines: Code: n1, d1 = int(x), 1 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 Instead we can just use: Code: 47 RCL 3 Kind regards Thomas RE: (12C) Decimal to Fraction - Dieter - 08-10-2018 07:06 PM (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 Dieter RE: (12C) Decimal to Fraction - Albert Chan - 08-11-2018 12:58 AM Code: pick_hi = (n2/d2 - x) < (x - 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 RE: (12C) Decimal to Fraction - Albert Chan - 08-11-2018 02:13 AM (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 RE: (12C) Decimal to Fraction - Thomas Klemm - 08-11-2018 09:38 AM (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: (…) Sorry for my poor wording. Thanks for clarifying Thomas RE: (12C) Decimal to Fraction - Thomas Klemm - 08-11-2018 10:05 AM (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: Thus we can still save an iteration using: Code: 01 STO 0 Best regards Thomas RE: (12C) Decimal to Fraction - Gamo - 05-07-2019 01:44 AM Quote:999 [n] Just acquired an Original HP-12C (made in Singapore) ran this and took about 40 seconds. Gamo |