為什么pw(n,2)在n=5時(shí)用我的編譯器和操作系統(tǒng)返回24?#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){
int n,i,ele;
n=5;
ele=pow(n,2);
printf("%d",ele);
return 0;}輸出是24.我在代碼:塊中使用GNU/GCC。怎么回事?我知道pow函數(shù)返回double,但是25適合于int類型,所以為什么這段代碼會(huì)打印24而不是25?如果n=4; n=6; n=3; n=2;代碼起作用了,但對五種情況卻不起作用。
3 回答

慕慕森
TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊
ele=(int)(0.5 + pow(n,2));/* ^ ^ *//* casting and rounding */

拉丁的傳說
TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
pow()
int
<math.h>
-Wfloat-conversion
-Wall -Wextra
pow()
- 3 回答
- 0 關(guān)注
- 522 瀏覽
添加回答
舉報(bào)
0/150
提交
取消