HP Forums

Full Version: [41/42/..] Micro-challenge: order X and Y by magnitude
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
Pretty good ;-)
now try and shave off two more bytes ;-)

Werner
(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
(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
(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}
(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
(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
(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 ??
(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
indeed
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
(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?
(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
(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
Reference URL's