[Numpy-discussion] max value of np scalars
Tue Sep 29 16:10:22 CDT 2009
On Tue, Sep 29, 2009 at 15:52, Neal Becker <email@example.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)))
> 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?
Could be. I'm not entirely sure why it would be doing this, but the
code does fall back to generic object implementations under certain
Of course, np.uint64(-1) is the correct answer for unsigned integer
types since we implement wraparound.
> 2) How can I correctly implement get_max?
np.iinfo() for integer types and np.finfo() for floating point types.
In : np.iinfo(np.uint64).max
In : np.finfo(np.float32).max
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion