Kathleen M Tacina | 24 Jan 16:29 2012
Picon

Unexpected behavior with np.min_scalar_type

I was experimenting with np.min_scalar_type to make sure it worked as expected, and found some unexpected results for integers between 2**63 and 2**64-1.  I would have expected np.min_scalar_type(2**64-1) to return uint64.  Instead, I get object.  Further experimenting showed that the largest integer for which np.min_scalar_type will return uint64 is 2**63-1.  Is this expected behavior?

On python 2.7.2 on a 64-bit linux machine:
>>> import numpy as np
>>> np.version.full_version
'2.0.0.dev-55472ca'
>>> np.min_scalar_type(2**8-1)
dtype('uint8')
>>> np.min_scalar_type(2**16-1)
dtype('uint16')
>>> np.min_scalar_type(2**32-1)
dtype('uint32')
>>> np.min_scalar_type(2**64-1)
dtype('O')
>>> np.min_scalar_type(2**63-1)
dtype('uint64')
>>> np.min_scalar_type(2**63)
dtype('O')

I get the same results on a Windows XP  machine running python 2.7.2 and numpy 1.6.1.

Kathy         
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Samuel John | 24 Jan 23:36 2012
Picon

Re: Unexpected behavior with np.min_scalar_type

I get the same results as you, Kathy.
*surprised*

(On OS X (Lion), 64 bit, numpy 2.0.0.dev-55472ca, Python 2.7.2.

On 24.01.2012, at 16:29, Kathleen M Tacina wrote:

> I was experimenting with np.min_scalar_type to make sure it worked as expected, and found some
unexpected results for integers between 2**63 and 2**64-1.  I would have expected
np.min_scalar_type(2**64-1) to return uint64.  Instead, I get object.  Further experimenting showed
that the largest integer for which np.min_scalar_type will return uint64 is 2**63-1.  Is this expected behavior?
> 
> On python 2.7.2 on a 64-bit linux machine:
> >>> import numpy as np
> >>> np.version.full_version
> '2.0.0.dev-55472ca'
> >>> np.min_scalar_type(2**8-1)
> dtype('uint8')
> >>> np.min_scalar_type(2**16-1)
> dtype('uint16')
> >>> np.min_scalar_type(2**32-1)
> dtype('uint32')
> >>> np.min_scalar_type(2**64-1)
> dtype('O')
> >>> np.min_scalar_type(2**63-1)
> dtype('uint64')
> >>> np.min_scalar_type(2**63)
> dtype('O')
> 
> I get the same results on a Windows XP  machine running python 2.7.2 and numpy 1.6.1. 
> 
> Kathy         
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion <at> scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
Mark Wiebe | 25 Jan 01:56 2012
Picon

Re: Unexpected behavior with np.min_scalar_type

On Tue, Jan 24, 2012 at 7:29 AM, Kathleen M Tacina <Kathleen.M.Tacina <at> nasa.gov> wrote:
I was experimenting with np.min_scalar_type to make sure it worked as expected, and found some unexpected results for integers between 2**63 and 2**64-1.  I would have expected np.min_scalar_type(2**64-1) to return uint64.  Instead, I get object.  Further experimenting showed that the largest integer for which np.min_scalar_type will return uint64 is 2**63-1.  Is this expected behavior?

This is a bug in how numpy detects the dtype of python objects.


You can see there it's only checking for a signed long long, not accounting for the unsigned case. I created a ticket for you here:


-Mark
 

On python 2.7.2 on a 64-bit linux machine:
>>> import numpy as np
>>> np.version.full_version
'2.0.0.dev-55472ca'
>>> np.min_scalar_type(2**8-1)
dtype('uint8')
>>> np.min_scalar_type(2**16-1)
dtype('uint16')
>>> np.min_scalar_type(2**32-1)
dtype('uint32')
>>> np.min_scalar_type(2**64-1)
dtype('O')
>>> np.min_scalar_type(2**63-1)
dtype('uint64')
>>> np.min_scalar_type(2**63)
dtype('O')

I get the same results on a Windows XP  machine running python 2.7.2 and numpy 1.6.1.

Kathy         

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Kathleen M Tacina | 25 Jan 21:30 2012
Picon

Re: Unexpected behavior with np.min_scalar_type

Thanks!

It was interesting to see why that happened. 

Kathy

On Tue, 2012-01-24 at 18:56 -0600, Mark Wiebe wrote:
On Tue, Jan 24, 2012 at 7:29 AM, Kathleen M Tacina <Kathleen.M.Tacina <at> nasa.gov> wrote:
I was experimenting with np.min_scalar_type to make sure it worked as expected, and found some unexpected results for integers between 2**63 and 2**64-1.  I would have expected np.min_scalar_type(2**64-1) to return uint64.  Instead, I get object.  Further experimenting showed that the largest integer for which np.min_scalar_type will return uint64 is 2**63-1.  Is this expected behavior?



This is a bug in how numpy detects the dtype of python objects.


https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/common.c#L18


You can see there it's only checking for a signed long long, not accounting for the unsigned case. I created a ticket for you here:


http://projects.scipy.org/numpy/ticket/2028


-Mark
 

On python 2.7.2 on a 64-bit linux machine:
>>> import numpy as np
>>> np.version.full_version
'2.0.0.dev-55472ca'
>>> np.min_scalar_type(2**8-1)
dtype('uint8')
>>> np.min_scalar_type(2**16-1)
dtype('uint16')
>>> np.min_scalar_type(2**32-1)
dtype('uint32')
>>> np.min_scalar_type(2**64-1)
dtype('O')
>>> np.min_scalar_type(2**63-1)
dtype('uint64')
>>> np.min_scalar_type(2**63)
dtype('O')

I get the same results on a Windows XP  machine running python 2.7.2 and numpy 1.6.1.

Kathy         

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Gmane