Advertisement
montimaj

EXP

Jun 28th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3. long double factorial(int itr)
  4. {
  5.     long double f=1.,i;
  6.     for(i=1.;i<=itr;++i)
  7.         f*=i;
  8.     return f;
  9. }
  10. long double my_exp(double x)
  11. {
  12.     long double exp1=1.,exp2=0,eps;
  13.     int itr=1;
  14.     do
  15.     {
  16.       exp1+=pow(x,itr)/factorial(itr);
  17.       eps=exp1-exp2;
  18.       exp2=exp1;
  19.       itr++;
  20.     }
  21.     while(fabs(eps)>1e-16);
  22.     return exp1;
  23. }
  24. int main()
  25. {
  26.     double x;
  27.     printf("Enter x: ");
  28.     scanf("%lf",&x);
  29.     printf("Library exp= %0.16lf\n",exp(x));
  30.     printf("My exp     = %0.16Lf\n",my_exp(x));
  31. }
  32. /*
  33. IO #1
  34. Enter x: 5
  35. Library exp= 148.4131591025765999
  36. My exp     = 148.4131591025766034
  37.  
  38. IO #2
  39. Enter x: 6.6678
  40. Library exp= 786.6630406518070231
  41. My exp     = 786.6630406518070064
  42.  
  43. IO #3
  44. Enter x: 1
  45. Library exp= 2.7182818284590451
  46. My exp     = 2.7182818284590452
  47. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement