% is not an operator [was Re: Verbose and flexible args and kwargs syntax]

Eelco hoogendoorn.eelco at gmail.com
Wed Dec 14 05:09:32 EST 2011


On 14 dec, 09:56, Jussi Piitulainen <jpiit... at ling.helsinki.fi> wrote:
> Steven D'Aprano writes:
> > On Mon, 12 Dec 2011 09:29:11 -0800, Eelco wrote:
>
> > [quoting Jussi Piitulainen <jpiit... at ling.helsinki.fi>]
> > >> They recognize modular arithmetic but for some reason insist that
> > >> there is no such _binary operation_. But as I said, I don't
> > >> understand their concern. (Except the related concern about some
> > >> programming languages, not Python, where the remainder does not
> > >> behave well with respect to division.)
>
> > I've never come across this, and frankly I find it implausible that
> > *actual* mathematicians would say that. Likely you are
> > misunderstanding a technical argument about remainder being a
> > relation rather than a bijunction. The argument would go something
> > like this:
>
> (For 'bijunction', read 'function'.)
>
> I'm not misunderstanding any argument. There was no argument. There
> was a blanket pronouncement that _in mathematics_ mod is not a binary
> operator. I should learn to challenge such pronouncements and ask what
> the problem is. Maybe next time.
>
> But you are right that I don't know how actual mathematicians these
> people are. I'm not a mathematician. I don't know where to draw the
> line.
>
> A Finnish actual mathematician stated a similar prejudice towards mod
> as a binary operator in a Finnish group. I asked him what is wrong
> with Knuth's definition (remainder after flooring division), and I
> think he conceded that it's not wrong. Number theorists just choose to
> work with congruence relations. I have no problem with that.
>
> He had experience with students who confused congruences modulo some
> modulus with a binary operation, and mixed up their notations because
> of that. That is a reason to be suspicious, but it is a confusion on
> the part of the students. Graham, Knuth, Patashnik contrast the two
> concepts explicitly, no confusion there.
>
> And I know that there are many ways to define division and remainder
> so that x div y + x rem y = x. Boute's paper cited in [1] advocates a
> different one and discusses others.
>
> [1] <http://en.wikipedia.org/wiki/Modulo_operation>
>
> But I think the argument "there are several such functions, therefore,
> _in mathematics_, there is no such function" is its own caricature.

Indeed. Obtaining a well defined function is just a matter of picking
a convention and sticking with it.

Arguably, the most elegant thing to do is to define integer division
and remainder as a single operation; which is not only the logical
thing to do mathematically, but might work really well
programmatically too.

The semantics of python dont really allow for this though. One could
have:

d, r = a // b

But it wouldnt work that well in composite expressions; selecting the
right tuple index would be messy and a more verbose form would be
preferred. However, performance-wise its also clearly the best
solution, as one often needs both output arguments and computing them
simultaniously is most efficient.

At least numpy should have something like:
d, r = np.integer_division(a, b)

And something similar in the math module for scalars.


> > "Remainder is not uniquely defined. For example, the division of -42
> > by -5 can be written as either:
>
> >     9*-5 + 3 = -42
> >     8*-5 + -2 = -42
>
> > so the remainder is either 3 or -2. Hence remainder is not a bijection
> > (1:1 function)."
>
> Is someone saying that _division_ is not defined because -42 div -5 is
> somehow both 9 and 8? Hm, yes, I see that someone might. The two
> operations, div and rem, need to be defined together.
>
> (There is no way to make remainder a bijection. You mean it is not a
> function if it is looked at in a particular way.)

Surjection is the word you are looking for

That is, if one buys the philosophy of modernists like bourbaki in
believing there is much to be gained by such pedantry.



More information about the Python-list mailing list