c++ 编写π的公式 疑问?

来源:百度知道 编辑:UC知道 时间:2024/06/16 06:47:45
1 i定义为double类型为什么报错: error C2296: '%' : illegal, left operand has type 'double'
Error executing cl.exe.

2 运行结果怎么只到3.14159 怎么改程序精确到3.1415926.....n位

i定义为double类型的程序如下:
#include <iostream.h>
double arctan(double x);

void main()
{
double a,b;
a=16*arctan(1.0/5.0);
b=4*arctan(1.0/239.0);

cout<<"PI="<<a-b<<endl;

}

double arctan(double x)
{
double i;
double r,f,e,sqr;
r=0;
e=x;
i=1;
sqr=x*x;

while(e/i>1e-15)
{
f=e/i;
r=(i%4==1) ? r+f : r-f;
e=e*sqr;
i+=2;
}
return r;

}

#include <iostream.h>
double arctan(double x);

void main()
{
double a,b;
a=16*arctan(1.0/5.0);
b=4*arctan(1.0/239.0);

cout<<"PI="<<a-b<<endl;

}

double arctan(double x)
{
int i; //注:只有整数才能用%求模
double r,f,e,sqr;
r=0;
e=x;
i=1;
sqr=x*x;

while(e/i>1e-15)
{
f=e/i;
r=(i%4==1) ? r+f : r-f; //注:只有整数才能用%求模
e=e*sqr;
i+=2;
}
return r;

}