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

Christopher Barker Chris.Barker@noaa....
Fri Mar 13 10:52:40 CDT 2009

Robert Kern wrote:
>>>   # Explicitly declared C types?
>>>   cdef long i, j, k
>>>   i = -1
>>>   j = 5
>>>   k = i % j
>> This one is what I'm really asking about.
> My opinion on this is that C semantics have been explicitly requested,
> so they should be used.

maybe ...

> One possibility (that may be opening a can of worms) is to have two
> sets of operators, one that does "native" semantics (C for cdef longs,
> Python for Python ints) and one that does Python semantics even on
> cdef longs.

ouch! no. I think this is a case of practicality vs. purity. A common 
use case would be that a person starts out with their code in python, 
then moves it to cython, then adds the cdef, testing (or not!) as they go.

The problem here is that yes, there are going to be differences when you 
apply a cdef, but a difference like this, that may very well not show up 
at all in tests (unless the user is aware enough of this particular 
issue to explicitly test for it). Now the code is broken in a subtle, 
and hard to find way that could turn up who knows when, with want data.

This is kind of like the "new division" issue with python itself -- it 
is much better to simply be explicit: "/" means float division, "//" 
means integer division, regardless of the types of the operands.

If you apply the same principle here, then we should one operator for "c 
style modulo", and one for "python style modulo", regardless of the 
types of the operands.


Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception


More information about the Numpy-discussion mailing list