[問題] 預設的 double -> int 隱式轉換或強制轉換
經過我自己測試的結果,
在Dev-C++ 或是VC++裡測試
double a=4.9;
int b=a; //or int b=(int)a;
cout b; << b會等於
好像就是直接捨位了.想知道這是C++的標準預設嗎?
在C裡面 math.h 還有提供 ceil() ,floor()來做進位跟捨位
但是我書上寫的這兩個函式的傳回值都是double,
如果真的要整數的話,勢必還要轉型,所以才會在意到底是轉成怎麼樣的.
或是有沒有可靠的預設函式可以直接拿到整數的?
另外我是自己做一個ceil函式啦,也想知道這樣搞跟#include<cmath> 之類的哪個比較快,
因為我真的沒有用很多cmath,math.h 裡的東西.
int IntUp(double adouble){ //無條件進位
int tempint;
tempint=adouble;
if(tempint<adouble){return (tempint+1);}
else{return tempint;}
};
int Int_4_5(double adouble){ //四捨五入
if(adouble>0){
int temp=IntUp(adouble);
if((temp-adouble)<0.5){
return temp;
}
else{
return temp-1;
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.130.186
推
04/28 05:30, , 1F
04/28 05:30, 1F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章