Post Reply 
(15C) Arithmetic with Fractions
09-23-2018, 09:24 AM (This post was last modified: 09-27-2018 02:19 AM by Gamo.)
Post: #1
(15C) Arithmetic with Fractions
Program to add, subtract, multiply, divide, simplify to lowest term and
convert to mixed fraction.

All procedure is the same from previous post for the
(11C) Arithmetic with Fractions.

This program is the updated version from 11C that improve the computation speed by using better GCD algorithm.

(11C) Arithmetic with Fractions link page at

http://www.hpmuseum.org/forum/thread-11400.html

Here is the clips of this updated version

https://youtu.be/wRrKXyRVof8

Program: 87 program lines
Code:

LBL A    // Store Fractions
STO 4
Rv
STO 3
Rv
STO 2
Rv
STO 1
CLx
RTN
--------------------------------------------------------
LBL B    // Add or Subtract 
RCL 1
RCL 4
x
RCL 2
RCL 3
x
+
STO 5
RCL 2
RCL 4
x
STO 6
GTO 5
-----------------------------------------------------------
LBL C    // Multiply 
RCL 1
RCL 3
x
STO 5
RCL 2
RCL 4
x
STO 6
RCL 5
GTO 5
----------------------------------------------------------
LBL D    // Divide
RCL 1
RCL 4
x
STO 5
RCL 2
RCL 3
x
STO 6
RCL 5
LBL 5    // Reduce Fractions using GCD
ENTER
ENTER
CLx
+
Rv
÷
LSTx
X<>Y
INT
x
-
TEST 0   // X≠0
GTO 5
+
RCL 6
X<>Y
÷
RCL 5
LSTx
÷
RTN
--------------------------------------------------
LBL E    // Mixed Fraction
STO 0
÷
LSTx
X<>Y
ENTER
FRAC
X<>Y
INT
Rv
x
EEX
4
÷
R^
+
FIX 4
RCL 0
X<>Y
RTN

This add-on section is for "Decimal to Fraction" Program

Procedure:

Display X.xxxx.... [GSB] 1 display Numerator [X<>Y] Denominator

If Numerator is larger than Denominator Press [E] for Mixed Fraction

Example: FIX 4

e^1 = 2.7183

[GSB] 1 display 193 [X<>Y] 71

answer is 193/71

[E] display 2.0051 [X<>Y] 71

Mixed Fraction answer is 2+ 51/71

Program: Decimal to Fraction [Total 132 program steps]
Code:

LBL 1
STO 0
STO 1
0
ENTER
ENTER
1
R^
LBL 2
INT
R^
x
+
STO 2
GSB3
X<>Y
÷
RND
RCL 0
RND
X=Y
GTO 4
+
CLx
RCL 2
ENTER
ENTER
R^
RCL 1
FRAC
1/x
STO 1
GTO 2
LBL 4
RCL 2
LBL 3
ENTER
ENTER
RCL 0
x
.
5
+
INT
RTN

Gamo
Find all posts by this user
Quote this message in a reply
09-23-2018, 05:12 PM (This post was last modified: 09-23-2018 05:14 PM by Dieter.)
Post: #2
RE: (15C) Arithmetic with Fractions
(09-23-2018 09:24 AM)Gamo Wrote:  Program to add, subtract, multiply, divide, simplify to lowest term and
convert to mixed fraction.
...
This program is the updated version from 11C that improve the computation speed by using better GCD algorithm.

So you now got a decent GCD algorithm. That's great, but why does the add/subtract function still use the slow iterative method for calculating the LCM of the two denominators? That's not required as the resulting fraction is reduced anyway in the next step.

And I think there also is an error in this LCM routine:

Code:
LBL 2
+
STO 3   <=== I think this should be STO 5
RCL 2
÷
RCL 1
x
...

But why don't you replace the whole code at LBL B with...

Code:
LBL B
RCL 1
RCL 4
x
RCL 2
RCL 3
x
+
STO 6   <== numerator of result
RCL 2
RCL 4
x
STO 5   <== denominator of result
GSB 5   <== determine GCD
GSB 3   <== reduce fraction (divide by GCD)
X<>Y
RTN

You don't have to determine (very slowly...) the LCM of the two denominators as in the next step the GCD is calculated. So simply set the common denominator to den1*den2. The whole LCM iteration can be removed completety, as shown in the example.

I also think that the lines between LBL 3 and LBL 6 can be removed. At LBL 3 the GCD is in X. The lines at LBL 3 now check whether the GCD is > 1 or not. If not, the fraction is not reduced. But this makes no sense as the GCD always is 1 or greater. So you always divide by the GCD. So I think you can delete all the lines after LBL 3 up to and including LBL 6. Please correct me if I'm wrong or if my interpretation of your program is not correct.

BTW, the division routine can be removed. All you have to do is swap the second numerator and denominator. If you want to stick to the way the program is used (enter data at LBL A), all you have to do is:

Code:
LBL D
RCL 3
RCL 4
STO 3
X<>Y
STO 4
---------------
LBL C
...

Dieter
Find all posts by this user
Quote this message in a reply
09-24-2018, 02:19 AM
Post: #3
RE: (15C) Arithmetic with Fractions
Dieter

Thank You for the review and recommendation.

I have changed to all of your recommended corrections.

List of program change are

1. The whole LBL B have been changed.
2. Delete LBL 3 and keep LBL 6
3. LBL B change GSB 3 to GSB 6
4. LBL C and LBL D change GTO 3 to GTO 6

Test run this update program and computation speed is much faster than previous version and program line reduced to only 107 lines.

Thank You again

Gamo
Find all posts by this user
Quote this message in a reply
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
09-25-2018, 02:08 AM (This post was last modified: 09-25-2018 06:22 AM by Gamo.)
Post: #5
RE: (15C) Arithmetic with Fractions
Dieter

Now the program steps is only 87 lines !!!

Thank You for the corrections again and is updated.

Your GCD routine is an excellent one.

GCD and all arithmetic results is integrated "all in one" label.

By the way I try this on HP-11C with this update
The computation speed is really fast now.

One minor nitpick

X≠0? is "TEST 0" for 15C Tongue


Thank You again

Gamo
Find all posts by this user
Quote this message in a reply
09-25-2018, 06:39 AM
Post: #6
RE: (15C) Arithmetic with Fractions
(09-25-2018 02:08 AM)Gamo Wrote:  One minor nitpick

X≠0? is "TEST 0" for 15C Tongue

Oh, yes, you're right.
I have corrected this in the original listing. #-)

Dieter
Find all posts by this user
Quote this message in a reply
09-27-2018, 02:14 AM (This post was last modified: 09-27-2018 02:37 AM by Gamo.)
Post: #7
RE: (15C) Arithmetic with Fractions
Since this program is now perfect.
Why not add a "Decimal to Fraction" for a complete fraction solver.

All this can be run on HP-11C and total program steps is 132 steps.

Program add-on is updated.

Gamo
Find all posts by this user
Quote this message in a reply
02-05-2019, 04:02 AM
Post: #8
RE: (15C) Arithmetic with Fractions
Can't thank you enough, Gamo and Dieter.

My 15C thanks too!

Cheers

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




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