Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4.  
  5. #define true 1
  6. #define false 0
  7. #define err -2000123000
  8. #define max 9223372036854775807
  9. #define PI 3.14159265358979323846
  10.  
  11. double myabs(double x) {
  12.     if (x < 0) return -x;
  13.     else return x;
  14. }
  15.  
  16. double stod(char s[100]) {
  17.     int i = 0, o = false, f = false;
  18.     double sm = 0, d = 1;
  19.     if (s[0] == '-') {
  20.         o = true;
  21.         i++;
  22.     }
  23.     if (s[0] == '\0' || s[0] == '\n') return 0;
  24.     while (s[i] != '\0' && s[i] != '\n') {
  25.         if (f) d *= 10;
  26.         if (s[i] >= '0' && s[i] <= '9') sm = sm * 10 + (s[i] - '0');
  27.         else if (s[i] == '.' && !f) f = true;
  28.         else return err;
  29.  
  30.         if (sm >= max / 100) return err;
  31.         i++;
  32.     }
  33.     if (o == true) return -1 * sm / d;
  34.     else return sm / d;
  35. }
  36.  
  37. void problem2() {
  38.     printf("vvedite x and eps\n");
  39.     double x, eps, sum = 0, fc = 1;
  40.     char s1[1000] = {'\0'}, s2[1000] = {'\0'};
  41.     scanf("%s %s", &s1, &s2);
  42.     x = stod(s1);
  43.     eps = stod(s2);
  44.     if (x == err || eps == err) {
  45.         printf("error");
  46.         return;
  47.     }
  48.     if (eps <= 0) {
  49.         printf("err: epsilon should be more than 0");
  50.         return;
  51.     }
  52.     if (myabs(x) >= 1000) {
  53.         printf("err: x too big");
  54.         return;
  55.     }
  56.     printf("right sin is %lf\n", sin(x));
  57.     while (x > PI + 0.1) x -= 2 * PI;
  58.     while (x < PI - 0.1) x += 2 * PI;
  59.     double z = x, xx = x;
  60.     int i = 1;
  61.     while (myabs(z) >= eps) {
  62.         if ((i / 2) % 2 == 0) sum += z;
  63.         else sum -= z;
  64.         fc *= (i + 1) * (i + 2);
  65.         /* if (xx>max/(x*x) || (x*x)>max/(xx) || fc>max/((i+1)*(i+2)) || ((i+1)*(i+2))>max/fc)
  66.          {
  67.              printf ("Run time error");
  68.              return 0;
  69.          }*/
  70.         xx *= x * x;
  71.         z = xx / fc;
  72.         i += 2;
  73.     }
  74.     printf(" my  sin  is %lf\n", sum);
  75.     printf(" actual epsilon is  %lf\n", myabs(sin(x) - sum));
  76.     printf("should be less than %lf\n", eps);
  77.     printf("n = %d", i / 2 + 1);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement