Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5. #include "f.h"
  6.  
  7. int main(void)
  8. {
  9.     double x,res,eps;
  10.     clock_t time;
  11.    
  12.     printf("x and EPS ");
  13.     if((scanf("%lf",&x)!=1) || scanf("%lf",&eps)!=1)
  14.     {
  15.         fprintf(stderr,"Can not read x or eps\n");
  16.         return ERROR_READ;
  17.     }
  18.     if(eps<0)
  19.     {
  20.         fprintf(stderr,"Incorrect EPS!\n");
  21.         return ERROR_READ;
  22.     }
  23.     time = clock();
  24.     res = fun(x,eps);
  25.     printf("RESULT: %lf\nCalculation error: %lf\nTIME: %lf\n",res,fabs(res - exp(x)),((double)(clock() - time))/CLOCKS_PER_SEC);
  26.    
  27.     return 0;
  28. }
  29. #include <math.h>
  30. #include "f.h"
  31.  
  32. double fun(double x, double eps)
  33. {
  34.     int i,flag = 0;
  35.     double res = 1,k = 1;
  36.  
  37.     if(x<0)
  38.     {
  39.         flag++;
  40.         x *= -1;
  41.     }
  42.     for(i = 1;k>eps;i++)
  43.     {
  44.         k *= x/i;
  45.         res += k;
  46.     }
  47.     if(flag%2 == 1)
  48.         res = 1/res;
  49.     return res;
  50. }
  51. #ifndef F_H
  52. #define F_H
  53.  
  54. double fun(double x0, double eps);
  55.  
  56. #define ERROR_READ 1
  57. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement