PHP 8.5.0 Beta 1 available for testing

Voting

: max(six, zero)?
(Example: nine)

The Note You're Voting On

mgcclx at gmail dot com
18 years ago
I wrote this function with many BCMath functions. It should be the fastest function in PHP to find the number pi into any precision, my test is it generate 2000 digits after the dot in 8 seconds. I don't think you need anything more than that.
<?php
//bcpi function with Gauss-Legendre algorithm
//by Chao Xu (Mgccl)
function bcpi($precision){
$limit = ceil(log($precision)/log(2))-1;
bcscale($precision+6);
$a = 1;
$b = bcdiv(1,bcsqrt(2));
$t = 1/4;
$p = 1;
while(
$n < $limit){
$x = bcdiv(bcadd($a,$b),2);
$y = bcsqrt(bcmul($a, $b));
$t = bcsub($t, bcmul($p,bcpow(bcsub($a,$x),2)));
$a = $x;
$b = $y;
$p = bcmul(2,$p);
++
$n;
}
return
bcdiv(bcpow(bcadd($a, $b),2),bcmul(4,$t),$precision);
}
?>

<< Back to user notes page

To Top