Taylor series sin (x) = x - x ^ 3/3 + x ^ 5/5 -!!!! X ^ 7/7 + x ^ 9/9) ... (circulation loop, 10 ^ (-5) expression, not only the power pow can also be used in a cyclic manner)

Taylor series sin (x) = x - value calculation sin (x) of x ^ 7/7 + x ^ 9/9) ... - x ^ 3/3 + x ^ 5/5!!!!. The last requirement is less than an absolute value of 10 ^ (- 5), and at this time the cumulative statistics of the number of items.

#include  <math.h>
#include  <stdio.h>
 
void main()
{
    int n = 1,count = 1;
    float x;
    double sum , term;
 
    printf("Input x: ");
    scanf("%f", &x);
 
    sum = x;
    term = x;
    do
    {
         //从第二项开始的也这是
        term = -term*x*x/((n+1)*(n+2));//每一项的项数,有规律的,每一项与前一项的区别
        sum = sum + term;//求和
        n+=2;
        count++;//数数
    }while (fabs(term) >= 1e-5);
    printf("sin(x) = %f, count = %d\n", sum, count);

}

Ideas:
!! 1 · x3 / 3 X5 / 5 POW (the X-, 3) == "and then not used, the cycle can solve power problems
2 · 1357 .. . . . . 2 I-. 1
Note:
In the code, I discovered how to express
* 10 ^ (- 5) *** this number, and can only be represented by the function pow
Further, this cycle *** *** the good idea.

Published 18 original articles · won praise 0 · Views 196

Guess you like

Origin blog.csdn.net/weixin_46456339/article/details/105310268