I have a Numpy array that I obtained by multiplying a Numpy array with a float.
a = np.array([3, 5, 7, 9]) * 0.1
The resulting numbers are precise without any rounding.
>>> a
array([0.3 0.5 0.7 0.9])
However, if I turn my array into a list with a.tolist(), there are entries like 0.30000000000000004 in my list instead of 0.3.
>>> a.tolist()
[0.30000000000000004, 0.5, 0.7000000000000001, 0.9]
My question is, how can I avoid this, and if someone knows, out of pure interest, why is this happening. Thank you very much for your help.
This issue comes with floating points in general (do
3 * .1in your Python console). In your case, you can simply divide by 10 rather than multiple by .1.a = np.array([3, 5, 7, 9]) / 10See also: Floating Point Error Mitigation Decimal Module