講完最笨的,再說一個更精巧的方法,程式碼如下:
~~~.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.000000000000 {
$z *= $a / $b;
$pi += $z;
$b += 2;
) =>".pi()."n";
~~~
源碼中和PHP自帶的pi()這個函數做了對比,精確度一致,那猜猜pi()函數是如何實現的呢?