/** * Returns the interest and principal payment for a given period for a cash flow with constant * periodic payments (annuities) and interest rate * * @param float Interest rate per period * @param int Number of periods * @param float Present Value * @param float Future Value * @param int Payment type: FINANCE_PAY_END (default): at the end of each period FINANCE_PAY_BEGIN: at the beginning of each period * @return array * @static * @access private */ function _interestAndPrincipal($rate, $per, $nper, $pv, $fv, $type) { $pmt = Math_Finance::payment($rate, $nper, $pv, $fv, $type); //echo "pmt: $pmt\n\n"; $capital = $pv; for ($i = 1; $i <= $per; $i++) { // in first period of advanced payments no interests are paid $interest = $type && $i == 1 ? 0 : -$capital * $rate; $principal = $pmt - $interest; $capital += $principal; //echo "$i\t$capital\t$interest\t$principal\n"; } return array($interest, $principal); }
function testpayment() { // various random calculations $this->assertTrue(abs(-36.157534 - Math_Finance::payment(0.08, 20, 355)) < FINANCE_PRECISION); $this->assertTrue(abs(-180.797585 - Math_Finance::payment(0.03, 5, 828)) < FINANCE_PRECISION); $this->assertTrue(abs(-166.305561 - Math_Finance::payment(0.29, 7, 477)) < FINANCE_PRECISION); $this->assertTrue(abs(-62.142857 - Math_Finance::payment(0, 7, 435)) < FINANCE_PRECISION); $this->assertTrue(abs(-258.137498 - Math_Finance::payment(0.1 / 12, 3 * 12, 8000, 0, FINANCE_PAY_END)) < FINANCE_PRECISION); $this->assertTrue(abs(-256.00413 - Math_Finance::payment(0.1 / 12, 3 * 12, 8000, 0, FINANCE_PAY_BEGIN)) < FINANCE_PRECISION); // cannot pass negative number of periods $this->assertType('object', Math_Finance::payment(0.29, -7, 435)); // cannot pass a type differenet from 0 and 1 $this->assertType('object', Math_Finance::payment(0.29, 7, 435, 0, 3)); }