The Museum of HP Calculators

HP Forum Archive 02

 Converting a decimal # to a fraction #Message #1 Posted by yummy on 14 Apr 2000, 3:48 p.m. Can someone show me how to do this? Thanks

 Re: Converting a decimal # to a fraction #Message #2 Posted by William L. Drylie on 14 Apr 2000, 10:14 p.m.,in response to message #1 by yummy A fraction a/b is defined by the equation a/b x b = a. The term a is the numerator and term b is the denominator of the fraction. Example, 5/8 = .6250 in decimal form. Multiplying the denominator 8 x .6250 yields the answer 5 which is the numerator so you have the fraction of 5/8 again. If you see a fraction in decimal form and have no idea what the fractional equivalent may be you can try this trial and error algorithim on your calculator. Start at 2 as the denominator and multiply this with the decimal. If you get a mixed number for the numerator, then it was not the proper denominator, try again with 3, and so on until you get a whole number (not zero). Example. .6250 x 2 = 1.25. Not a proper numerator. .6250 x 3 = 1.8750 .6250 x 4 = 2.5 etc.. until, .6250 x 8 = 5 the correct numerator. This will be the only solution that will give a whole number numerator until you go to another multiple of 8 such as 16, .6250 x 16 = 10 this would be 10/8 = 1.25 which is not .6250 so you will know you went to far. If all else fails, go purchase an hp32SII which does the converting for you automatically. I hope I have helped you. Cheers Bill.

 Re: Converting a decimal # to a fraction #Message #3 Posted by r. d. baertschiger. on 15 Apr 2000, 2:35 a.m.,in response to message #2 by William L. Drylie What kind of calculator are you using? If you have a 48 series try using \->Q. This will return a fraction given a decimal number. ..6250 = '5/8'. rdb.

 Re: Converting a decimal # to a fraction #Message #4 Posted by Erwin on 15 Apr 2000, 4:31 a.m.,in response to message #3 by r. d. baertschiger. \->Q ? I also like too know this trick, but wat key's must i use? Erwin

 Re: Converting a decimal # to a fraction #Message #5 Posted by r. d. baertschiger. on 15 Apr 2000, 3:14 p.m.,in response to message #4 by Erwin If you still need more information on using the to 'Q' function, the following may help. For an hp-48sx place a decimal number on the stack then press left shift, the orange key, and then 'EVAL', '->Q'. This will return a near approximation to the stack, i.e. pi returns '22/7'. If you are using an hp-48gx place a decimal number on the stack and then press the alpha key twice to set alpha entry mode. Then press right shift, the green key, and zero to place a right arrow on the input line, then press the large down arrow key, row three column five, to place a 'Q' after the arrow, then press 'ENTER'. Again this returns an approximation fraction. If you are using an hp-49g, use the same instructions as for the 48gx, substituting red shift for green shift and 'Q' 'Y^X' for down arrow, Also the 49g requires a switch to RAD mode for some imbecilic reason. Hope this has helped. rdb.

 Re: Converting a decimal # to a fraction #Message #6 Posted by Erwin on 15 Apr 2000, 3:53 p.m.,in response to message #5 by r. d. baertschiger. Aha, That's a lot easier than than going through the menu's too get the "->Q" softkey. Thanks, Erwin

 Re: Converting a decimal # to a fraction #Message #7 Posted by yummy on 15 Apr 2000, 10:23 p.m.,in response to message #5 by r. d. baertschiger. Thanks guys

 Re: Converting a decimal # to a fraction #Message #8 Posted by Xose on 15 Apr 2000, 5:49 a.m.,in response to message #2 by William L. Drylie Hi! If you've got a 48 or a 32SII no problem: they have that function built in. For other calculators try this little program. I've implemented it on a 28S and a 42S and it works fine. You can even specify a default precission. The code below is for the 28S (48X style, but not 100% compatible!), if anyone wants the 42S code just ask for it. Copyrights: This code is a personal modification on Thomas Affinito & Jurjen E. Bos. 'Fract' program. %decimal_number -> 'fraction' (with default precision) %decimal_number %precission -> 'fraction' ('fraction' is EVALuable to the original number, the '6' in the first row of code is the default precission) « IF DUP FP THEN 6 END NEG ALOG 2 / 1 0 4 ROLL DUP ABS 5 ROLLD SIGN -> X P A B S « X ABS WHILE 'ABS(FLOOR(X*A+.5)-X*A)>=P*A' EVAL REPEAT INV DUP IP ABS A * B + A 'B' STO 'A' STO FP END DROP 'FLOOR(X*A+.5)' EVAL S * A » "'" ROT ->STR + "/" + SWAP ->STR + "'" + STR-> » Hope this helps!

 Re: Converting a decimal # to a fraction #Message #9 Posted by Erwin on 15 Apr 2000, 6:07 a.m.,in response to message #8 by Xose Got it. I thought that the 48 didn't had this function, but it's also explained on page 16-5 of the user's guide. Thank's for the HP28s program. The program for the HP42s, is that also suiteble for a HP41CV? If so, can you give me that? Thank you, Erwin

 Re: Converting a decimal # to a fraction #Message #10 Posted by Xose on 15 Apr 2000, 2:10 p.m.,in response to message #9 by Erwin I've translated the HP42S 'Fract' code to a HP41C and it works, though is not as user-friendly as on the HP42S. You can find both HP42S and HP41C programs here: http://www.geocities.com/xose_manuel2/calc/fract.txt I've included the 42S code because it's output is nicer (it even shows you if the fraction is 'equal to', 'less than' or 'greater than' the original decimal number, more or less the same way the HP32SII does), but you would need the Extended Functions Module or a HP41CX to use it. To use it on the 41C series: X: %decimal_number -> Y: %numerator X: %denominator Y: %decimal_number X: %precission -> Y: %numerator X: %denominator the default precission is the number at the forth step (12). The code can be improved, I'm sure. If you find any mistake or do any improvement, please, let me know. Greetings, Xose

 Re: Converting a decimal # to a fraction #Message #11 Posted by Peter Greb on 18 Apr 2000, 3:20 p.m.,in response to message #2 by William L. Drylie Hi! I am interested in a ->Q prg for my HP15C, but I don't understand the 28S code. Can you tell me where to get the T.A & J.E.B 'Fract' prg or comment your prg so that I can (try to) write a prg for the 15C. Thanx, Peter...

 Re: Converting a decimal # to a fraction #Message #12 Posted by Xose on 19 Apr 2000, 6:12 p.m.,in response to message #11 by Peter Greb I know almost nothing about programming a 15C, but since it uses the traditional keystroke method maybe the 'Fract' code for the HP-41C could be easy to adapt. I've uploaded the 'Fract' source codes for HP-28S, HP-42S and HP-41C to my site, you'll find them here: http://www.geocities.com/xose_manuel2/calc/fract.html I've tried to comment the 28S code (excuses for my english :), I hope it'll help you. I found the original code at the Center for Computational Linguistics (ftp://ftp.ccl.kuleuven.ac.be/pub/hp28), but it's only for the 28S and uncommented.

 Re: Converting a decimal # to a fraction #Message #13 Posted by Peter Greb on 20 Apr 2000, 11:36 a.m.,in response to message #12 by Xose Hi! Well, works fine (41C version) on the 15C. Some minor chnages required, FRC = FRAC, no STO 01 = STO 1 etc. XEQ 10 = GSB .0, LBL 10 = LBL .0 and some TEST X instead of x y etc. You could use recall and store arithmetic (RCL + 3...) but I didn't use it here. Thanx, Peter So it makes FRAC for HP15C: 01·LBL A 02 ENTER 03 FRAC 04 12 05 X Y 06 X=0 07 Rv 08 Rv 09 CHS 10 10^X 11 2 12 / 13 STO 1 14 Rv 15 STO 5 16 ENTER 17 ABS 18 STO 0 19 / 20 STO 4 21 1 22 STO 2 23 CLX 24 STO 3 25 RCL 0 26·LBL 0 27 RCL 0 28 RCL 2 29 * 30 ENTER 31 ENTER 32 2 33 1/X 34 + 35 GSB .0 36 - 37 ABS 38 RCL 1 39 RCL 2 40 * 41 TEST 7 42 GTO 1 43 Rv 44 Rv 45 1/X 46 ENTER 47 INT 48 ABS 49 RCL 2 50 * 51 RCL 3 52 + 53 RCL 2 54 STO 3 55 Rv 56 STO 2 57 Rv 58 FRAC 59 GTO 0 60·LBL 1 61 RCL 0 62 RCL 2 63 * 64 2 65 1/X 66 + 67 GSB .0 68 RCL 4 69 * 70 RCL 2 71 RTN 72·LBL .0 73 CHS 74 TEST 4 75 GTO 2 76 1 77 - 78·LBL 2 79 INT 80 CHS 81 RTN

 Re: Converting a decimal # to a fraction #Message #14 Posted by rupert9000 on 16 Apr 2000, 11:59 a.m.,in response to message #1 by yummy Some months ago I developed the little program enclosed below in order to convert a decimal diameter value into a diameter value expressed in inches and fractions of a inch (for CAD related purposes). The listing is self-explaining because the algorithm is very simple (sort of trial and error), moreover, it is easily translable into any HP calculator language. On a calculator however this algorithm can get slow in the case of irrational numbers and very low values for epsilon. There is an algorithm based on modular arithmetics that does the same calculation in a more efficient way, if I'm not wrong. ------------------------------------------------------------------------------ REM Fractions DIM mnt, r, eps, delta AS DOUBLE DIM i, j AS LONG CLS PRINT "" INPUT "Mantissa "; mnt neweps: INPUT "Epsilon "; eps i = 1 j = 1 iterate: j = j + 1 r = i / j delta = ABS(r - mnt) IF delta mnt THEN GOTO iterate END IF result: PRINT "" PRINT "Fraction = "; i; " / "; j; " ( "; r; " )" PRINT "Error = "; 100 * delta / mnt; " %" PRINT "" choice\$ = "" INPUT "New epsilon (Y/N) "; choice\$ choice\$ = UCASE\$(choice\$) IF choice\$ = "Y" THEN PRINT "" GOTO neweps END IF SYSTEM ------------------------------------------------------------------------------

Go back to the main exhibit hall