Using the following code:
import numpy as np
k = .2
A = np.arange(.01, k, .01, dtype=np.float64)
# values
print(A)
print([A[i] for i in range(len(A))])
# comparison
print(A == [A[i] for i in range(len(A))])
# check for value
print(.06 in A)
The corresponding output is:
[0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14
0.15 0.16 0.17 0.18 0.19]
[0.01, 0.02, 0.03, 0.04, 0.05, 0.060000000000000005, 0.06999999999999999, 0.08, 0.09, 0.09999999999999999, 0.11, 0.12, 0.13, 0.14, 0.15000000000000002, 0.16, 0.17, 0.18000000000000002, 0.19]
[ True True True True True True True True True True True True
True True True True True True True]
False
What sort of magic is happening here ? How am I supposed to iterate over the array keeping precision ?
EDIT: as precised in Is floating point math broken ? this lost of precision is a known issue, language agnostic. I would know what is the python way to handle this ? Using an epsilon seems to be a little hacky for me