有人可以解释一下吗(直接来自 docs - 强调我的):
math.ceil(x) Return the ceiling of x as a float, the smallest integer value greater than or equal to x.
math.floor(x) Return the floor of x as a float, the largest integer value less than or equal to x.
.ceil
和 .floor
根据定义应该计算整数,为什么会返回 float ?
编辑:
这得到了一些很好的论据,说明为什么他们应该返回 float ,而我刚刚习惯了这个想法,@jcollado指出他们实际上确实在 Python 3 中返回整数 ...
最佳答案
正如其他答案所指出的,在 python 中,它们返回 float 可能是因为历史原因以防止溢出问题。但是,它们在 python 3 中返回整数。
>>> import math
>>> type(math.floor(3.1))
<class 'int'>
>>> type(math.ceil(3.1))
<class 'int'>
您可以在 PEP 3141 中找到更多信息.
关于python - 为什么 Python 的 math.ceil() 和 math.floor() 操作返回 float 而不是整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8582741/