Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <iostream>
- #include <math.h>
- #include <float.h>
- int main() {
- setlocale(0, "Russian");
- const int maxN = 500;
- short n = 0;
- float x, cosN = 1, y = cosN, eps = FLT_EPSILON;
- printf("Введите x: ");
- scanf("%f", &x);
- while (fabs(cosN) >= eps && n <= maxN) {
- cosN *= -(x*x) / ((2 * n + 1)*(2 * n + 2));
- y += cosN;
- n++;
- }
- if (n > maxN) {
- puts("Превышено максимально допустимое число повторений.");
- }
- else {
- float cosForCheck = cos(x);
- float delta = fabs(y - cosForCheck);
- if (delta > eps) {
- puts("Значение посчитано неточно.");
- printf("Библиотечная cos(%6.2f) = %f\n", x, cosForCheck);
- printf("Через разложение cos(%6.2f) = %f\n", x, y);
- printf("Погрешность: %f\n", delta);
- printf("Итераций: %d\n", n);
- }
- else {
- printf("cos(%6.2f) = %f\nВычислено за %d итераций\n", x, y, n);
- }
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement