[Numpy-discussion] Poll: Semantics for % in Cython
Dag Sverre Seljebotn
dagss@student.matnat.uio...
Fri Mar 13 01:41:07 CDT 2009
Robert Kern wrote:
> 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.
Whether division returns float or not is an orthogonal and unrelated
issue (and when it does, which is not the default, // is the C division
operator).
When we say that this affects division; what we mean is that -7 // 6
returns -2 in Python; so that (-7 // 6)*6 + (-7 % 6) == -2*6 + 5 == -7.
With C behaviour of % and /, I believe -7 // 6 == -1.
(And when I use // it is to be unambigious; by default you can use / as
well for the same thing.)
> 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
This one is what I'm really asking about.
> 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?
C wraparound. Suggestions welcome :-)
> I recommend making % behave consistently with the other operators;
> i.e. if <x>+<y> uses C semantics, <x>%<y> should, too.
--
Dag Sverre
More information about the Numpy-discussion
mailing list