Re: Problem with calculation....
| From: | Scott S. Nicholson | Date: | Tue, 23 May 2000 13:03:19 +0000 |
| Subject: | Re: Problem with calculation.... | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-350@lists.php.net to get a copy of this message | ||
> > It's working wonderfully, until it gets up to $i = 345 (the 345th
iteration
> > of the loop, out of 360 in this particular instance) at which point it
stops
> > rounding the numbers properly and produces results like
'9531.5099999999'
> > (should be '9531.51'), such that (due to rounding errors?) the results
of
> > iterations 345 through 360 get progressively less accurate.
> >
> > Is this a bug? Is there any way around it?
>
> Can you show a bit more of your code? It's a bit hard to get a feel for
> what might be happening... For a wild guess - there might be a flag,
> controlling the rounding, that is/is not getting set. Or a value going
> 'out of range'?
I'm pretty sure it can't be either of the above. Here's an actual code
snippet:
/* BEGIN CODE SNIPPET */
function amortize($amount, $interest, $term, $extra=0) {
/* figure monthly payment, rounded to 2 decimals */
$monthlyPayment = number_format(($amount * (($interest/1200) / (1 - pow((1
+ ($interest/1200)), -($term * 12))))), 2);
/* add any extra monthly payment user will make */
$monthlyPayment += $extra;
/* Set 0th row of array to values of loan at inception */
$table[0]['interest'] = 0;
$table[0]['principal'] = 0;
$table[0]['pToDate'] = 0;
$table[0]['iToDate'] = 0;
$table[0]['balance'] = $amount;
/* Set rows 1 through n to values of loan at that month, where n = number
of months in loan */
for ($i = 1; $i <= ($term * 12); $i++) {
$table[$i]['interest'] = number_format(($table[$i-1]['balance'] *
($interest/1200)), 2);
$table[$i]['principal'] = $monthlyPayment - $table[$i]['interest'];
$table[$i]['pToDate'] = $table[$i-1]['pToDate'] +
$table[$i]['principal'];
$table[$i]['iToDate'] = $table[$i-1]['iToDate'] +
$table[$i]['interest'];
$table[$i]['balance'] = $table[$i-1]['balance'] -
$table[$i]['principal'];
}
return($table);
}
/* END CODE SNIPPET */
For debugging, I've echoed the values of each item in the array as I go
through the loop, and they are all set perfectly until it gets to row 345,
when they stop rounding properly. (Here are values for rows 344 and 345):
MONTH 344
['interest'] - 62.65
['principle'] - 602.65
['pToDate'] - 89862.33
['iToDate'] - 139000.87
['balance'] - 10137.67
MONTH 345
['interest'] - 59.14
['principle'] - 606.16
['pToDate'] - 90468.49
['iToDate'] - 139060.01
['balance'] - 9531.5099999999
This is when $amount = 100000 ($), $interest = 7 (%), and $term = 30 (years)
Different amounts manifest the problem differently (for instance, if I
change the interest rate to 5%, it's row 341 where it starts to get off).
It occurs to me that perhaps the number_format() is messing it up? Would the
commas that that function inserts mess up calculations? I also just realized
that the problem manifests itself when 'balance' gets below $10,000. I'll
have to try it with something other than number_format()...
Scott Nicholson
scottn@pnx1.com