#include <stdio.h>
#include <math.h>
int main()
{
int loop, place_value=0, c = 5;
for(loop = 0; loop < c; loop++)
{
place_value = 0;
place_value = pow(10, loop);
printf("%d \n", place_value);
}
return 0;
}这段代码给出了
10
99
1000
9999 为什么第三行和第五行是99和9999,而不是100和10000?
当正常请求电源时,它会给出正确的答案。
#include <stdio.h>
#include <math.h>
int main()
{
printf ("%d", (int) pow (10,3 ));
return 0;
}1000发布于 2021-02-06 01:17:56
经验法则:永远不要使用带整数的浮点函数(特别是像pow或log这样的复杂函数)。
简单地实现整数次幂
unsigned intpow(unsigned x)
{
unsigned result = 1;
while(x --) result *= 10;
return result;
}它会快得多,甚至(最快的)
int intpow1(unsigned x)
{
const static unsigned vals[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, /* ... */};
#if defined(CHECK_OVERFLOW)
if(x >= sizeof(vals)/ sizeof(vals[0])) return -1;
#endif
return vals[x];
}https://stackoverflow.com/questions/66066756
复制相似问题