0

have the following FV function

private static function fv($r,$n,$p,$pv=0)
{
    $sum = $pv;
    for ( $i=0;$i<$n;$i++ ) {
     $sum += ($sum * $r) + $p;
    }
    return $sum;
}

These are the values I'm passing in:

0.0067 444 1834.58

This is the value I'm expecting from my deskcheck: 4983453.613 This is what I'm getting from my method: 5036155.12

Driving me nuts. I've tried a bunch of other functions on-line; none of them work!

Mina
  • 610
  • 7
  • 21

3 Answers3

0

I just ran this through an Excel spreadsheet (set cell A1 to 0, set cell A2 to =A1 + (A1*0.0067) + 1834.58, copy down to A445 (representing 444 iterations starting from 0). The final iteration comes up with 5036155.12 as you say your function does.

How are you calculating the 4983453.613 you say you are expecting? Have you double checked your method there? I don't get that at any stage.

The first three iterations come out as:

1834.58
3681.451686
5540.697412

and the final three as:

4965710.242
5000815.08
5036155.122
Vicky
  • 12,934
  • 4
  • 46
  • 54
0
private static function fv($r=0,$n=0,$pv=0)
{

    for ( $i=0;$i<$n;$i++ ) {
        $pv+=($pv*$r);
    }
    return $pv;
}

echo fv(0.0067,444,1834.58);

A little tidying up and the above should do the trick.. $p was not set at any time, because of your scoping, you can repurpose $pv within the function without having to define $sum.

SW4
  • 69,876
  • 20
  • 132
  • 137
0

This is a rounding error, but only in so far as 0.0067 is not precise enough.

For $r = 0.0067 I get the same result as your are getting.

For $r = 0.00666666666666666666666 I get the result you want.

I suggest you pass in $r = 1 / 150 for the best possible precision.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • you're right. it is a rounding error. Just tested it right now and I get the right value. Thanks a lot. – Mina Oct 05 '11 at 14:49