用泰勒展开实现pow函数,实际效果还不如直接循环
但是这对于初步理解泰勒展开还是有一点帮助的
1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 5 double exp_x(double x){ 6 double p=x,ans=1; 7 for(int i=1;i<=10000;++i){ 8 ans+=p; 9 p=p*x/(i+1); 10 } 11 return ans; 12 } 13 14 double log_e(double x){ 15 if(x<=0){ 16 puts("Error"); 17 exit(0); 18 } 19 double ans=0; 20 while(x>=1){ 21 ans=ans+1; 22 x/=exp_x(1); 23 } 24 double p=x-1,sgn=1; 25 for(int i=1;i<=10000;++i){ 26 ans+=sgn*p/i; 27 p=p*(x-1); 28 sgn=-sgn; 29 } 30 return ans; 31 } 32 33 double power(double x,double y){return exp_x(log_e(x)*y);} 34 35 int main(){ 36 double x,y; 37 while(cin>>x>>y&&(x||y)){ 38 cerr<<power(x,y)<<endl; 39 cerr<<pow(x,y)<<endl; 40 } 41 system("pause"); 42 return 0; 43 }