Post Reply 
(15C) Arithmetic with Fractions
09-24-2018, 12:27 PM (This post was last modified: 09-25-2018 06:38 AM by Dieter.)
Post: #4
RE: (15C) Arithmetic with Fractions
(09-24-2018 02:19 AM)Gamo Wrote:  I have changed to all of your recommended corrections.

There is still room for improvement. ;-)

The add/subtract routine at LBL B has a final X<>Y which is not required if you store numerator and denominator in the same way as at LBL C and D. Simply swap R5 and R6:

Code:
LBL B    // Add or Subtract 
RCL 1
RCL 4
x
RCL 2
RCL 3
x
+
STO 5
RCL 2
RCL 4
x
STO 6
GSB 5
GTO 6

After this change all functions end with GSB 5 GTO 6. So you can combine LBL 5 and LBL 6: At the end of the GCD routine, remove the RTN and continue with the LBL 6 stuff directly. This means: at the end of the LBL B and C routines you replace GSB 5 GTO 6 with a simple GTO 5, and at the end of LBL D the program directly continues with LBL 5.

The GCD routine at LBL 5 does not require any registers so R0, R7 and R8 don't have to be used. Also the initial test is not required. If you keep it (X>Y? X<>Y) you may save one loop if X is greater than Y, but I removed it anyway:

Code:
LBL 5
ENTER
ENTER
CLX
+
Rv
÷
LstX
X<>Y
INT
x
-
X≠0?     // TEST 0 on the 15C
GTO 5
+        // after this addition the GCD fills the whole stack
RCL 6
X<>Y
÷
RCL 5
LstX
÷
RTN
LBL E
...

Finally, the two consecutive X<>Y at LBL E do not make any sense, so they should be removed. The final program should have about 90 lines then. ;-)

Edit: here is another listing of the GCD routine including the stack contents, so you can see how it works. One step could be saved by using a R^ command, but the way it is now it can also be used on the 12C.

Code:
LBL 5   //  b       a       ?       ?
ENTER   //  b       b       a       ?
ENTER   //  b       b       b       a
CLX     //  0       b       b       a
+       //  b       b       a       a
Rv      //  b       a       a       b
÷       //  a/b     a       b       b
LstX    //  b       a/b     a       b
X<>Y    //  a/b     b       a       b 
INT     // [a/b]    b       a       b
x       // b*[a/b]  a       b       b
-       // a mod b  b       b       b
X≠0?    // if the remainder is not zero yet
GTO 5   // repeat loop with b and (a mod b)
+       //  b       b      b      b
RTN     // else b is the desired GCD

HTH,

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(15C) Arithmetic with Fractions - Gamo - 09-23-2018, 09:24 AM
RE: (15C) Arithmetic with Fractions - Gamo - 09-24-2018, 02:19 AM
RE: (15C) Arithmetic with Fractions - Dieter - 09-24-2018 12:27 PM
RE: (15C) Arithmetic with Fractions - Gamo - 09-25-2018, 02:08 AM
RE: (15C) Arithmetic with Fractions - Gamo - 09-27-2018, 02:14 AM



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