(11C) Arithmetic with Fractions

09152018, 07:02 AM
(This post was last modified: 09152018 11:11 AM by Gamo.)
Post: #1




(11C) Arithmetic with Fractions
Program to add, subtract, multiply, divide, simplify to lowest term and
convert to mixed fraction. In the form of a/b [+  x ÷] c/d = e/f If f ˂ e then get the result for Mixed Fraction of g+ h/f Procedure: a [ENTER] b [ENTER] c [ENTER] d [A] [A] Store Fractions [B] For Add or Subtract [C] For Multiply [D] For Divide [E] For Mixed Fraction Each [B] [C] [D] give answer display of e [X<>Y] f as fraction of e/f If f ˂ e press [E] give answer display of g.000h [X<>Y] f of g+ h/f  Example: 111/33 + 7/17 111 [ENTER] 33 [ENTER] 7 [ENTER] 17 [A] display 0.0000 [B] display 706 [X<>Y] 187 Answer is 706/187 // Answer f ˂ e [E] display 3.0145 [X<>Y] 187 Answer of Mixed Fraction is 3+ 145/187  Example: 1001/98 x 123/321 1001 [ENTER] 98 [ENTER] 123 [ENTER] 321 [A] display 0.0000 [C] display 5863 [X<>Y] 1498 Answer is 5863/1498 // Answer f < e [E] display 3.1369 [X<>Y] 1498 Answer of Mixed Fraction is 3+ 1369/1498  Program: [Total of 135 program steps] Code:
https://youtu.be/yJOskANA4gM Gamo 

09162018, 04:58 PM
(This post was last modified: 09162018 05:53 PM by Dieter.)
Post: #2




