Maximum allowed value for a numpy data type

PythonNumpy

Python Problem Overview


I am working with numpy arrays of a range of data types (uint8, uint16, int16, etc.). I would like to be able to check whether a number can be represented within the limits of an array for a given datatype. I am imagining something that looks like:

>>> im.dtype
dtype('uint16')
>>> dtype_max(im.dtype)
65535
>>> dtype_min(im.dtype)
0

Does something like this exist? By the way, I feel like this has to have been asked before, but my search came up empty, and all of the "similar questions" appear to be unrelated.

Edit: Of course, now that I've asked, one of the "related" questions does have the answer. Oops.

Python Solutions


Solution 1 - Python

min_value = np.iinfo(im.dtype).min
max_value = np.iinfo(im.dtype).max

docs:

  • np.iinfo (machine limits for integer types)
  • np.finfo (machine limits for floating point types)

Solution 2 - Python

You're looking for numpy.iinfo for integer types. Documentation here.

There's also numpy.finfo for floating point types. Documentation here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjdmcbrView Question on Stackoverflow
Solution 1 - PythonBruno GelbView Answer on Stackoverflow
Solution 2 - PythonAlexView Answer on Stackoverflow