圆周率PI
//这是一个求圆周率PI的程序,没有错误但是运行结果不对,请帮忙看一下#include<iostream.h>
#include<math.h>
double arctan(double x);
int main()
{
double a;
double b;
a = 16.0 * arctan(1 / 5.0);
b = 4.0 * arctan(1 / 239.0);
cout<<"PI= "<<a-b<<endl;
return 0;
}
double arctan(double x)arctanx=x-x3/3+x5/5-x7/7+.....由于没公式编译器,所以把x的几次方直接这么写了
{
double result = 0.0;
double sqr = x * x;
double num = x;//fenzi
int den = 1;//fenmu
double f = num / den;
int flag = -1;
while(abs(f) > 1e-15)
{
result += f;
num = num * sqr * flag;
den += 2;
f = num / den;
}
return result;
}