[Numpy-discussion] What type should / return in python 3k when applied to two integer types?
josef.pktd@gmai...
josef.pktd@gmai...
Thu Aug 27 15:35:57 CDT 2009
On Thu, Aug 27, 2009 at 3:57 PM, Charles R
Harris<charlesr.harris@gmail.com> wrote:
>
>
> On Thu, Aug 27, 2009 at 1:46 PM, Robert Kern <robert.kern@gmail.com> wrote:
>>
>> On Thu, Aug 27, 2009 at 12:43, Charles R
>> Harris<charlesr.harris@gmail.com> wrote:
>> >
>> >
>> > On Thu, Aug 27, 2009 at 1:27 PM, Robert Kern <robert.kern@gmail.com>
>> > wrote:
>> >>
>> >> On Thu, Aug 27, 2009 at 11:24, Charles R
>> >> Harris<charlesr.harris@gmail.com> wrote:
>> >> > I'm thinking double. There is a potential loss of precision for 64
>> >> > bit
>> >> > ints
>> >> > but nothing else seems reasonable for a default. Thoughts?
>> >>
>> >> Python int / Python int => Python float
>> >>
>> >> no matter how many decimal places the two ints have. I also say double.
>> >
>> > What about //?
>> >
>> > In [1]: x = ones(1, dtype=uint64)
>> >
>> > In [2]: y = ones(1, dtype=int64)
>> >
>> > In [3]: floor_divide(x,y).dtype
>> > Out[3]: dtype('float64')
>>
>> Ewww. It should be an appropriate integer type. Probably whatever x*y is.
>
> In [5]: (x*y).dtype
> Out[5]: dtype('float64')
> ??
>
> The problem is that numpy doesn't have an integer type that can support all
> the possible return values. Python doesn't have that problem, so mapping
> Python behaviour to numpy is a square peg round hole sort of thing. Float64
> doesn't do the job either. The only two really "correct" options would seem
> to be raising an error for this combination of types or returning object
> arrays containing long ints.
>
> Chuck
>
I'm always a bit surprised about integers in numpy and try to avoid
calculations with them. So I would be in favor of x/y is correct
floating point answer.
Josef
>>> x = np.ones(1, dtype=np.uint64); y = np.ones(1, dtype=np.int64)
>>> np.true_divide((0*x),0)
array([ 0.])
>>> np.true_divide((0*x),0).dtype
dtype('float64')
>>> np.true_divide((0*x),0.)
array([ NaN])
>>> np.true_divide((x),0)
array([ 0.])
>>> np.true_divide((x),0.)
array([ Inf])
floor doesn't return an integer
>>> np.floor(x).dtype
dtype('float64')
More information about the NumPy-Discussion
mailing list