HP Forums
Micro challenge: improve this RPN code - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: Micro challenge: improve this RPN code (/thread-14189.html)



Micro challenge: improve this RPN code - David Hayden - 12-18-2019 04:15 PM

While waiting for my Woodstock LP, I've been playing with my 29C and writing my own versions of some of the programs in the solutions packs. One of them, Gaussian Quadrature on page 22 of the Math pack takes numbers "a" and "b" on the stack in Y and X, and stores (a+b)/2 and (a-b)/2 in two registers like so:
Code:
STO 7
STO 8
X<>Y
ST+ 7
ST- 8
2
ST/ 7
ST/ 8
I look at my code and think "there must be a better way." Can you improve it?

The goal is for 29C code only. I won't list all the 29C instructions, but here are some important differences with earlier and later models:
Stack manipulation: X<>Y, RUP and RDN only. No direct stack STO/RCL
It has STO arithmetic, but not RCL arithmetic
It does have sigma+ and sigma-.
It doesn't matter which registers the results go in.

Looking forward to smacking my head and saying "why didn't I think of that!!" Smile

Dave


RE: Micro challenge: improve this RPN code - Albert Chan - 12-18-2019 04:53 PM

I don't know 29C, but seems you can save code by (a+b)/2 = b + (a-b)/2

Example, for HP12C, a=10, b=2

10 Enter 2

STO 1     ; M1=b
-
2
/
STO 2     ; M2 = (a−b)/2
STO+ 1   ; M1 = (a+b)/2


RE: Micro challenge: improve this RPN code - rprosperi - 12-18-2019 07:40 PM

Albert "Identity Man" Chan strikes again!

And having shared that identify, we can all scratch our heads, again, and wonder... 'how does he keep all those memorized?' Huh


RE: Micro challenge: improve this RPN code - hth - 12-18-2019 07:45 PM

Using the rewrite idea from Albert:

Code:
-
LASTX
X<>Y
2
/
+
LASTX

Results are in X and Y.


RE: Micro challenge: improve this RPN code - David Hayden - 12-18-2019 08:58 PM

SMACK!

Just as promised. The funny thing is that I kept looking to use a slightly different identy: (a-b)/2 = (a+b)/2 - b

Thanks,
Dave