pow

C语言中pow的形式为 double pow(double item1 , double item2);皆为double型,对于int型,就可能出现错误,例如:

const int sections = 10;

for(int t= 0; t < 5; t++){
   int i = pow(sections, 5- t -1);  
   cout << i << endl;
}

9999
1000
99
10
1

这是因为在计算的时候,精度缺失;

一种补救方法就是使用round(),毕竟,出现偏差,大致也是999.999或者10000.0001这种,因此四舍五入即可。

猜你喜欢

转载自blog.csdn.net/lansehuanyingyy/article/details/80452513
pow