I want to check the range for a given NumPy data type, e.g. uint16. Is there any function to check it?

+1 vote

Best answer

There are two NumPy functions *iinfo()* and *finfo()* for this task. The *iinfo()* function returns the machine limits for integer types, whereas *finfo()* function returns the machine limits for floating point types. You can see the upper limit and lower limit allowed by the machine for a given data type using these functions.

Here are some examples

>>> import numpy as np

>>> np.iinfo(np.uint16)

iinfo(min=0, max=65535, dtype=uint16)

>>> np.iinfo(np.int8)

iinfo(min=-128, max=127, dtype=int8)

>>> np.finfo(np.float16)

finfo(resolution=0.001, min=-6.55040e+04, max=6.55040e+04, dtype=float16)

>>> np.finfo(np.float32)

finfo(resolution=1e-06, min=-3.4028235e+38, max=3.4028235e+38, dtype=float32)

>>> np.iinfo(np.uint32)

iinfo(min=0, max=4294967295, dtype=uint32)

>>>