Using basic arithmetics for calculating Pi with arbitary precision

半世苍凉 提交于 2019-12-05 10:04:02

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

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

Codepad link:

#include <iostream>
#include <cmath>
int main()
{
    double p16 = 1, pi = 0, precision = 10;

    for(int k=0; k<=precision; k++)
    {
        pi += 1.0/p16 * (4.0/(8*k + 1) - 2.0/(8*k + 4) - 1.0/(8*k + 5) - 1.0/(8*k+6));
        p16 *= 16;
    }
    std::cout<<std::setprecision(80)<<pi<<'\n'<<M_PI;
}

Output:

3.141592653589793115997963468544185161590576171875
3.141592653589793115997963468544185161590576171875

This is actually the Bailey-Borwein-Plouffe formula, also taken from the link from wikipedia.

You can just do the Taylor envelope of the arctan(1) and then you will get pi/4 just summing all the rest part. The taylor envelope of arctan(1)

http://en.wikipedia.org/wiki/Taylor_series

also you can use the euler formula with z=1 and then multiply the result by 4.

http://upload.wikimedia.org/math/2/7/9/279bed5a2ea3b80a71f5b22078090168.png

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!