Is int(ceil(x)) well-behaved?

floating-point, math, python

Solution

A correctly implemented `ceil` returns the exact mathematical value of ceil(x), with no error. When IEEE 754, or any reasonable floating-point system, is in use, `ceil` is not subject to rounding errors.

This does not prevent adverse effects from sources other than the `ceil` function. For example, `ceil(1.000000000000000000000000000000001)` will return 1 because `1.000000000000000000000000000000001` is converted to a floating-point value before `ceil` is called, and that conversion rounds its result. Similarly, a conversion from `double` to `float` followed by a call to `ceil` may yield a value that is not the ceiling of the original `double` value.

The conversion of the result of `ceil` to `int` of course relies on the range of `int`. As long as the value is in range, the conversion should not change the value.

Problem

If I have a floating point number x, which is within the range of [0, 106], is it guaranteed in Python that int(ceil(x)) will be rounded up correctly? It seems possible from what little I know that ceil may be rounded down leading to an incorrect result. Something like: x = 7.6, ceil(x)=7.999.., int(ceil(x))=7. Can that happen?

Original source