Reputation: 1
Please follow the code lines:
A = np.array([0 for i in range(10)])
A[:] = 0.1/2
A
Out[36]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
A[:] = float(0.2/2)
A[:]
Out[38]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
I can't understand, why numpy is giving such a strange behavior?
I am using numpy 1.13.3
Upvotes: 0
Views: 423
Reputation: 15206
Because when you define your array it is of dtype=int
. Define it as float
and it will work as you expect.
example:
a = np.arange(10) # dtype = int
a[:] = 0.1 # converts to int so to 0
print a
a = np.arange(10.) # dtype = float
# or equivalently a = np.arange(10, dtype=float)
a[:] = 0.1 # works as you expect
print a
Upvotes: 2