Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. int fact(int n)
  4. {
  5.     int res = 1;
  6.     for (int i = 1; i <= n; i++)
  7.         res *= i;
  8.     return res;
  9. }
  10. int main()
  11. {
  12.     float x, eps;
  13.     if (scanf("%f %e", &x, &eps) == 2)
  14.     {
  15.         if (eps <= 0 || eps > 1)
  16.         {
  17.             printf("Input error.");
  18.             return 1;
  19.         }
  20.         float s = 0, curr = x;
  21.         int i = 0;
  22.         float next = pow(-1, i + 1) * pow(x, 2 * (i + 1) + 1) / fact(2 * (i + 1) + 1);
  23.         do
  24.         {
  25.             s += curr;
  26.             curr = next;
  27.             i++;
  28.             next = pow(-1, i + 1) * pow(x, 2 * (i + 1) + 1) / fact(2 * (i + 1) + 1);
  29.         } while (fabs(curr) > eps);
  30.         float f = sin(x);
  31.         printf("%.6f %.6f %.6f %.6f", s, f, fabs(f - s), fabs((f - s) / f));
  32.         return 0;
  33.     }
  34.     else
  35.     {
  36.         printf("Input error.");
  37.         return 1;
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement