Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 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 - sin(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,k;
  36.  
  37.     if(x<0)
  38.     {
  39.         x *= -1;
  40.         flag = 1;
  41.     }
  42.     x -= floor(x/(2*M_PI))*2*M_PI;
  43.     if(x>M_PI)
  44.     {
  45.         x -= M_PI;
  46.         flag++;
  47.     }
  48.  
  49.     if(x>1)
  50.     {
  51.         x -= M_PI/2;
  52.         if(x<0)
  53.             x*= -1;
  54.         k = 1;
  55.         res = 1;
  56.         for(i = 1;k>eps;i++)
  57.         {
  58.             k *= x*x/((2*i-1)*2*i);
  59.             if(i%2)
  60.                 res -= k;
  61.             else
  62.                 res += k;
  63.         }
  64.     }else
  65.     {
  66.         k = x;
  67.         res = x;
  68.         for(i = 1;k>eps;i++)
  69.         {
  70.             k *= x*x/(2*i*(2*i + 1));
  71.             if(i%2)
  72.                 res -= k;
  73.             else
  74.                 res += k;
  75.         }
  76.     }
  77.     if(flag%2)
  78.         res *= -1;
  79.     return res;
  80. }
  81. #ifndef F_H
  82. #define F_H
  83.  
  84. double fun(double x0, double eps);
  85.  
  86. #define ERROR_READ 1
  87. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement