<style><?php include('style.css');?></style><h1>116 digits of bcpow+</h1><h2 style="margin:1.6em">To change number, use ?x=2&y=3.5</h2>
<form action="" method="GET" style="margin:2em">
<?php
set_time_limit(4);
echo'<input name="x" placeholder="2" autofocus size="6" value="'.($_GET['x']??'').'">^<input name="y" placeholder="3" autofocus size="6" value="'.($_GET['y']??'').'"><button type="submit">Calc</button></form>';
// Set the scale (number of decimal places) for bcmath operations
$scale = 100;
// Function to calculate the natural logarithm (ln) of a number using a Taylor series
function bcln($x, $scale) {
if (bccomp($x, '1', $scale) == 0) {
return '0'; // ln(1) = 0
}
if (bccomp($x, '0', $scale) <= 0) {
return '0'; // ln(0) or negative number doesn't have real log
}
// Convert x into a number slightly greater than 1 for better convergence (x - 1)
$y = bcdiv(bcsub($x, '1', $scale), bcadd($x, '1', $scale), $scale);
$z = bcmul($y, $y, $scale); // y^2
$total = $y;
$term = $y;
$i = 1;
// Use the series expansion ln((1+x)/(1-x)) = 2(x + x^3/3 + x^5/5 + ...)
while ($i < 285) {
$term = bcmul($term, $z, $scale); // term *= y^2
$i += 2;
$total = bcadd($total, bcdiv($term, $i, $scale), $scale); // Add (x^n)/n
}
return bcmul($total, '2', $scale); // Multiply the series by 2
}
// Function to calculate e^x using the series expansion e^x = 1 + x + x^2/2! + x^3/3! + ...
function bcexp($x, $scale) {
$total = '1';
$term = '1';
$i = 1;
// Compute e^x using the Taylor series
while ($i < 285) {
$term = bcdiv(bcmul($term, $x, $scale), $i, $scale); // term *= x / i
$total = bcadd($total, $term, $scale); // Add the new term to total
$i++;
}
return $total;
}
// Function to calculate x^y using BCMath
function bcpow_custom($x, $y, $scale) {
// ln(x)
$ln_x = bcln($x, $scale);
// y * ln(x)
$y_ln_x = bcmul($y, $ln_x, $scale);
// e^(y * ln(x)) to get the result of x^y
return bcexp($y_ln_x, $scale);
}
// Example usage: Calculate 2^3.5 to 100 digits
$x = $_GET['x']??'2';
$y = $_GET['y']??'3.5';
$result = bcpow_custom($x, $y, $scale);
echo '<p>'.substr($result, 0, -4);
?></p>