Cpp求PI

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstdio>

using namespace std;

double arctan(double x);
inline double getItem(double x,int i);

int main(int argc, char *argv[])
{
	bool b1,b2,b3;
	cout <<"test 1E-15:"<< 1E-15<<endl;
	cout << "测试逗号表达式:" << (b1=true,b2=false)<<endl;
	
	double pi = 16.0 * arctan(1.0/5.0) - 4.0 * arctan(1.0/239.0);

	cout<< pi<<endl;
	printf("pi=%.30lf \n",pi);
	
    return 0;
}

double arctan(double x){
	double sum = 0,item = 0;
	for(int i=0; (item = getItem(x,i)),(abs(item) >= 1E-15); ++i){
		sum += item;
	}
	return sum;
}

inline double getItem(double x,int i){
	return pow(-1,i%2) * pow(x, 2 * i + 1) /
		(2 * i + 1);
}

  

posted @ 2013-05-15 01:04  庚武  Views(359)  Comments(0Edit  收藏  举报