编写程序求π的值

#include<iostream>
using namespace std;

double power(double x, int n)
{
    double vie = 1.0;
    while (n--)
    {
        vie *= x;
    }
    return vie;
}

double arctan(double x)
{
    int i = 1;
    double sum = 0;
    while (power(x, 2 * i - 1) > 1e-15)
    {
        sum += power(x, 2 * i - 1) / (2 * i - 1)*power(-1, i - 1);
        i++;
    }
    return sum;
}
int main()
{
    double PI;
    PI = 16 * arctan(1 / 5.0) - 4 * arctan(1 / 239.0);
    cout << PI << endl;
    return 0;
}

 

posted @ 2017-11-23 12:04  暮雨青枫  阅读(855)  评论(0编辑  收藏  举报