Problem with calculation....
| From: | Scott S. Nicholson | Date: | Tue, 23 May 2000 06:00:11 +0000 |
| Subject: | Problem with calculation.... | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-335@lists.php.net to get a copy of this message | ||
I'm putting together a mortgage calculator for a client, and have run into a
problem.
Basically, I'm using a for loop to run a series of calculations n times,
where n is the number of months in a mortgage.
After each calculation I round the results to the nearest penny, and then
store them in an array. Each iteration of the for loop uses the values
stored in the array by the previous iteration.
more or less:
for ($i = 1; $i <= $months; $i++) {
$array[$i] = round_number_to_two_decimals(--CALCULATION GOES HERE and
references array[$i-1]--);
}
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?
Please help.
TIA
Scott Nicholson
scottn@pnx1.com