求π的值
点击查看代码
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double arctanx(double x)
{
double sum=0.0;
double one=x;
int n=1;
while (fabs(one) >= 0.000001)
{
if (n % 4 == 1)
{
sum = sum + one;
}
else if (n % 4 == 3)
{
sum = sum - one;
}
one = one * n * x * x;
n = n + 2;
one = one / n;
}
return sum;
}
int main()
{
double x=1.0/sqrt(3);
double pi;
pi = 6*arctanx(x);
cout << fixed << setprecision(10) << pi << endl;
return 0;
}

浙公网安备 33010602011771号