C++基础01

//公式求π
//π=16arctan(1/5)-4arctan(1/239)
//arctan(x)公式arctan(x)=x/1-x*x*x/3+x*x*x*x*x/5+...=∑(-1)的n次方*x的2n+1次方/2n+1
#include<iostream>
using namespace std;

int main()
{
	double a,b;
	double arctan(double x);
	a=16.0*arctan(1/5.0);
	b=4.0*arctan(1/239.0);
	cout<<"PI="<<a-b<<endl;
	return 0;
}

double arctan(double x)
{
int i;
double r,e,f,sqr;
sqr=x*x;
r=0;
e=x;
i=1;

while(e/i>1e-15)
{
	f=e/i;
	r=(i%4==1)?r+f:r-f;
	e=e*sqr;
	i+=2;
}
return r;
}

你可能感兴趣的:(C++基础01)