How to check if a number is a np.float64 or np.float32 or np.float16?

numpy, python

Solution

You can use `np.floating`:

In [11]: isinstance(np.float16(1), np.floating)
Out[11]: True

In [12]: isinstance(np.float32(1), np.floating)
Out[12]: True

In [13]: isinstance(np.float64(1), np.floating)
Out[13]: True

Note: non-numpy types return False:

In [14]: isinstance(1, np.floating)
Out[14]: False

In [15]: isinstance(1.0, np.floating)
Out[15]: False

to include more types, e.g. python floats, you can use a tuple in isinstance:

In [16]: isinstance(1.0, (np.floating, float))
Out[16]: True

Problem

Other than using a set of or statements `isinstance( x, np.float64 )` or `isinstance( x, np.float32 )` or `isinstance( np.float16 )` Is there a cleaner way to check of a variable is a floating type?

Original source

Related problems