2014-04-24 43 views
0

我有一個雙變量double dub1。如果它是15的整數倍,我想得到除法結果(例如30/15 - > 2 ok)。如果它不是整數倍,我想將它四捨五入爲最大值(例如20/15 - > 2 ok)。我該如何處理第一部分的問題。C++上限值計算

int divres = dub1/15; //For the second part 
divres++; 
+0

可能重複[如何我是否以偏見進行浮點舍入(總是舍入或舍入)?] (http://stackoverflow.com/questions/685907/how-do-i-do-floating-point-rounding-with-a-bias-always-round-up-or-down) – Shoe

回答

4

使用std::ceil功能,位於<cmath>

int divres = static_cast<int>(std::ceil(dub1/15)); 

OK,沒有ceil,你可以使用std::fmod方式如下:的

int divres = dub1/15; 
if (std::fmod(dub1, 15) != 0) { 
    divres++; 
} 
+0

我不想使用小區。 –

+0

@JoachimPileborg他說'dub1'是一個雙精度浮點數。 – Barmar

+1

@AvbAvb那你想要什麼? – Hiura

0
int divres = dub1/15; //For the second part 
if(dub1 - (15*divres) >.000001) // check to see if it is not equal 
    divres++;