当将输出与excel函数的输出进行比较时,FV PHP函数会为某些测试返回错误的输出
我使用以下函数在Excel中查找未来值(FV)作为FV函数。但是,在输出中有一些异常错误。如果我给函数输入-FV(0.00375384,0107450,0)与Excel函数相比,它给出了正确的答案。但是,如果我把-FV(0.00375,60,0526.51444361646,0)那么输出值是错误的。Excel函数给出134505.46,而php函数给出659.088 我不知道为什么相同的函数在第一次测试中返回错误的值,在第二次测试中返回正确的值。请在这个问题上帮助我。谢谢当将输出与excel函数的输出进行比较时,FV PHP函数会为某些测试返回错误的输出,php,Php,我使用以下函数在Excel中查找未来值(FV)作为FV函数。但是,在输出中有一些异常错误。如果我给函数输入-FV(0.00375384,0107450,0)与Excel函数相比,它给出了正确的答案。但是,如果我把-FV(0.00375,60,0526.51444361646,0)那么输出值是错误的。Excel函数给出134505.46,而php函数给出659.088 我不知道为什么相同的函数在第一次测试中返回错误的值,在第二次测试中返回正确的值。请在这个问题上帮助我。谢谢 这是对MathPHP
这是对MathPHP库稍加修改的函数
<?php
/**
* Future value for a loan or annuity with compound interest.
*
* Same as the =FV() function in most spreadsheet software.
*
* The basic future-value formula derivation:
* https://en.wikipedia.org/wiki/Future_value
*
* PMT*((1+r)ᴺ - 1)
* FV = -PV*(1+r)ᴺ - ----------------
* r
*
* The (1+r*when) factor adjusts the payment to the beginning or end
* of the period. In the common case of a payment at the end of a period,
* the factor is 1 and reduces to the formula above. Setting when=1 computes
* an "annuity due" with an immediate payment.
*
* Examples:
* The future value in 5 years on a 30-year fixed mortgage note of $265000
* at 3.5% interest paid at the end of every month. This is how much loan
* principle would be outstanding:
* fv(0.035/12, 5*12, 1189.97, -265000, false)
*
* The present_value is negative indicating money borrowed for the mortgage,
* whereas payment is positive, indicating money that will be paid to the
* mortgage.
*
* @param float $rate
* @param int $periods
* @param float $payment
* @param float $present_value
* @param bool $beginning adjust the payment to the beginning or end of the period
*
* @return float
*/
function fv(float $rate, int $periods, float $payment, float $present_value, bool $beginning = false)
{
$when = $beginning ? 1 : 0;
if ($rate == 0) {
$fv = -($present_value + ($payment * $periods));
return $fv;
}
$initial = 1 + ($rate * $when);
$compound = pow(1 + $rate, $periods);
$fv = -(($present_value * $compound) + (($payment * $initial * ($compound - 1)) / $rate));
return $fv;
}