Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<math.h>
- long double factorial(int itr)
- {
- long double f=1.,i;
- for(i=1.;i<=itr;++i)
- f*=i;
- return f;
- }
- long double my_exp(double x)
- {
- long double exp1=1.,exp2=0,eps;
- int itr=1;
- do
- {
- exp1+=pow(x,itr)/factorial(itr);
- eps=exp1-exp2;
- exp2=exp1;
- itr++;
- }
- while(fabs(eps)>1e-16);
- return exp1;
- }
- int main()
- {
- double x;
- printf("Enter x: ");
- scanf("%lf",&x);
- printf("Library exp= %0.16lf\n",exp(x));
- printf("My exp = %0.16Lf\n",my_exp(x));
- }
- /*
- IO #1
- Enter x: 5
- Library exp= 148.4131591025765999
- My exp = 148.4131591025766034
- IO #2
- Enter x: 6.6678
- Library exp= 786.6630406518070231
- My exp = 786.6630406518070064
- IO #3
- Enter x: 1
- Library exp= 2.7182818284590451
- My exp = 2.7182818284590452
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement