note 46965 added to ref.bc
| From: | Diabolos at GMail dot com | Date: | Thu, 28 Oct 2004 18:42:50 +0000 |
| Subject: | note 46965 added to ref.bc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-79286@lists.php.net to get a copy of this message | ||
Here's a function to compute the natural exponential function in arbitrary precision using the
basic bcMath arithmetic operations.
EXAMPLE:
To compute the exponential function of 1.7 to 36 decimals:
$y = bcExp("1.7", 36);
The result:
4.331733759839529271053448625299468628
would be returned in variable $y
NOTE:
In practice, the last couple of digits may be inaccurate due to small rounding errors. If you
require a specific degree of precision, always compute 3-4 decimals beyond the required precision.
The program code for the natural exponential function is:
******************************************
Function bcExp($xArg, $NumDecimals)
{
$x = Trim($xArg);
$PrevSum = $x - 1;
$CurrTerm = 1;
$CurrSum = bcAdd("1", $x, $NumDecimals);
$n = 1;
While (bcComp($CurrSum, $PrevSum, $NumDecimals))
{
$PrevSum = $CurrSum;
$CurrTerm = bcDiv(bcMul($CurrTerm, $x, $NumDecimals), $n + 1, $NumDecimals);
$CurrSum = bcAdd($CurrSum, $CurrTerm, $NumDecimals);
$n++;
}
Return $CurrSum;
}
----
Manual Page -- http://www.php.net/manual/en/ref.bc.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46965
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46965&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46965&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46965&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46965&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46965&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46965&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46965&report=yes
Search -- http://master.php.net/manage/user-notes.php