RE: (11C) Arithmetic with Fractions
(09152018 07:02 AM)Gamo Wrote: Program to add, subtract, multiply, divide, simplify to lowest term and 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 HP41C. 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 Usage: Calculate a/b [+ – x ÷] c/d a [ENTER] b [ENTER] c ENTER d [A] => add [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 

09162018, 09:17 PM
Post: #3




RE: (11C) Arithmetic with Fractions
You may want to try the example given in RATIONAL ARITHMETIC IN FLOATINGPOINT by W. Kahan for the HP67/97:
\(\frac{123,456,799}{123,456}\frac{988,297,396}{988,291}=\frac{31}{189751872}\) Cheers Thomas 

09162018, 10:06 PM
Post: #4




RE: (11C) Arithmetic with Fractions
(09162018 09:17 PM)Thomas Klemm Wrote: You may want to try the example given in RATIONAL ARITHMETIC IN FLOATINGPOINT by W. Kahan for the HP67/97: I'm afraid the very basic methods implemented in this program will not be able to handle this. The program first calculates the product of the two denominators which in this case has 12 significant digits and thus more than the 11/15/67/97 can handle. A better algorithm would first determine the LCM of the denominators. Dieter 

09162018, 11:41 PM
(This post was last modified: 09182018 07:07 PM by Thomas Klemm.)
Post: #5




RE: (11C) Arithmetic with Fractions
(09162018 10:06 PM)Dieter Wrote: A better algorithm would first determine the LCM of the denominators. But even then the \(\begin{align*} \frac{123,456,799}{123,456}\frac{988,297,396}{988,291} &= \frac{123,456,799\times1,537}{123,456\times1,537}\frac{988,297,396\times192}{988,291\times192} \\ &= \frac{189,753,100,063}{189,751,872}\frac{189,753,100,032}{189,751,872} \\ &= \frac{189,753,100,063189,753,100,032}{189,751,872} \\ &= \frac{31}{189751872} \end{align*} \) W. Kahan explains in the linked paper how to avoid this. Kind regards Thomas 

09172018, 01:12 AM
Post: #6




RE: (11C) Arithmetic with Fractions
Hi, Thomas Klemm,
Thanks for the link to Kahan paper. I tried it, it work perfectly. However, Kahan's example does not shown the algorithm's power. Doing the math as fixed fraction already avoid the overflow problem ... 123456799/123456  988297396/988291 = (1000 + 799/123456)  (1000 + 6396/988291) = 799/(643*192)  6396/(643*1537) = (799 * 1537  6396 * 192) / (643*192*1537) = (1228063  1228032) / 189751872 = 31 / 189751872 

09172018, 01:46 AM
Post: #7




RE: (11C) Arithmetic with Fractions
(09162018 10:06 PM)Dieter Wrote: A better algorithm would first determine the LCM of the denominators. A HP11C program to determine the LCM of the numbers in X and Y. Code: LBL A From Thomas Klemm, post #8 http://www.hpmuseum.org/forum/thread11144.html 

09172018, 03:47 AM
(This post was last modified: 09172018 05:08 AM by Gamo.)
Post: #8




RE: (11C) Arithmetic with Fractions
Thanks Dieter for the review and your program is shorter with faster computation.
Grad that this bring back your memory this memory also good to share your memory with great program. Thanks again. Thomas Klemm I try your large Fractions Subtraction on HP Prime result is 1 / 6.250.000 12C Android Emulator result is 25 / 47.437.968 (same program as 11C) Wolfram Alpha gave correct result same as stated in RATIONAL ARITHMETIC IN FLOATINGPOINT by W. Kahan Remark: Even HP Prime can't do it. Gamo 

09172018, 08:14 AM
Post: #9




RE: (11C) Arithmetic with Fractions
Hello Gamo:
I can see you put a lot of hard work into your HP11C program. Looking good. (09172018 03:47 AM)Gamo Wrote: Thomas Klemm When I do the large Fractions Subtraction on my HP Prime, I get the intended result. See the screenshot below. 

09172018, 09:40 AM
(This post was last modified: 09172018 09:41 AM by Gamo.)
Post: #10




RE: (11C) Arithmetic with Fractions
Thank You Carsen
My bad I did this calculation on HOME mode and got that result. When change to CAS mode got the correct answer. Gam0 

09172018, 04:04 PM
(This post was last modified: 09192018 05:09 PM by Albert Chan.)
Post: #11




RE: (11C) Arithmetic with Fractions
(09172018 01:12 AM)Albert Chan Wrote: Hi, Thomas Klemm, I made up a better example, to show Kahan's algorithm, where mixed fraction trick won't work: M/N = 1234567/12345648  635720/6357263 From Kahan's paper: (using HP12C, with my Mod/Gcd program) G = gcd(12345648, 6357263) = 181 b = 12345648 / 181 = 68208 d = 6357263 / 181 = 35123 a = 1234567 % 181 = 147 c = 635720 % 181 = 48 ad  bc = 147*35123  68208*48 = 1889097 = 181 * 10437 g =gcd(G, ad  bc) = 181 M = 10437 + Det({{6820, 68208}, {3512, 35123}}) = 10437  7636 = 2,801 N = G/g * b * d = 68208 * 35123 = 2,395,669,584 

09172018, 06:44 PM
Post: #12




RE: (11C) Arithmetic with Fractions
Just want to clarify, Kahan's algorithm basically mechanized below steps:
G = gcd(12345648, 6357263) = 181 M/N = 1234567/12345648  635720/6357263 = (6820 G + 147) / (68208 G)  (3512 G + 48) / (35123 G) = (35123 G *(6820 G + 147)  68208 G *(3512 G + 48)) / (68208 * 35123 * G^2) = ((6820*35123  3512*68208) G + (147*35123  68208*48)) / (68208 * 35123 * G) = (7636 G + 1889097) / (68208 * 35123 * G) = (7636 G + 10437 G) / (68208 * 35123 * G) = 2,801 / 2,395,669,584 

09182018, 06:51 PM
Post: #13




RE: (11C) Arithmetic with Fractions
(09162018 11:41 PM)Thomas Klemm Wrote: But even then the nominator would be 100 ... I just noticed this – because I make the same mistake all the time. ;) Actually I proofread all my fractionrelated posts to check if I did it again and wrote "nominator" instead of "numerator". And I wonder if this is because both of usually speak German. Or does this also happen to others with a different background? Dieter 

