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().

🌐
Python⇒Speed
pythonspeed.com › articles › float64-float32-precision
The problem with float32: you only get 16 million values
February 1, 2023 - In practice a little trickery in the encoding is used to give 24 bits of range. That means that for a given level of precision, 32-bit floats only give you 224 = 16777216 positive values, and the same number of negative values, with 0 at the center.
Discussions

How to convert np.float32 to Python float easily?
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. Here is the code: Reproducing code example: i... More on github.com
🌐 github.com
1
July 29, 2019
BUG: Weird conversion behavior from np.float32 to Python float
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 f... More on github.com
🌐 github.com
3
February 16, 2024
numpy - How to force python float operation on float32 rather than float64? - Stack Overflow
I want to do some math operations (+, -, *, /) on float32 rather than on float64 type. I need do these operations on number or numpy.array, and also some numpy math functions, such as sqrt mean. Ho... More on stackoverflow.com
🌐 stackoverflow.com
python - Numpy's float32 and float comparisons - Stack Overflow
EDIT2: people insist that printing float_32 with more precision will give me its representation. However as I already commented according to nympy's docs: the % formatting operator requires its arguments to be converted to standard python types More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 › 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)
🌐
Python documentation
docs.python.org › 3 › tutorial › floatingpoint.html
15. Floating-Point Arithmetic: Issues and Limitations — Python 3.14.3 documentation
Stop at any finite number of bits, and you get an approximation. On most machines today, floats are approximated using a binary fraction with the numerator using the first 53 bits starting with the most significant bit and with the denominator ...
🌐
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....
Find elsewhere
🌐
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. Here is the code: Reproducing code example: import numpy as np x = np.float32(1.9) x.toli...
Author   ringsaturn
🌐
University of Texas
cs.utexas.edu › ~mitra › csSpring2017 › cs303 › lectures › math.html
Doing Math in Python
You can explicitly convert numbers of one type to another with built-in functions that Python provides: x = 123 y = float (x) # y = 123.0 z = 34.89 w = int (z) # w = 34 Note that when you convert to int, the function truncates instead of rounding.
🌐
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
Top answer
1 of 5
13

The numbers compare equal because 58682.7578125 can be exactly represented in both 32 and 64 bit floating point. Let's take a close look at the binary representation:

32 bit:  01000111011001010011101011000010
sign    :  0
exponent:  10001110
fraction:  11001010011101011000010

64 bit:  0100000011101100101001110101100001000000000000000000000000000000
sign    :  0
exponent:  10000001110
fraction:  1100101001110101100001000000000000000000000000000000

They have the same sign, the same exponent, and the same fraction - the extra bits in the 64 bit representation are filled with zeros.

No matter which way they are cast, they will compare equal. If you try a different number such as 58682.7578124 you will see that the representations differ at the binary level; 32 bit looses more precision and they won't compare equal.

(It's also easy to see in the binary representation that a float32 can be upcast to a float64 without any loss of information. That is what numpy is supposed to do before comparing both.)

import numpy as np

a = 58682.7578125
f32 = np.float32(a)
f64 = np.float64(a)

u32 = np.array(a, dtype=np.float32).view(dtype=np.uint32)
u64 = np.array(a, dtype=np.float64).view(dtype=np.uint64)

b32 = bin(u32)[2:]
b32 = '0' * (32-len(b32)) + b32  # add leading 0s
print('32 bit: ', b32)
print('sign    : ', b32[0])
print('exponent: ', b32[1:9])
print('fraction: ', b32[9:])
print()

b64 = bin(u64)[2:]
b64 = '0' * (64-len(b64)) + b64  # add leading 0s
print('64 bit: ', b64)
print('sign    : ', b64[0])
print('exponent: ', b64[1:12])
print('fraction: ', b64[12:])
2 of 5
2

The same value is stored internally, only it doesn't show all digits with a print

Try:

 print "%0.8f" % float_32

See related Printing numpy.float64 with full precision

🌐
Readthedocs
bitstring.readthedocs.io › en › stable › exotic_floats.html
Exotic Floating Point Formats — bitstring 4.3 documentation
Python floats are typically 64 bits long, but 32 and 16 bit sizes are also supported through the struct module. These are the well-known IEEE formats.
🌐
Google Groups
groups.google.com › g › jep-project › c › tiKRsN-7lMM
Problem getting Python float32 as Java Float instead of Double
October 25, 2018 - I know you were hoping to avoid pulling Java classes into your Python but I see no other way to accomplish exactly what you are requesting. If you are concerned about performance, then I would recommend float[], NdArray, or DirectNdArray instead of List<Float>, since those types support bulk conversion while maintaining 32 bits without creating the boxed objects.
🌐
GitHub
github.com › milvus-io › milvus › discussions › 20870
Double-precision to single-precision float conversions in pymilvus package · milvus-io/milvus · Discussion #20870
Milvus server stores float vector as float32 array. All the ANN search computation is based on float32 type. Although the python client use double-precision array to store vector data, the value in this array actually is float32, the digits after the no.6 position are meaningless.
Author   milvus-io
🌐
Stack Overflow
stackoverflow.com › questions › 43335367 › can-i-use-python-float-with-precision-of-float32
serialization - Can I use python float with precision of (float32)? - Stack Overflow
Python floats are 64-bit (double in C/Java). However you can serialize/deserialize these to 32-bit floats (float in C/Java) using the struct module with the "f" format: