考虑一下 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 中,这种行为确实是相反的;见 How can I force division to be floating point? Division keeps rounding down to 0?对于相反的,特定于 2.x 的问题。
最佳答案
看看PEP-238 : 更改除法运算符
The // operator will be available to request floor division unambiguously.
关于python - 为什么整数除法产生一个 float 而不是另一个整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1282945/