On Sat, 19 Mar 2022 at 02:09, Nathan Levett <nathan.levett...@gmail.com> 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 it themselves, 
> it struck me as odd that it was not an already included feature. I was even 
> more shocked to see a list indicating that most languages don't include one 
> consister with Euclidean division (disheartening to realise that the number 
> theory way of doing things is not the default). I was so shocked at it's lack 
> from python that it motivated me to post this, I suppose!
>
> I guess I'm posting to check how open anyone would be to the idea? I'm not 
> sure if '%%' is defined anywhere, but it seemed like an intuitive suggestion 
> if not already used as an operator, parallel to the syntax of the '**' 
> operator.
>
> Keen to know how open y'all're to it!

Given that you're clearly not talking about the default division
operator, nor the divmod function (which I believe performs the
precise same operation, I'm guessing you're talking about the way this
behaves with negative numbers? As far as I know, there's only one sane
way to interpret a//b, a%b where a and b are both positive integers,
so either negative numbers or non-integers must be involved here.

In general, if the divisor is a positive integer, you will always get
a remainder that is in range(divisor), which is a very useful feature.
Not all languages behave the same way here; for instance, -7%3 is 2 in
Python, but -1 in JavaScript. I'm not sure which one you'd prefer, but
my understanding of Wikipedia on the subject is that it agrees with
Python and not JavaScript.

If the divisor is negative, Python (and many languages) will give a
negative remainder, thus ensuring that the remainder is between zero
and the divisor. Is this the part you're disputing? Honestly, it's not
something that I've ever had any reason to question, but I could well
imagine it'd be useful both ways.

To get the opposite behaviour, all you'd have to do is, when the
divisor is negative, flip its sign, then flip the sign of its
quotient:

def divmod_the_other_way(x, y):
    if y < 0: return -(x // -y), x % y
    return x // y, x % y

As David Mertz points out, it would be VERY difficult to add this as a
syntactic feature (especially since it doesn't really extend to other
data types), but a variant divmod function in the math module would be
doable if there's enough justification for it.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/EFT4L6C5EQ2CCLSMIGCQLCVVGWDMKOS7/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to