Post Reply 
fraction program hp 28s?
06-21-2024, 12:24 PM (This post was last modified: 06-21-2024 12:28 PM by Rafa.)
Post: #1
fraction program hp 28s?
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!
Find all posts by this user
Quote this message in a reply
06-21-2024, 10:51 PM (This post was last modified: 06-21-2024 11:08 PM by Gerson W. Barbosa.)
Post: #2
RE: fraction program hp 28s?
(06-21-2024 12:24 PM)Rafa Wrote:  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 >>

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')
Find all posts by this user
Quote this message in a reply
06-22-2024, 08:51 PM (This post was last modified: 06-22-2024 08:54 PM by C.Ret.)
Post: #3
RE: fraction program hp 28s?
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.
Find all posts by this user
Quote this message in a reply
06-22-2024, 10:44 PM (This post was last modified: 06-22-2024 10:59 PM by Peet.)
Post: #4
RE: fraction program hp 28s?
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.

My calculators - former: CBM PR100, HP41CV, HP11C, HP28S - current: HP48G, HP35S, Prime, DM41X, DM42, HP12C
Find all posts by this user
Quote this message in a reply
06-23-2024, 06:08 PM
Post: #5
RE: fraction program hp 28s?
(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.

But that’s the very nature of RrPL (Rot’en roll Programming Language)!

Good weekend to you all.

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.
Find all posts by this user
Quote this message in a reply
06-23-2024, 06:43 PM (This post was last modified: 06-23-2024 06:56 PM by Rafa.)
Post: #6
RE: fraction program hp 28s?
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!
Find all posts by this user
Quote this message in a reply
06-23-2024, 06:52 PM (This post was last modified: 06-23-2024 06:58 PM by Rafa.)
Post: #7
RE: fraction program hp 28s?
(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?
Find all posts by this user
Quote this message in a reply
06-24-2024, 03:03 AM
Post: #8
RE: fraction program hp 28s?
(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:


DIR
  FPRM
  \<< DUP
    IF PRTST NOT
    THEN 1 SF 2 "'" ROT ROT DCMP 1 + 1 CF
      WHILE OVER 1 \=/
      REPEAT DCMP 2 +
      END DROP2 DUP SIZE 1 - 1 SWAP SUB "'" + STR\->
    END
  \>>
  DCMP
  \<< DUP2 MOD
    IF NOT
    THEN DUP \->STR 0
      DO 1 + 4 ROLL 4 ROLL SWAP OVER / DUP2 6 ROLLD 5 ROLLD SWAP MOD
      UNTIL
      END DUP
      IF 1 \=/
      THEN SWAP "^" + SWAP \->STR +
      ELSE DROP
      END ROT DUP
      IF PRTST
      THEN DUP 2 - 1 FS? + ROT 4 ROLL DROP
      ELSE ROT ROT
      END "*" + 4 ROLL SWAP + ROT ROT
    END
  \>>
  PRTST
  \<< DUP 2 / FP
    IF
    THEN DUP \v/ 1
      DO 2 + 3 DUPN SWAP OVER
      UNTIL < ROT ROT MOD NOT OR
      END SWAP DROP MOD NOT
    END NOT
  \>>
END

Might be slow, depending on the factors. Also rather long.

1987587504 FPRM ->

'2^4*3^3*7*17*23*41^2'
Find all posts by this user
Quote this message in a reply
06-24-2024, 07:39 PM (This post was last modified: 06-24-2024 08:18 PM by C.Ret.)
Post: #9
RE: fraction program hp 28s?
(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.
Find all posts by this user
Quote this message in a reply
06-25-2024, 01:36 PM (This post was last modified: 06-25-2024 01:36 PM by Rafa.)
Post: #10
RE: fraction program hp 28s?
(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.
Find all posts by this user
Quote this message in a reply
06-25-2024, 05:27 PM (This post was last modified: 06-25-2024 05:36 PM by C.Ret.)
Post: #11
RE: fraction program hp 28s?
As I said, you are welcome.

[Image: attachment.php?aid=13663]

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.


Attached File(s) Thumbnail(s)
   
Find all posts by this user
Quote this message in a reply
06-25-2024, 07:46 PM
Post: #12
RE: fraction program hp 28s?
(06-25-2024 05:27 PM)C.Ret Wrote:  As I said, you are welcome.

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.

great work, thanks a lot! i'm gonna study it on sunday. really nicely done!
Find all posts by this user
Quote this message in a reply
06-26-2024, 02:13 AM (This post was last modified: 06-26-2024 02:42 AM by Thomas Klemm.)
Post: #13
RE: fraction program hp 28s?
To analyse the algorithm I used the following program for the HP-42S:
Code:
00 { 4-Byte Prgm }
01 STO÷ ST Y
02 1/X
03 FP
04 END

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 }
01 1
02 RCL ST Y
03 ABS
04 FP
05▸LBL 00
06 STO÷ ST Y
07 1/X
08 FP
09 1ᴇ-4
10 X≥Y?
11 GTO 01
12 R↓
13 GTO 00
14▸LBL 01
15 R↓
16 R↓
17 ×
18 LASTX
19 END

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.
Find all posts by this user
Quote this message in a reply
Post Reply 




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