尝试使用 C# 将 Pi 计算为小数点后 N 位

发布于 2024-08-20 08:12:24 字数 305 浏览 1 评论 0 原文

注意:我已经阅读过此主题,但我不知道不理解它并且它没有提供我可以使用的解决方案。我对数字问题感到很糟糕。

将 Pi 生成用户想要的小数位数的简单方法是什么?这不是家庭作业,只是尝试完成此处列出的一些项目:

链接

Note: I've already read this topic, but I don't understand it and it doesn't provide a solution I could use. I'm terrible with number problems.

What's a simple way to generate Pi to what number of decimals a user wants? This isn't for homework, just trying to complete some of the projects listed here:

Link

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

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

发布评论

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

评论(3

记忆消瘦 2024-08-27 08:12:24

计算 pi 数字的经典算法是 Gauss-Legendre 算法。虽然它不如一些更现代的算法那么快,但它确实具有易于理解的优点。

Let

a_0 = 1
b_0 = 1/Sqrt(2)
t_0 = 1/4
p_0 = 1

then

a_(n+1) = (a_n + b_n) / 2
b_(n+1) = Sqrt(a_n * b_n)
t_(n+1) = t_n - p_n * (a_n - a_(n+1))^2
p_(n+1) = 2 * p_n

then

pi =. (a_n + b_n)^2 / (4 * t_n)

Here(=. 表示“大约等于”)该算法表现出二次收敛(每次迭代时正确的小数位数加倍)。

我将让您将其转换为 C#,包括发现任意精度算术库。

A classic algorithm for calculating digits of pi is the Gauss-Legendre algorithm. While it is not as fast as some of the more modern algorithms it does have the advantage of being understandable.

Let

a_0 = 1
b_0 = 1/Sqrt(2)
t_0 = 1/4
p_0 = 1

Then

a_(n+1) = (a_n + b_n) / 2
b_(n+1) = Sqrt(a_n * b_n)
t_(n+1) = t_n - p_n * (a_n - a_(n+1))^2
p_(n+1) = 2 * p_n

Then

pi =. (a_n + b_n)^2 / (4 * t_n)

Here (=. means "approximately equal to") This algorithm exhibits quadratic convergence (the number of correct decimal places doubles with each iteration).

I'll leave it to you to translate this to C# including discovering an arbitrary-precision arithmetic library.

假装爱人 2024-08-27 08:12:24

您谈论的主题使用泰勒级数计算 PI 值。使用该主题中编写的函数“double F (int i)”将为您提供“i”项后的 PI 值。

这种计算 PI 的方法有点慢,我建议您查看 PI快速算法

您还可以在此处找到一种实现,可将 PI 计算到第 n 位。

祝你好运!

The topic your talking about calculate the value of PI using the taylor series. Using the function "double F (int i)" wrote on that topic will give you the value of PI after "i" terms.

This way of calculating PI is kind of slow, i suggest you to look at the PI fast algorithm.

You can also find one implementation here that get the calculate PI to the n th digit.

Good luck!

沙与沫 2024-08-27 08:12:24

如果您仔细研究这个非常好的指南:

并行编程模式:使用 .NET Framework 4 理解和应用并行模式

您将在第 70 页找到这个可爱的实现(我这边做了一些细微的更改):

static decimal ParallelPartitionerPi(int steps)
{
    decimal sum = 0.0;
    decimal step = 1.0 / (decimal)steps;
    object obj = new object();
    Parallel.ForEach(Partitioner.Create(0, steps),
        () => 0.0,
        (range, state, partial) =>
            {
                for (int i = range.Item1; i < range.Item2; i++)
            {
                decimal x = (i + 0.5) * step;
                partial += 4.0 / (1.0 + x * x);
            }
            return partial;
        },
        partial => { lock (obj) sum += partial; });
    return step * sum;
}

If you take a close look into this really good guide:

Patterns for Parallel Programming: Understanding and Applying Parallel Patterns with the .NET Framework 4

You'll find at Page 70 this cute implementation (with minor changes from my side):

static decimal ParallelPartitionerPi(int steps)
{
    decimal sum = 0.0;
    decimal step = 1.0 / (decimal)steps;
    object obj = new object();
    Parallel.ForEach(Partitioner.Create(0, steps),
        () => 0.0,
        (range, state, partial) =>
            {
                for (int i = range.Item1; i < range.Item2; i++)
            {
                decimal x = (i + 0.5) * step;
                partial += 4.0 / (1.0 + x * x);
            }
            return partial;
        },
        partial => { lock (obj) sum += partial; });
    return step * sum;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文