Perform two successive binary operations on same input

02232020, 01:03 AM
(This post was last modified: 02232020 07:41 AM by cdmackay.)
Post: #1




Perform two successive binary operations on same input
[this is perhaps a little simplistic to be of interest, but…]
I often want to perform two successive binary operations (i.e. operations with two operands/arguments) on the same input, leaving the two results in the two lower stack levels. An example might be finding the quotient and remainder (DIV/MOD) of two integers, on calculators that don't have a combined operation such as e.g 41/SandMath QREM, 50g IDIV2, Prime iquorem(). That often requires the first operation to be a binary operation followed by one or more unary operations, e.g. ÷ IP, followed by the second binary operation, e.g. MOD. My clumsy efforts are as follows; I've not yet considered using STO/RCL methods, which might be useful if stack preserving was a requirement. This is for interactive use; of course a program could be simpler. I'd be interested in hearing of quicker/easier/more elegant methods. RPN (stack not preserved) Code: Y ENTER ENTER X ENTER if the RPN example has just a single first binary operation, with no successive unary operations, it's much simpler: RPN (stack not preserved) Code: Y ENTER ENTER X RPL (uses stack levels 1–4) Code: Y X DUP2 if the RPL example has just a single first binary operation, with no successive unary operations, it's much simpler: RPL (uses stack levels 1–4) Code: Y X Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 17B/II/II+ 28S 42S/DM42 32SII 48GX 50g 35s WP34S PrimeG2 WP43S/pilot Casio, Rockwell 18R 

02232020, 01:53 AM
Post: #2




RE: Perform two successive binary operations on same input
arg! I stupidly forgot that RPL LASTARG will restore both args, which makes the RPL case trivial; ah well.
Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 17B/II/II+ 28S 42S/DM42 32SII 48GX 50g 35s WP34S PrimeG2 WP43S/pilot Casio, Rockwell 18R 

02232020, 01:55 AM
(This post was last modified: 02232020 02:02 AM by cdmackay.)
Post: #3




RE: Perform two successive binary operations on same input
Although, LASTARG will only work if there's just one operation.
e.g. in my example, on many calcs the quotient requires two operations: ÷ IP, and so LASTARG won't restore the original Y X. The RPL example in the first post will allow successive unary operations after the first binary operation. Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 17B/II/II+ 28S 42S/DM42 32SII 48GX 50g 35s WP34S PrimeG2 WP43S/pilot Casio, Rockwell 18R 

02232020, 07:34 AM
Post: #4




RE: Perform two successive binary operations on same input
and there's a similar use of LASTX that can be used to simplify the RPN case, again if there's only the one binary operation. I'll edit that in too.
Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 17B/II/II+ 28S 42S/DM42 32SII 48GX 50g 35s WP34S PrimeG2 WP43S/pilot Casio, Rockwell 18R 

02232020, 09:39 AM
(This post was last modified: 02232020 09:44 AM by Csaba Tizedes.)
Post: #5




RE: Perform two successive binary operations on same input
(02232020 01:03 AM)cdmackay Wrote: I often want to perform two successive binary operations... Sure! Check the attachment, there is my opinion about "TrickyGeniusProgrammingStyle"  just as a practical engineer point of view... So, with STO and RCL, with one flag and with lots of labels: LBL A and LBL B: the two binary operation LBL S: store the stack status LBL T: store previous result LBL R: restore the stack status variable R: previous result flag 0: set, if the stack X is the previous result for the first running clear the flag 0 Code:
Csaba https://www.hpmuseum.org/forum/attachment.php?aid=8104 

02232020, 04:10 PM
Post: #6




RE: Perform two successive binary operations on same input
On the hp50g, I've done this several way depending on the desired operations. Sometimes I do things like (assuming two operands on the stack):
MAX LASTARG MIN In other cases: DUP2 "ops leaving single result" UNROT I often need things like IDIV2 but that's so slow that IQUOT (or even / FLOOR) and MOD with LASTART or DUP2 are much faster. 

02232020, 05:23 PM
Post: #7




RE: Perform two successive binary operations on same input
hah! very good, I agree
thanks for all the suggestions, will study. forgot about UNROT too. I've been mostly doing this interactive, so hadn't even thought about performance e.g. IDIV2, good point thanks! Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 17B/II/II+ 28S 42S/DM42 32SII 48GX 50g 35s WP34S PrimeG2 WP43S/pilot Casio, Rockwell 18R 

« Next Oldest  Next Newest »

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