Python 中是否有等同于 // 运算符的上限?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14822184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a ceiling equivalent of // operator in Python?
提问by Cradam
I found out about the //operator in Python which in Python 3 does division with floor.
我发现了//Python 中的运算符,它在 Python 3 中用 floor 进行除法。
Is there an operator which divides with ceil instead? (I know about the /operator which in Python 3 does floating point division.)
是否有一个与 ceil 相除的运算符?(我知道/在 Python 3 中进行浮点除法的运算符。)
采纳答案by Charles Salvia
There is no operator which divides with ceil. You need to import mathand use math.ceil
没有与 ceil 相除的运算符。您需要import math并使用math.ceil
回答by poke
You could do (x + (d-1)) // dwhen dividing xby d, i.e. (x + 4) // 5.
你可以做(x + (d-1)) // d划分时x通过d,即(x + 4) // 5。
回答by Travis Griggs
You can always just do it inline as well
你也可以随时内联
((foo - 1) // bar) + 1
In python3, this is just shy of an order of magnitude faster than forcing the float division and calling ceil(), provided you care about the speed. Which you shouldn't, unless you've proven through usage that you need to.
在 python3 中,如果您关心速度,这比强制浮点除法和调用 ceil() 快一个数量级。你不应该这样做,除非你已经通过使用证明你需要这样做。
>>> timeit.timeit("((5 - 1) // 4) + 1", number = 100000000)
1.7249219375662506
>>> timeit.timeit("ceil(5/4)", setup="from math import ceil", number = 100000000)
12.096064013894647
回答by casevh
Note that math.ceil is limited to 53 bits of precision. If you are working with large integers, you may not get exact results.
请注意,math.ceil 的精度限制为 53 位。如果您正在处理大整数,则可能无法获得准确的结果。
The gmpy2libary provides a c_divfunction which uses ceiling rounding.
所述gmpy2libary提供一种c_div它采用天花板的舍入函数。
Disclaimer: I maintain gmpy2.
免责声明:我维护 gmpy2。
回答by dlitz
You can just do upside-down floor division:
你可以做倒置的地板划分:
def ceildiv(a, b):
return -(-a // b)
This works because Python's division operator does floor division(unlike in C, where integer division truncates the fractional part).
这是有效的,因为Python 的除法运算符进行地板除法(不像在 C 中,整数除法会截断小数部分)。
This also works with Python's big integers, because there's no (lossy) floating-point conversion.
这也适用于 Python 的大整数,因为没有(有损)浮点转换。
Here's a demonstration:
这是一个演示:
>>> from __future__ import division # a/b is float division
>>> from math import ceil
>>> b = 3
>>> for a in range(-7, 8):
... print(["%d/%d" % (a, b), int(ceil(a / b)), -(-a // b)])
...
['-7/3', -2, -2]
['-6/3', -2, -2]
['-5/3', -1, -1]
['-4/3', -1, -1]
['-3/3', -1, -1]
['-2/3', 0, 0]
['-1/3', 0, 0]
['0/3', 0, 0]
['1/3', 1, 1]
['2/3', 1, 1]
['3/3', 1, 1]
['4/3', 2, 2]
['5/3', 2, 2]
['6/3', 2, 2]
['7/3', 3, 3]
回答by Raymond Hettinger
Solution 1: Convert floor to ceiling with negation
解决方案 1:使用否定将地板转换为天花板
def ceiling_division(n, d):
return -(n // -d)
Reminiscent of the Penn & Teller levitation trick, this "turns the world upside down (with negation), uses plain floor division (where the ceiling and floor have been swapped), and then turns the world right-side up (with negation again)"
让人想起Penn & Teller 悬浮技巧,这“将世界颠倒(带否定),使用普通地板划分(天花板和地板已交换),然后将世界向右颠倒(再次否定) ”
Solution 2: Let divmod() do the work
解决方案 2:让 divmod() 完成工作
def ceiling_division(n, d):
q, r = divmod(n, d)
return q + bool(r)
The divmod()function gives (a // b, a % b)for integers (this may be less reliable with floats due to round-off error). The step with bool(r)adds one to the quotient whenever there is a non-zero remainder.
所述divmod()函数给出(a // b, a % b)为整数(这可能是用浮漂较不可靠,由于舍入误差)。bool(r)每当有非零余数时,步骤 with就将商加一。
Solution 3: Adjust the numerator before the division
解决方案3:在除法之前调整分子
def ceiling_division(n, d):
return (n + d - 1) // d
Translate the numerator upwards so that floor division rounds down to the intended ceiling. Note, this only works for integers.
将分子向上平移,使楼层划分四舍五入到预期的天花板。请注意,这仅适用于整数。
Solution 4: Convert to floats to use math.ceil()
解决方案 4:转换为浮点数以使用 math.ceil()
def ceiling_division(n, d):
return math.ceil(n / d)
The math.ceil()code is easy to understand, but it converts from ints to floats and back. This isn't very fast and it may have rounding issues. Also, it relies on Python 3 semantics where "true division" produces a float and where the ceil()function returns an integer.
该math.ceil()代码很容易理解,但它从整数到彩车和背部转换。这不是很快,它可能有舍入问题。此外,它依赖于 Python 3 语义,其中“真除法”产生一个浮点数,而ceil()函数返回一个整数。
回答by A.L. Verminburger
Simple solution: a // b + 1
简单的解决方案:a // b + 1

