[issue43255] Ceil division with /// operator

2022-02-04 Thread Nathaniel Manista


Change by Nathaniel Manista :


--
nosy: +Nathaniel Manista

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43255] Ceil division with /// operator

2021-02-19 Thread Guido van Rossum


Change by Guido van Rossum :


--
nosy:  -gvanrossum

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43255] Ceil division with /// operator

2021-02-19 Thread Boštjan Mejak

Boštjan Mejak  added the comment:

Mr. Stinner, I really don't understand what are you hinting at.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43255] Ceil division with /// operator

2021-02-18 Thread STINNER Victor


STINNER Victor  added the comment:

> Mr. Stinner, in what way would int.bit_count() be beneficial to me?

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:
---
x = x - ((x >> 1) & 0x);
x = (x & 0x) + ((x >> 2) & 0x);
x = (x + (x >> 4)) & 0x0F0F0F0F;
return (uint32_t)((uint64_t)x * (uint64_t)0x01010101) >> 24;
---

I reformatted it as:
---
// 32-bit SWAR (SIMD Within A Register) popcount

// Binary: 0 1 0 1 ...
const uint32_t M1 = 0x;
// Binary: 00 11 00 11. ..
const uint32_t M2 = 0x;
// Binary:     ...
const uint32_t M4 = 0x0F0F0F0F;
// 256**4 + 256**3 + 256**2 + 256**1
const uint32_t SUM = 0x01010101;

// Put count of each 2 bits into those 2 bits
x = x - ((x >> 1) & M1);
// Put count of each 4 bits into those 4 bits
x = (x & M2) + ((x >> 2) & M2);
// Put count of each 8 bits into those 8 bits
x = (x + (x >> 4)) & M4;
// Sum of the 4 byte counts
return (uint32_t)((uint64_t)x * (uint64_t)SUM) >> 24;
---

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43255] Ceil division with /// operator

2021-02-18 Thread Boštjan Mejak

Boštjan Mejak  added the comment:

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?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43255] Ceil division with /// operator

2021-02-18 Thread STINNER Victor


STINNER Victor  added the comment:

> 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. 

You may like the new int.bit_count() attribute of Python 3.10 ;-)
https://docs.python.org/dev/library/stdtypes.html#int.bit_count

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43255] Ceil division with /// operator

2021-02-18 Thread Tim Peters


Tim Peters  added the comment:

(len(moves) + 1) // 2

--
nosy: +tim.peters

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43255] Ceil division with /// operator

2021-02-18 Thread Boštjan Mejak

Boštjan Mejak  added the comment:

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.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43255] Ceil division with /// operator

2021-02-18 Thread Mark Dickinson


Change by Mark Dickinson :


--
resolution: not a bug -> rejected

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43255] Ceil division with /// operator

2021-02-18 Thread Mark Dickinson


Mark Dickinson  added the comment:

I can't recall where I saw this originally, but you can use the spaceship 
operator "--0--" to turn floor division into ceiling division:

>>> 12//5
2
>>> --0-- 12//5
3

There's also the ++0++ operator when you want to emphasize that you really *do* 
mean floor division:

>>> ++0++ 12//5
2

But yes, -(-n // d) is the easy (if a little bit cryptic) way to get the 
ceiling of n / d.

--
nosy: +mark.dickinson

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43255] Ceil division with /// operator

2021-02-18 Thread STINNER Victor


STINNER Victor  added the comment:

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:

def ceil_div(x, y):
rem=x % y
if rem:
x += y - rem
return x//y

I let you adjust it for negative numbers ;-)

--
nosy: +vstinner
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43255] Ceil division with /// operator

2021-02-18 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

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).

--
nosy: +serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43255] Ceil division with /// operator

2021-02-18 Thread Boštjan Mejak

New submission from Boštjan Mejak :

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.

--
components: Library (Lib)
messages: 387231
nosy: PedanticHacker, gvanrossum, rhettinger
priority: normal
severity: normal
status: open
title: Ceil division with /// operator
type: enhancement
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com