1 //编写一个C++程序求PI的值
 2 /*
 3  PI=16arctan(1/5)-4arctan(1/239)
 4  其中arctan用如下形式的极数计算:
 5     arctan=x-(x^3/3)+(x^5/7)-(x^7/7)+...
 6 */
 7 #include<iostream>
 8 using namespace std;
 9 double arctan(double x){
10     double sqr = x*x;
11     double e = x;
12     double r = 0;
13     int i = 1;
14     while(e/i>1e-16){
15         double f = e/i;
16         r = (i%4==1)?r+f:r-f;
17         e = e*sqr;
18         i+=2;
19     }
20     return r;
21 }
22 int main()
23 {
24     double a = 16.0*arctan(1/5.0);
25     double b = 4.0*arctan(1/239.0);
26     cout<<"PI="<<a-b<<endl;
27     system("pause");
28     return 0;
29 }

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