ccmny

arctg x

Jan 26th, 2012
116
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 <math.h>
  4.  
  5. typedef struct
  6. {
  7.     double value;
  8.     int elnum;
  9.     int precreached;
  10. } Result;
  11.  
  12. Result arctg(double x, double eps, int m);
  13.  
  14. int main(int argc, char ** argv)
  15. {
  16.     double dx;
  17.     double precision;
  18.     int m;
  19.     double x;
  20.     Result tempresult;
  21.    
  22.     puts("Enter dx");
  23.     fflush(stdin);
  24.     if(scanf("%lf", &dx) == EOF)
  25.         return 1;
  26.  
  27.     puts("Enter precision");
  28.     fflush(stdin);
  29.     if(scanf("%lf", &precision) == EOF)
  30.         return 1;
  31.  
  32.     puts("Enter number of samples");
  33.     fflush(stdin);
  34.     if(scanf("%d", &m) == EOF)
  35.         return 1;
  36.    
  37.     //dx = 0.01l;
  38.     //precision = 0.0000000000000000000000000000000000000000000000000000000001l;
  39.     //m = 1000;
  40.    
  41.     x = -1.0l;
  42.  
  43.     while((x += dx) < 1.0f)
  44.     {
  45.         tempresult = arctg(x, precision, m);
  46.        
  47.         printf("%8.5f, %15.12f, arctg, %5d, PRECISION %s\n",
  48.                 x,
  49.                 tempresult.value,
  50.                 tempresult.elnum,
  51.                 (tempresult.precreached ?
  52.                     "REACHED" : "NOT REACHED"));       
  53.     }
  54.     return 0;
  55. }
  56.  
  57. Result arctg(double x, double eps, int m)
  58. {
  59.     int i;
  60.     double delta = 2 * eps;
  61.     Result result;
  62.     result.value = 0l;
  63.    
  64.     for(i = 0; i < m && fabs(delta) >= eps; i++)
  65.     {
  66.         delta = pow(-1, i) * (pow(x, (2 * i) + 1)) / ((2 * i) + 1);
  67.         result.value += delta;
  68.     }
  69.  
  70.     if(fabs(delta) < eps)
  71.         result.precreached = 1;
  72.     else
  73.         result.precreached = 0;
  74.  
  75.     result.elnum = i;
  76.     return result;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment