[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

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

2022-03-18 Thread Nathan Levett
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 it themselves, it struck me as odd that it was not an already i