【发布时间】:2008-10-08 17:16:35
【问题描述】:
使用一个比另一个有好处吗?在 Python 2 中,它们似乎都返回相同的结果:
>>> 6/3
2
>>> 6//3
2
【问题讨论】:
-
引入
//运算符进行整数除法的原因请参考The Problem with Integer Division。
标签: python math syntax operators floor-division
使用一个比另一个有好处吗?在 Python 2 中,它们似乎都返回相同的结果:
>>> 6/3
2
>>> 6//3
2
【问题讨论】:
//运算符进行整数除法的原因请参考The Problem with Integer Division。
标签: python math syntax operators floor-division
在 Python 3.x 中,5 / 2 将返回 2.5,5 // 2 将返回 2。前者为浮点除法,后者为地板除法,有时也称为整数除法。 p>
在 Python 2.2 或更高版本的 2.x 行中,整数没有区别,除非您执行 from __future__ import division,这会导致 Python 2.x 采用 3.x 行为。
无论以后导入,5.0 // 2 都将返回2.0,因为这是运算的地板除法结果。
您可以在 PEP 238: Changing the Division Operator 找到详细说明。
【讨论】:
python -Qnew。其他划分选项:-Qold(默认)、-Qwarn、-Qwarnall
5.0 / 2 在所有版本中都返回 2.5,5 / 2.0 也是如此 - 只有当两个操作数都是 int 时,旧的行为才会有所不同。
为了澄清 Python 2.x 行,/ 既不是地板除法也不是真正的除法。
/ 当两个 args 都是int 时是地板除法,但当一个 args 都是@987654324 时是真正的除法@。
【讨论】:
// 是楼层划分。它总是会给你结果的整数下限。另一个是“常规”划分。
【讨论】:
// 实现“地板除法”,无论您的类型如何。所以
1.0/2.0 会给出0.5,但1/2、1//2 和1.0//2.0 都会给出0。
【讨论】:
math.floor() 或math.fmod()。
/ 和 // 是二元运算符(左右两个操作数,分子和分母)
import math
N = 1004291331219602346 # huge number
print(N//100) #=> 10042913312196023 is correct answer
print(math.floor(N/100)) #=> 10042913312196024 is wrong answer
print(math.ceil(N/100)) #=> 10042913312196024 is wrong answer
print(int(N/100)) #=> 10042913312196024 is wrong answer
我在想int(x/y)的评价。
首先,Python 计算表达式 x/y 并得到 INEXACT 浮点数 z。
其次,Python 计算表达式int(z)。
当重要性的损失不容忽视时,我们会得到错误的结果。
【讨论】:
前面的答案很好。我想补充一点。直到某些值,它们都导致相同的商。在那个楼层除法运算符 (//) 工作正常但除法 (/) 运算符之后:
>>> int(755349677599789174 / 2) # Wrong answer
377674838799894592
>>> 755349677599789174 // 2 # Correct answer
377674838799894587
【讨论】:
双斜线// 是楼层除法:
>>> 7//3
2
【讨论】:
/ → 浮点除法
// → 楼层划分
让我们看看 Python 2.7 和 Python 3.5 中的一些示例。
Python 2.7.10 与 Python 3.5
print (2/3) ----> 0 Python 2.7
print (2/3) ----> 0.6666666666666666 Python 3.5
Python 2.7.10 与 Python 3.5
print (4/2) ----> 2 Python 2.7
print (4/2) ----> 2.0 Python 3.5
现在,如果您希望(在 Python 2.7 中)获得与 Python 3.5 中相同的输出,您可以执行以下操作:
Python 2.7.10
from __future__ import division
print (2/3) ----> 0.6666666666666666 # Python 2.7
print (4/2) ----> 2.0 # Python 2.7
而在 Python 2.7 和 Python 3.5 中 floor 除法没有任何区别。
138.93//3 ---> 46.0 # Python 2.7
138.93//3 ---> 46.0 # Python 3.5
4//3 ---> 1 # Python 2.7
4//3 ---> 1 # Python 3.5
【讨论】:
-100 // 33 => -4; 100 // -33 => -4;但由于 floor func 的舍入方向,与上一个相比,下一个似乎违反直觉:-100 // -33 => 3.
>>> print 5.0 / 2
2.5
>>> print 5.0 // 2
2.0
【讨论】:
Python 2.7 和其他即将推出的 Python 版本:
/)左手操作数除以右手操作数
示例:4 / 2 = 2
//)操作数的除法,其结果是除去小数点后的数字的商。但是,如果其中一个操作数为负数,则结果为下限,即从零四舍五入(向负无穷大):
示例:9//2 = 4 和 9.0//2.0 = 4.0、-11//3 = -4、-11.0//3 = -4.0
/ 部门和// 楼层部门操作员的操作方式相似。
【讨论】:
大家都已经回答了,//是楼层划分。
为什么这很重要,// 是明确的地板除法,从 2.2 开始的所有 Python 版本,包括 Python 3.x 版本。
/ 的行为可以根据:
__future__ 是否导入(模块本地)-Q old 或 -Q new
【讨论】:
// 是楼层划分。它总是会为您提供结果的底值。/,是浮点除法。下面是/和//的区别;
我已经在 Python 3.7.2 中运行了这些算术运算。
>>> print (11 / 3)
3.6666666666666665
>>> print (11 // 3)
3
>>> print (11.3 / 3)
3.7666666666666667
>>> print (11.3 // 3)
3.0
【讨论】:
等式的答案四舍五入到下一个较小的整数或浮点数,小数点为 .0。
>>>print 5//2
2
>>> print 5.0//2
2.0
>>>print 5//2.0
2.0
>>>print 5.0//2.0
2.0
【讨论】:
5.0//2 结果为2.0,而不是2,因为// 运算符的返回值的返回类型 遵循Python 强制(类型转换)规则。
Python 促进将较低数据类型(整数)转换为较高数据类型(浮点数)以避免数据丢失。
【讨论】:
Python 3.x 说明
只是为了补充之前的一些答案。
重要的是要注意:
a // b
是楼层划分。如:
math.floor(a/b)
不是int除法。如:
int(a/b)
不是四舍五入到 0 浮点除法。如:
圆形(a/b,0)
因此,当涉及到正数和负数时,行为方式会有所不同,如下例所示:
1 // 2 为 0,如:
math.floor(1/2)
-1 // 2 是 -1,如:
math.floor(-1/2)
【讨论】:
Operation Result Notes x / yquotient of x and y x // yfloored quotient of x and y (1) 注意事项:
- 也称为整数除法。结果值是一个整数,尽管结果的类型不一定是 int。结果总是向负无穷大四舍五入:
1//2是0,(-1)//2是-1,1//(-2)是-1,(-1)//(-2)是0。
Operation Result Notes x / yquotient of x and y (1) x // y(floored) quotient of x and y (4)(5) 注意事项:
1。对于(普通或长)整数除法,结果是整数。结果总是向负无穷取整:1/2 为 0,(-1)/2 为 -1,1/(-2) 为 -1,(-1)/(-2) 为 0。请注意如果任一操作数是长整数,则无论数值如何,结果都是长整数。
4. Deprecated since version 2.3: The floor division operator, the modulo operator, and the divmod()function are no longer defined for complex numbers. Instead, convert to a floating point number using theabs()function if appropriate.5。也称为整数除法。结果值是一个整数,尽管结果的类型不一定是 int。
【讨论】: