您的位置:首页 > 编程语言 > C语言/C++

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

2015-12-17 20:08 489 查看
//编写一个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 = 0;
int i = 1;
while(e/i>1e-16){
double f = e/i;
r = (i%4==1)?r+f:r-f;
e = e*sqr;
i+=2;
}
return r;
}
int main()
{
double a = 16.0*arctan(1/5.0);
double b = 4.0*arctan(1/239.0);
cout<<"PI="<<a-b<<endl;
system("pause");
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: