Example #1
0
<?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;
    $n = 0;
    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);
}
echo bcpi($_GET['precision']);
Example #2
0
File: pi.php Project: crempp/bin
    return bcdiv(bcpow(bcadd($a, $b), 2), bcmul(4, $t), $precision);
}
?>
    
<html>
  <head>
    <script>
      
    </script>
  </head>
  <body>
    <div><?php 
echo date(DATE_RFC822) . " at " . gethostname();
?>
</div>
    <div>Calculating pi to a precision of <?php 
echo $precision;
?>
</div>
    <div style="width: 700px; word-wrap:break-word;">
        
        <?php 
flush();
echo bcpi($precision);
?>
    </div>
  </body>
</html>