[Numpy-discussion] max value of np scalars

Charles R Harris charlesr.harris at gmail.com
Tue Sep 29 17:14:29 EDT 2009


On Tue, Sep 29, 2009 at 2:52 PM, Neal Becker <ndbecker2 at gmail.com> wrote:

> I need the max value of an np scalar type.  I had used this code:
>
> def get_max(is_signed, base_type, total_bits):
>    print 'get_max:', is_signed, base_type, total_bits
>    if is_signed:
>        return (~(base_type(-1) << (total_bits-1)))
>    else:
>        print type(base_type (-1) << total_bits)
>        return (~(base_type (-1) << total_bits))
>
> This doesn't work for e.g., np.uint64.  As the 'print' shows,
>  get_max: False <type 'numpy.uint64'> 10
> <type 'long'>
>
> The type of np.uint64 (-1) << 10 is not np.uint64, but long.  This seems
> very strange to me.
>
> So, 2 questions.
>
> 1) Is this expected behavior?
>
> 2) How can I correctly implement get_max?
>
> Some odd behavior here:

In [24]: left_shift(uint64(-1), 1)
Out[24]: 36893488147419103230L

In [25]: type(left_shift(uint64(-1), 1))
Out[25]: <type 'long'>

In [26]: type(left_shift(uint32(-1), 1))
Out[26]: <type 'numpy.int64'>

In [27]: type(uint32(-1))
Out[27]: <type 'numpy.uint32'>

In [28]: type(left_shift(uint32(-1), 1))
Out[28]: <type 'numpy.int64'>

In [29]: type(uint64(-1))
Out[29]: <type 'numpy.uint64'>

I don't think the arguments should be promoted for what should(?) be bitwise
operations. Needs some discussion.

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


More information about the NumPy-Discussion mailing list