(50g) NPV and IRR for uneven cashflows

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (https://www.hpmuseum.org/forum/forum-10.html)
+--- Forum: General Software Library (https://www.hpmuseum.org/forum/forum-13.html)
+--- Thread: (50g) NPV and IRR for uneven cashflows (/thread-23255.html)



(50g) NPV and IRR for uneven cashflows - 2old2randr - 2025-03-11

Attached is a System RPL program that calculates the NPV, NFV and IRR of a set of periodic uneven cashflows.

This is a form based program that takes an interest rate and a matrix of [amount, repeat] pairs. The matrix can be read from a global variable or edited on screen. NPV, NFV, number of periods and total of cash flows are calculated on the fly whenever either input is modified. IRR is computed when a soft key (F4) is pressed.

As it stands, this provides equivalent functionality to an HP 17b ii.

Although the computations are straightforward, this program may be of interest to those learning SysRPL since it illustrates several techniques in the use of ^IfMain (the SysRPL equivalent of INFORM). For example, it shows how to:
* Implement read-only fields (computed fields)
* Change values in computed fields when an input field value is modified.
* Implement fields that accept an array and use grobs to display values.
* Use the Filer to choose a variable to read into a field from within a program.
* Use the Matrix Writer to edit a field that contains an array.
* Restrict the array field to accept only arrays that have real numbers in two columns and more than one row.
* Add an additional menu item while retaining default behaviour for OK / Cancel soft keys.
* Block exit from the form when input values are missing.

Upon exit from the form, if the OK key was pressed, the cash flow array, NPV, NFV and IRR are placed on the stack. The cash flow matrix can be saved into a variable if needed for future use. I% is stored in a global variable that is created on startup if not already present.

In the attached zip file:
1. CFlows.s is the SysRPL source code.
2. CFlows is the compiled program (just copy to the calculator and run). Checksum #F7A3h, 2759.5 bytes.

Hopefully, this helps someone learning SysRPL - I was looking for example programs like this when I started out a couple of years ago but sadly, did not find many.

Cheers
Sudhir