Find if number float64

I have a number

eg.

a = 1.22373 type(a) is float 

How wise I want to find if the number

 float64 

or not.

How will I find using python or numpy?

+7
python floating-point numpy
source share
2 answers

Use isinstance :

 >>> f = numpy.float64(1.4) >>> isinstance(f, numpy.float64) True >>> isinstance(f, float) True 

numpy.float64 inherits from python's native float type. This is because it is both a float and float64 (@Bakuriu thanks for the tip). But if you check the python instance variable float for type float64, you will get False as a result:

 >>> f = 1.4 >>> isinstance(f, numpy.float64) False >>> isinstance(f, float) True 
+13
source share

If you are only comparing numpy types, you might be better off defining your comparison by the number that identifies each dtype, which is what C base code does. On my system, 12 is the number for np.float64 :

 >>> np.dtype(np.float64).num 12 >>> np.float64(5.6).dtype.num 12 >>> np.array([5.6]).dtype.num 12 

To use it also with values ​​other than numpy, you can skip your way through it with something like:

 def isdtype(a, dt=np.float64): try: return a.dtype.num == np.dtype(dt).num except AttributeError: return False 
+1
source share

All Articles