python - 为什么 Python 的 math.ceil() 和 math.floor() 操

有人可以解释一下吗(直接来自 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/

相关文章:

linux - bash中带有变量的别名

python - 如何模拟请求和响应?

python - 在 Python 中循环列表

linux - 获取后台进程的退出代码

python - Django 数据库设置 'Improperly Configured' 错误

mysql - 重命名 MySQL 数据库

linux - Linux 的虚拟串行端口

linux - 如何获取终端的字符编码

python - 谁能解释python的相对进口?

python - 在 Python 中使用 "continue"语句的示例?