c - Calculate the value of e^x -


enter image description here

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

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -