Showing posts with label stack. Show all posts
Showing posts with label stack. Show all posts

Saturday, June 6, 2026

RPN: Solving T * w + Z = Y * w + X (featuring DM42, HP 67)

RPN:   Solving T * w + Z = Y * w + X  (featuring DM42, HP 67)


Introduction and Code



This is inspired in part by the new Casio fx-92 being released where it has added the linear equation:

ax + b = cx + d



( https://tiplanet.org/forum/viewtopic.php?p=280173&sid=2174c3a99692ac8898ff3957db84031a#p280173 (in French), retrieved February 6, 2026)



Solve the equation for w:

T * w + Z = Y * w + X


where the solution is w = (Z – X) ÷ (Y – T)


The values for T, Z, Y, and X are taken directly from the four-level classic RPN stack.



HP 42S/DM42/HP 41C (programmed with a DM42) Code


LBL “TZYX1”

x<>y

R↓

-

R↓

-

R↑

x<>y

÷

RTN



HP 67 Code


001: LBL A; 31, 25, 11

002: x<>y; 35, 52

003: R↓; 35, 53

004: -; 51

005: R↓; 35, 53

006: -; 51

007: R↑; 35, 54

008: x<>y; 35, 52

009: ÷; 81

010: RTN; 35, 52



Stack Results:


Start

Finish

T

Z - X

Z

Z - X

Y

Y

X

(Z – X) ÷ (Y – T) = W (solution)



Examples


Example 1: 5 * w + 20 = 10 * w + 30


T: 5

Z: 20

Y: 10

X: 30


Solution: w = -2




Example 2: 3 * w – 18 = -2 * w + 16


T: 3

Z: -18

Y: -2

X: 16


Solution: w = 6.8




Example 3: 5 * w + 3 = w + 2


T: 5

Z: 3

Y: 1

X: 2


Solution: w = -0.25




Eddie


All original content copyright, © 2011-2026. 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.

Saturday, February 14, 2026

RPN: Absolute Value Equations with the HP 11C and DM41X

RPN: Absolute Value Equations with the HP 11C and DM41X



Introduction: Solving |z * w + y| = x


Today’s blog focuses on solving the absolute value equation:


|z * w + y| = x


for the variable w, and the values of z, y, and x are given and are on the Classic RPN stack.


For example, in the problem |5 * w + 7| = 2, the stack would be set up as:


t: (anything, it doesn’t matter)

z: 5

y: 7

x: 2


(This is why I set the variable as w instead of the usual x.)


One approach is to use memory registers and other uses only stack operations. Today’s algorithm focuses on the latter.


Caution: In the above equation, x will always be non-negative. The equation will never be valid if x is negative.



The Algebra


Solve for w:

|z * w + y| = x


This leads us to solve the two equations:


(I)

z * w + y = -x

z * w = -x – y

w = -x/z – y/z


Let w- = -x/z – y/z = (-x/z) + (-y/z)


(II)

z * w + y = x

z * w = x – y

w = x/z - y/z


Let w+ = x/z – y/z = (x/z) + (-y/z)


Then:


w- = -x/z – y/z

w- = (-x/z) + (-y/z)

w- = (-x/z) + (-y/z) + (-x/z) + (x/z)

w- = 2 * (-x/z) + (x/z) + (-y/z)

w- = 2 * (-x/z) + w+


RPN Code: HP 11C (adoptable for other RPN calculators)


LBL A

001

42, 21, 11

Program start

R↓

002

33


R↓

003

33


X<>Y

004

34


R↓

005

33


1/x

006

15


×

007

20


LST x

008

43, 36


X<>Y

009

34


R↓

010

33


×

011

20


LST x

012

43, 36


R↑

013

43, 33


X<>Y

014

34


R↓

015

33


X<>Y

016

34


CHS

017

16


X<>Y

018

34


+

019

40


ENTER

020

36


ENTER

021

36


LST x

022

43, 36


-

023

30


LST x

024

43, 36


-

025

30


RTN

026

43, 32

Program end


RPN Code: DM41X (HP 41C series, no module is required)


LBL “ASBEQ”

RDN

RDN

X<>Y

RDN


1/x

×

LAST X

X<>Y

RDN

×

LAST X

R↑

X<>Y


RDN


X<>Y

CHS

X<>Y


+

ENTER

ENTER

LAST X

-

LAST X

-


RTN



Notes:

* RDN is shown as R↓

* To enter R↑, press XEQ, ALPHA, R, SHIFT, ENTER, ALPHA



Subroutines Used


We used several techniques to manipulate the stack. They are presented below:


Rotate stack from X, Y, Z, T to Z, X, Y, T

R↓

R↓

X<>Y

R↓


Source:

Ball, John A. Algorithms for RPN Calculators John Wiley & Sons: New York. 1978. ISBN 0-471-03070-8. pg. 78



Multiply Y and Z by 1/X. Stack: X, Y, Z, T → X, Y/X, Z/X, T


1/x

×

LAST X

X<>Y

R↓

×

LAST X

R↑

X<>Y


Change X, Y to Y+ X, Y – X


+

ENTER

ENTER

LAST X

-

LAST X

-


The resulting stack is:


z

z

y + x

y – x


Doing LAST X, -, twice is effectively subtracting whatever is in L register twice.



Examples


|4 * w – 6| = 5


Stack:

z: 4

y: -6

x: 5


Results:

y: 2.75

x: 0.25


|2 * w + 5| = 3


Stack:

z: 2

y: 5

x: 3


Results:

y: -1

x: -4




Eddie


All original content copyright, © 2011-2026. 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.

Sunday, January 18, 2026

HP 15C: Distance and Slope Between Two Points Using Polar Conversion and the Stack

HP 15C: Distance and Slope Between Two Points Using Polar Conversion and the Stack



HP 15C Program: Distance and Slope



This short program calculates the slope and distance between two Cartesian points (x1, y1) and (x2, y2) using the four level stack and rectangular-polar conversion. The code can be adopted to other Hewlett Packard, Swiss Micros, and other RPN with four-stacks. RPL will need a short adjustment.



Input Stack:

T: y2

Z: x2

Y: y1

X: x1



Code:

LBL A

001

42, 21, 11

Program start

X<>Y

002

34


R↓

003

33


-

004

30


R↓

005

33


-

006

30


CHS

007

16


R↑

008

43, 33

Y: Δy, X: Δx

→P

009

43, 1

Rectangular to polar conversion; calculate distance

X<>Y

010

34


TAN

011

25

Calculate slope

X<>Y

012

34


RTN

013

43, 32

Program end









Reference formulas



Distance = √((x2^2 – x1^2) + (y2^2 – y1^2))

Slope = (y2 – y1) ÷ (x2 – x1) = tan(Θ)



Derivation:

Let y’ = y2 – y1 and x’ = x2 – x1

Then by rectangular to polar function, angle:

Θ = arctan( y’ / x’ )

tan Θ = y’ / x’

tan Θ = (y2 – y1) ÷ (x2 – x1)



Examples



Example 1: (-3, 8) to (11, 16)

Stack:

T: 16

Z: 11

Y: 8

X: -3

Result:

Y: slope ≈ 0.5714

X: distance ≈ 16.1245



Example 2: (5, 6) to (7, 9)

Stack:

T: 9

Z: 7

Y: 6

X: 5

Result:

Y: slope = 1.5000

X: distance ≈ 3.6056


Eddie


All original content copyright, © 2011-2026. 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.

HP 48G Collection

 HP 48G Collection  Contents:  Intensity of Spherical Light Source  Speed of Light in Dry Air  Pressure of Air  Earth's Gravity a...