i know there many examples of problem tried write different 1 myself. using taylor series e^x = 1 + x/1! + x^2/2! + x^3/3! + ...... code compiles , runs wont output correct answer imputes , i'm not sure why. usable code or should scrap it?
#include <iostream> #include <cmath> using namespace std; int main() { double final,power,end_n = 1.0,e=1.0,x=2.0, n; cout<< "n: "; // enter 5 test cin>> n; while (n>1){ power = pow(x,n); end_n = end_n*n; e= (power/end_n)+e; n--; } final =e+x; cout<< final; return 0; }
i have no idea reasoning is, @ all. code particular expansion trivially simple:
double x; cin >> x; double oldres, res=1, top=1, bottom=1; int iter=1; { oldres=res; // calculate difference between iterations ++iter; // next iteration top*=x; // multiply 1 x each iteration bottom*=(iter-1); // multiply iteration number each iteration res+=top/bottom; // , add fraction result } while(fabs(res-oldres)>.1); // while difference still large cout << res; // done, show result
Comments
Post a Comment