如何在php中将pi计算为设定的位数? [英] How can pi be calculated to a set number of digits in PHP?
本文介绍了如何在php中将pi计算为设定的位数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何计算PHP中pi的值(最多X个十进制数字).
How can I calculate the value of pi in PHP up to X decimal numbers.
4个小数点
3.141
3.141
64个小数点
3.141592653589793238462643383279502884197169399375105820974944592
3.141592653589793238462643383279502884197169399375105820974944592
推荐答案
找到了@Konamiman发布的断开链接的来源.
Found the source for the broken link @Konamiman posted.
将结果与以下内容进行比较: http://www.angio.net/pi/digits /50.txt 和它们相同.
Compared the results to: http://www.angio.net/pi/digits/50.txt and they are the same.
// Source: http://mgccl.com/2007/01/22/php-calculate-pi-revisited
function bcfact($n)
{
return ($n == 0 || $n== 1) ? 1 : bcmul($n,bcfact($n-1));
}
function bcpi($precision)
{
$num = 0;$k = 0;
bcscale($precision+3);
$limit = ($precision+3)/14;
while($k < $limit)
{
$num = bcadd($num, bcdiv(bcmul(bcadd('13591409',bcmul('545140134', $k)),bcmul(bcpow(-1, $k), bcfact(6*$k))),bcmul(bcmul(bcpow('640320',3*$k+1),bcsqrt('640320')), bcmul(bcfact(3*$k), bcpow(bcfact($k),3)))));
++$k;
}
return bcdiv(1,(bcmul(12,($num))),$precision);
}
echo bcpi(1000);
这篇关于如何在php中将pi计算为设定的位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文