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
This means Python integers may expand to accommodate any integer and will not overflow. 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)
🌐
Snyk
snyk.io › advisor › numpy › functions › numpy.float32
How to use the numpy.float32 function in numpy | Snyk
ex_g = OCLArray.empty((Nz, Ny, Nx), np.complex64) ey_g = OCLArray.empty((Nz, Ny, Nx), np.complex64) ez_g = OCLArray.empty((Nz, Ny, Nx), np.complex64) kxs_g = OCLArray.from_array(kxs.astype(np.float32)) kys_g = OCLArray.from_array(kys.astype(np.float32)) t = time.time() p.run_kernel("debye_wolf_lattice", (Nx, Ny, Nz), None, ex_g.data, ey_g.data, ez_g.data, u_g.data, np.float32(1.), np.float32(0.), # np.float32(-dx*(Nx-1)//2.),np.float32(dx*(Nx-1)//2.), # np.float32(-dy*(Ny-1)//2.),np.float32(dy*(Ny-1)//2.), # np.float32(-dz*(Nz-1)//2.),np.float32(dz*(Nz-1)//2.), np.float32(dx*(-Nx//2)), np.float32(dx*(Nx//2-1)), np.float32(dy*(-Ny//2)), np.float32(dy*(Ny//2-1)), np.float32(dz*(-Nz//2)), np.float32(dz*(Nz//2-1)), np.float32(1.*lam/n0), np.float32(alpha1), np.float32(alpha2), kxs_g.data, kys_g.data, np.int32(len(kxs)), np.float32(sigma) ) u = u_g.get()
🌐
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
🌐
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.
🌐
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?
🌐
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 › 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
🌐
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
🌐
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. ... Double-precision floating-point number type, compatible with Python float and C double.
🌐
NumPy
numpy.org › doc › 1.22 › user › basics.types.html
Data types — NumPy v1.22 Manual
Data-types can be used as functions to convert python numbers to array scalars (see the array scalar section for an explanation), python sequences of numbers to arrays of that type, or as arguments to the dtype keyword that many numpy functions or methods accept. Some examples: >>> import numpy as np >>> x = np.float32(1.0) >>> x 1.0 >>> y = np.int_([1,2,4]) >>> y array([1, 2, 4]) >>> z = np.arange(3, dtype=np.uint8) >>> z array([0, 1, 2], dtype=uint8)
🌐
GitHub
github.com › numpy › numpy › issues › 22618
Question: numpy.float32 produces wrong conversion of certain integer values · Issue #22618 · numpy/numpy
November 18, 2022 - import numpy as np # Produces equivalent values i=16777342 print(int(i)) print(float(i)) print(np.float32(i)) print(np.float64(i)) print() # Does not produce equivalent values i=16777343 print(int(i)) print(float(i)) print(np.float32(i)) print(np.float64(i)) print() # Produces equivalent values i=16777344 print(int(i)) print(float(i)) print(np.float32(i)) print(np.float64(i)) print() # Does not produce equivalent values i=16777345 print(int(i)) print(float(i)) print(np.float32(i)) print(np.float64(i)) print() # Does not produce equivalent values i=181567487 print(int(i)) print(float(i)) print(np.float32(i)) print(np.float64(i))
Author   SilentAndromeda
🌐
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. ... Double-precision floating-point number type, compatible with Python float and C double.
🌐
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) To convert the type of an array, use the .astype() method (preferred) or the type itself as a function. For example: >> import numpy as np >>> c = np.arange(5, dtype=np.uint8) >>> (c.astype(float)) array([ 0., 1., 2., 3., 4.]) >>> (np.int8(c)) array([0, 1, 2, 3, 4], dtype=int8)
🌐
Sling Academy
slingacademy.com › article › explaining-numpy-float32-type-4-examples
Explaining numpy.float32 type (4 examples) - Sling Academy
In essence, numpy.float32 is a dtype that represents a 32-bit single precision floating point number as specified by the IEEE 754 standard.
🌐
IncludeHelp
includehelp.com › python › how-to-convert-numpy-array-type-and-values-from-float64-to-float32.aspx
Python - How to convert NumPy array type and values from Float64 to Float32?
# Import numpy import numpy as np # Creating a numpy array arr = np.ones(4,dtype="float64") # Display original array print("Original Array:\n",arr,"\n") # Display type of original array print("type of Original Array:\n",arr.dtype,"\n") # Converting array into float32 arr = np.float32(arr) # Display type of modified array print("type of modified array:\n",arr.dtype,"\n")