Question

I have a double variable double dub1. In case it is integer multiple of 15, I want to get the result of division (e.g. 30/15 -> 2 ok). In case it is not integer multiple, I want to round it to upper value (e.g. 20/15 -> 2 ok). How should I handle the first part intelegently.

int divres = dub1/15; //For the second part
divres++;  
Was it helpful?

Solution

Use the std::ceil function, located in <cmath>:

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

OK, without ceil, you could use std::fmod the following way:

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

OTHER TIPS

int divres = dub1/15; //For the second part
if(dub1 - (15*divres) >.000001) // check to see if it is not equal
   divres++; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top