Post Reply 
[41/42/..] Micro-challenge: order X and Y by magnitude
03-08-2016, 01:53 PM
Post: #1
[41/42/..] Micro-challenge: order X and Y by magnitude
Really very small challenge. Given two numbers in X and Y, order them so that the smallest in magnitude is in X. To make it a bit more of a challenge, you can only use the stack, and must leave L, Z and T untouched.
Basically, perform a X<>Y when abs(X) > abs(Y).
When the magnitudes are equal, you have a choice.
BTW should you need a test
X>=a?
where a=0 or Y, present on the 42S but not on the 41, do
X<a?
X=a?
instead, in succession.

Good luck, Werner
Find all posts by this user
Quote this message in a reply
03-08-2016, 02:53 PM
Post: #2
RE: [41/42/..] Micro-challenge: order X and Y by magnitude
This appears to work based on a couple quick tests.

Code:
LBL "MAGORD"
X>Y?
X<>Y
CHS
X>Y?
GTO 01
CHS
RTN
LBL 01
CHS
X<>Y
RTN
END

Tests (y, x -> y, x):
8, 4 -> 8, 4
4, 8 -> 8, 4
-8, 4 -> -8, 4
4, -8 -> -8, 4
-8, -4 -> -8, -4
-4, -8 -> -8, -4
-4, 8 -> 8, -4
8, -4 -> 8, -4
8, -8 -> 8, -8
-8, 8 -> 8, -8
Visit this user's website Find all posts by this user
Quote this message in a reply
03-08-2016, 03:50 PM
Post: #3
RE: [41/42/..] Micro-challenge: order X and Y by magnitude
Pretty good ;-)
now try and shave off two more bytes ;-)

Werner
Find all posts by this user
Quote this message in a reply
03-08-2016, 04:03 PM
Post: #4
RE: [41/42/..] Micro-challenge: order X and Y by magnitude
(03-08-2016 03:50 PM)Werner Wrote:  Pretty good ;-)
now try and shave off two more bytes ;-)

Werner

How about three?

LBL "MAGORD" -> LBL "MAG"

Big Grin
Visit this user's website Find all posts by this user
Quote this message in a reply
03-08-2016, 07:33 PM
Post: #5
RE: [41/42/..] Micro-challenge: order X and Y by magnitude
(03-08-2016 01:53 PM)Werner Wrote:  Really very small challenge. Given two numbers in X and Y, order them so that the smallest in magnitude is in X.

That's why I always wanted a MAXABS and MINABS function on the WP34s. ;-)

Seriously: I think these would be two very useful functions. Maybe on the 43s ?-)

Dieter
Find all posts by this user
Quote this message in a reply
03-08-2016, 08:39 PM
Post: #6
RE: [41/42/..] Micro-challenge: order X and Y by magnitude
(03-08-2016 04:03 PM)Dave Britten Wrote:  
(03-08-2016 03:50 PM)Werner Wrote:  Pretty good ;-)
now try and shave off two more bytes ;-)

Werner

How about three?

LBL "MAGORD" -> LBL "MAG"

Big Grin

I came up with an exact copy of your program, except for the missing second RTN and the one-character LBL. { 16-byte Prgm}
Find all posts by this user
Quote this message in a reply
03-08-2016, 09:18 PM
Post: #7
RE: [41/42/..] Micro-challenge: order X and Y by magnitude
(03-08-2016 07:33 PM)Dieter Wrote:  That's why I always wanted a MAXABS and MINABS function on the WP34s. ;-)

There are lots of useful extra functions that could have been added Smile

Would comparison commands be more useful? i.e. |x|=|y|? |x|>|y|? |x|>1? |x|<= rr extended to the complex domain as well c|x|<|y|?



Pauli
Find all posts by this user
Quote this message in a reply
03-08-2016, 09:23 PM
Post: #8
RE: [41/42/..] Micro-challenge: order X and Y by magnitude
(03-08-2016 09:18 PM)Paul Dale Wrote:  Would comparison commands be more useful? i.e. |x|=|y|? |x|>|y|? |x|>1? |x|<= rr extended to the complex domain as well c|x|<|y|?

Yes, of course. This is even more flexible and min/max functions could be implemented easily by the user.

Dieter
Find all posts by this user
Quote this message in a reply
03-08-2016, 10:31 PM
Post: #9
RE: [41/42/..] Micro-challenge: order X and Y by magnitude
(03-08-2016 07:33 PM)Dieter Wrote:  
(03-08-2016 01:53 PM)Werner Wrote:  Really very small challenge. Given two numbers in X and Y, order them so that the smallest in magnitude is in X.

