48G fraction capability...WOW!!



#2

Hi folks.

My recently acquired 48g continues to amaze me. If I use the Q symbolic function to convert .2356789 into a fraction, it comes up with 238010/1009791, which of course is correct. What amazes me is how the 48G found it. For instance, if I were to convert .2356789 to a fraction manually, I wouldn't be able to get past 2356789/10000000, since there are no common integer factors to the numerator and denominator. But the (amazing!) 48G found a non-integer common factor of 9.90205873703, which reduced 2356789/10000000 to 238010/1009791. This must be one sophisticated algorithm to do that. Can any of you math guru's shed any light on how this algorithm works?

Thanks, and best regards, Hal


#3

If you like the built in ->Q function, you'll love QPI_4.3 for the 48, which

"...approximates any floating point numbers by a rational number, square root, multiple of PI, exponential or a logarithm depending on which approximation seems best." If you've got your serial cable, it's available for free download under "Best Programs" from Eric Rechlin's HP Program Archive:

http://www.hpcalc.org/best.php


#4

The algorithm for simple P/Q representation relies (usually) on continuous fractions, you can probably google something useful out of these words.

For (A+SQRT(B))/C representation, it is much more tricky (I have not found the final word), the problem here is of the limited precision of the input.

Very quickly you get large A, B and C and there is some black magic (to my limited knowledge) at work.

See also Simon Plouffe's amazing Inverter, alas no algorithm is provided. If anyone has information (past the continuous fraction stuff), I'd be very happy to read it here.

#5

When I divide 238010 by 1009791, I get .235702239 on my 41CX.

On my 50g I get .235702239374. I think the denominator is supposed to be 1009891.

#6

For the 49 series, see Joe Horn's PDQ2 application, which he posted as UserRPL source code for a directory object, so you can study his algorithm if you like. Of
course, it uses some commands that aren't available on the 48 series. As posted, it's assumed that library 256 is attached.

Regards,
James

#7

First, as was already pointed out, the OP made a typo--the fraction given by the HP48G is 238010/1009891.

Second, I suspect the OP was being a little facetious. The result is of course "correct", but only correct within the displayed twelve digits of the calc. Both Maple and Mathematica give exactly the same 100 digit approximation to the fraction:

.2356789000000990205873703201632651444561838851915701793559899038609117221561534858712474910658675045

I don't know what algorithm the calculator uses in the calculation of -> Q, but the user should beware that the result approximates the first twelve digits of what may actually be a slightly different decimal fraction. It is correct, yes, but only within the 12 digit confines of the calculator.

Les


#8

While were at it can someone help me find the analogous function to -> Q on the HP49G+? Not having much luck with the notoriously difficult PDF manual.

best,

Les


#9

Well, you can always do ALPHA RightShift -> (over the 0 key), Q.

Or you could press CAT, RightShift CursorDown, and then LeftShift CursorUp a few times until you find ->Q.

Or LeftShift CONVERT (over the 6 key), REWRI, NXT, ->Q.

In general, I find that if I know which command I want and its correct spelling, it's easiest to just type it in.

In the 49g+ AUR manual, see pages 3-135 and I-17.

Note that the result depends on the display mode.

Regards,
James

Edited: 18 Sept 2006, 6:26 a.m.


#10

Thank you so much. I don't have the AUR and it seems the function is not documented in the standard User's Guide that ships on the CD.

Les


#11

The AUR can be downloaded from http://www.hpcalc.org/details.php?id=6374.

The only change, other than the model name, for the 50g would be that flag -78 controls which port (USB or serial) "wired" communications uses.

There would be very few differences from the 49G, for that matter, although that depends on which ROM revision is used. I recommend updating the 49G to the latest available; see this thread for an example of how to do this.

Regards,
James

#12

What's also interesting in this regard is that if you set the 'Number Format' to a "fixed" mode, such as 8 places, the calculation of the result appears to ONLY use that number of digits.

So the decimal number will be diplayed as entered except that it will have a trailing zero if you push it into the stack, but the resulting fraction will be DIFFERENT.

At 8 decimal places calculated precision, .2356789 ~ 4900/20791.

At 9 decimal places calculated precision, .2356789 ~ 23311/98910.

Deep down, computers may always be inherently dumb, but they are EXCELLENT workers!

#13

Hi, Hal:

Hal posted:

"But the (amazing!) 48G found a non-integer common factor of
9.90205873703, which reduced 2356789/10000000 to 238010/1009791. This must be one sophisticated algorithm to do that converting decimals to fractions"

    Nothing of the sort, and certainly no "non-integer common factors" in sight. This is usually done by continued fractions.

    As part of one of my S&SMC challenges (#13, to be precise), I gave this simple routine, DEC2FRC, which does exactly that, and further allows
    extra control of the desired precision. As it is so short and perfectly understandable (it uses nothing but basic arithmetic functions, INT is "integer part", "FP" is fractional part, "INF" is "infinity" i.e., 1E99 or so), you'll be able to easily understand it and port it to any desired machine or language.

    The listing, description, and examples follows. Best regards from V.

    ---------

    Here's a 5-line subprogram I wrote for the occasion which will do the conversion:

       100 SUB DEC2FRC(X,N,D,W) @ IF X=0 THEN N=0 @ D=1 @ END
    110 U=0 @ V=1 @ N=1 @ D=0 @ Y=INF @ Z=ABS(X) @ F=SGN(X) @ X=Z @ W=ABS(W)
    120 C=INT(X) @ IF FP(X)=0 THEN N=N*F @ END ELSE X=1/FP(X) @ S=N @ T=D
    130 N=N*C+U @ U=S @ D=D*C+V @ V=T @ R=N/D @ IF ABS(R/Z-1)<W THEN N=N*F @ END
    140 IF R=Y OR MAX(N,D)>1.E+12 THEN N=U*F @ D=V @ END ELSE Y=R @ GOTO 120
    This subprogram is based in the continued fraction expansion of the given decimal
    number, and it simply computes the subsequent approximants till the tolerance
    is met, returning the last one.

    To use it, you simply call DEC2FRC, passing it the following parameters:

        X: passed by value,     is the decimal number to convert to fractional form
    N: passed by reference, is the variable where the numerator will be returned
    D: passed by reference, is the variable where the denominator will be returned
    W: passed by value, is a tolerance value which lets you control the
    relative error of the fraction so you can get smaller
    fractions which still result in acceptable errors;
    specifying this tolerance as 0 will return the most
    precise fraction (smallest error) found
    For example, let's convert -PI to fractional form calling DEC2FRC from
    the command line:
       >CALL DEC2FRC(-PI,N,D,0) @ N,D,N/D,PI

    -1146408 364913 -3.14159265359 3.14159265359

    so our fraction is -1146408/364913, which agrees with PI to 12 digits.
    Let's suppose that we don't want so close an approximation, but prefer
    instead a simpler fraction. We'll call DEC2FRC again, but this time
    we'll specify a tolerance of 0.00001 for the maximum relative error:
       >CALL DEC2FRC(-PI,N,D,1E-5) @ N,D,N/D,PI

    -355 113 -3.14159292035 3.14159265359

    and so this time our fraction is -355/113, which agrees with -PI to nearly
    8 digits, far better than our tolerance would have us believe. So our subprogram
    does work, and you can freely use it in your own programs which require fraction
    output.

    ---------------

Best regards from V.


Edited: 18 Sept 2006, 6:02 a.m.


#14

Thank you all for your responces, and my appologies for the typo in the original post.
Thanks especially to you, Valentin for your detailed responce, and your sub-program. I'm going to try porting the program to RPN...if I have any trouble I'll get back to you.
Thanks again and best regards, Hal


#15

Hi, Hal:

Hal posted:

"Thanks especially to you, Valentin for your detailed responce, and your sub-program. I'm going to try porting the program to RPN...if I have any trouble I'll get back
to you."

    You're welcome. Just a few hints:

        100 SUB DEC2FRC(X,N,D,W) @ IF X=0 THEN N=0 @ D=1 @ END

    X and W are input parameters, N and D are output
    parameters. In RPN they are taken-from/left-in the
    stack at the beginning/end, respectively, or they can
    be taken to and from registers.

    All "END" instructions mean the subprogram returns.

    110 U=0 @ V=1 @ N=1 @ D=0 @ Y=INF @ Z=ABS(X) @ F=SGN(X) @ X=Z @ W=ABS(W)

    @ is used simply to separate statements on the line,
    irrelevant in RPN where each statement goes on
    its own step or line

    INF should be 1E99 or 1E499, depending on your RPN model

    ABS is "absolute value"

    SGN is "sign" (0 for X=0, 1 for X>0, -1 for X<0)

    120 C=INT(X) @ IF FP(X)=0 THEN N=N*F @ END ELSE X=1/FP(X) @ S=N @ T=D

    INT is "integer part"

    FP is "fractional part"

    130 N=N*C+U @ U=S @ D=D*C+V @ V=T @ R=N/D @ IF ABS(R/Z-1)<W THEN N=N*F @ END

    nothing new

    140 IF R=Y OR MAX(N,D)>1.E+12 THEN N=U*F @ D=V @ END ELSE Y=R @ GOTO 120

    1.E+12 must be either 1E12 or 1E10 depending on your model

    MAX(X,Y) is "maximum", returns X if X>=Y, else returns Y

    I hope this helps.

Best regards from V.

Possibly Related Threads...
Thread Author Replies Views Last Post
  HP PRIME : strange behavior when trying user key capability Damien 12 1,265 11-03-2013, 11:02 AM
Last Post: Joe Horn
  HP Prime program: rounding to a fraction Patrice 3 509 10-31-2013, 06:16 AM
Last Post: Joe Horn
  HP15c continued fraction for Ln(Gamma) Tom Grydeland 0 316 09-30-2013, 05:48 AM
Last Post: Tom Grydeland
  HP-15C LE, wow! New owner euphoria. Sasu Mattila 16 1,449 09-07-2012, 06:55 PM
Last Post: lars Bergström
  Truncating to Integer & Fraction--HP-35A/21A Matt Agajanian 11 911 03-31-2012, 10:36 PM
Last Post: Matt Agajanian
  My HP 15C LE has just arrived! Wow! Antonio Maschio (Italy) 16 1,089 01-29-2012, 03:27 PM
Last Post: Massimo Gnerucci (Italy)
  hp12C capability Todd Israels 8 741 10-14-2011, 12:20 AM
Last Post: Todd Israels
  Message output capability of HP35s? John H (UK) 16 1,129 04-13-2010, 06:35 AM
Last Post: John H (UK)
  HP 48gII displays division as a fraction Peter Klein 5 543 04-10-2010, 05:32 PM
Last Post: Peter Klein
  Reduce fraction on HP50G olivier scalbert 2 324 03-12-2010, 09:22 PM
Last Post: Glenn Shields

Forum Jump: