I’m using SWIG to wrap my C++ code into Python. But the conversion of floating point numbers is strange.
For example, if I have the function below (written in C++)
float foo() {
float x=62.02;
return x;
}
and executes it (after wrapping with SWIG) in Python
>>> import mymodule
>>> mymodule.foo()
62.02000045776367
>>>
it returns 62.02000045776367 instead of 62.02.
Is there a way to tell SWIG how to make the right conversion?
Other than the lossless
float->doubleconversion, there is no conversion going on.62.02cannot be represented exactly as a Cfloat. The moment you dofloat x=62.02, the variable will contain the value that you mention.I highly recommend reading What Every Computer Scientist Should Know About Floating-Point Arithmetic.