NIKOLAY_TETUS

delete_later

Nov 4th, 2021
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdbool.h>
  4.  
  5. long fact(int n)
  6. {
  7.     if (n == 0)
  8.         return 1;
  9.     else
  10.         return(n * fact(n - 1));
  11. }
  12.  
  13. long double check(long double x)
  14. {
  15.     return sinl(x) / x;
  16. }
  17.  
  18. int main()
  19. {
  20.     long double x;
  21.     printf("x> ");
  22.     scanf("%Lf", &x);
  23.  
  24.     //Проверяем принадлежность x промежутку из условия
  25.     if (x < -M_PI || x > M_PI)
  26.     {
  27.         printf("x must be on [-Pi, +Pi], aborted.");
  28.         return 0;
  29.     }
  30.  
  31.     long double epsilon = 1e-5; //Точность нахождения последовательности
  32.     int it = 2;                 //Счётчик итераций
  33.     long double S = 1;          //Сумма бесконечной последовательности
  34.     long double diffValue;      //Значение которое нужно прибавить/отнять в следующей итерации
  35.     short isNegative = true;    //Определяет знак для итерации
  36.  
  37.     do
  38.     {
  39.         diffValue = powl(x, it) / (long double)fact(it + 1);
  40.  
  41.         if (isNegative)
  42.             S -= diffValue;
  43.         else
  44.             S += diffValue;
  45.  
  46.         it += 2;                    //Увеличиваем итерацию
  47.         isNegative = !isNegative;   //Меняем знак операции для следующей итерации
  48.     }while(diffValue > epsilon);
  49.  
  50.     printf("Sum:       %Lf \n", S);
  51.     printf("Sum check: %Lf", check(x));
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment