(11C) Arithmetic with Fractions
09-16-2018, 04:58 PM (This post was last modified: 09-16-2018 05:53 PM by Dieter.)
Post: #2
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: (11C) Arithmetic with Fractions
(09-15-2018 07:02 AM)Gamo Wrote:  Program to add, subtract, multiply, divide, simplify to lowest term and
convert to mixed fraction.
...

https://youtu.be/yJOskANA4gM

I watched the video. It's a really nice one, but does the calculation really take that long? I assume this is due to an ineffective GCD routine.

Then I recalled the days in the early 1980s when I got my new HP-41C. And a fraction program was one of the first things I wrote for it. These memories made me think about writing such a program once again. It has 100 lines and should run reasonably fast. The program can be this short as a/b : c/d is the same as a/b · d/c, so the multiplication and division routine are essentially the same, with an X⇄Y command being the only difference. Your program can also take advantage of this: you may remove the complete division routine. If the user can press [CHS] [B] for subtraction he may also press [X⇄Y] [C] for division. ;-)

So, for the mentioned sentimental reasons I tried a fraction program once again. I hope you don't mind me posting it here in this thread. I think it should run on the 11C, 15C, 67 and 97, maybe also on other HPs (e.g. the 34C). The code uses the syntax of the Panamatik HP67 emulator. Here X!=0? is the X≠0? test (that's TEST 0 on the 15C). Add a FIX 0 (or FIX DSP 0) after LBL 0 and LBL E if you prefer.

Code:
LBL B CHS LBL A STO 2 Rup * Rdown X<>Y STO*2 * Rup + RCL 2 GTO 0 LBL C X<>Y LBL D Rdown * Rdown * Rup LBL 0 STO 2 ABS STO 4 X<>Y STO 1 ABS STO 3 LBL 1 RCL 3 RCL 3 RCL 4 / INT RCL 4 STO 3 * - STO 4 X!=0? GTO 1 RCL 3 STO/1 STO/2 RCL 1 RCL 2 * ENTER ABS X!=0? / RCL 1 ABS * STO 1 PAUSE RCL 2 ABS STO 2 R/S LBL E STO 2 ABS STO 4 X<>Y STO 1 ABS STO 3 RCL 1 RCL 2 * STO 0 ABS X!=0? STO/0 RCL 3 RCL 3 RCL 4 / INT RCL 0 * PAUSE ABS RCL 4 * - RCL 0 * PAUSE RCL 3 RCL 0 * STO 1 RCL 4 STO 2 R/S GTO E

Usage:
Calculate a/b [+ – x ÷] c/d

a [ENTER] b   [ENTER]   c ENTER d

[B] => subtract
[C] => multiply
[D] => divide

[E] => convert to mixed fraction

The result of each operation is in Y (numerator) and X (denominator). Even after a mixed fraction has been displayed the simple fraction still is in Y and X. So chain calculations are possible.

111 [ENTER] 33  [ENTER]  7 [ENTER] 17
[A] => "706" 187
[E] => "3" "145" 187

Now subtract 1/187
1 [ENTER] 187
[B] => "705" 187

[R/S] => "3" "144" 187

Note: after a calculation you can simply press [R/S] instead of [E] to view the result as a mixed fraction. Another [R/S] repeats the output.

Multiplication example:

1001 [ENTER] 98  [ENTER]  123 [ENTER] 321
[C] => "5863" 1498
[E] => "3" "1369" 1498

Note: the program also displays negative fractions correctly.
With the last result still on the stack, multiply by (–2):

–2 [ENTER] 1
[C] => "–5863" 749
[E] => "–7" "–620" 749

The [E] key can be used independently for displaying mixed fractions.
Example:
355 [ENTER] 113
[E] => "3" "16" 113.

Note: the result of a calculation is always stored in R1 (numerator) and R2 (denominator), i.e. it can also be retrieved from there. So you can do some other calculations and finally return to the last fraction result with a simple RCL 1 RCL 2.

Finally: thank you, Gamo, for reminding me of this topic. Writing a fraction program after 35 years was a lot of fun. :-)

Dieter
 « Next Oldest | Next Newest »

 Messages In This Thread (11C) Arithmetic with Fractions - Gamo - 09-15-2018, 07:02 AM RE: (11C) Arithmetic with Fractions - Dieter - 09-16-2018 04:58 PM RE: (11C) Arithmetic with Fractions - Thomas Klemm - 09-16-2018, 09:17 PM RE: (11C) Arithmetic with Fractions - Dieter - 09-16-2018, 10:06 PM RE: (11C) Arithmetic with Fractions - Steve Simpkin - 09-17-2018, 01:46 AM RE: (11C) Arithmetic with Fractions - Thomas Klemm - 09-16-2018, 11:41 PM RE: (11C) Arithmetic with Fractions - Dieter - 09-18-2018, 06:51 PM RE: (11C) Arithmetic with Fractions - Valentin Albillo - 09-18-2018, 09:25 PM RE: (11C) Arithmetic with Fractions - Dieter - 09-18-2018, 10:38 PM RE: (11C) Arithmetic with Fractions - Massimo Gnerucci - 09-19-2018, 07:45 AM RE: (11C) Arithmetic with Fractions - Albert Chan - 09-17-2018, 01:12 AM RE: (11C) Arithmetic with Fractions - Albert Chan - 09-17-2018, 04:04 PM RE: (11C) Arithmetic with Fractions - Gamo - 09-17-2018, 03:47 AM RE: (11C) Arithmetic with Fractions - Carsen - 09-17-2018, 08:14 AM RE: (11C) Arithmetic with Fractions - Gamo - 09-17-2018, 09:40 AM RE: (11C) Arithmetic with Fractions - Albert Chan - 09-17-2018, 06:44 PM RE: (11C) Arithmetic with Fractions - Thomas Klemm - 09-18-2018, 07:19 PM RE: (11C) Arithmetic with Fractions - Thomas Klemm - 09-18-2018, 07:29 PM RE: (11C) Arithmetic with Fractions - Gamo - 09-22-2018, 06:33 AM RE: (11C) Arithmetic with Fractions - Dieter - 09-24-2018, 12:59 PM RE: (11C) Arithmetic with Fractions - Dieter - 09-24-2018, 05:29 PM

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