fraction program hp 28s? - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: General Forum (/forum-4.html) +--- Thread: fraction program hp 28s? (/thread-21925.html) |
fraction program hp 28s? - Rafa - 06-21-2024 12:24 PM hi, i tried to key in this fraction program for hp 28s but i keep getting a syntax error. Can someone help me figure out what am i doing wrong, and perhaps share a working fraction program if the one i'm trying is incorrect? here is the one i found: FRAC: << EVAL 'A' STO A ABS 'C' STO 1 'B' STO FLUP B IP 'B' STO "'" A B * .5 + FLOOR ->STR + "/" + B ->STR + STR-> >> FLUP: << DO 'B' C STO/ C INV FP 'C' STO UNTIL C .0001 <= END >> thanks in advance! RE: fraction program hp 28s? - Gerson W. Barbosa - 06-21-2024 10:51 PM (06-21-2024 12:24 PM)Rafa Wrote: FRAC: << EVAL 'A' STO A ABS No problem here. 2.34 FRAC -> '117/50' In order to minimize typing errors try to use the menus. For example, instead of [SHIFT] [U] [S] [T] [R] use [SHIFT] [D] [INS] to enter ->STR "'" is a single quote between double quotes Also, make sure you enter STO/ rather than STO /, with a space in between. Sorry if some or all suggestions sound obvious to you. Edited to fix a typo, ironically. ('117/50', not '117.50') RE: fraction program hp 28s? - C.Ret - 06-22-2024 08:51 PM Bonjour, These two programs awakened my curiosity. In fact, I noticed three points there which seemed unusual to me. Both use global variables for each calculation they perform as well as to exchange their parameters. This is a bit curious for RPL code where generally it is the stack that is used for these tasks. But perhaps this comes from the fact that these are adaptations of code from other machines. I am thinking in particular of semi-algebraic machines like the HP-35 or HP-33s. But maybe I'm wrong. Second, it's been a long time since I've seen RPL code that uses in-memory arithmetic (here an STO/ instruction). And it is especially the EVAL instruction placed at the very beginning of the code that I find a little surprising; but this is a point of view that I believe is very personal and which is linked to my specific way of using an HP-28S. In fact this is no problem at all. FRAC: « EVAL 'A' STO A ABS 'C' STO 1 'B' STO FLUP B IP 'B' STO "'" A B * .5 + FLOOR →STR + "/" + B ->STR + STR→ » FLUP: « DO 'B' C STO/ C INV FP 'C' STO UNTIL C .0001 ≤ END » The algorithm used is interesting and a little different from those I use for this task. So I had fun trying it as is on my machine. To try it. Like GERSON, I found no problems and it works great as written. Then I re-wrote it in my own way using no memory registers, just using the stack as I have been accustomed to for all these years (since 1991). I put my commented version below. I have grouped the two programs into one for convenience to simplify the presentation. My version no longer uses registers A B and C (only the stack) and also does not use an intermediate character string to construct the algebraic fraction but the EXSUB instructions from the menu (Algebra). « →NUM 1 OVER ABS %% Put numeric initial A B C values in the stack (in this order) DO SWAP OVER / %% ⇔ 'B' C STO/ SWAP INV FP %% ⇔ C INV FP 'C' STO UNTIL DUP .0001 ≤ END DROP IP %% ⇔ B IP 'B' STO SWAP OVER * .5 + FLOOR %% ⇔ A*B rounded to the nearest integer 'x/y' 1 ROT EXSUB %% set numerator (ie. rounded A.B) 3 ROT EXSUB » %% set denominator (ie. B) Rereading my code, I realize that it is much more difficult to see the algorithm and to understand what it does. You really need a decryptor. This is the major disadvantage of RPL. The idea of using global variables might not be a bad idea. Like sometimes the use of local variable which avoids all these meaningless SWAP ROT and ROLL. But that’s the very nature of RrPL (Rot’en roll Programming Language)! Good weekend to you all. RE: fraction program hp 28s? - Peet - 06-22-2024 10:44 PM It seems fractures were a thing on the 28s back then, I found this in my old notes from the late 1980's: Bruchrechnen: GGT (größter gemeinsamer Teiler) << DO DUP 3 ROLLD MOD DUP UNTIL 0 == END DROP >> KGV (kleinstes gemeinsames Vielfaches) << DUP2 * ROT ROT GGT / >> STATUS (setzt Standartflageinstellung, 4 FIX usw.) e.g. Flags {31,35-42,48,49,52,55,59,63} << #4920609187307716608d STOF >> D->BR (Dezimalzahl als Bruch anzeigen) << STD DUP DUP IP ->STR " " + SWAP FP 1 DUP2 DO DUP 3 ROLLD MOD UNTIL DUP ,00001 < END DROP 0 FIX DUP 3 ROLLD / RND 3 ROLLD / RND STD ->STR "/" + SWAP ->STR + + CLLCD 2 DISP STATUS >> Note: it was important to use the correct decimal separator (./,) according to flag 48 settings. RE: fraction program hp 28s? - Rafa - 06-23-2024 06:08 PM (06-22-2024 08:51 PM)C.Ret Wrote: Rereading my code, I realize that it is much more difficult to see the algorithm and to understand what it does. You really need a decryptor. This is the major disadvantage of RPL. The idea of using global variables might not be a bad idea. Like sometimes the use of local variable which avoids all these meaningless SWAP ROT and ROLL. Many thanks, i tried your program and it works perfectly! i intend to look at it again sometime to try to understand how does it work. RE: fraction program hp 28s? - Rafa - 06-23-2024 06:43 PM is it possible to write a program that gives the improper fraction? And do you also happen to have a prime factorisation program for the 28s? cheers! RE: fraction program hp 28s? - Rafa - 06-23-2024 06:52 PM (06-22-2024 10:44 PM)Peet Wrote: Note: it was important to use the correct decimal separator (./,) according to flag 48 settings. Danke sehr! i saved the programs for kgV and ggT. The fraction program has a tiny quirk in the way it shows result. for example a 12,5 coms back as 12,525/2 instead of just the fraction 25/2. the reiteration of the decimal number in the result is not a big deal but that there's no separation between it and the fraction result don't look too pretty. maybe i entered the program wrongly? RE: fraction program hp 28s? - Gerson W. Barbosa - 06-24-2024 03:03 AM (06-23-2024 06:43 PM)Rafa Wrote: And do you also happen to have a prime factorisation program for the 28s? This was written originally for the HP-28S. Hopefully no 48G-only instructions. Code:
Might be slow, depending on the factors. Also rather long. 1987587504 FPRM -> '2^4*3^3*7*17*23*41^2' RE: fraction program hp 28s? - C.Ret - 06-24-2024 07:39 PM (06-23-2024 06:08 PM)Rafa Wrote: Many thanks, i tried your program and it works perfectly! i intend to look at it again sometime to try to understand how does it work. You're welcome ! My code does exactly the same thing as the original code you post but at each step, the values A B and C are not put back into memory. They are simply placed in the stack. The misfortune is that they do not occupy the same level throughout the process. On the contrary, they are moved to be processed or are shifted under the effect of other treatments. To see things more clearly, we need to draw a diagram for each instruction showing the contents and evolution of the stack. This requires a little work, but if necessary, I can prepare one with, for example, the value 2.34 (06-24-2024 03:03 AM)Gerson W. Barbosa Wrote: Might be slow, depending on the factors. Also rather long. I found in my archives the program that I used for the prime factor decomposition. Like your program it is sometimes a little slow; although it depends a lot on the number to be broken down. But clearly, it is much shorter. Note that using no string's instructions for composing the final algebraic expressions greatly shortens the length of the code and facilitates it. I give below the listing of a version of my program. Everyone is free to draw inspiration from it for their own codes, comments or asking. I must have a faster version somewhere using a wheel for the pseudo-prime generation. I can't find it anymore, it doesn't matter, it's also much longer than this version: « 'a' SWAP 2 WHILE DUP2 SQ ≥ REPEAT 'b' ROT 0 WHILE SWAP 4 PICK DUP2 MOD NOT REPEAT / SWAP 1 + END DROP 5 ROLLD ROT 4 ROLLD IF DUP THEN ^ 1 4 PICK EXSUB * ELSE DROP2 END ROT ROT 1 + END DROP * 1 1 EXSUB » Example : 45 ⇒ '1*3^2*5' 107 ⇒ '1*107' 1987587504 ⇒ '1*2^4*3^3*7*17*23*41^2' 350697021041 ⇒ '1*23^7*103' P.S.: I found a commented version but in French. If anyone is interested, I will post a translated version here. RE: fraction program hp 28s? - Rafa - 06-25-2024 01:36 PM (06-24-2024 07:39 PM)C.Ret Wrote: To see things more clearly, we need to draw a diagram for each instruction showing the contents and evolution of the stack. This requires a little work, but if necessary, I can prepare one with, for example, the value 2.34 i would very much appreciate seeing such a diagram! whenever you find the time to do it. RE: fraction program hp 28s? - C.Ret - 06-25-2024 05:27 PM As I said, you are welcome. In the diagram, 5 levels of the stack are shown. The program follows the blue ribbon from left to right. The instructions in the Do...UNTIL...END structure are superimposed, so each line corresponds to a loop. The last line is the end of the program (which I put at the bottom of the diagram to save space). The small exponents in green indicate the variables A B and C as well as the result N/D. Each step can be checked on the HP-28S; simply insert a HALT instruction at the very beginning of the code and use the SST menu command (CONTRL) to advance step by step. RE: fraction program hp 28s? - Rafa - 06-25-2024 07:46 PM (06-25-2024 05:27 PM)C.Ret Wrote: As I said, you are welcome. great work, thanks a lot! i'm gonna study it on sunday. really nicely done! RE: fraction program hp 28s? - Thomas Klemm - 06-26-2024 02:13 AM To analyse the algorithm I used the following program for the HP-42S: Code: 00 { 4-Byte Prgm } Example Using Free42: 1 ENTER 0.34 R/S y: 2.941176470588235294117647058823529 x: 0.941176470588235294117647058823529 R/S y: 3.125000000000000000000000000000001 x: 0.0625 R/S y: 50.00000000000000000000000000000002 x: 0 But it's easier to follow if the numbers are written as fractions: y: 1 x: 17/50 y: 50/17 x: 16/17 y: 50/17 * 17/16 y: 1/16 y: 50/17 * 17/16 * 16/1 x: 0/1 Now we can see that this is Euclid's algorithm to calculate the greatest common divisor in disguise. In the Y-register the intermediate values cancel leaving us with 50/1. We may also notice that the first two rounds are a somewhat convoluted way to calculate the fraction part of the number. Instead we could start with: Code: 1 OVER ABS FP Here's the program for the HP-42S: Code: 00 { 28-Byte Prgm } Example 2.34 R/S y: 117 x: 50 I didn't bother to round the result. Also I noticed that for PI I get an Out of Range error message. |