(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: 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 |
|||
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 Dieter |
|||
08-11-2018, 12:58 AM
Post: #43
|
|||
|
|||
RE: (12C) Decimal to Fraction
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 |
|||
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 |
|||
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: (…) Sorry for my poor wording. Thanks for clarifying Thomas |
|||
08-11-2018, 10:05 AM
(This post was last modified: 05-15-2022 08:46 AM by Thomas Klemm.)
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: Thus we can still save an iteration using: Code: 01 STO 0 Best regards Thomas |
|||
05-07-2019, 01:44 AM
(This post was last modified: 05-07-2019 01:44 AM by Gamo.)
Post: #47
|
|||
|
|||
RE: (12C) Decimal to Fraction
Quote:999 [n] Just acquired an Original HP-12C (made in Singapore) ran this and took about 40 seconds. Gamo |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)