[Numpy-discussion] Poll: Semantics for % in Cython

Robert Kern robert.kern@gmail....
Thu Mar 12 18:05:10 CDT 2009

On Thu, Mar 12, 2009 at 17:45, Sturla Molden <sturla@molden.no> wrote:
>> 2009/3/13 Charles R Harris <charlesr.harris@gmail.com>:
>>> That said, I think it best to leave '%' with its C default and add a
>>> special
>>> modulus function for the python version. Changing its meaning in C-like
>>> code
>>> is going to confuse things.
>> This is Cython code, so I think there is an argument to be made that
>> it is Python-like!
> I'll just repeat what I've already said on the Cython mailing list:
> I think C types should behave like C types and Python objects like Python
> objects. If a C long suddenly starts to return double when divided by
> another C long, then that will be a major source of confusion on my part.
> If I want the behaviour of Python integers, Cython lets me use Python
> objects. I don't declare a variable cdef long if I want it to behave like
> a Python int.

That may be part of the confusion. The expression "-1%5" has no
variables. Perhaps Dag can clarify what he is asking about:

  # Constants?  (No one uses just constants in expressions,
  # really, but consistency with the other choices will
  # affect this.)
  -1 % 5

  # Explicitly declared C types?
  cdef long i, j, k
  i = -1
  j = 5
  k = i % j

  # Python types?
  i = -1
  j = 5
  k = i % j

  # A mixture?
  cdef long i
  i = -1
  j = 5
  k = i % j

When I do (2147483647 + 2147483647) in current Cython, to choose
another operation, does it use C types, or does it construct PyInts?
I.e., do I get C wraparound arithmetic, or do I get a PyLong?

I recommend making % behave consistently with the other operators;
i.e. if <x>+<y> uses C semantics, <x>%<y> should, too.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

More information about the Numpy-discussion mailing list