A few years after I graduated I put my Hp 42S in a drawer. It slowly drained it's batteries. All the programs I spent so much time writing were gone and it just laid the for about 15 years. But, recently I screwed myself on some important thing using a windows calculator. So I invested some cash in a pack of LR44 batteries and in a few minutes I was back in a RPN world. A few hours later I started rewriting my library. Here is current iteration of my quadratic formula quadratic equation solver. I know this was done many times but as it is now, I find it simple and useful. Main goals were:
- do not use registers
- try to use only only stack
- after presenting results in readable form preserve both solution in Y and X register.
So here it is:
Code:
The program calculates both solutions to quadratic equation using quadratic formula.
Equation: ax^2 + bx + c = 0
Formula: x1,2 = (-b+-SQRT(b^2-4ac))/2a
INPUT
Program expects input parameters (quadratic equations coefficients) on stack:
T ? - not important, it will be overwritten
Z a
Y b
X c
OUTPUT
Output is on the stack
T 2a Left over from calculation
Z SQRT(Discriminant) I did not manage to leave Discriminant on the stack and calculate using only stack.
Y x1 First root
X x2 Second root.
Optionaly; Discriminant can be stored in a variable, and put on stack after calculation.
NOTES
The program uses only stack and ALPHA register.
LBL 00 Error handling routine
"Coefficient " Inform a user about an error.
|-"a is 0"
AVIEW
CLX Because of OCD, restore coefficients on the stack as they were entered
RDOWN [0, a, b, c]
STOP At this point user can examine existing coefficients, enter new ones and reexecute program, either with R/S or XEQ.
LBL "QUADFML" This is the entry point into the program execution. Stack should be populated with [?,a,b,c].
RCL ST Z [a,b,c,a]
X=0?
GTO 00
* [a, a, b, ac]
4 stack [a,b,ac,4]
* [a, a, b, 4ac]
RCL ST Y [a, b, 4ac, b]
X^2 [a, b, 4ac, b^2]
x<>Y [a, b, b^2, 4ac]
- [a, a, b, b^2-4ac] v nadaljevanju uporabim D = b^2-4ac SD=SQRT(D)
"Discriminant:LF" --LF denotes LineFeed symbol. Optional: ommit if you do not need to display discriminant.
PROMPT
STO "QUADDISC" [a,a,b,D] -- this step is optional see below
SQRT [a,a,b,SD] we must calculate SQRT(D) now, because there is not enough place on stack to hold 2a, D, SQRT(D), x1,....
2 [a,b,SD,2]
RCL* ST T [a,b,SD,2a] a is not needed anymore
x<> ST Z [a,2a,SD,b]
+/- [a,2a,SD.-b]
ENTER [2a,SD,-b,-b]
RCL- ST Z [2a,SD,-b,-b-SD]
RCL/ ST T [2a,SD,-b,x1] x1=(-b-SD)/2a
x<>y [2a,SD,x1,-b]
RCL+ ST Z [2a,SD,x1,-b+SD]
RCL/ ST T [2a,SD,x1,x2] x2=(-b+SD)/2a, stack now contains SQRT(D) and both solutions. I have not found a way to preserve discriminant using only stack.
-- optional formated output
"x1="
ARCL ST Y
|-"x2=" |- = concat symbol = [shift] [ALPHA] [ENTER]
ARCL ST X
AVIEW
In the past I did write some programs to so symbolic algebra and equation solving, symbolic integration, simplification and factorization of algebraic expression… all the sweeties that 42S lacked right from the box. I just have to find all my paper notes somewhere in the drawers.