使用基本的算术计算皮与arbitary precision算术、基本、precision、arbitary

2023-09-11 23:04:56 作者:随风流浪

我要寻找一个公式/算法来计算PI〜3.14在一个给定的precision。

I am looking for a formula/algorithm to calculate PI~3.14 in a given precision.

公式/算法必须只有非常基本的算术为

The formula/algorithm must have only very basic arithmetic as

+:增加 - :减法 *:乘法 /:分裂

因为我想实现C ++这些操作并希望保持执行尽可能简单(无BIGNUM库是允许的)。

because I want to implement these operations in C++ and want to keep the implementation as simple as possible (no bignum library is allowed).

我发现,这个公式来计算Pi为pretty的简单:

I have found that this formula for calculating Pi is pretty simple:

Pi/4 = 1 - 1/3 + 1/5 - 1/7 + ...  = sum( (-1)^(k+1)/(2*k-1) , k=1..inf )

(注意,(-1)^(K + 1)可以通过上述操作员很容易地实现)。

(note that (-1)^(k+1) can be implemented easily by above operators).

但是关于这个公式的问题是无法以指定的位数来计算的数目。换句话说,没有直接的方法来确定何时停止计算。

But the problem about this formula is the inability to specify the number of digits to calculate. In other words, there is no direct way to determine when to stop the calculation.

也许一个解决此问题的是计算之间的的区别N-1 和第 N 次核算的长期和考虑到它作为当前误差

Maybe a workaround to this problem is calculating the difference between n-1th and nth calculated term and considering it as the current error.

不管怎样,我要寻找一个公式/算法,具有这些特性,也收敛速度快于丕

Anyway, I am looking for a formula/algorithm that have these properties and also converges faster to Pi

推荐答案

在你原来的(收敛速度慢)为例,该误差项,可以计算,因为这是一个的交流系列的;看到 http://en.wikipedia.org/wiki/Alternating_series#Approximating_Sums

In your original (slowly converging) example, the error term can be computed because this is an alternating series; see http://en.wikipedia.org/wiki/Alternating_series#Approximating_Sums

从本质上讲,下一个uncomputed项是一个绑定上的错误。

Essentially, the next uncomputed term is a bound on the error.