Re: Problem with calculation....

From: Date: Tue, 23 May 2000 06:11:38 +0000
Subject: Re: Problem with calculation....
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-336@lists.php.net to get a copy of this message
On 23 May, Scott S. Nicholson wrote: > 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. 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'? Cheers -- David Robley | WEBMASTER & Mail List Admin RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/ AusEinet | http://auseinet.flinders.edu.au/ Flinders University, ADELAIDE, SOUTH AUSTRALIA

« previous php.general (#336) next »