[Numpy-discussion] Oddity with numpy.int64 integer division
Warren Focke
focke at slac.stanford.edu
Mon Apr 23 22:04:10 EDT 2007
But even C89 required that x == (x/y)*y + (x%y), and that's not the case
here.
w
On Mon, 23 Apr 2007, David M. Cooke wrote:
> On Apr 23, 2007, at 16:41 , Christian Marquardt wrote:
> > On Mon, April 23, 2007 22:29, Christian Marquardt wrote:
> >> Actually,
> >>
> >> it happens for normal integers as well:
> >>
> >>>>> n = np.array([-5, -100, -150])
> >>>>> n // 100
> >> array([ 0, -1, -1])
> >>>>> -5//100, -100//100, -150//100
> >> (-1, -1, -2)
> >
> > and finally:
> >
> >>>> n % 100
> > array([95, 0, 50])
> >>>> -5 % 100, -100 % 100, -150 % 100
> > (95, 0, 50)
> >
> > So plain python / using long provides consistent results across //
> > and %, but numpy doesn't...
>
> Python defines x // y as returning the floor of the division, and x %
> y has the same sign as y. However, in C89, it is implementation-
> defined (i.e., portability-pain-in-the-ass) whether the floor or ceil
> is used when the signs of x and y differ. In C99, the result should
> be truncated. From the C99 spec, sec. 6.5.5, #6:
> When integers are divided, the result of the / operator
> is the algebraic quotient with any fractional part
> discarded.76) If the quotient a/b is representable, the
> expression (a/b)*b + a%b shall equal a.
>
> Numpy follows whatever the C compiler decides to use, because of
> speed-vs.-Python compatibility tradeoff.
>
> >
> > Christian.
> >
> >> On Mon, April 23, 2007 22:20, Christian Marquardt wrote:
> >>> Dear all,
> >>>
> >>> this is odd:
> >>>
> >>>>>> import numpy as np
> >>>>>> fact = 28250000L * 86400L
> >>>>>> nn = np.array([-20905000L])
> >>>>>> nn
> >>> array([-20905000], dtype=int64)
> >>>>>> nn[0] // fact
> >>> 0
> >>>
> >>> But:
> >>>
> >>>>>> long(nn[0]) // fact
> >>> -1L
> >>>
> >>> Is this a bug in numpy, or in python's implementation of longs? I
> >>> would
> >>> think both should give the same, really... (Python 2.5, numpy
> >>> 1.0.3dev3725,
> >>> Linux, Intel compilers...)
> >>>
> >>> Many thanks for any ideas / advice,
> >>>
> >>> Christian
>
> --
> |>|\/|<
> /------------------------------------------------------------------\
> |David M. Cooke http://arbutus.physics.mcmaster.ca/dmc/
> |cookedm at physics.mcmaster.ca
>
>
More information about the NumPy-Discussion
mailing list