C++由近似公式π/2=(2/1)*(2/3)*(4/3)*(4/5)……求π精确到10^-6
为什么下面这段代码输出总是0#include<iostream>#include<cmath>usingnamespacestd;intmain(){doublepi=1...
为什么下面这段代码输出总是0
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double pi=1,c;
int i=1;
while(1){
if(i%2==0)c=(double)i/(double)(i+1);
else c=(double)(i+1)/(double)i;
pi=pi*c;
i++;
if(c<1e-6)break;
}
cout << 2*pi<< endl;
return 0;
} 展开
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double pi=1,c;
int i=1;
while(1){
if(i%2==0)c=(double)i/(double)(i+1);
else c=(double)(i+1)/(double)i;
pi=pi*c;
i++;
if(c<1e-6)break;
}
cout << 2*pi<< endl;
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询