dongliuzhuan1219 2015-02-11 10:53
浏览 89
已采纳

基于PHPExcel的函数RATE()返回不正确的值

The newest version, found here: https://phpexcel.codeplex.com/SourceControl/latest#trunk/Classes/PHPExcel/Calculation/Financial.php of the RATE() function is not working for me.

I created a new PHP page, cut out that specific function and tested it but with a big difference in output to Excel.

<?php
define('FINANCIAL_MAX_ITERATIONS', 128); 
define('FINANCIAL_PRECISION', 1.0e-08); 

function RATE($nper, $pmt, $pv, $fv = 0.0, $type = 0, $guess = 0.1) {
        //$nper = (int) PHPExcel_Calculation_Functions::flattenSingleValue($nper);
        //$pmt  = PHPExcel_Calculation_Functions::flattenSingleValue($pmt);
        //$pv       = PHPExcel_Calculation_Functions::flattenSingleValue($pv);
        //$fv       = (is_null($fv))    ? 0.0   :   PHPExcel_Calculation_Functions::flattenSingleValue($fv);
        //$type = (is_null($type))  ? 0     :   (int) PHPExcel_Calculation_Functions::flattenSingleValue($type);
        //$guess    = (is_null($guess)) ? 0.1   :   PHPExcel_Calculation_Functions::flattenSingleValue($guess);

        $rate = $guess;
        if (abs($rate) < FINANCIAL_PRECISION) {
            $y = $pv * (1 + $nper * $rate) + $pmt * (1 + $rate * $type) * $nper + $fv;
        } else {
            $f = exp($nper * log(1 + $rate));
            $y = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv;
        }
        $y0 = $pv + $pmt * $nper + $fv;
        $y1 = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv;

        // find root by secant method
        $i  = $x0 = 0.0;
        $x1 = $rate;
        while ((abs($y0 - $y1) > FINANCIAL_PRECISION) && ($i < FINANCIAL_MAX_ITERATIONS)) {
            $rate = ($y1 * $x0 - $y0 * $x1) / ($y1 - $y0);
            $x0 = $x1;
            $x1 = $rate;
            if (($nper * abs($pmt)) > ($pv - $fv))
                $x1 = abs($x1);

            if (abs($rate) < FINANCIAL_PRECISION) {
                $y = $pv * (1 + $nper * $rate) + $pmt * (1 + $rate * $type) * $nper + $fv;
            } else {
                $f = exp($nper * log(1 + $rate));
                $y = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv;
            }

            $y0 = $y1;
            $y1 = $y;
            ++$i;
        }
        return $rate;
    }   //  function RATE()

$nper = 180;
$pmt = 6729.045954705334;
$pv = -400000;

$test = RATE($nper, $pmt, $pv);
?>

The function call above yields: 0.008774735218308

Excel 2013:

=RATE(180;6729,04595470533;-400000)

Yields: 0,0158263127885

Any ideas?

  • 写回答

1条回答 默认 最新

  • douzhi1937 2015-04-16 12:50
    关注

    I set some static values and checked if the value was within. In that case i use one $guess otherwise i use a different $guess. In Excel it works regardless of $guess and passed in values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000