c - Calculate the value of e^x -

i need calculate value of e^x. loop infinity , should stop when precision 0.001, not know how precision.
float exp(float x){ float ex=1,precision=0.000,powx=1.0,fat=1.0; int partint; int i,j; for(i=1; precision!= 0.001; i++){ for(j=1; j<=i; j++){ powx *= x; fat *=j; } ex += powx/fat; partint = ex; // cast integer precision= ex - partint; // precision //printf("%f %f %f %f\n",powx,fat,precision,ex); powx = fat = 1.0; } return ex; } int main(){ printf("%f",exp(2)); return 0; }
you use double prev keep track of result produced previous iteration. stop when abs(prev - ex) < precision.
Comments
Post a Comment