Continuing from Difference between Python float and numpy float32:
import numpy as np
a = 58682.7578125
print(type(a), a)
float_32 = np.float32(a)
print(type(float_32), float_32)
print(float_32 == a)
Prints:
<class 'float'> 58682.7578125
<class 'numpy.float32'> 58682.8
True
I fully understand that comparing floats for equality is not a good idea but still shouldn't this be False (we're talking about differences in the first decimal digit, not in 0.000000001) ? Is it system dependent ? Is this behavior somewhere documented ?
EDIT: Well it's the third decimal:
print(repr(float_32), repr(a))
# 58682.758 58682.7578125
but can I trust repr
? How are those stored internally in the final end ?
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
and:
print(repr(float(float_32)))
prints
58682.7578125
An interesting insight is given by @MarkDickinson here, apparently repr
should be faithful (then he says it's not faithful for np.float32
).
So let me reiterate my question as follows:
- How can I get at the exact internal representation of
float_32
anda
in the example ? If these are the same, then problem solved if not, - What are the exact rules for up/downcasting in a comparison between python's
float
andnp.float32
? I 'd guess that it upcasts float_32 to float although @WillemVanOnsem suggests in the comments it's the other way round
My python version:
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32