83 lines
1.5 KiB
C
83 lines
1.5 KiB
C
|
/* DEC *NetPresentValue(net, flows, nflow, intr)
|
||
|
*
|
||
|
* ARGUMENT
|
||
|
* DEC *net, **flows, *intr;
|
||
|
* int nflow;
|
||
|
*
|
||
|
* DESCRIPTION
|
||
|
* Calculates the net present value of a series of cash flows.
|
||
|
* The nflow cash flows in the array flows are processed at interest
|
||
|
* rate intr. The net present value, rounded to two decimal places,
|
||
|
* is stored in net.
|
||
|
*
|
||
|
* SIDE EFFECTS
|
||
|
* None.
|
||
|
*
|
||
|
* RETURNS
|
||
|
* net if successful, otherwise GM_NULL.
|
||
|
*
|
||
|
* POSSIBLE ERRORS
|
||
|
* GM_NULLPOINTER
|
||
|
* GM_ARGVAL
|
||
|
*
|
||
|
* AUTHOR
|
||
|
* Jared Levy
|
||
|
* Copyright (C) 1988-1990 Greenleaf Software Inc. All rights reserved.
|
||
|
*
|
||
|
* MODIFICATIONS
|
||
|
*
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include "gm.h"
|
||
|
#include "gmsystem.h"
|
||
|
|
||
|
DEC *NetPresentValue(net, flows, nflow, intr)
|
||
|
DEC *net, **flows, *intr;
|
||
|
int nflow;
|
||
|
{
|
||
|
int i;
|
||
|
DEC dtemp, *temp=&dtemp, dooopi, *ooopi=&dooopi;
|
||
|
DEC dtnet, *tnet=&dtnet, dpow, *pow=&dpow;
|
||
|
|
||
|
_MacStart(GM_NPV);
|
||
|
|
||
|
if (nflow<0) {
|
||
|
_MacErr(GM_ARGVAL);
|
||
|
_MacRet(GM_NULL);
|
||
|
}
|
||
|
_MacInVarD(intr);
|
||
|
if (!flows||!net) {
|
||
|
_MacErr(GM_NULLPOINTER);
|
||
|
_MacRet(GM_NULL);
|
||
|
}
|
||
|
for (i=0;i<nflow;i++) {
|
||
|
_MacInVarD(flows[i]);
|
||
|
}
|
||
|
|
||
|
if (CompareDecimal(intr,&decMinusHundred)!=1) {
|
||
|
_MacErr(GM_ARGVAL);
|
||
|
_MacRet(GM_NULL);
|
||
|
}
|
||
|
|
||
|
_MacDZero(tnet);
|
||
|
|
||
|
_MacDCopy(temp, intr);
|
||
|
temp->dc.id+=2;
|
||
|
(void) _AddDec80Bit(temp, temp, &decOne);
|
||
|
(void) _DivDec80Bit(ooopi, &decOne, temp);
|
||
|
|
||
|
_MacDCopy(pow, (&decOne));
|
||
|
|
||
|
for (i=0;i<nflow;i++) {
|
||
|
(void) _MulDec80Bit(temp, flows[i], pow);
|
||
|
(void) _AddDec80Bit(tnet, tnet, temp);
|
||
|
(void) _MulDec80Bit(pow, pow, ooopi);
|
||
|
}
|
||
|
|
||
|
_ScaleDec80Bit(net, tnet, 2);
|
||
|
|
||
|
_MacRet(net);
|
||
|
}
|