Re: [Cython] [cython-users] Integer division
Or perhaps we should respect float division even with cdivision... Thoughts? On Mon, Sep 7, 2015 at 5:13 PM, Robert Bradshaw <robertwb@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@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@gmail.com>:
On Mon, Sep 7, 2015 at 3:46 PM, Robert Bradshaw <robertwb@gmail.com> wrote:
On Mon, Sep 7, 2015 at 3:29 PM, Antony Lee <antony.lee@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/e61da2eb8a292bc34285c895aade523b6d35...
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@gmail.com>:
On Mon, Sep 7, 2015 at 11:59 AM, Antony Lee <antony.lee@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@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@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@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@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
participants (1)
-
Robert Bradshaw