I need to precisely save a lot of floating numbers to a file in C++ program, and then read them in Python.
You known that the precision of floating numbers may be lost when we convert them into/out from string.
I'm wondering that "Is there an equivalent of union in Python?" If so, I can do it as following:
C++ program:
union Convert_t {
// this occupys 8 bytes
double asDouble;
// this occupies 8 bytes too, so we just only treat a double as a int64
int64_t asIntegr;
};
// when preparing data
Convert_t data;
data.asDouble = real_data;
out_csv_stream << data.asIntegr << ',';
When read data in Python program:
import typing
import pandas as pd
# now the type of contents should be int64
pd.read_csv(csv_path)
# I don't known how to writing correctly follow lines
class PyConvert : typing.Union
asDouble : numpy.float64
asIntegr : numpy.int64
I think my question is different to that question and answer. That answer doesn't show how to convert data into a floating type, and it was using cdll.LoadLibrary() that was not required for me.