Problem with Rate function
<h1>Short description:</h1>

<p>The rate function calculates the rate neccessary to reach the future value.</p>

<p>nper - number of periods (number of periods)
pmt  - payments (every month or year)
pv   - present value (the initial amount)
fv   - future value (the amount to reach)</p>

<h1>Question:</h1>

<p>I have used the funtion Rate (php code below) and i have some troubles with it.  I use it with the following values:</p>

<h2>Wrong</h2>

<pre><code> Excel - RATE(228;-196,02;-49005;961546,464646;1;0,05) gives: 0,0119
 Php   - RATE(228,-196.02,-49005,961546.464646,1,0.05) gives: 0.0022
</code></pre>

<h2>Good</h2>

<pre><code> Excel - RATE(228;-196,02;-49005;161546,464646;1;0,05) gives:0,003
 Php   - RATE(228,-196.02,-49005,161546.464646,1,0.05) gives:0.003
</code></pre>

<p>It looks like that when the difference between <code>$nper</code>, <code>$pv</code>, <code>$pmt</code> and <code>$fv</code> becomes bigger the code goes wrong.</p>

<p>Does anyone know what the problem is? Thanks in advance.</p>

<pre><code>define('FINANCIAL_MAX_ITERATIONS', 128);
define('FINANCIAL_PRECISION', 1.0e-08);


function rate($nper, $pmt, $pv, $fv, $type, $guess) 
{

    $rate = $guess;
    if (abs($rate) &lt; 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) &gt; FINANCIAL_PRECISION) &amp;&amp; ($i &lt; FINANCIAL_MAX_ITERATIONS)) {
        $rate = ($y1 * $x0 - $y0 * $x1) / ($y1 - $y0);
        $x0 = $x1;
        $x1 = $rate;

        if (abs($rate) &lt; 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;
} 
</code></pre>
