PHP implements simple and high-precision PI method

巴扎黑
Release: 2016-11-08 09:29:26
Original
1095 people have browsed it

This article is the continuation of the previous article [PHP implements dart throwing method to find PI, the stupidest but most interesting](http://www.yinqisen.cn/blog-676.html)

After talking about the stupidest thing, let’s talk about one more 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, and the accuracy is consistent. So guess how the pi() function is implemented?

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