Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 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 || x<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 - log(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 counter1 = 0,counter2 = 0,i;
  35.         double res = 0,k = 1;
  36.        
  37.    
  38.         while(x >= 1)
  39.         {  
  40.                 x /= 2;
  41.                 counter1 ++;
  42.         }
  43.         while(x <= 0.1)
  44.         {
  45.                 x *= 2;
  46.                 counter2 ++;
  47.         }
  48.        
  49.         x -= 1;
  50.         for(i = 1;fabs(k/(1+x))>eps;i++)
  51.         {      
  52.                 k *= x;
  53.                 if(i%2 == 0)
  54.                         res -= k/i;
  55.                 else
  56.                         res += k/i;
  57.         }
  58.        
  59.  
  60.         res += (counter1*M_LN2 - counter2*M_LN2);
  61.        
  62.         return res;
  63. }
  64. #ifndef F_H
  65. #define F_H
  66.  
  67. double fun(double x0, double eps);
  68.  
  69. #define ERROR_READ 1
  70. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement