Reputation: 23821
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.
Upvotes: 5
Views: 3778
Reputation: 284850
Mostly, it's just the repr
of numpy arrays that's fooling you.
Consider your example above:
import numpy as np
x = float(1) - np.array([1e-10, 1e-5])
print x
print x[0]
print x[0] == 1.0
This yields:
[ 1. 0.99999 ]
0.99999999999
False
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 decimal
class 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.float128
dtype, but operations will be much slower than using native floats.
Upvotes: 6
Reputation: 6777
I guess all that depends on the handling of very small float numbers, by Python and by the underlying C libraries, that at a certain point tend to loose precision.
If you need that level of precision, imho you should rely on something different, such as fractionary numbers etc.
I don't know whether there already is something to handle that, but if you could manage to represent that numbers in a different way (such as 1/10000000000
and 1/100000
) and then calculate the floating point result only at the end of all calculations, you should avoid all these problems.
(Of course, you need some class that automagically handles fractionary calculations to avoid having to reimplement formulas, etc.)
Upvotes: 1