Help please solve the problem. It is necessary to calculate the sum of the Taylor series with the decree. accuracy and with maximum accuracy (n is the maximum value for which the calculation of the formula is correct).
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> double f(int n, int x) { double res; res = pow(x, 2*n + 1); res *= pow(-1,n); res *= (double)powf(-0.5, n); return res; } factorial(n) { int r, s; s = 2 * n + 1; for (r = 1; s > 1; r *= (s--)) ; return r; } int main() { float e; double sum = 0; int n = 0, x=0; double previous, current, S; scanf("%d\n", &x); scanf("%d\n", &n); scanf_s("%f", &e); current = f(n,x)/factorial(n); sum += current; n++; do { previous = current; current = f(n,x); sum += current; n++; } while (abs(current - previous) > e); printf("sum = %f\n", sum); _getch(); return 0; }
inttype.int factorial(int n), notfactorial(n). And in general, the task of finding the sum of a Taylor series is primarily the problem of the correct methods of summing up floating numbers: either from smaller to larger, or Kahan, or something else. For some reason this was forgotten ... - AnT