09182018, 07:19 PM
Post: #14




RE: (11C) Arithmetic with Fractions
Thank's for notifying me about that. It's fixed now in my post and I hope it doesn't happen anymore.
(09182018 06:51 PM)Dieter Wrote: And I wonder if this is because both of usually speak German. Or does this also happen to others with a different background? That's an interesting question. Though in German there is a related word Nominierung for English nomination the German word Zähler for English numerator is not related at all. Not sure if we could still consider it a false friend. Kind regards Thomas 

09182018, 07:29 PM
Post: #15




RE: (11C) Arithmetic with Fractions
(09182018 06:51 PM)Dieter Wrote: because I make the same mistake all the time. ;) At least I know now where I copied it from: HP17bII+ Programming tdistribution 

09182018, 09:25 PM
Post: #16




RE: (11C) Arithmetic with Fractions
.
Hi, Dieter: (09182018 06:51 PM)Dieter Wrote: And I wonder if this is because both of usually speak German. Or does this also happen to others with a different background? In Spanish it would be "numerador" and "denominador", never seen anyone using "nominador" instead of "numerador" either verbally or in print. V. . All My Articles & other Materials here: Valentin Albillo's HP Collection 

09182018, 10:38 PM
Post: #17




RE: (11C) Arithmetic with Fractions
(09182018 09:25 PM)Valentin Albillo Wrote: In Spanish it would be "numerador" and "denominador", never seen anyone using "nominador" instead of "numerador" either verbally or in print. The English word "numerator" and the Spanish "numerador" are both derived from the Latin verb "numerare" = "to count". So a "numerator" is someone or something that "counts". A "counter", if you like. And that's exactly what the German word means: "Zähler" simply is a direct translation of the Latin "numerator". Which makes the use of a word like "nominator" look even more strange. #) Dieter 

09192018, 07:45 AM
(This post was last modified: 09192018 07:47 AM by Massimo Gnerucci.)
Post: #18




RE: (11C) Arithmetic with Fractions
(09182018 10:38 PM)Dieter Wrote:(09182018 09:25 PM)Valentin Albillo Wrote: In Spanish it would be "numerador" and "denominador", never seen anyone using "nominador" instead of "numerador" either verbally or in print. And, of course, we are close to the latin root: numeratore and denominatore. Greetings, Massimo +×÷ ↔ left is right and right is wrong 

09222018, 06:33 AM
(This post was last modified: 09222018 08:53 AM by Gamo.)
Post: #19




RE: (11C) Arithmetic with Fractions
Thomas Klemm
I try your large Fractions Subtraction again. But this time on the HP12C Platinum This is the only one of the Modern Voyager Series that be able to do it correctly. Because of the internal precision of 15 digits. Update: I try to squeeze program all into the Classic 12C but can't do it because of the limited memory since my "Arithmetic with Fraction" used up 8 register and took 98 program lines when run the "Error 6" shown. With the HP12C Platinum I don't have to worry about memory capacity and successfully make it and not even use half the program lines limit of 400 lines. As Dieter recommended to change the algorithm for the GCD part of the program to speed up the computation process and I did updated to this program for HP12C Platinum it run very fast. Later I'll post the update version both for HP 12C Platinum and HP15C instead of the 11C as a new post. For a mean time here is the clip for large Fractions On HP12C Platinum https://youtu.be/G0m1EFGgihk Thank You Gamo 

09242018, 12:59 PM
Post: #20




RE: (11C) Arithmetic with Fractions
(09222018 06:33 AM)Gamo Wrote: I try to squeeze program all into the Classic 12C but can't do it because of the limited memory since my "Arithmetic with Fraction" used up 8 register and took 98 program lines when run the "Error 6" shown. I just posted a few suggestions for the 15C version which will reduce the number of steps significantly. The program only needs R1...R6, so R7 and R8 are not required. You can also free up R5 and R6 by storing the result not there but, say, in R3 and R4 which are no longer required once the result has been calculated. I am sure you can squeeze the whole program into a regular "classic" 12C. ;) Dieter 

« Next Oldest  Next Newest »

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