Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.78 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 - cos(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.         else
  55.             flag++;
  56.         k = x;
  57.                 res = x;
  58.                 for(i = 1;k>eps;i++)
  59.                 {
  60.                         k *= x*x/(2*i*(2*i + 1));
  61.                         if(i%2)
  62.                                 res -= k;
  63.                         else
  64.                                 res += k;
  65.                 }
  66.     }else
  67.     {
  68.                 k = 1;
  69.                 res = 1;
  70.                 for(i = 1;k>eps;i++)
  71.                 {
  72.                         k *= x*x/((2*i-1)*2*i);
  73.                         if(i%2)
  74.                                 res -= k;
  75.                         else
  76.                                 res += k;
  77.                 }
  78.     }
  79.     if(flag%2)
  80.         res *= -1;
  81.     return res;
  82. }
  83. #ifndef F_H
  84. #define F_H
  85.  
  86. double fun(double x0, double eps);
  87.  
  88. #define ERROR_READ 1
  89. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement