Home Catalog Evaluate Download

An Example RPN Calculator Program

Here is an example of what a program for the RPN calculator looks like. This particular program doesn't involve the graphing capability of the calculator. Instead it uses a successive approximation algorithm to solve for the square root of any positive number (yes, there is a calculator key that already does this but our goal is to learn programming). You can read about the algorithm and its implementation in the comments at the top of the program.

Like all RPN calculator programs, this program resides in a standard .TXT file such as can be constructed using Notepad. You have the option of using Notepad to construct your programs but more typically you will write and edit your programs simply by clicking on the calculator's keys.

This program consists of only 27 statements, and demonstrates a loop that keeps repeating until a specific accuracy has been achieved. The RPN calculator's language is a low-level language akin to assembly language. A high-level language such as C++ would produce a much more readable program, but the single-step feature of the RPN calculator makes the operation of this program very visible and very easy to understand. You will be able to watch the calculator perform every step of the algorithm and observe the consequences of each operation on the registers and operand stack.

; file SquareRoot.txt ; The following equation describes a successive approximation ; algorithm that will produce the positive square root of a ; positive real number n. The value A in the equation is the ; prior approximation for the result [that is, for sqrt(n)] and ; B is the new, improved approximation. Every time we compute ; a B value it then becomes the A value to be plugged back into ; this same equation to arrive at an even better approximation ; B for the square root we seek. ; B = ( A + ( n/A ) ) / 2 ; As an illustration, suppose we seek sqrt(9) and for an ; initial guess we use 1. Repeating this equation we would ; then compute the following sequence of values: ; A B ; --------- --------- ; 1.00000 5.00000 ; 5.00000 3.40000 ; 3.40000 3.02353 ; 3.02353 3.00009 ; You can observe that these successive approximations converge ; very rapidly on the true value sqrt(9) = 3. ; To use the program to compute sqrt(n) use the keystroke sequence ; n, GSB 0 (that is, place n into the X stack location and then ; jump into the code at Label 0). When the program terminates ; the approximation to sqrt(n) will be left in the X stack position. ; Incidentally, this program has a bug (or perhaps we should just ; call it a weakness). For a hint of the problem re-read the first ; sentence of these comments. You can fix this bug by adding a new ; conditional test, and then jumping out of the program if the test ; fails (it probably makes sense to just leave the defective operand ; visible in the X stack location to alert the operator of the problem). LBL 0 ; at the start of the program n must be in X STO 1 ; we use Reg[1] to hold n 1 ; initialize A with a first guess STO 2 ; we use Reg[2] to hold A LBL 1 ; we return to this label each time we ; need to compute B = ( A + (n/A) )/2 RCL 1 ; X = n RCL 2 ; X = A, Y = n / ; X = n/A RCL 2 ; X = A, Y = n/A + ; X = A + n/A 2 ; X = 2, Y = A + n/A / ; X = (A+n/A)/2, we've now computed B STO 3 ; we use Reg[3] to hold B ; We now need to decide whether we can quit iterating. RCL 2 ; X = A, Y = B - ; X = B - A RCL 2 ; X = A, Y = B-A / ; X = (B-A)/A which is the change in the approximation ; as a fraction of the prior approximation ABS ; X = ABS((B-A)/A) .00000001 ; X = our tolerance, Y = ABS((B-A)/A) x>y ; test if ABS((B-A)/A) has shrunk below our tolerance GTO 2 ; we're done! RCL 3 ; Reg[3] holds B STO 2 ; here B becomes A GTO 1 ; branch back for another iteration LBL 2 RCL 3 ; Reg[3] holds B R/S ; stop and show the answer

Return to the Catalog of Programs