Re: precision using pow()
| From: | Marek Kilimajer | Date: | Fri, 06 Sep 2002 09:33:04 +0000 |
| Subject: | Re: precision using pow() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-115417@lists.php.net to get a copy of this message | ||
Hi,
Split the calculation up an use echo to find the problem.
David Rice wrote:
Hi: I am attempting to calculate a mortgage payment using the following code. <?PHP$P = 100000; // principal outstanding $I = 4.5; // annual interest rate percentage $N = 20; // number of years// The formula that I am using //(P*(((1+i/200)^(1/6)-1))/(1-(((1+i/200)^(1/6)))^-(n*12))) $Pmt = $P*((pow(1+($I/200),(1/6))-1)/(1-pow(pow((1+$I/200),(1/6)),-($N*12)))); echo $Pmt; ?> which produces: 630.40515566726 What I am looking for is 632.65 (rounded to 2 places). (I am not looking for a rounding mechanism, just a more accurate result) The manual states that the pow function attempts to return an Integer. Is this the source of the inaccurate result? The manual also says to look at exp but appears to be missing some sort of usage example. I have no idea how that works. I tried a few things with no success. I also came across a mention of bcpow() but am unable to use this in the build that I am using. So is it some boneheaded coding error on my part or am I asking for too much of pow()? Thanks David