Programming with RpnCalc Financial

Introduction — your first simple program

A program is a list of "instructions" which are commands for the calculator to execute. For the most part, these are simple key presses of the type you've been using all along. Normally, a program executes the instructions one by one in order, until the end of the program is reached. Some instructions can act to change the flow of control of the program, often conditionally, depending on the result of a calculation.

To switch RpnCalc Financial to programming mode, use the "Program/Run" function P/R.

The display should now show

00 -

"00" is the instruction number (sometimes known as the "program counter"). Instruction number 00 is special in that it does not actually hold an instruction. If a program should find itself trying to execute instruction 00, it will stop, and control will return to the user.

As you press calculator keys, the key you've selected will be added to the program.

To begin, let's clear out any existing program with f PRGM. This command clears program memory.

The display should now show

00 -
Program Cleared

Let's write a simple program to take a number, double it, and add fifteen. If you were to do this manually, you would enter the number, multiply by two, and add 15. The keystrokes to do this would be (number) ENTER 2 × 1 5 +

Let's start adding this to our program: The program is written to allow the user to simply enter the number, and then hit R/S to do the calculation. This means that the first program function will be ENTER

The display should now show

01 - ENTER

This shows that instruction 01 has been added to the program, and it's the ENTER key. Now press 2

02 - 2

This shows that instruction 02 has been added to the program, and it's the 2 key. Next comes ×

03 - ×

This should be pretty obvious by now. Now press the rest of 1 5 +. At this point, your display should look like

06 - +

Your program is six instructions long; the last instruction is the + key. Return to run mode with P/R

Now, let's try an example: Enter 30 and press R/S. (No need to use the ENTER key, it's already part of the program.)

75.00

That's the correct answer. The program executed the instructions in storage, and stopped when it reached the end. You can do more calculations by simply entering more numbers and hitting R/S again.

Aside: if you ever want to know how much program memory was used, use the MEM function.

Examining the program

The SST and BST keys allow you to move back and forth in program memory to see your program as it's entered.

Try it: Press P/R.

00 -

And now press SST and BST a few times. You should see that the program looks like:

01 - ENTER
02 - 2
03 - ×
04 - 1
05 - 5
06 - +

(Note: the blue box shows your program, and does not literally represent what you'll see on the display)

You can also use GTO . ("go to") to move directly to any instruction you want. For example, try GTO . 0 3

03 - ×

Hint: RpnCalcFinancial has a new feature: the Prog function on the right side of the keyboard (in portrait mode) shows you your entire program at a glance.

Modifying the program

Adding to the end

You can add more instructions to the end of the program by going to the last instruction and simply entering more. For example, let's take the result of every calculation and add it to register 0 with STO + 0:

GTO . 0 6
STO + 0

07 - STO + 0

This examples illustrates an important point: each instruction represents a single action, even if it takes multiple keystrokes to do it.

Modifying the middle

You can also replace any instruction with a new instruction. You do this by using SST, BST, or GTO . to select the instruction before the one you want to modify, and then you enter the new instruction.

For example, in our program, let's multiply by four instead of two:

GTO . 0 1
4

02 - 4

If you care to review your program again, you'll see that it now looks like

01 - ENTER
02 - 4
03 - ×
04 - 1
05 - 5
06 - +
07 - STO + 0

Testing the program

It doesn't take much complexity for bugs to creep into a program. One of the most useful debugging techniques is the ability to "single-step" through a program. That is, to execute it one instructions at a time and see the results as you go.

Let's give it a shot with our little program here. Return to run mode with P/R and enter a number you want computed.

Instead of pressing R/S, we'll press the SST button. This will execute a single instruction and then stop.

2 0 SST

20.00
01 - ENTER

This shows that instruction "01 - ENTER" was executed, and the results can be seen in the display. Hit SST again.

4
02 - 4

Instruction 02 was simply the digit 4. Hit SST again.

80.00
03 - ×

Here you can see that the multiply instruction at 03 has been executed.

Press SST four more times.

95.00
07 - STO + 0

And now we've come to the end of our program.

Control flow

RpnCalc Financial has a number of functions that affect the way a program executes. We will cover them one by one.

Pause

The PSE function pauses program execution for one second. This gives the user a chance to see a value calculated by the program.

For example, let's rewrite our little program to show the user the result of multiplying X by four before going on to add fifteen.

01 - ENTER
02 - 4
03 - ×
04 - PSE
05 - 1
06 - 5
07 - +
08 - STO + 0

Let's give it a run:

20 R/S

80.00

95.00
Memory 0 = 190.00

Stop

The R/S instruction causes a running program to stop execution. This could be done to make a program stop running, or more commonly, to give the user a chance to look at data for longer than PSE would allow, or to enter more data.

For example, let's modify our little program again, this time allowing the user to specify the number to be added:

01 - ENTER
02 - 4
03 - ×
04 - R/S
05 - +
06 - STO + 0

Let's give it a run:

20 R/S

80.00

17 R/S

97.00
Memory 0 = 192.00

Go To

GTO function causes the program to continue from a new location. As an example, let's write a program that shows the user the year-to-year effect of 5% inflation on price.

01 - ENTER		User enters base price
02 - RCL 0		inflation rate stored in register 0
03 - %
04 - +
05 - PSE		Let user see the new prices for a second
06 - GTO 02		And go back for another round

(Aside: Note the use of GTO without . in this case.)

One final note before we start this program running. Because of the GTO instruction, the program will keep running forever and ever. In the biz, we call this an "infinite loop".

RpnCalc Financial allows you to interrupt a running program at any time by pressing any key. We'll use this when we've had enough.

OK, let's run it:

Save inflation rate in register 0: 5 STO 0

Enter an initial value and start the program: 200 R/S

210.00
220.50
231.53
243.10
255.26
268.02
281.42
295.49
310.27
325.78

Interrupt it: R/S

325.78
Interrupted

Conditionals

Conditionals are instructions that determine if a certain condition holds true, and if so, the next instruction is executed normally. If the condition does not hold true, the next instruction is skipped.

The conditionals available in financial mode are

For example, lets take our inflation calculator from above, but change it so that it stops executing once the value in the display is greater than the value in register 1.

01 - ENTER		User enters base price
02 - RCL 0		inflation rate stored in register 0
03 - %
04 - +
05 - PSE		Let user see the new price for a second
06 - RCL 1		Stop value in register 1
07 - X≤Y		Is the stop value less than or equal the price?
08 - R/S		If so, halt the program
09 - ROLL		Otherwise, bring the price back
10 - GTO 02		And go back for another round

Now, store an inflation rate (say 5) into register 0, and a stop point (say 1000) into register 1.

Enter an initial price (say 200) and hit R/S. You should see inflation values displayed every second until your target value is exceeded.


Saving and restoring programs

Programs are stored on your SD card, in the directory org/efalk/RpnCalc.

To take advantage of RpnCalc's save and restore functionality, you will need to install a "file picker" application. The one named OI File Manager works admirably. You can download it for free from the Android Market.

Periodically, RpnCalc Financial will save the current program under the name "autosave.rpn". This file is automatically restored whenever RpnCalc Financial starts.

In addition, RpnCalc Financial allows you to explicitly save and restore programs under the names of your choosing. Select Menu > More to access the file functions. These are:

The use of these menu items should be self-obvious.


Some useful programs

This section includes a number of useful financial programs for RpnCalc. To use any of them, simply download them from this page, copy them to your Android's org/efalk/RpnCalc directory, and then use the Load Program menu item to load them into the calculator.

  1. Annual bond
  2. Day-basis bond
  3. Depreciation
  4. Declining Balance Depreciation
  5. Sum-Of-Year Depreciation
  6. Full Part
  7. Effective rate
  8. Leasing
  9. Leasing 2
  10. Leasing 3
  11. Rent or Buy
  12. Blank programming form

Annual bond

annualbond.rpn

Day-basis bond

day-basis-bond.rpn

Straight-Line Depreciation

depreciation.rpn

This program is similar to the built-in SL depreciation function except that it handles partial-year calculations.

To use, first clear financial registers with FIN and then store the following values into registers:

RegisterValue
PVBook value
FVSalvage value
nLife in years

Enter the year desired and press ENTER.

Key in the number of months in the first year. Press R/S. The program will display the amount of depreciation for the desired year. Press X≤Y to see the remaining depreciable value.

Press R/S again for the amount of depreciation and remaining depreciable value for the next year. Repeat as desired.

Declining Balance Depreciation

db_depr.rpn

This program is similar to the built-in DB depreciation function except that it handles partial-year calculations.

To use, first clear financial registers with FIN and then store the following values into registers:

RegisterValue
PVBook value
FVSalvage value
iDeclining Balance factor
nLife in years

Enter the year desired and press ENTER.

Key in the number of months in the first year. Press R/S. The program will display the amount of depreciation for the desired year. Press X≤Y to see the remaining depreciable value.

Press R/S again for the amount of depreciation and remaining depreciable value for the next year. Repeat as desired.

Sum-Of-Year Depreciation

soyd_depr.rpn

This program is similar to the built-in SOYD depreciation function except that it handles partial-year calculations.

To use, first clear financial registers with FIN and then store the following values into registers:

RegisterValue
PVBook value
FVSalvage value
nLife in years

Enter the year desired and press ENTER.

Key in the number of months in the first year. Press R/S. The program will display the amount of depreciation for the desired year. Press X≤Y to see the remaining depreciable value.

Press R/S again for the amount of depreciation and remaining depreciable value for the next year. Repeat as desired.

Full Part

full_part.rpn

This program allows you to compute a depreciation schedule that starts out as a declining balance depreciation and then switches to a straight-line depreciation at an appropriate time. The ideal crossover point is when the end of the year in which the declining-balance depreciation last exceeds or equals the amount of straight-line depreciation.

To use, first clear all registers with REG and then store the following values into registers:

RegisterValue
PVBook value
FVSalvage value
nLife in years
iDeclning balance factor

Enter the year desired and press ENTER.

Key in the number of months in the first year. Press R/S. The program will display the amount of depreciation for the desired year. Press X≤Y to see the remaining depreciable value.

Press RCL 1 to see total depreciation through current year.

Press R/S again for the amount of depreciation and remaining depreciable value for the next year. Repeat as desired.

Effective rate

effective-rate.rpn

Leasing

leasing.rpn

Leasing 2

leasing2.rpn

Leasing 3

leasing3.rpn

Rent or Buy

rentorbuy.rpn

Over the long term, it's almost always better financially to buy a home (or other large capital expense) than to rent, but over the short term that's not necessarily so. This program helps you make the "rent or buy" decision.

What this program does is to calculate the rate of return on the proposed investment in your house, and then calculate the rate of return that your down payment and extra monthly payments would earn in a savings account.

To use, first clear financial registers with FIN and then store the following values into registers:

RegisterValue
R1Down payment
R2Life of the mortgage
R3Annual interest rate
R4Monthly taxes
R5Estimated monthly maintenance costs
R6Closing costs
R7Selling cost as a percentage of selling price. Include sales commission, escrow fees, etc.
R8Monthly rent
R9Annual interest rate on savings or other investment
R.0State and Federal tax rate. This is the sum of the tax rates and is used to factor in the mortgage interest rate deduction.
nNumber of years involved in the investment
iEstimated yearly appreciation rate in percent
PVPrice of the house

Press R/S and the program will compute the net proceeds from selling the house. (A negative number would represent a loss.)

Press R/S again to compute the yield on your investment of the house.

Press R/S again to compute the value of a savings account or other investment.

Compare the net proceeds from selling your house to the value of the savings account. If the latter is larger, you should rent instead of buy.

Blank programming form

blank.rpn

This is simply a "blank" RpnCalc program to make it a little easier to write your own. Write keystrokes after the program line number on each line of this file, delete the unused blank lines at the end, save it to a new name, and copy it to your Android.