Reputation: 10204
There seems to be a subtle difference between numpy.float
and numpy.float64
.
>>> import numpy as np
>>> isinstance(2.0, np.float)
True
>>> isinstance(2.0, np.float64)
False
Can someone clarify this? Thanks
Upvotes: 15
Views: 17768
Reputation: 32125
np.float
is an alias for python float
type.
np.float32
and np.float64
are numpy specific 32 and 64-bit float types.
float?
Init signature: float(self, /, *args, **kwargs)
Docstring:
float(x) -> floating point number
Convert a string or number to a floating point number, if possible.
Type: type
np.float?
Init signature: np.float(self, /, *args, **kwargs)
Docstring:
float(x) -> floating point number
Convert a string or number to a floating point number, if possible.
Type: type
np.float32?
Init signature: np.float32(self, /, *args, **kwargs)
Docstring: 32-bit floating-point number. Character code 'f'. C float compatible.
File: c:\python\lib\site-packages\numpy\__init__.py
Type: type
np.float64?
Init signature: np.float64(self, /, *args, **kwargs)
Docstring: 64-bit floating-point number. Character code 'd'. Python float compatible.
File: c:\python\lib\site-packages\numpy\__init__.py
Type: type
Thus, when you do isinstance(2.0, np.float)
, it is equivalent to isinstance(2.0, float)
as 2.0 is a plain python built-in float type... and not the numpy type.
isinstance(np.float64(2.0), np.float64)
would obviously be True
.
Upvotes: 17
Reputation: 27
This would seem to be the difference between a 32-bit floating point number and a 64-bit floating point number (in C, a float vs. a double), and 2.0 ends up being a 32-bit floating point number.
Upvotes: -6