《用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 }

转载于:https://www.cnblogs.com/sun-/p/5055171.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值