C++问题:求e^x=1+x+x^2/2!+x^3/3!+…+x^n/n! (x^n/n!<0.000001)

编一个C++程序:输入x,求e^x=?
#include<iostream>
#include<math.h>
using namespace std;

int main()
{
double ex, x, p;
int i;

cout<<"请输入x:"; cin>>x;

ex = 0; p = 1; i = 0;

while (1e-6)
{
ex += p;
++i;
p = p * x / i;
}

cout<<"e的"<<x<<"次方等于:"<<ex<<endl;

return 0;
}
以上程序有什么问题?

第1个回答  2009-03-07
while (1e-6)
改为:
while(fabs(p-0)>1e-6)

改过以后的代码:
#include<iostream>
#include<math.h>
using namespace std;

int main()
{
double ex, x, p;
int i;

cout<<"请输入x:"; cin>>x;

ex = 0; p = 1; i = 0;

while(fabs(p-0)>1e-6)
{
ex += p;
++i;
p = p * x / i;
}

cout<<"e的"<<x<<"次方等于:"<<ex<<endl;

return 0;
}本回答被提问者采纳
第2个回答  2009-03-07
while (1e-6)???
相似回答