首页 > 编程语言 > 详细

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

时间:2015-12-17 20:59:52      阅读:820      评论:0      收藏:0      [点我收藏+]
 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的值》

原文:http://www.cnblogs.com/sun-/p/5055171.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!