Home > Article > Backend Development > PHP implements simple and high-precision PI method
After talking about the stupidest one, let’s talk about a more sophisticated method. The code is as follows:
~~~.php
// pi = 2 + 2/3 + 2/3*2/5 + 2/ 3*2/5*2/7 + ...
$pi = (double)2.0; $z = (double)2.0;
$a = 1; $b = 3;
while ($z > 0.0000000000001) {
$z *= $a / $b;
$pi += $z;
$b += 2;
$a++;
}
echo $pi."n";
echo "PHP PI() =>".pi()."n";
~~~
The source code has been compared with the pi() function that comes with PHP. The accuracy is consistent. So guess what the pi() function is? What was achieved?