Advertisement
constk

Lab5_Task1

Nov 13th, 2019
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <math.h>
  5. #include <float.h>
  6.  
  7. int main() {
  8. setlocale(0, "Russian");
  9.  
  10. const int maxN = 500;
  11. short n = 0;
  12. float x, cosN = 1, y = cosN, eps = FLT_EPSILON;
  13. printf("Введите x: ");
  14. scanf("%f", &x);
  15.  
  16. while (fabs(cosN) >= eps && n <= maxN) {
  17. cosN *= -(x*x) / ((2 * n + 1)*(2 * n + 2));
  18. y += cosN;
  19. n++;
  20. }
  21.  
  22. if (n > maxN) {
  23. puts("Превышено максимально допустимое число повторений.");
  24. }
  25. else {
  26. float cosForCheck = cos(x);
  27. float delta = fabs(y - cosForCheck);
  28. if (delta > eps) {
  29. puts("Значение посчитано неточно.");
  30. printf("Библиотечная cos(%6.2f) = %f\n", x, cosForCheck);
  31. printf("Через разложение cos(%6.2f) = %f\n", x, y);
  32. printf("Погрешность: %f\n", delta);
  33. printf("Итераций: %d\n", n);
  34. }
  35. else {
  36. printf("cos(%6.2f) = %f\nВычислено за %d итераций\n", x, y, n);
  37. }
  38. }
  39.  
  40. system("pause");
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement