0

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?

Akhil
  • 2,269
  • 6
  • 32
  • 39

2 Answers2

2

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Indeed it turns out I wasn't printing with enough precision. I was also misled by this post, which supported my point: https://github.com/coolfluid/coolfluid3/issues/253 – Akhil Apr 07 '14 at 22:25
-1

print type(PyList_GetItem(_ret, i))

My bet is it will show float.

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

Fred Mitchell
  • 2,145
  • 2
  • 21
  • 29