What is the correct / common way to print and convert numpy.float64 to string? I noticed that using printor str()will lose some accuracy. However, it reprmaintains full accuracy. For instance:
>>> import numpy
>>> print numpy.float64('6374.345407799015')
6374.3454078
>>> print repr(numpy.float64('6374.345407799015'))
6374.3454077990154
I assume that only the call to print turns into a call to the str()float64 object. So, __str__()for numpy.float64 implemented with something like, '%s' % (float(self))or somehow drops float64 with built-in Python float()? I tried to quickly look around the numpy source for this, but did not immediately understand what was happening.
I always thought that I repr()should return valid Python code that can be used eval()to recreate the object. Is this a common convention? Fortunately, in this case numpy does not follow this convention because it repr()only returns the raw number as a string, and not something like "numpy.float64('6374.345407799015')".
So, all this confuses me. What is the correct way to convert numpy.float64 to a string and / or print it, ensuring that you always have the same full precision?
source
share