How to get the range of valid Numpy data types?

PythonNumpyTypes

Python Problem Overview


I'm interested in finding for a particular Numpy type (e.g. np.int64, np.uint32, np.float32, etc.) what the range of all possible valid values is (e.g. np.int32 can store numbers up to 2**31-1). Of course, I guess one can theoretically figure this out for each type, but is there a way to do this at run time to ensure more portable code?

Python Solutions


Solution 1 - Python

Quoting from a numpy discussion list:

> That information is available via numpy.finfo() and numpy.iinfo(): >
> In [12]: finfo('d').max > Out[12]: 1.7976931348623157e+308 >
> In [13]: iinfo('i').max > Out[13]: 2147483647 >
> In [14]: iinfo('uint8').max > Out[14]: 255

Link here.

Solution 2 - Python

You can use numpy.iinfo(arg).max to find the max value for integer types of arg, and numpy.finfo(arg).max to find the max value for float types of arg.

>>> numpy.iinfo(numpy.uint64).min
0
>>> numpy.iinfo(numpy.uint64).max
18446744073709551615L
>>> numpy.finfo(numpy.float64).max
1.7976931348623157e+308
>>> numpy.finfo(numpy.float64).min
-1.7976931348623157e+308

iinfo only offers min and max, but finfo also offers useful values such as eps (the smallest number > 0 representable) and resolution (the approximate decimal number resolution of the type of arg).

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
QuestionastrofrogView Question on Stackoverflow
Solution 1 - PythonperimosocordiaeView Answer on Stackoverflow
Solution 2 - PythonMark RushakoffView Answer on Stackoverflow