[問題] 預設的 double -> int 隱式轉換或強制轉換

看板C_and_CPP (C/C++)作者 (sapphira)時間16年前 (2009/04/28 03:44), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/2 (看更多)
經過我自己測試的結果, 在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
文章代碼(AID): #19zWi7uc (C_and_CPP)
文章代碼(AID): #19zWi7uc (C_and_CPP)