cmath库里,pow函数是基于浮点运算的。
pow赋值给int的时候:指数用常量,结果正确;指数用int、double,结果错误
pow赋值给double的时候,指数用常量、int、double,结果都正确
所以在使用pow函数时,底数必须是常量(double型),结果才不会出现偏差。
定义pow函数
int pow(int a,int b){
int res =1;
int c =b;
while(b--){
res*=a
}
return res;
}