Python's standard float type is a C double: http://docs.python.org/2/library/stdtypes.html#typesnumeric

NumPy's standard numpy.float is the same, and is also the same as numpy.float64.

Answer from John Zwinck on Stack Overflow
Top answer
1 of 2
63

Python's standard float type is a C double: http://docs.python.org/2/library/stdtypes.html#typesnumeric

NumPy's standard numpy.float is the same, and is also the same as numpy.float64.

2 of 2
3

Data type-wise numpy floats and built-in Python floats are the same, however boolean operations on numpy floats return np.bool_ objects, which always return False for val is True. Example below:

In [1]: import numpy as np
   ...: an_np_float = np.float32(0.3)
   ...: a_normal_float = 0.3
   ...: print(a_normal_float, an_np_float)
   ...: print(type(a_normal_float), type(an_np_float))

0.3 0.3
<class 'float'> <class 'numpy.float32'>

Numpy floats can arise from scalar output of array operations. If you weren't checking the data type, it is easy to confuse numpy floats for native floats.

In [2]: criterion_fn = lambda x: x <= 0.5
   ...: criterion_fn(a_normal_float), criterion_fn(an_np_float)

Out[2]: (True, True)

Even boolean operations look correct. However the result of the numpy float isn't a native boolean datatype, and thus can't be truthy.


In [3]: criterion_fn(a_normal_float) is True, criterion_fn(an_np_float) is True
Out[3]: (True, False)

In [4]: type(criterion_fn(a_normal_float)), type(criterion_fn(an_np_float))
Out[4]: (bool, numpy.bool_)

According to this github thread, criterion_fn(an_np_float) == True will evaluate properly, but that goes against the PEP8 style guide.

Instead, extract the native float from the result of numpy operations. You can do an_np_float.item() to do it explicitly (ref: this SO post) or simply pass values through float().

🌐
NumPy
numpy.org › doc › stable › user › basics.types.html
Data types — NumPy v2.4 Manual
NumPy provides numpy.iinfo and numpy.finfo to verify the minimum or maximum values of NumPy integer and floating point values respectively · >>> np.iinfo(int) # Bounds of the default integer on this system. iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64) >>> np.iinfo(np.int32) # Bounds of a 32-bit integer iinfo(min=-2147483648, max=2147483647, dtype=int32) >>> np.iinfo(np.int64) # Bounds of a 64-bit integer iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)
🌐
NumPy
numpy.org › doc › stable › reference › arrays.scalars.html
Scalars — NumPy v2.4 Manual
numpy.float32: 32-bit-precision floating-point number type: sign bit, 8 bits exponent, 23 bits mantissa.
🌐
Snyk
snyk.io › advisor › numpy › functions › numpy.float32
How to use the numpy.float32 function in numpy | Snyk
def get_dummy_encoder_output(encoder_out_shape=(100, 80, 5)): """ This only provides an example to generate dummy encoder output """ (T, B, D) = encoder_out_shape encoder_out = {} encoder_out["encoder_out"] = torch.from_numpy( np.random.randn(*encoder_out_shape).astype(np.float32) ) seq_lengths = torch.from_numpy(np.random.randint(low=1, high=T, size=B)) # some dummy mask encoder_out["encoder_padding_mask"] = torch.arange(T).view(1, T).expand( B, -1 ) >= seq_lengths.view(B, 1).expand(-1, T) encoder_out["encoder_padding_mask"].t_() # encoer_padding_mask is (T, B) tensor, with (t, b)-th element indicate # whether encoder_out[t, b] is valid (=0) or not (=1) return encoder_out ·
🌐
Quora
quora.com › What-is-np-float32-and-np-float64-in-numpy-in-simple-terms
What is np.float32 and np.float64 in numpy in simple terms? - Quora
Answer (1 of 2): np.float32 - It means that each value in the numpy array would be a float of size 32 bits. np.float64- It means that each value in the numpy array would be a float of size 64.
🌐
NumPy
numpy.org › doc › 1.22 › user › basics.types.html
Data types — NumPy v1.22 Manual
NumPy provides numpy.iinfo and numpy.finfo to verify the minimum or maximum values of NumPy integer and floating point values respectively · >>> np.iinfo(int) # Bounds of the default integer on this system. iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64) >>> np.iinfo(np.int32) # Bounds of a 32-bit integer iinfo(min=-2147483648, max=2147483647, dtype=int32) >>> np.iinfo(np.int64) # Bounds of a 64-bit integer iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)
🌐
NumPy
numpy.org › doc › 2.1 › reference › arrays.scalars.html
Scalars — NumPy v2.1 Manual
numpy.float32: 32-bit-precision floating-point number type: sign bit, 8 bits exponent, 23 bits mantissa.
🌐
KooR.fr
koor.fr › Python › API › scientist › numpy › float32 › Index.wp
KooR.fr - classe float32 - module numpy - Description de quelques librairies Python
Single-precision floating-point ... on this platform (win32 AMD64): `numpy.float32`: 32-bit-precision floating-point number type: sign bit, 8 bits exponent, 23 bits mantissa....
🌐
Python⇒Speed
pythonspeed.com › articles › float64-float32-precision
The problem with float32: you only get 16 million values
February 1, 2023 - Libraries like NumPy and Pandas ... or 64-bit floats) to numpy.float32 (“single-precision” or 32-bit floats) cuts memory usage in half....
Find elsewhere
🌐
GitHub
github.com › numpy › numpy › issues › 6860
How to set float32 as default · Issue #6860 · numpy/numpy
December 19, 2015 - I use cuBLAS + numpy, cuBLAS run very fast on float32, 10times faster than CPU. However, I need to set dtype=float32 everytime by hand, it's tedious. random.rand() even doesn't support to create float32 array. Is there anyway to set the ...
Author   fayeshine
🌐
w3resource
w3resource.com › numpy › data-types.php
NumPy: Data types - w3resource
>>> import numpy as np >>> np.array([3, 5, 7], dtype='f') array([3., 5., 7.], dtype=float32)
🌐
GitHub
github.com › numpy › numpy › issues › 25836
BUG: Weird conversion behavior from np.float32 to Python float · Issue #25836 · numpy/numpy
February 16, 2024 - Describe the issue: I found out that converting np.float32 to a Python float via .item() gives a weird result. While I understand NumPy retains the float32 internal representation of the value, I feel that for such a simple value the fol...
Author   fandreuz
🌐
Edureka Community
edureka.co › home › community › categories › python › difference between python float and numpy float32
Difference between Python float and numpy float32 | Edureka Community
March 4, 2019 - What is the difference between the built in float and numpy.float32? For example, here is a code: ... > 58682.8 What is the built in float format?
🌐
GitHub
github.com › numpy › numpy › issues › 22618
Question: numpy.float32 produces wrong conversion of certain integer values · Issue #22618 · numpy/numpy
November 18, 2022 - numpy.float32 function does not convert certain large int values <2,147,483,648 to the same float32 equivalent. This issue does not occur with conversion to numpy.float64 or python native float.
Author   SilentAndromeda
🌐
JAX Documentation
docs.jax.dev › en › latest › _autosummary › jax.numpy.float32.html
jax.numpy.float32 — JAX documentation
A JAX scalar constructor of type float32. While NumPy defines scalar types for each data type, JAX represents scalars as zero-dimensional arrays.
🌐
GitHub
github.com › numpy › numpy › issues › 14150
How to convert np.float32 to Python float easily? · Issue #14150 · numpy/numpy
July 29, 2019 - Hi, I try to convert np.float32 to a Python float in my project, and I find it's not eaisly. I have to convert it to str and then convert to float. ... import numpy as np x = np.float32(1.9) x.tolist() # 1.899999976158142 x.item() # 1.899999976158142 x.view() # 1.9 str(x) # '1.9' float(str(x)) # 1.9
Author   ringsaturn
🌐
NumPy
numpy.org › doc › 2.3 › reference › arrays.scalars.html
Scalars — NumPy v2.3 Manual
numpy.float32: 32-bit-precision floating-point number type: sign bit, 8 bits exponent, 23 bits mantissa.