Consider the following numpy vector of number:
a = np.array([.1, .2, .2, .2, .2, .1])
Obviously, the sum of these numbers gives 1. However, when computing
b = np.sum(a)
I get
print (b)
0.9999999999999999
Could you anyone explain why and how to solve this approximation issue?
This is due to the machine floating point accuracy. It is explained here in detail: https://docs.python.org/3/tutorial/floatingpoint.html
You can use the following to fix it: