When performing a std::ceil
on a double value the value will round up to a whole number. So 3.3 will become 4.0. Which can be casted or truncated to an int
. Which will 'chop off' the part after the comma. So:
int foo = (int)std::ceil(3.3);
So at first glance this will store 4 in foo. However, the double is a floating point value. So it might either be 4.000000001
or 3.999999999
. The latter would be truncated to 3.
But in practice I've never seen this behaviour occurring. Can I safely assume that any implementation will return 4? Or is it only the IEEE-754
that does this. Or have I just been lucky?
Rounding (or ceil-ing) a double will always, always, always be exact.
For floating point numbers below 2^(m+1), where m is the number of mantissal bits, all integers have exact representations, so the result can be exactly represented.
For floating point numbers above 2^(m+1)... they're already integers. Makes sense, if you think about it: there aren't enough mantissal bits to stretch down to the right of the decimal point. So again rounding/ceil-ing is exact.