Home  >  Article  >  Backend Development  >  PHP implements simple and high-precision PI method

PHP implements simple and high-precision PI method

巴扎黑
巴扎黑Original
2016-12-07 13:51:411229browse

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?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn