Sunday, February 4, 2024

Some RPN Routines: Including Maximum and Minimum of Two Numbers

Some RPN Routines:  Including Maximum and Minimum of Two Numbers



Here are some RPN routines that hopefully you find helpful.   I am assuming that the values are stored in memory registers before calculation and a general four-level stack is used (HP 11C, 12C, 15C, 42S, 32S, 32SII, DM41X, DM32, DM42, DM15, WP34, etc.).  



(a + b + c) × (a + b - c)


Assume that: 

Memory register 1 = a

Memory register 2 = b

Memory register 3 = c

(Your memory registers may have different names or labels)



If recall arithmetic is allowed:


RCL 3

RCL 1

RCL+ 2

+

LST x

RCL- 3

×


(LST x = LAST x)


If recall arithmetic isn't available:


RCL 3

RCL 1

RCL 2

+

+

LST x

RCL 3

-

×



Example:  a= 10, b = 2, c = 7.   Result:  95



 (s + a) × (s + b) × (s + c)


Assume that: 

Memory register 1 = a

Memory register 2 = b

Memory register 3 = c

Memory register 4 = s

(Your memory registers may have different names or labels)


If recall arithmetic is allowed:


RCL 4

ENTER

ENTER

ENTER

RCL+ 1

x<>y

RCL+ 2

×

x<>y

RCL+ 3

×


If recall arithmetic isn't available:


RCL 4

ENTER

ENTER

ENTER

RCL 1

+

x<>y

RCL 2

+

×

x<>y

RCL 3

+

×


Example:  a = 4, b = 8, c = 2, s = 3.   Result:  385



 (s - a) × (s - b) × (s - c)


Assume that: 

Memory register 1 = a

Memory register 2 = b

Memory register 3 = c

Memory register 4 = s

(Your memory registers may have different names or labels)


If recall arithmetic is allowed:


RCL 4

ENTER

ENTER

ENTER

RCL- 1

x<>y

RCL- 2

×

x<>y

RCL- 3

×


If recall arithmetic isn't available:


RCL 4

ENTER

ENTER

ENTER

RCL 1

-

x<>y

RCL 2

-

×

x<>y

RCL 3

-

×


Example:  a = 8, b = 10, c = 3, s = 24.   Result:  4,704



The Maximum and Minimum of the X and Y Stacks



In a program, we can arrange the stack to show the maximum and minimum between two numbers as follows:



Maximum on the X Stack, Minimum on the Y Stack:


...

x≤y   

x<>y

...


If the value of x is less than or equal to y, swap the stack values.  Otherwise, leave the stack as is.



Minimum on the X Stack, Maximum on the X Stack:


HP 12C, HP 11C, HP 10C, and the HP 41C/DM41 family, due to the lack of the  x≥y command:


... 

x≤y

x<>y

x<>y

...


Same routine as the maximum routine, except an extra swap command is placed.



All others:


... 

x≥y

x<>y

...


x≥y is TEST 9 on the HP 15C.  


If the value of x is greater than or equal to y, swap the stack values.  Otherwise, leave the stack as is.

Of course, if your calculator has the maximum (MAX) and minimum (MIN) functions, by all means, use them.  



Eddie


All original content copyright, © 2011-2024.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.