//编写一个C++程序求PI的值
/*
PI=16arctan(1/5)-4arctan(1/239)
其中arctan用如下形式的极数计算:
arctan=x-(x^3/3)+(x^5/7)-(x^7/7)+...
*/
#include<iostream>
using namespace std;
double arctan(double x){
double sqr = x*x;
double e = x;
double r = ;
int i = ;
while(e/i>1e-){
double f = e/i;
r = (i%==)?r+f:r-f;
e = e*sqr;
i+=;
}
return r;
}
int main()
{
double a = 16.0*arctan(/5.0);
double b = 4.0*arctan(/239.0);
cout<<"PI="<<a-b<<endl;
system("pause");
return ;
}

《用C++语言编写一个程序,求PI的值》-LMLPHP

05-08 15:32