02-23-2020, 01:03 AM

[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)

if the RPN example has just a single first binary operation, with no successive unary operations, it's much simpler:

RPN (stack not preserved)

RPL (uses stack levels 1–4)

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)

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`

R↓ x<>y R↓

<first binary operation>

<successive unary operations>

ENTER R↓ R↓ # dup first result to Z

<second binary operation>

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`

<first binary operation>

x<>y

LASTX

<second binary operation>

RPL (uses stack levels 1–4)

Code:

`Y X DUP2`

<first binary operation>

<successive unary operations>

3 ROLLD # move first result to level 3; alt: ROT ROT

<second binary operation>

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`

<first binary operation>

LASTARG

<second binary operation>