![]() |
#2
寒风中的细雨2010-10-01 22:47
|
下面的代码是我写的,应该没有问题了,可是输出地时候sum的值出现问题了。我截图给大家看哈,编译器VC6!

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x,Item=1,sum=0;
double nume=1,deno=1;
int sign=1,i=1;
cout<<"Please enter the number:\n";
cin>>x;
while (fabs(Item)>=1e-8)
{
sum+=Item;
nume*=x;
deno*=i++;
Item=sign*nume/deno;
sign*=-1;
}
cout.precision(8);
cout<<"The result of "<<x<<" is "<<sum<<endl;
return 0;
}
#include <cmath>
using namespace std;
int main()
{
double x,Item=1,sum=0;
double nume=1,deno=1;
int sign=1,i=1;
cout<<"Please enter the number:\n";
cin>>x;
while (fabs(Item)>=1e-8)
{
sum+=Item;
nume*=x;
deno*=i++;
Item=sign*nume/deno;
sign*=-1;
}
cout.precision(8);
cout<<"The result of "<<x<<" is "<<sum<<endl;
return 0;
}
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
后面Double型的sum输出就变为-1.#IND了,大家指教一下这是怎么回事儿?