Question

I am embedding python code in my c++ program. The use of PyFloat_AsDouble is causing loss of precision. It keeps only up to 6 precision digits. My program is very sensitive to precision. Is there a known fix for this? Here is the relevant C++ code:

_ret = PyObject_CallObject(pFunc, pArgs);
vector<double> retVals;
for(size_t i=0; i<PyList_Size(_ret); i++){
    retVals[i] = PyFloat_AsDouble(PyList_GetItem(_ret, i));
}

retVals[i] has precision of only 6, while the value returned by the python code is a float that can have a higher precision. How to get full precision?

Était-ce utile?

La solution

Assuming that the Python object contains floating point values stored to double precision, then your code works as you expect.

Most likely you are simply mis-diagnosing a problem that does not exist. My guess is that you are looking at the values in the debugger which only displays the values to a limited precision. Or you are printing them out to a limited precision.

Autres conseils

print type(PyList_GetItem(_ret, i))

My bet is it will show float.

Edit: in the python code, not in the C++ code.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top