I am beginner in programming and Python. I am doing some simple maths operations. So 3/2
in Python interpreter gives 1
as we know. But -3/2
gives -2
. Can you point out the difference here?
我是编程和Python的初学者。我正在做一些简单的数学运算。所以Python解释器中的3/2给出1。但3/2 2。你能指出这里的区别吗?
3 个解决方案
#1
5
In Python 2, /
performs integer division. What this means is that the result, if it is not an integer, is rounded down to the next integer value. When the value is negative, this naturally rounds to a greater-magnitude negative number.
在Python 2中,/执行整数除法。这意味着,如果结果不是整数,则将其四舍五入到下一个整数值。当这个值是负数时,它自然会变成一个更大的负数。
Intuitively, the result of integer division is simply the mathematical floor of the result of float division. For this reason, integer division is also commonly referred to as floor division.
从直观上看,整数除法的结果就是浮点除法的结果。由于这个原因,整数除法通常也被称为地板除法。
floor(1.5) # Returns 1.0
floor(-1.5) # Returns -2.0
It's possible to alter this behavior in Python 2 by putting from __future__ import division
at the top of your module. This import will make the /
operator indicate only true division (float division), and enable explicit floor division (integer division) with the //
operator. These conventions are the standard in Python 3.
在Python 2中,可以通过将__future__导入部分放在模块的顶部来更改此行为。这个导入将使/操作符只指示真正的除法(浮点除法),并使//操作符允许显式的地板除法(整数除法)。这些约定是Python 3中的标准。
from __future__ import division
print(3/2) # 1.5
print(3//2) # 1
As @Dunes notes in the comments, it's worth noting that -
has a higher precedence than /
, and therefore -3/2
is equivalent to (-3)/2
rather than -(3/2)
. If the division were applied first, the result would indeed be -1
.
正如@Dunes在评论中指出的,值得注意的是-的优先级高于/,因此-3/2等同于(-3)/2而不是-(3/2)。如果首先应用除法,结果确实是-1。
#2
4
-3/2 == -1.5 , floor(-1.5) = -2
likewise
同样的
3/2 == 1.5 , floor(1.5) = 1
#3
1
Python has two division operators.
Python有两个分区操作符。
-
/
/
-
//
/ /
Here, //
will always round the result to the nearest integer (irrespective of the type of operands). This is called floor division. But /
will round to the nearest integer, if both the operands are integers wheres it does the actual division if either of the operands is a float.
这里,//将始终将结果四舍五入到最近的整数(与操作数的类型无关)。这叫做地板除法。但是/将四舍五入到最近的整数,如果两个操作数都是整数,当其中一个操作数是浮点数时,它将执行实际的除法。
The difference can be clearly understood with this example,
通过这个例子可以清楚地理解这种差异,
>>> 11/4
2
>>> 11.0/4
2.75
>>> 11//4
2
>>> 11.0//4.0
2.0
Quoting from Python Documentation on floor division,
引用Python文档的地板除法,
Mathematical division that rounds down to nearest integer. The floor division operator is
//
. For example, the expression11 // 4
evaluates to2
in contrast to the2.7
5 returned by float true division. Note that(-11) // 4
is-3
because that is-2.75
rounded downward. See PEP 238.最接近整数的数学除法。楼层分机操作员是//。例如,与float true division返回的2.75相比,表达式11 // 4的值为2。注意,(-11)// 4是-3,因为-2。75是向下的。看到PEP 238。
The last line in the quoted text would be the answer to your actual question.
引文中的最后一行是你实际问题的答案。
#1
5
In Python 2, /
performs integer division. What this means is that the result, if it is not an integer, is rounded down to the next integer value. When the value is negative, this naturally rounds to a greater-magnitude negative number.
在Python 2中,/执行整数除法。这意味着,如果结果不是整数,则将其四舍五入到下一个整数值。当这个值是负数时,它自然会变成一个更大的负数。
Intuitively, the result of integer division is simply the mathematical floor of the result of float division. For this reason, integer division is also commonly referred to as floor division.
从直观上看,整数除法的结果就是浮点除法的结果。由于这个原因,整数除法通常也被称为地板除法。
floor(1.5) # Returns 1.0
floor(-1.5) # Returns -2.0
It's possible to alter this behavior in Python 2 by putting from __future__ import division
at the top of your module. This import will make the /
operator indicate only true division (float division), and enable explicit floor division (integer division) with the //
operator. These conventions are the standard in Python 3.
在Python 2中,可以通过将__future__导入部分放在模块的顶部来更改此行为。这个导入将使/操作符只指示真正的除法(浮点除法),并使//操作符允许显式的地板除法(整数除法)。这些约定是Python 3中的标准。
from __future__ import division
print(3/2) # 1.5
print(3//2) # 1
As @Dunes notes in the comments, it's worth noting that -
has a higher precedence than /
, and therefore -3/2
is equivalent to (-3)/2
rather than -(3/2)
. If the division were applied first, the result would indeed be -1
.
正如@Dunes在评论中指出的,值得注意的是-的优先级高于/,因此-3/2等同于(-3)/2而不是-(3/2)。如果首先应用除法,结果确实是-1。
#2
4
-3/2 == -1.5 , floor(-1.5) = -2
likewise
同样的
3/2 == 1.5 , floor(1.5) = 1
#3
1
Python has two division operators.
Python有两个分区操作符。
-
/
/
-
//
/ /
Here, //
will always round the result to the nearest integer (irrespective of the type of operands). This is called floor division. But /
will round to the nearest integer, if both the operands are integers wheres it does the actual division if either of the operands is a float.
这里,//将始终将结果四舍五入到最近的整数(与操作数的类型无关)。这叫做地板除法。但是/将四舍五入到最近的整数,如果两个操作数都是整数,当其中一个操作数是浮点数时,它将执行实际的除法。
The difference can be clearly understood with this example,
通过这个例子可以清楚地理解这种差异,
>>> 11/4
2
>>> 11.0/4
2.75
>>> 11//4
2
>>> 11.0//4.0
2.0
Quoting from Python Documentation on floor division,
引用Python文档的地板除法,
Mathematical division that rounds down to nearest integer. The floor division operator is
//
. For example, the expression11 // 4
evaluates to2
in contrast to the2.7
5 returned by float true division. Note that(-11) // 4
is-3
because that is-2.75
rounded downward. See PEP 238.最接近整数的数学除法。楼层分机操作员是//。例如,与float true division返回的2.75相比,表达式11 // 4的值为2。注意,(-11)// 4是-3,因为-2。75是向下的。看到PEP 238。
The last line in the quoted text would be the answer to your actual question.
引文中的最后一行是你实际问题的答案。