[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-20 Thread Soni L.
We feel like this may make sense to bring up, too: https://rust-lang.github.io/rfcs/2169-euclidean-modulo.html On 2022-03-20 04:06, Om Joshi wrote: > Has anyone in this thread linked this blog post yet? > > http://python-history.blogspot.com/2010/08/why-pythons-integer-division-floors.html > > Mu

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-20 Thread Om Joshi
Has anyone in this thread linked this blog post yet? http://python-history.blogspot.com/2010/08/why-pythons-integer-division-floors.html Much is a rehash of this thread but for completeness it might be useful to read Guido van Rossum's thoughts and the subsequent discussion in the comments sect

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-19 Thread Ben Rudiak-Gould
On Fri, Mar 18, 2022 at 8:30 PM MRAB wrote: > Wikipedia describes Euclidean division. > > Basically, the modulo is non-negative: > > a == b * q + r where 0 <= r < abs(b) That convention in the Wikipedia article dates back to a 2004 edit by an anonymous (IP) editor. The only reference in that

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-19 Thread Steven D'Aprano
On Sat, Mar 19, 2022 at 03:49:28PM -0500, Tim Peters wrote: > [Steven D'Aprano ] > > Sure, for floats. I certainly wouldn't want to change the behaviour for > > floats. We could change the behaviour for ints (or at least we could if > > not constrained by backwards compatibility) or add a new funct

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-19 Thread Tim Peters
[Steven D'Aprano ] > Sure, for floats. I certainly wouldn't want to change the behaviour for > floats. We could change the behaviour for ints (or at least we could if > not constrained by backwards compatibility) or add a new function. We could - but why would we? Just because a thing _can_ be don

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-18 Thread Steven D'Aprano
On Fri, Mar 18, 2022 at 11:16:47PM -0500, Tim Peters wrote: > [Steven D'Aprano [\ > > Alas, math.remainder goes through float: > > > > >>> math.remainder(3**500, 3) # Should be 0. > > 1.0 > > >>> math.remainder(3**500 + 2, 3) # Should be 2. > > 1.0 > > You mean -1 here: remainder() returns a mem

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-18 Thread Tim Peters
[Steven D'Aprano [\ > Alas, math.remainder goes through float: > > >>> math.remainder(3**500, 3) # Should be 0. > 1.0 > >>> math.remainder(3**500 + 2, 3) # Should be 2. > 1.0 You mean -1 here: remainder() returns a member of the equivalence class with least absolute value, and abs(-1) < abs(2).

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-18 Thread MRAB
On 2022-03-19 02:38, Steven D'Aprano wrote: Hi Nathan, and welcome! On Fri, Mar 18, 2022 at 05:13:16AM -, Nathan Levett wrote: First time posting here ~ I've recently encountered that python does not have an OOTB operator for modulo that is consistent with Euclidean division. Although it

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-18 Thread Steven D'Aprano
On Fri, Mar 18, 2022 at 04:26:54PM -0500, Tim Peters wrote: > Another choice is made by math.remainder: > > >>> import math > >>> math.remainder(7, 10) > -3.0 Alas, math.remainder goes through float: >>> math.remainder(3**500, 3) # Should be 0. 1.0 >>> math.remainder(3**500 + 2, 3) # Should

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-18 Thread Steven D'Aprano
Hi Nathan, and welcome! On Fri, Mar 18, 2022 at 05:13:16AM -, Nathan Levett wrote: > First time posting here ~ I've recently encountered that python does > not have an OOTB operator for modulo that is consistent with Euclidean > division. Although it should be easy for anyone who wants thi

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-18 Thread Tim Peters
[Nathan Levett ] > First time posting here ~ I've recently encountered that python does not > have an OOTB operator for modulo that is consistent with Euclidean > division. You need to be very explicit about what you intend - my best guess about what you intend isn't supported directly by any prog

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-18 Thread Chris Angelico
On Sat, 19 Mar 2022 at 07:16, Ben Rudiak-Gould wrote: > > On Fri, Mar 18, 2022 at 8:31 AM Chris Angelico wrote: >> >> if y < 0: return -(x // -y), x % y > > > I think that should be > > if y < 0: return -(x // -y), x % -y > Hmm. According to Wikipedia [1] the result of divmod(7,-3) shoul

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-18 Thread Ben Rudiak-Gould
On Fri, Mar 18, 2022 at 8:31 AM Chris Angelico wrote: > if y < 0: return -(x // -y), x % y I think that should be if y < 0: return -(x // -y), x % -y ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to pyt

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-18 Thread Chris Angelico
On Sat, 19 Mar 2022 at 02:09, Nathan Levett wrote: > > Howdy python gang, > > First time posting here ~ I've recently encountered that python does not have > an OOTB operator for modulo that is consistent with Euclidean division. > Although it should be easy for anyone who wants this to create i

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-18 Thread David Mertz, Ph.D.
Are you speaking of how to generalize modulo to negative numbers (about which programming languages vary)? The bar for adding new syntax to Python is VERY high. Probably easier is getting a function in the `math` module. But even there, you'll need to explain what behavior you want AND why that be

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-18 Thread André Roberge
On Fri, Mar 18, 2022 at 12:09 PM Nathan Levett wrote: > Howdy python gang, > > First time posting here ~ I've recently encountered that python does not > have an OOTB operator for modulo that is consistent with Euclidean > division. Are you referring to something different than the divmod built

[Python-ideas] Re: A modulo operator consistent with euclidean division.

2022-03-18 Thread Damian Shaw
Can you give some examples of how it would be used differently than the current modulo operator and what value it would bring? For those who have not taken number theory courses in a long time (or never!) it's not clear how this would be useful for Python. Damian (he/him) On Fri, Mar 18, 2022 at