50G fraction question Q-> function max denominator
10-24-2019, 09:59 PM
Post: #4
 Joe Horn Senior Member Posts: 2,000 Joined: Dec 2013
RE: 50G fraction question Q-> function max denominator
(10-24-2019 02:58 PM)100LL Wrote:  The 32SII allows you to convert a decimal to fraction and impose a maximum number for the denominator (handy because 64 is usually the highest desired).

Wondering if there's a way to get the 50G to do the same.

No, there's no way to specify a maximum denominator for the ->Q function, but here's a tiny program that does exactly what you seek. It's a flashback from way back in 1991 (HP 48 Goodies Disk #3). To make it work on an HP 50g, be sure to replace the SYSEVAL in the listing with the FLASHEVAL mentioned immediately above the listing. Enjoy!

Code:
DEC2FRAC for the HP 48SX/GX (faster and more complete than ->Q) Improved Decimal-to-Fraction, by Joseph K. Horn, 21 March 1991 THE PROBLEM: The HP 48SX (->Q function) and HP 32SII (with maximum denominator set) both miss many solutions, and slowly recalculate the summations for each term rather than using a fast recursion formula to get each term from the last two. THE SOLUTION: This new algorithm finds the very best solution, very quickly. ALGORITHM: Continued Fractions by fast recursion formula, then make a single calculated jump backwards to the best possible fraction before the specified maximum denominator. Copyright (c) 1991 by Joseph K. Horn.  May be used freely in any application that has no documentation.  May be in a documented application if the above author is credited. INPUT: 2: Decimal Number to be converted to a fraction 1: Maximum Allowed Denominator (a positive integer) OUTPUT: 1: 'Numerator/Denominator' Note: System flag -3 is cleared (to allow symbolic results). EXAMPLE of completeness: What fraction is closest to the number "e", but with a denominator not bigger than 20? The HP 48SX and the HP 32SII say it's 19/7.  They both say that the next better fraction is 87/32.  But there are two fractions between these which they miss: 49/18 and 68/25. Press 1 e^x 20 DEC2FRAC and see '49/18', the correct answer. EXAMPLE of speed improvement: the golden ratio, (sqrt(5)+1)/2, which is approximately 514229/317811.  This is found by the HP 48SX ->Q function in 3.23 seconds, and by DEC2FRAC in 1.29 seconds. Note: The SYSEVAL in this program only works on the HP 48 (S/SX/G/GX). It does not work in the HP 50g, which must use #162006h FLASHEVAL instead of that SYSEVAL. %%HP:T(3); @ DEC2FRAC, by Joseph K. Horn \<< DUP2 @ Must be two arguments.  Exit now if max denominator < 2, IF 1 > SWAP FP AND @ or if decimal fraction is an integer. THEN \-> f c @ Store decimal fraction, and max denominator. \<< 0 1 f @ Calculate only denominators.  Do numerator only at end. WHILE OVER c < OVER AND @ Do until bigger than max denominator REPEAT INV DUP FP 4 ROLLD IP OVER * ROT + ROT @ This is the END DROP DUP2 c @ recursion formula continued fraction expansion. IF DUP2 > @ Is there a possible "missing" fraction? THEN - OVER / CEIL * - @ This is the new, fast "jump backwards". ELSE 3 DROPN @ (Sometimes there's no need to jump.) END DUP2 1 2 @ Take the new denominator & the previous one, and START DUP f * 0 RND SWAP / f - ABS SWAP @ turn into fractions. NEXT @ See which one's closest to the original decimal fraction. IF > @ Compare the two solutions, and THEN SWAP @ pick the better one. END DROP DUP f * 0 RND SWAP @ Calculate the numerator. \>> @ End of real work; now clean up the output. IF DUP ABS 1 > @ Is the denominator greater than 1? THEN -3 CF # 5603Eh SYSEVAL @ If so, make output into 'A/B' form. ELSE DROP @ Otherwise, get rid of extraneous denominator, END @ and exit program. ELSE DROP @ If bad arguments, do nothing to "decimal fraction", but END @ get rid of "maximum denominator" and exit program. \>>

<0|ΙΈ|0>
-Joe-
 « Next Oldest | Next Newest »

 Messages In This Thread 50G fraction question Q-> function max denominator - 100LL - 10-24-2019, 02:58 PM RE: 50G fraction question Q-> function max denominator - ijabbott - 10-24-2019, 04:24 PM RE: 50G fraction question Q-> function max denominator - burkhard - 10-24-2019, 09:09 PM RE: 50G fraction question Q-> function max denominator - Joe Horn - 10-24-2019 09:59 PM RE: 50G fraction question Q-> function max denominator - cdmackay - 10-24-2019, 11:02 PM RE: 50G fraction question Q-> function max denominator - Joe Horn - 10-25-2019, 05:26 AM RE: 50G fraction question Q-> function max denominator - 100LL - 10-25-2019, 01:47 PM RE: 50G fraction question Q-> function max denominator - cdmackay - 10-25-2019, 04:52 PM RE: 50G fraction question Q-> function max denominator - 100LL - 10-25-2019, 09:42 PM RE: 50G fraction question Q-> function max denominator - Joe Horn - 10-25-2019, 11:42 PM RE: 50G fraction question Q-> function max denominator - ijabbott - 10-26-2019, 08:56 AM RE: 50G fraction question Q-> function max denominator - DavidM - 10-29-2019, 02:16 PM RE: 50G fraction question Q-> function max denominator - DavidM - 11-08-2019, 05:54 PM RE: 50G fraction question Q-> function max denominator - Albert Chan - 11-08-2019, 07:01 PM RE: 50G fraction question Q-> function max denominator - DavidM - 11-08-2019, 08:43 PM RE: 50G fraction question Q-> function max denominator - Joe Horn - 11-08-2019, 09:01 PM RE: 50G fraction question Q-> function max denominator - Albert Chan - 11-08-2019, 10:05 PM RE: 50G fraction question Q-> function max denominator - DavidM - 11-09-2019, 01:46 PM

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