[Numpy-discussion] What type should / return in python 3k when applied to two integer types?

Charles R Harris charlesr.harris at gmail.com
Thu Aug 27 17:56:54 EDT 2009


On Thu, Aug 27, 2009 at 3:22 PM, Christopher Barker
<Chris.Barker at noaa.gov>wrote:

> Robert Kern wrote:
> > On Thu, Aug 27, 2009 at 12:43, Charles R
> > Harris<charlesr.harris at gmail.com> wrote:
> >> In [3]: floor_divide(x,y).dtype
> >> Out[3]: dtype('float64')
> >
> > Ewww. It should be an appropriate integer type. Probably whatever x*y is.
>
> +1 if you are working with integers, you should get integers, because
> that's probably what you want. -- they can overflow, etc. anyway, so
> buyer beware!
>
> In [7]: x.dtype
> Out[7]: dtype('int64')
>
> In [8]: y.dtype
> Out[8]: dtype('uint64')
>
> In [9]: (x * y).dtype
> Out[9]: dtype('float64')
>
> hmmm -- I thought we had removed this kind of silent upcasting
> (particularly int-> float), but I guess when you mix two types, numpy
> has to choose something!
>
> In any case, x/y should probably return the same type as x*y.
>

Another possibility is to cast the signed type to unsigned of the same
precisions. But then

uint64(1)//int64(-1) == 0

which may be too much of a surprise. Note that int64(x)//uint(64) always
fits in int64, so the order of the types could be significant also. However,
too possibilities for the return type is likely a complexity too far.

The cast from signed to unsigned type of the same precision is what I would
have chosen for numpy in the first place. Then again, I tend to be a bit
"odd" about some things ;)

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090827/a886cc57/attachment.html>


More information about the NumPy-Discussion mailing list