I have a number
e.g.
a = 1.22373
type(a) is float
Like wise I want to find if a number is
float64
or not.
How I will find using Python or NumPy?
Float64 is just a type alias to Double .
float32 is a 32 bit number - float64 uses 64 bits. That means that float64's take up twice as much memory - and doing operations on them may be a lot slower in some machine architectures. However, float64's can represent numbers much more accurately than 32 bit floats. They also allow much larger numbers to be stored.
Range. A variable of type float64 can store decimal numbers ranging from 2.2E-308 to 1.7E+308. This applies to negative and positive numbers.
Use isinstance:
>>> f = numpy.float64(1.4)
>>> isinstance(f, numpy.float64)
True
>>> isinstance(f, float)
True
numpy.float64 is inherited from python native float type. That because it is both float and float64 (@Bakuriu thx for pointing out). But if you will check python float instance variable for float64 type you will get False
in result:
>>> f = 1.4
>>> isinstance(f, numpy.float64)
False
>>> isinstance(f, float)
True
I find this is the most readable method for checking Numpy number types
import numpy as np
npNum = np.array([2.0])
if npNum.dtype == np.float64:
print('This array is a Float64')
# or if checking for multiple number types:
if npNum.dtype in [
np.float32, np.float64,
np.int8, np.uint8,
np.int16, np.uint16,
np.int32, np.uint32,
np.int64, np.uint64
]:
print('This array is either a float64, float32 or an integer')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With