I’m running into an issue with python automatically rounding very small numbers (smaller than 1e-8) when subtracting an array from an single float. Take this example:
import numpy as np
float(1) - np.array([1e-10, 1e-5])
Any thoughts on how to force python not to round? This is forcing me to divide by zero in some cases, and becoming a problem. The same problem arises when subtracting from an numpy array.
Mostly, it’s just the
reprof numpy arrays that’s fooling you.Consider your example above:
This yields:
So the first element isn’t actually zero, it’s just the pretty-printing of numpy arrays that’s showing it that way.
This can be controlled by
numpy.set_printoptions.Of course, numpy is fundementally using limited precision floats. The whole point of numpy is to be a memory-efficient container for arrays of similar data, so there’s no equivalent of the
decimalclass in numpy.However, 64-bit floats have a decent range of precision. You won’t hit too many problems with 1e-10 and 1e-5. If you need, there’s also a
numpy.float128dtype, but operations will be much slower than using native floats.