Some useful bcmath functions:
<?php
function bcfact($fact, $scale = 100)
{
if($fact == 1) return 1;
return bcmul($fact, bcfact(bcsub($fact, '1'), $scale), $scale);
}
function bcexp($x, $iters = 7, $scale = 100)
{
$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;
}
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);
$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;
}
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";
?>