When testing my library, Construct, I found out that tests fail when numbers are built then parsed back to a float. Should floats not be represented exactly as in-memory floats?
In [14]: d = struct.Struct("<f")
In [15]: d.unpack(d.pack(1.23))
Out[15]: (1.2300000190734863,)
Floating point is inherently imprecise, but you are packing a double-precision float (
binary64
) into a single-precision (binary32
) space there. See Basic and interchange formats in the Wikipedia article on IEEE floating point formats; the Pythonfloat
format uses double precision (see the standard types docs; Floating point numbers are usually implemented using double in C).Use
d
to use double precision:From the Format characters section: