You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since adding new operator is a syntax-level change, it needs a PEP. It is better to start with discussing on the Python-ideas mailing list (https://mail.python.org/mailman3/lists/python-ideas.python.org/). Let's see if this idea will be accepted favorably.
BTW, the ceiling division can be expressed as -(-a // b) or -(a // -b).
I'm not convinced that this operation is so common that it deserves a new operator. As Serhiy wrote, it must be first on python-ideas first. I close the issue.
--
floor division is x//y
integer ceil division can be implemented with math.ceil(x/y) for small numbers.
For large integer numbers, I like to use something like:
Well, I was hoping to avoid importing the math module just to have ceil division capabilities.
Anyway, my need for ceiling is that I am coding a chess GUI where I have a table with 2 columns and I create a new row whenever White makes a move. So, my implementation for row creation is math.ceil(len(moves) // 2) where moves is a list of chess moves.
So, the sequence of table rows after White and Black make their moves is initially 0 (since there are no moves yet) and then 1 (first row created after White made a move) and then 1 (again on row 1 after Black made a move, but on column 2), then 2 2 3 3 4 4 5 5 6 6 and so on.
If we had the ceil operator in Python, I could do: len(moves) /// 2 :)
By the way -- do you happen to know any better idea to get that kind of a sequence? That is 0 1 1 2 2 3 3 4 4 5 5 6 6 and so on.
Anyway, my need for ceiling is that I am coding a chess GUI where I have a table with 2 columns and I create a new row whenever White makes a move. So, my implementation for row creation is math.ceil(len(moves) // 2) where moves is a list of chess moves.
Activity
PedanticHacker commentedon Feb 18, 2021
I hope I'm not too late for a Python 3.10 feature request. I love the fact that Python 3.x does floor division with the // operator.
We, however, don't have a ceil division operator, besides importing the math module and using its math.ceil() function.
Is it possible we have /// as a ceil division operator in Python 3.10?
I just wanted to put this thought out of my head if anyone becomes entertained by this idea.
serhiy-storchaka commentedon Feb 18, 2021
Since adding new operator is a syntax-level change, it needs a PEP. It is better to start with discussing on the Python-ideas mailing list (https://mail.python.org/mailman3/lists/python-ideas.python.org/). Let's see if this idea will be accepted favorably.
BTW, the ceiling division can be expressed as -(-a // b) or -(a // -b).
vstinner commentedon Feb 18, 2021
I'm not convinced that this operation is so common that it deserves a new operator. As Serhiy wrote, it must be first on python-ideas first. I close the issue.
--
floor division is x//y
integer ceil division can be implemented with math.ceil(x/y) for small numbers.
For large integer numbers, I like to use something like:
I let you adjust it for negative numbers ;-)
mdickinson commentedon Feb 18, 2021
I can't recall where I saw this originally, but you can use the spaceship operator "--0--" to turn floor division into ceiling division:
There's also the ++0++ operator when you want to emphasize that you really *do* mean floor division:
But yes, -(-n // d) is the easy (if a little bit cryptic) way to get the ceiling of n / d.
PedanticHacker commentedon Feb 18, 2021
The spaceship operator is kinda cool! :)
Well, I was hoping to avoid importing the math module just to have ceil division capabilities.
Anyway, my need for ceiling is that I am coding a chess GUI where I have a table with 2 columns and I create a new row whenever White makes a move. So, my implementation for row creation is math.ceil(len(moves) // 2) where moves is a list of chess moves.
So, the sequence of table rows after White and Black make their moves is initially 0 (since there are no moves yet) and then 1 (first row created after White made a move) and then 1 (again on row 1 after Black made a move, but on column 2), then 2 2 3 3 4 4 5 5 6 6 and so on.
If we had the ceil operator in Python, I could do: len(moves) /// 2 :)
By the way -- do you happen to know any better idea to get that kind of a sequence? That is 0 1 1 2 2 3 3 4 4 5 5 6 6 and so on.
tim-one commentedon Feb 18, 2021
(len(moves) + 1) // 2
vstinner commentedon Feb 18, 2021
You may like the new int.bit_count() attribute of Python 3.10 ;-)
https://docs.python.org/dev/library/stdtypes.html#int.bit_count
PedanticHacker commentedon Feb 18, 2021
My hat off to you, Mr. Peters! Your suggestion (len(moves) + 1) // 2 works perfectly and is much more elegant than --0-- len(moves) // 2. :)
Mr. Stinner, in what way would int.bit_count() be beneficial to me?
vstinner commentedon Feb 18, 2021
I found https://www.chessprogramming.org/Population_Count when I investigated the C implementation of _Py_popcount32(), which is used by int.bit_count().
I read it when I tried to document this surprising code:
---
I reformatted it as:
---
// 32-bit SWAR (SIMD Within A Register) popcount
---
PedanticHacker commentedon Feb 19, 2021
Mr. Stinner, I really don't understand what are you hinting at.