Post Reply 
Help with an algorithm for converting decimals to fractions
08-28-2014, 06:48 AM (This post was last modified: 08-28-2014 06:49 AM by Joe Horn.)
Post: #6
RE: Help with an algorithm for converting decimals to fractions
Here's a little HP 50g program that is a demo of the algorithm for converting a decimal into a fraction without storing the partial quotients of the expanded continued fraction.

Instructions:
(1) Clear the stack. Put calculator in exact mode.
(2) Place on level 1 of the stack either a decimal number, or an algebraic ratio of two integers, e.g. '123/234'.
(3) Run the program repeatedly. Stop when level 4 shows infinity.

Each iteration will display the following on the stack:

4: Current value of N. Stop when it's infinity.
3: Previous Convergent p/q in the form of { p q }
2: Current Convergent a/b in the form { a b }
1: Decimal evaluation of a/b

Warning: Program must be loaded and run in exact mode.

'ITER8' ("Iterate") by Joe Horn
Code:
%%HP: T(3)A(R)F(.);
\<<
  IF DEPTH 1. SAME
  THEN
    IF DUP TYPE 0. ==
    THEN 1.E50 * R\->I 50 ALOG /
    END PROPFRAC { 0 1 } { 1 0 }
  ELSE DROP 3. ROLL EVAL FXND SWAP OVER IDIV2 ROT SWAP / PROPFRAC 4. ROLLD OVER 4. ROLLD * ADD
  END DUP EVAL SWAP \->NUM SWAP /
\>>

<0|ΙΈ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Help with an algorithm for converting decimals to fractions - Joe Horn - 08-28-2014 06:48 AM



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