PHP implements simple and high-precision PI method

巴扎黑
Release: 2023-03-03 11:50:02
Original
1229 people have browsed it

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?

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template