PHP 中如何将 pi 计算为指定位数?

发布于 2024-08-13 03:18:07 字数 251 浏览 1 评论 0原文

如何在 PHP 中计算 pi 的值,最多 X 个十进制数字。

小数点后 4 位

3.141

64 位小数

3.141592653589793238462643383279502884197169399375105820974944592

How can I calculate the value of pi in PHP up to X decimal numbers.

4 decimal points

3.141

64 decimal points

3.141592653589793238462643383279502884197169399375105820974944592

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

緦唸λ蓇 2024-08-20 03:18:07

找到@Konamiman 发布的损坏链接的来源。

将结果与:http://www.angio.net/pi/digits/50 进行比较。 txt 并且它们是相同的。

// 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);

Found the source for the broken link @Konamiman posted.

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);
灼痛 2024-08-20 03:18:07

在php中计算PI?

当我们已经有了 M_PI 常量时,为什么还要进行计算呢?

M_PI 包含值 3.14159265358979323846

对于 PI 来说,10000 位小数是一个相当大的数字。

请参阅:http://www.php.net/manual/en/math。常量.php

Calculating PI in php?

Why calculate when we already have the M_PI constant?

M_PI contains the value 3.14159265358979323846.

10000 decimal places for PI is quite a number.

See: http://www.php.net/manual/en/math.constants.php

凯凯我们等你回来 2024-08-20 03:18:07

您可以通过 Chudnosky 级数计算 Pi。如果您的服务器具有非常高的内存,您可以轻松地将 Pi 计算到许多小数位。如果您想将其获取到某个特定的小数位,请将努力增加到一个非常大的数字并使用 substr 将其缩短到所需的小数位。如果你觉得这个过程很慢,那么你可以通过google搜索得到pi值。

You can calculate Pi by the Chudnosky series. If you have a server with a very high ram you can easily calculate Pi to many decimal places. If you want to get it to some specific decimal place then, increase the efforts to a really large number and use substr to shorten it to your required decimal place. If you think this process is very slow, then you can get pi value by searching it on google.

谁对谁错谁最难过 2024-08-20 03:18:07

据我所知,PHP 无法保存那么长的数字,但 22/7 是一种计算 PI 的古老方法,尽管这不会比 PHP 中包含的 PI 常数更好。也许您正在尝试输出一长串 PI 作为练习。

PHP cannot hold a number that long as far as I know, but 22/7 is an ancient way to calculate PI, though that will not be any better than the PI constant included in PHP. Perhaps you are trying to output a long string of PI as an exercise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文