Guest User

Untitled

a guest
Feb 15th, 2020
70
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. double fact(double d) {
  5.     if (d < 1) {
  6.         return 1;
  7.     } else {
  8.         return d*fact(d-1);
  9.     }
  10. }
  11.  
  12.  
  13.  
  14. int main() {
  15.     double x, part, sum = 0, eps, diff;
  16.     int n = 0 ,i = 1;;
  17. //    x = 3.14;
  18. //    eps = 0.0001;
  19.     scanf("%lf %lf", &x, &eps);
  20.     if (eps > 0 && eps < 1) {
  21.         diff = 1 + eps;
  22.         part = x;
  23.         //printf("%f %f %f", sin(x)-sum, eps);
  24.  
  25. //printf("%lf", fact(3));
  26.         for (i = 1; diff > eps; i++){
  27.  
  28.             sum += part;
  29.             //part =  pow((-1),i-1) * pow(x, 2 * i - 1) /fact(2 * i - 1);
  30.             part*= (-1) * x * x / (2. * i)/(2. * i + 1.);
  31.             diff = fabs(sin(x)-sum);
  32.             n++;
  33.         }
  34.  
  35. printf("%s %lf %s %lf %s %d", "x = ",x," sum = ",sum," n = " ,n);
  36.  
  37.  
  38.  
  39.  
  40. //    0     0.001   0
  41. //    3.14      0.0001  0.00161324
  42. //    1.57      0.00001 1
  43. //    1.05      0.0001  0.86602
  44. //    2.06      0.0001  0.869296   // mine is  0.882776
  45.  
  46.  
  47.  
  48.    } else{printf("%s", "Invalid data");}
  49.     return 0;
  50. }
RAW Paste Data