-
October 22nd, 2010, 08:57 AM
#1
getting wrong result with my program
program will approximate the value of e...when x=5.7.. but i am not getting the result right . could anybody help me and show my mistake please...
x=1+1/1!+1/2!+1/3!+1/4!+1/5!+.......
when the difference between two successive approximations is less tan 10e-9
Code:
int main()
{
double x,myexp1,myexp2,expx,fact,i;
fact=1;
ofstream output("e:result12.dat");
cout<<" ....."<<endl;
output<<" ..... "<<endl;
x=5.7;
myexp1=1;
myexp2=x;
i=2;
expx=myexp1+myexp2;
while (fabs(expx-myexp1 >= 10e-9))
{
fact=fact*1;
myexp1=expx;
expx=expx+pow(x,i)/fact;
i=i+1;
cout<< "..... " <<"i= "<<expx<<endl;
output<< ".... " <<"i= "<<expx<<endl;
}
system("PAUSE");
return 0;
}
-
October 22nd, 2010, 09:07 AM
#2
Re: getting wrong result with my program
Your factorial is incorrect :
Code:
fact = fact * 1; // should be fact*i
-
October 22nd, 2010, 09:09 AM
#3
Re: getting wrong result with my program
Originally Posted by buldogao87
Code:
int main()
{
double x,myexp1,myexp2,expx,fact,i;
fact=1;
ofstream output("e:result12.dat");
cout<<" ....."<<endl;
output<<" ..... "<<endl;
x=5.7;
myexp1=1;
myexp2=x;
i=2;
expx=myexp1+myexp2;
while (fabs(expx-myexp1 >= 10e-9))
{
fact=fact*1;
myexp1=expx;
expx=expx+pow(x,i)/fact;
i=i+1;
cout<< "..... " <<"i= "<<expx<<endl;
output<< ".... " <<"i= "<<expx<<endl;
}
system("PAUSE");
return 0;
}
The red line doesn't make any sense!
Did you mean// Edit: Wow! Philip was faster!
Victor Nijegorodov
-
October 22nd, 2010, 10:15 AM
#4
Re: getting wrong result with my program
yes ... Thanks so much...
-
October 22nd, 2010, 01:32 PM
#5
Re: getting wrong result with my program
This is also incorrect:
Code:
while (fabs(expx-myexp1 >= 10e-9))
It should be:
Code:
while (fabs(expx-myexp1) >= 10e-9)
Without that change I get an error C2668. BTW: If the compiler would tolerate the wrong statement, it would even work, since e is approached from below...
But I had to make some more changes to your program to actually compile and correctly calculate the (approximate) value of e. And only that way it matches the approximation formula from post #1. Here is the corrected version, with the changes marked in red:
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <math.h>
using namespace std;
int main()
{
double /* x, */myexp1,myexp2,expx,fact,i;
fact=1;
ofstream output("e:result12.dat");
cout<<" ....."<<endl;
output<<" ..... "<<endl;
// x=5.7; // That's not needed anymore
myexp1=1;
myexp2=1;
i=2;
expx=myexp1+myexp2;
cout << setprecision(12); // In order to see all changes up to the last iteration
output << setprecision(12); // ditto
while (fabs(expx-myexp1) >= 10e-9)
{
fact=fact*i;
myexp1=expx;
expx=expx+1.0/fact;
i=i+1;
cout<< "..... " <<"i= "<<expx<<endl;
output<< ".... " <<"i= "<<expx<<endl;
}
system("PAUSE");
return 0;
}
I hope I didn't miss any change.
... And I hope this wasn't homework I now have done for you...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|