利用泰勒級數計算sinx的值(遞歸方法)


 1 #include<stdio.h>
 2 #include<math.h>
 3 double mypow(double x,int i)
 4 {
 5     if (i==1||i==0) return x;
 6     return x*mypow(x,i-1);
 7 }
 8 double orz (int i)
 9 {
10     if (i==1||i==0) return 1;
11     return i*orz(i-1);
12 }
13 int main()
14 {
15     int i=1,count=0;
16     double x,sinx=0;
17     printf("Input x:\n");
18     scanf("%lf",&x);
19     do
20     {
21         sinx=sinx+pow(-1,count)*mypow(x,i)/orz(i);
22         i+=2;
23         count++;
24     }while(fabs(mypow(x,i)/orz(i)>1e-5));
25     count++;
26     printf("sin(x)=%.3f,count=%d\n",sinx,count);
27     return 0;
28 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM