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
 Joe Horn Senior Member Posts: 1,841 Joined: Dec 2013
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-
 « Next Oldest | Next Newest »

 Messages In This Thread Help with an algorithm for converting decimals to fractions - Namir - 08-27-2014, 08:55 PM RE: Help with an algorithm for converting decimals to fractions - Paul Dale - 08-27-2014, 10:12 PM RE: Help with an algorithm for converting decimals to fractions - Thomas Klemm - 08-27-2014, 10:32 PM RE: Help with an algorithm for converting decimals to fractions - CosmicTruth - 08-28-2014, 12:51 AM RE: Help with an algorithm for converting decimals to fractions - Joe Horn - 08-28-2014, 06:03 AM 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)