Reputation: 2399
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?
Upvotes: 0
Views: 1208
Reputation: 2161
print type(PyList_GetItem(_ret, i))
My bet is it will show float.
Edit: in the python code, not in the C++ code.
Upvotes: -1
Reputation: 613282
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.
Upvotes: 2