Tobias Hermann
Tobias Hermann

Reputation: 10936

serializing float32 values in python and deserializing them in C++

I have a bunch of 32-bit floating point values in a Python script, need to store them to disc and load them in a C++ tool. Currently they are written in human-readable format. However the loss of precision is too big for my (numeric) application.

How do I best store (and load) them without loss?

Upvotes: 1

Views: 775

Answers (2)

Netch
Netch

Reputation: 4562

Currently they are written in the default human-readable format.

That's the reason. If require e.g. "%.9e" or "%.9g", it will print float32 values with precision enough to restore (provided there is no errors in conversions).

Also, for Python3, repr() gives a shortest decimal representation for a value that is converted back to the same binary value; but this is not true for Python2.

Answering a question from comments,

If I understand correctly, there are values that have a finite floating point representation in base 2, but the base 10 representation is infinite,

No, it is finite anyway, but just longer. For example (Python3)

>>> math.pi
3.141592653589793
>>> '%.60g' % math.pi
'3.141592653589793115997963468544185161590576171875'

but, all digits after the final significant one do not matter (remember Python float is double precision):

>>> float('3.1415926535897931')
3.141592653589793
>>> float('3.1415926535897932')
3.141592653589793
>>> float('3.1415926535897933')
3.141592653589793
>>> float('3.1415926535897933') - float('3.1415926535897931')
0.0

Finally, hexadecimal is more reliable if available in your code. I expect your Python and C++ versions fresh enough to support %a (stdio), float.hex (Python).

Upvotes: 2

Alexis Pierru
Alexis Pierru

Reputation: 342

You can use float.hex in python to get the hexadecimal representation of your number, then read it using the std::hexfloat stream manipulator in C++.

Upvotes: 2

Related Questions