Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- typedef struct
- {
- double value;
- int elnum;
- int precreached;
- } Result;
- Result arctg(double x, double eps, int m);
- int main(int argc, char ** argv)
- {
- double dx;
- double precision;
- int m;
- double x;
- Result tempresult;
- puts("Enter dx");
- fflush(stdin);
- if(scanf("%lf", &dx) == EOF)
- return 1;
- puts("Enter precision");
- fflush(stdin);
- if(scanf("%lf", &precision) == EOF)
- return 1;
- puts("Enter number of samples");
- fflush(stdin);
- if(scanf("%d", &m) == EOF)
- return 1;
- //dx = 0.01l;
- //precision = 0.0000000000000000000000000000000000000000000000000000000001l;
- //m = 1000;
- x = -1.0l;
- while((x += dx) < 1.0f)
- {
- tempresult = arctg(x, precision, m);
- printf("%8.5f, %15.12f, arctg, %5d, PRECISION %s\n",
- x,
- tempresult.value,
- tempresult.elnum,
- (tempresult.precreached ?
- "REACHED" : "NOT REACHED"));
- }
- return 0;
- }
- Result arctg(double x, double eps, int m)
- {
- int i;
- double delta = 2 * eps;
- Result result;
- result.value = 0l;
- for(i = 0; i < m && fabs(delta) >= eps; i++)
- {
- delta = pow(-1, i) * (pow(x, (2 * i) + 1)) / ((2 * i) + 1);
- result.value += delta;
- }
- if(fabs(delta) < eps)
- result.precreached = 1;
- else
- result.precreached = 0;
- result.elnum = i;
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment