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

Jussi Piitulainen jpiitula at ling.helsinki.fi
Wed Dec 14 07:22:27 EST 2011


Eelco writes:
> On 14 dec, 09:56, Jussi Piitulainen wrote:
> > 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.

The current Scheme draft does this. For each rounding method, it
provides an operation that provides both the quotient and the
remainder, an operation that provides the quotient, and an operation
that provides the remainder. The both-values operation is more awkward
to compose, as you rightly say.

It's just a matter of naming them all. Python has a good default
integer division as the pair of operators // and %. Python also
supports the returning of several values from functions as tuples. It
can be done.

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

Um, no, I mean function. The allegedly alleged problem is that there
may be two (or more) different values for f(x,y), which makes f not a
_function_ (and the notation f(x,y) maybe inappropriate).

Surjectivity is as much beside the point as bijectivity, but I think
we have surjectivity for rem: Z * Z -> Z if we use a definition that
produces both positive and negative remainders, or rem: Z * Z -> N if
we have non-negative remainders (and include 0 in N, which is another
bone of contention). We may or may not want to exclude 0 as the
modulus, or divisor if you like. It is at least a special case.

It's injectivity that fails: 9 % 4 == 6 % 5 == 3 % 2, while Python
quite sensibly has (9, 4) != (6, 5) != (3, 2). (How I love the
chaining of the comparisons.)

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

I think something is gained. Not sure I would call it philosophy.



More information about the Python-list mailing list