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

前端 未结 8 1940
刺人心
刺人心 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:19

    As pointed out by other answers, in python they return floats probably because of historical reasons to prevent overflow problems. However, they return integers in python 3.

    >>> import math
    >>> type(math.floor(3.1))
    
    >>> type(math.ceil(3.1))
    
    

    You can find more information in PEP 3141.

提交回复
热议问题