note 94610 added to ref.bc
| From: | thomas at tgohome dot com | Date: | Sat, 14 Nov 2009 11:17:12 +0000 |
| Subject: | note 94610 added to ref.bc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162901@lists.php.net to get a copy of this message | ||
Some useful bcmath functions:
<?php
/*
* Computes the factoral (x!).
* @author Thomas Oldbury.
* @license Public domain.
*/
function bcfact($fact, $scale = 100)
{
if($fact == 1) return 1;
return bcmul($fact, bcfact(bcsub($fact, '1'), $scale), $scale);
}
/*
* Computes e^x, where e is Euler's constant, or approximately 2.71828.
* @author Thomas Oldbury.
* @license Public domain.
*/
function bcexp($x, $iters = 7, $scale = 100)
{
/* Compute e^x. */
$res = bcadd('1.0', $x, $scale);
for($i = 0; $i < $iters; $i++)
{
$res += bcdiv(bcpow($x, bcadd($i, '2'), $scale), bcfact(bcadd($i, '2'),
$scale), $scale);
}
return $res;
}
/*
* Computes ln(x).
* @author Thomas Oldbury.
* @license Public domain.
*/
function bcln($a, $iters = 10, $scale = 100)
{
$result = "0.0";
for($i = 0; $i < $iters; $i++)
{
$pow = bcadd("1.0", bcmul($i, "2.0", $scale), $scale);
//$pow = 1 + ($i * 2);
$mul = bcdiv("1.0", $pow, $scale);
$fraction = bcmul($mul, bcpow(bcdiv(bcsub($a, "1.0", $scale), bcadd($a,
"1.0", $scale), $scale), $pow, $scale), $scale);
$result = bcadd($fraction, $result, $scale);
}
$res = bcmul("2.0", $result, $scale);
return $res;
}
/*
* Computes a^b, where a and b can have decimal digits, be negative and/or very large.
* Also works for 0^0. Only able to calculate up to 10 digits. Quite slow.
* @author Thomas Oldbury.
* @license Public domain.
*/
function bcpowx($a, $b, $iters = 25, $scale = 100)
{
$ln = bcln($a, $iters, $scale);
return bcexp(bcmul($ln, $b, $scale), $iters, $scale);
}
$precision = 35;
echo "3^4.5, precision 15, iters 25 = " . bcpowx('3', '4.5',
25, $precision) . "\n";
echo "4.5^3, precision 15, iters 25 = " . bcpowx('4.5', '3',
25, $precision) . "\n";
echo "8^1/2, precision 15, iters 25 = " . bcpowx('8', '0.5',
25, $precision) . "\n";
echo "28^-1, precision 15, iters 25 = " . bcpowx('28', '-1',
25, $precision) . "\n";
echo "432^0, precision 15, iters 25 = " . bcpowx('432', '0',
25, $precision) . "\n";
echo "0^0, precision 15, iters 25 = " . bcpowx('0.0', '0',
25, $precision) . "\n";
echo "9^999, precision 15, iters 25 = " . bcpowx('9', '999',
25, $precision) . "\n";
echo "9^9999, precision 15, iters 25 = " . bcpowx('9', '9999',
25, $precision) . "\n";
echo "9^99999, precision 15, iters 25 = " . bcpowx('9',
'99999', 25, $precision) . "\n";
echo "9^999999, precision 15, iters 25 = " . bcpowx('9',
'999999', 25, $precision) . "\n";
echo "9^9999999, precision 15, iters 25 = " . bcpowx('9',
'9999999', 25, $precision) . "\n";
echo "9^99999999, precision 15, iters 25 = " . bcpowx('9',
'99999999', 25, $precision) . "\n";
echo "9^999999999, precision 15, iters 25 = " . bcpowx('9',
'999999999', 25, $precision) . "\n";
echo "9^9999999999, precision 15, iters 25 = " . bcpowx('9',
'9999999999', 25, $precision) . "\n";
echo "9^99999999999, precision 15, iters 25 = " . bcpowx('9',
'99999999999', 25, $precision) . "\n";
echo "9^999999999999, precision 15, iters 25 = " . bcpowx('9',
'999999999999', 25, $precision) . "\n";
?>
----
Server IP: 92.48.74.199
Probable Submitter: 86.157.97.69
----
Manual Page -- http://www.php.net/manual/en/ref.bc.php
Edit -- https://master.php.net/note/edit/94610
Del: integrated -- https://master.php.net/note/delete/94610/integrated
Del: useless -- https://master.php.net/note/delete/94610/useless
Del: bad code -- https://master.php.net/note/delete/94610/bad+code
Del: spam -- https://master.php.net/note/delete/94610/spam
Del: non-english -- https://master.php.net/note/delete/94610/non-english
Del: in docs -- https://master.php.net/note/delete/94610/in+docs
Del: other reasons-- https://master.php.net/note/delete/94610
Reject -- https://master.php.net/note/reject/94610
Search -- https://master.php.net/manage/user-notes.php