Why do Python's math.ceil() and math.floor() operations return floats instead of integers?

前端 未结 8 1957
刺人心
刺人心 2020-12-04 15:11

Can someone explain this (straight from the docs- emphasis mine):

math.ceil(x) Return the ceiling of x as a float, the small

8条回答
  •  囚心锁ツ
    2020-12-04 15:41

    The range of floating point numbers usually exceeds the range of integers. By returning a floating point value, the functions can return a sensible value for input values that lie outside the representable range of integers.

    Consider: If floor() returned an integer, what should floor(1.0e30) return?

    Now, while Python's integers are now arbitrary precision, it wasn't always this way. The standard library functions are thin wrappers around the equivalent C library functions.

提交回复
热议问题