Post Reply 
Programing advice please (50g)
06-30-2023, 03:51 PM
Post: #1
Programing advice please (50g)
I need a method in RPN mode to SWAP x and y registers if x>y. I prefer full RPN without IF THEN ELSE END structures. I try « DUP2 > SWAP IFT » but it always ended with one element on the stack. Any advice?

Thank you.
Visit this user's website Find all posts by this user
Quote this message in a reply
06-30-2023, 04:11 PM
Post: #2
RE: Programing advice please (50g)
The SWAP needs to be in program quotes << SWAP >> so it gets put on the stack for IFT to execute. Without them it gets executed before IFT is reached.

Nigel (UK)
Find all posts by this user
Quote this message in a reply
06-30-2023, 04:25 PM
Post: #3
RE: Programing advice please (50g)
Hi,


You can try one of these solutions too:

<< DUP2 > {SWAP} IFT >>

<< DUP2 > :: SWAP IFT >>

Bruno
Sanyo CZ-0124 ⋅ TI-57 ⋅ HP-15C ⋅ Canon X-07 + XP-140 Monitor Card ⋅ HP-41CX ⋅ HP-28S ⋅ HP-50G ⋅ HP-50G
Find all posts by this user
Quote this message in a reply
06-30-2023, 04:39 PM
Post: #4
RE: Programing advice please (50g)
Thank you all, it worked but I struggle to understand it Smile
Visit this user's website Find all posts by this user
Quote this message in a reply
06-30-2023, 04:46 PM
Post: #5
RE: Programing advice please (50g)
To save a few bytes you could avoid the use of IFT altogether:
Code:
\<<
    DUP2
    MIN
    UNROT
    MAX
\>>
Find all posts by this user
Quote this message in a reply
06-30-2023, 07:24 PM (This post was last modified: 07-01-2023 06:45 PM by FLISZT.)
Post: #6
RE: Programing advice please (50g)
(06-30-2023 04:39 PM)mchris Wrote:  Thank you all, it worked but I struggle to understand it Smile

The regular syntax - as mentioned by Nigel (UK) - is:
Code:
<< TEST << … … >> IFT >>
            ↑
    if the test is true
or
Code:
<< TEST << … >> << … >> IFTE >>
           ↑       ↑
the test is true   the test is false

But instead of instructions inside chevrons, you can use lists (at least with hp-48GX … 50G / not with hp-28):
Code:
<< TEST { … … } { … … } IFTE >>
… generally, this syntax is faster than the one with chevrons (and lighter if my memory serves me right).

It's also possible to use a TAG :: before ONE instruction.
For example:
<< DUP2 > :: SWAP :: DROP2 IFTE >>

Last but not least example:
<< 1 == << 9 'l' STO+ >> IFT >>
… add 9 to the register l, if the value on the stack is equal to 1.

But… writing 'l' is impossible inside a list.
So, if you want use lists, the only solution I know of is that one (with a TAG):
<< 1 == { 0 ::l STO+ } IFT >>

I hope I'm clear enough…
Anyway, DavidM's solution to your question is the best. Smile

Edit: typo

Bruno
Sanyo CZ-0124 ⋅ TI-57 ⋅ HP-15C ⋅ Canon X-07 + XP-140 Monitor Card ⋅ HP-41CX ⋅ HP-28S ⋅ HP-50G ⋅ HP-50G
Find all posts by this user
Quote this message in a reply
06-30-2023, 08:41 PM (This post was last modified: 07-01-2023 04:25 AM by Joe Horn.)
Post: #7
RE: Programing advice please (50g)
A 3-step solution: << MAX LASTARG MIN >>. This assumes the default setting for flag -55 (Save Last Args) which almost everybody leaves that way.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
06-30-2023, 09:38 PM
Post: #8
RE: Programing advice please (50g)
(06-30-2023 04:39 PM)mchris Wrote:  ...it worked but I struggle to understand it Smile

This is the fundamental nature of RPL when learning it.... Smile

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
07-01-2023, 12:48 AM
Post: #9
RE: Programing advice please (50g)
To get the result,
in the following order
minVal (first place) maxVal (second place ,
in Joe Horn program,

invert the command MAX and MIN as follows:

<< MIN LASTARG MAX >>
Find all posts by this user
Quote this message in a reply
Post Reply 




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