PHP: Calculate PI

Note: Newer version of the pi calculation is here and a even more faster one here.
I want to release some Benchmark system can be uploaded to any web host and check how good the web hosting is. Pi (?) calculations spread widely as one of the most popular CPU benchmark system, that's why I have created a Pi calculator that can get to the accuracy you want. For now, there is still one thing I'm not sure yet, I will explain it after show you the code.

function bcpi($precision=30){
        $accuracy = ($precision+5)*45/32;
//accuracy worked till the 180th $precision, almost used 30 second to calculate
	bcscale($precision+5);
	$n = 1;
	$bcatan1 = 0;
	$bcatan2 = 0;
	while($n < $accuracy){
//atan functions
		$bcatan1 = bcadd($bcatan1, bcmul(bcdiv(pow(-1, $n+1), $n * 2 - 1), bcpow(0.2, $n * 2 -1)));
		$bcatan2 = bcadd($bcatan2, bcmul(bcdiv(pow(-1, $n+1), $n * 2 - 1), bcpow(bcdiv(1,239), $n * 2 -1)));
		++$n;
	}
	return	bcmul(4,bcsub(bcmul(4, $bcatan1),$bcatan2),5);
}

This Pi formula is not the fastest(converge speed), but it is one of the easiest. it uses Machin's formula + Taylor Series work together.

So, this is the BCMath version of atan() and pi() .
Why not use the original pi()? because from the statement of Keamos at gmail on 13-Jan-2006 09:31, it shows pi() have a precision limit. Note this script is very slow, and the time it need is increase squarely, so 100 precision could use up to 1 second.

Now, I have only one problem, the Pi's accuracy, Pi's accuracy are not the same as it's precision. Precision is the number of decimal it returns, the accuracy is the closeness to the real pi's value rounded the same precision. I want to output any precision with 100% accuracy, then I must find the relationship between two of them. You guessed it right, I don't know :aok:
Edit: it looks like precision:accuracy is somewhere around 45:32.
Just Added on 2007-01-20: cut off the last few numbers because it usually not 100% accurate
If anything know something about that, please tell me about it. Thanks in advance.


Comments

Anonymous's picture

[...] PHP, como curiosidad

[...] PHP, como curiosidad tiene definida la funci?n pi(), que devuelve una aproximaci?n a esta constante (3.1415926535898), adem?s dispone de una variable constante llamada M_PI que nos devuelve exactamente el mismo valor, limitado a 14 decimales. Pero si somos una persona que nos gusta complicarnos la vida, o simplemente presentarnos al proximo concurso de recordadores de decimales de PI, esta es nuestra funci?n,… Alvy, esto va por ti [...]

Anonymous's picture

WebDevLogs: PHP: Calculate

WebDevLogs: PHP: Calculate PI...

...

Anonymous's picture

[...] PHP: Calculate PI

[...] PHP: Calculate PI [...]

Post new comment

  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <span> <fn>
  • Lines and paragraphs break automatically.
  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].
  • Mathematical equations and graphs can be added between [tex] and [/tex], [graph] and [/graph] tags.
  • Textual smileys will be replaced with graphical ones.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Honey Pot that kill bots