That's why I always wanted a MAXABS and MINABS function on the WP34s. ;-)

Seriously: I think these would be two very useful functions. Maybe on the 43s ?-)

Dieter

??

Would a function to order the stack from hi to lo and/or vice versa be useful ??

2speed HP41CX,int2XMEM+ZEN, HPIL+DEVEL, HPIL+X/IO, I/R, 82143, 82163, 82162 -25,35,45,55,65,67,70,80
Find all posts by this user
Quote this message in a reply
03-08-2016, 11:20 PM
Post: #10
RE: [41/42/..] Micro-challenge: order X and Y by magnitude
(03-08-2016 10:31 PM)TASP Wrote:  ??
Would a function to order the stack from hi to lo and/or vice versa be useful ??

My suggestion was a function that returns the value with the minimum/maximum absolute value of X and Y. Alternatively, as Pauli said, tests commands could be implemented that compare the magnitudes of X and Y.

A dedicated stack sort command maybe is a bit, err... "special". ;-)

Dieter
Find all posts by this user
Quote this message in a reply
03-09-2016, 12:07 AM
Post: #11
RE: [41/42/..] Micro-challenge: order X and Y by magnitude
indeed

2speed HP41CX,int2XMEM+ZEN, HPIL+DEVEL, HPIL+X/IO, I/R, 82143, 82163, 82162 -25,35,45,55,65,67,70,80
Find all posts by this user
Quote this message in a reply
03-09-2016, 10:34 AM
Post: #12
RE: [41/42/..] Micro-challenge: order X and Y by magnitude
I'm not in favour of having all functions predefined - where's the fun, then? I'd rather have the means to define and include my own programs/functions and to use them exactly like the built-in ones.
In that regard, the RTN+1 function of the 34 is a stroke of genius, you can make your own |x|>|y|? function - too bad you're restricted to three characters for the name, but oh well.

But we digress.
here's my 'order X and Y in order of magnitude':

Code:
 X>Y?
 X<>Y
 +/-
 X<=Y?
 +/-
 X<=Y?
 RTN
 +/-
 X<>Y
 RTN


Cheers, Werner
Find all posts by this user
Quote this message in a reply
03-09-2016, 12:28 PM
Post: #13
RE: [41/42/..] Micro-challenge: order X and Y by magnitude
(03-09-2016 10:34 AM)Werner Wrote:  I'm not in favour of having all functions predefined - where's the fun, then? I'd rather have the means to define and include my own programs/functions and to use them exactly like the built-in ones.
In that regard, the RTN+1 function of the 34 is a stroke of genius, you can make your own |x|>|y|? function - too bad you're restricted to three characters for the name, but oh well.

But we digress.
here's my 'order X and Y in order of magnitude':

Code:
 X>Y?
 X<>Y
 +/-
 X<=Y?
 +/-
 X<=Y?
 RTN
 +/-
 X<>Y
 RTN


Cheers, Werner

Yeah, I really wish the HPs had RTN+1. On the 41 and 42, you can at least fake it somewhat if you designate a flag as "execute the next instruction" and adhere to that calling convention, e.g. "XEQ ABSX<Y?, FS?C 09, RTN, do something else..." Would also love to have SKIP and BACK. Here's hoping that, with Free42 being GPL, the DM-42L firmware will be open source as well. Assuming SwissMicros doesn't negotiate a different licensing scheme with Thomas, I would assume it would have to be GPL as well.

Also, I haven't had a chance to try your program yet, but the odd number of CHS instructions being run would leave one of the values inverted on the stack, wouldn't it?
Visit this user's website Find all posts by this user
Quote this message in a reply
03-09-2016, 12:48 PM
Post: #14
RE: [41/42/..] Micro-challenge: order X and Y by magnitude
(03-09-2016 12:28 PM)Dave Britten Wrote:  Also, I haven't had a chance to try your program yet, but the odd number of CHS instructions being run would leave one of the values inverted on the stack, wouldn't it?

No, it won't ;-))

Werner
Find all posts by this user
Quote this message in a reply
03-09-2016, 12:59 PM
Post: #15
RE: [41/42/..] Micro-challenge: order X and Y by magnitude
(03-09-2016 12:48 PM)Werner Wrote:  
(03-09-2016 12:28 PM)Dave Britten Wrote:  Also, I haven't had a chance to try your program yet, but the odd number of CHS instructions being run would leave one of the values inverted on the stack, wouldn't it?

No, it won't ;-))

Werner

Oh right, missed that one of them is hiding after a conditional. That's what I get for hastily reading code 5 minutes before leaving for work. Wink
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




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