考虑 Python 中的这种划分:
Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2/2
1.0
这是故意的吗?我强烈记得早期版本返回 int/int = int
。我应该怎么办?是否有新的除法运算符或我必须始终投射?
在 2.x 中,行为确实被逆转了;请参阅 如何强制除法为浮点数?除法一直向下舍入为 0? 相反,2.x 特定的问题。
原文由 Jonas Byström 发布,翻译遵循 CC BY-SA 4.0 许可协议
看一下 PEP-238 :更改除法运算符