(15C) Arithmetic with Fractions - Printable Version +- HP Forums ( https://www.hpmuseum.org/forum)+-- Forum: HP Software Libraries ( /forum-10.html)+--- Forum: General Software Library ( /forum-13.html)+--- Thread: (15C) Arithmetic with Fractions ( /thread-11446.html) |

(15C) Arithmetic with Fractions - Gamo - 09-23-2018 09:24 AM
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:
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:
Gamo RE: (15C) Arithmetic with Fractions - Dieter - 09-23-2018 05:12 PM
(09-23-2018 09:24 AM)Gamo Wrote: Program to add, subtract, multiply, divide, simplify to lowest term and 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` But why don't you replace the whole code at LBL B with... Code: `LBL B` 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` Dieter RE: (15C) Arithmetic with Fractions - Gamo - 09-24-2018 02:19 AM
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 RE: (15C) Arithmetic with Fractions - Dieter - 09-24-2018 12:27 PM
(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 ` 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` 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 ? ?` HTH, Dieter RE: (15C) Arithmetic with Fractions - Gamo - 09-25-2018 02:08 AM
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 Thank You again Gamo RE: (15C) Arithmetic with Fractions - Dieter - 09-25-2018 06:39 AM
(09-25-2018 02:08 AM)Gamo Wrote: One minor nitpick Oh, yes, you're right. I have corrected this in the original listing. #-) Dieter RE: (15C) Arithmetic with Fractions - Gamo - 09-27-2018 02:14 AM
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 RE: (15C) Arithmetic with Fractions - Jlouis - 02-05-2019 04:02 AM
Can't thank you enough, Gamo and Dieter. My 15C thanks too! Cheers JL |