《用C++語言編寫一個程序,求PI的值》


 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 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM