The / (division) and // (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. Plain or long integer division yields an integer of the same type; the result is that of mathematical division with the ‘floor’ function applied to the result. Division by zero raises the ZeroDivisionError exception.
问题在于Python2.7中的整数除法
4/3
在Python3中(其中
^{pr2}$/
是浮点除法,//
是整数除法),这在不将其更改为4.0/3
的情况下就可以工作了,或者您可以使用整数除法。
4/3
向下取整时求值为1。在请改用
4.0
强制浮点运算:或者使用Python 3,其中浮点除法是默认值,或者使用
^{pr2}$from __future__ import division
在Python 2中实现相同的效果:此行为记录在Binary arithmetic operators section下:
请参见PEP 238,了解为什么在python3中更改了此行为,以及对
from __future__ import division
语句的引用。在相关问题 更多 >
编程相关推荐