[Cython] [cython-users] Integer division

Robert Bradshaw robertwb at gmail.com
Tue Sep 8 05:54:37 CEST 2015


On Mon, Sep 7, 2015 at 5:42 PM, Antony Lee <antony.lee at berkeley.edu> wrote:
> My 2c (not cc'ed to cython-devel, as I am not registered):
>
> - language_level=3 implies __future__ division.
> - if __future__ division is set: int / int => float (whether variables are
> untyped (= typed at runtime by python) or typed (including by the
> inferencer))
> - if __future__ division is not set: int / int => int, with
> opposite-sign-case and zero-divisor-case handled depending on cdivision
> - in both cases, int // int => int, with opposite-sign-case and
> zero-divisor-case handled depending on cdivision (also for % and divmod).

That's what I'm leaning towards as well, but it is backwards
incompatible. Anyone else have any opinions?

> 2015-09-07 17:15 GMT-07:00 Robert Bradshaw <robertwb at gmail.com>:
>>
>> Or perhaps we should respect float division even with cdivision...
>> Thoughts?
>>
>> On Mon, Sep 7, 2015 at 5:13 PM, Robert Bradshaw <robertwb at gmail.com>
>> wrote:
>> > Oh, that's another issue. I'll clarify this in the documentation.
>> >
>> > On Mon, Sep 7, 2015 at 3:59 PM, Antony Lee <antony.lee at berkeley.edu>
>> > wrote:
>> >> Actually I realized that it's another problem; setting cdivision to
>> >> True (as
>> >> I did in the directive of the example I gave, because I basically
>> >> always set
>> >> this to True) overrides __future__ division and restores the C division
>> >> semantic that int / int = int.  The documentation actually says that
>> >> the
>> >> cdivision directive should only affect the case of operands with
>> >> different
>> >> signs and of division by zero, not whether int / int = int.
>> >> Antony
>> >>
>> >> 2015-09-07 15:50 GMT-07:00 Robert Bradshaw <robertwb at gmail.com>:
>> >>>
>> >>> On Mon, Sep 7, 2015 at 3:46 PM, Robert Bradshaw <robertwb at gmail.com>
>> >>> wrote:
>> >>> > On Mon, Sep 7, 2015 at 3:29 PM, Antony Lee <antony.lee at berkeley.edu>
>> >>> > wrote:
>> >>> >> Sorry, I intended the entire discussion to apply to the case of
>> >>> >> Python3
>> >>> >> semantics.  Indeed, even with language_level=3 and from __future__
>> >>> >> import
>> >>> >> division, foo.decl(3, 3) prints "1, 2, 0, 1" whereas foo.nodecl(3,
>> >>> >> 3)
>> >>> >> prints
>> >>> >> "1.0, 0.5, 2.0, 1.0".
>> >>> >
>> >>> > from __future__ import division behaves as expected; I just added
>> >>> > some
>> >>> > even more explicit tests:
>> >>> >
>> >>> >
>> >>> > https://github.com/cython/cython/commit/e61da2eb8a292bc34285c895aade523b6d353414
>> >>> >
>> >>> > I'm surprised language_level=3 doesn't automatically set this
>> >>> > directive.
>> >>>
>> >>> Are you sure you're setting it correctly?
>> >>> https://github.com/cython/cython/blob/master/tests/run/cython3.pyx#L30
>> >>>
>> >>> >> 2015-09-07 15:07 GMT-07:00 Robert Bradshaw <robertwb at gmail.com>:
>> >>> >>>
>> >>> >>> On Mon, Sep 7, 2015 at 11:59 AM, Antony Lee
>> >>> >>> <antony.lee at berkeley.edu>
>> >>> >>> wrote:
>> >>> >>> > I would like to suggest that division of integers with "/"
>> >>> >>> > prints a
>> >>> >>> > warning
>> >>> >>> > at compile time.  In my opinion, "/" should be restricted to
>> >>> >>> > float
>> >>> >>> > division,
>> >>> >>> > and "//" should be used when rounding semantics are required.
>> >>> >>> >
>> >>> >>> > Otherwise, we get weird edge cases such as:
>> >>> >>> >
>> >>> >>> > #cython: cdivision=True, infer_types=True
>> >>> >>> > def decl(int m, int n):
>> >>> >>> >     for i in range(1, m):
>> >>> >>> >         for j in range(1, n):
>> >>> >>> >             print(i / j) # integer division
>> >>> >>> >
>> >>> >>> > def nodecl(m, n):
>> >>> >>> >     for i in range(1, m):
>> >>> >>> >         for j in range(1, n):
>> >>> >>> >             print(i / j) # float division
>> >>> >>>
>> >>> >>> Slight correction: int or float division depending on whether
>> >>> >>> you're
>> >>> >>> using Python 2 or Python 3. The runtime type of i/j isn't
>> >>> >>> something we
>> >>> >>> can delegate to the C compiler.
>> >>> >>>
>> >>> >>> > (whose behavior may even change if type inference rules
>> >>> >>> > changes).
>> >>> >>> >
>> >>> >>> > A more extreme choice would be to have "/" always behave like
>> >>> >>> > float
>> >>> >>> > (double)
>> >>> >>> > division even with integer arguments.
>> >>> >>>
>> >>> >>> You mean like putting from __future__ import division at the top
>> >>> >>> of
>> >>> >>> your file? Or compiling with -3 (for Python 3 language semantics)?
>> >>> >>>
>> >>> >>> --
>> >>> >>>
>> >>> >>> ---
>> >>> >>> You received this message because you are subscribed to the Google
>> >>> >>> Groups
>> >>> >>> "cython-users" group.
>> >>> >>> To unsubscribe from this group and stop receiving emails from it,
>> >>> >>> send
>> >>> >>> an
>> >>> >>> email to cython-users+unsubscribe at googlegroups.com.
>> >>> >>> For more options, visit https://groups.google.com/d/optout.
>> >>> >>
>> >>> >>
>> >>> >> --
>> >>> >>
>> >>> >> ---
>> >>> >> You received this message because you are subscribed to the Google
>> >>> >> Groups
>> >>> >> "cython-users" group.
>> >>> >> To unsubscribe from this group and stop receiving emails from it,
>> >>> >> send
>> >>> >> an
>> >>> >> email to cython-users+unsubscribe at googlegroups.com.
>> >>> >> For more options, visit https://groups.google.com/d/optout.
>> >>>
>> >>> --
>> >>>
>> >>> ---
>> >>> You received this message because you are subscribed to the Google
>> >>> Groups
>> >>> "cython-users" group.
>> >>> To unsubscribe from this group and stop receiving emails from it, send
>> >>> an
>> >>> email to cython-users+unsubscribe at googlegroups.com.
>> >>> For more options, visit https://groups.google.com/d/optout.
>> >>
>> >>
>> >> --
>> >>
>> >> ---
>> >> You received this message because you are subscribed to the Google
>> >> Groups
>> >> "cython-users" group.
>> >> To unsubscribe from this group and stop receiving emails from it, send
>> >> an
>> >> email to cython-users+unsubscribe at googlegroups.com.
>> >> For more options, visit https://groups.google.com/d/optout.
>>
>> --
>>
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "cython-users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to cython-users+unsubscribe at googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cython-users+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


More information about the cython-devel mailing list