This is also incorrect:
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...Code:while (fabs(expx-myexp1) >= 10e-9)
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:
I hope I didn't miss any change.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; }
... And I hope this wasn't homework I now have done for you...





Reply With Quote