Re: Problem with calculation....

From: Date: Tue, 23 May 2000 15:30:07 +0000
Subject: Re: Problem with calculation....
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-363@lists.php.net to get a copy of this message
Scott, That's a large number of steps you're doing, so you should use the full precision there is in the floating-point numbers. I think you should base the iterative calculation on the raw previous result rather than the rounded-up value. Store the results rounded up, but let the calculation go through with the full precision. You'll be surprised how different the results can be. Jan "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. > > TIA > Scott Nicholson > scottn@pnx1.com > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#363) next »