0

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

1
  • Check your dtype of A after init. Commented Dec 7, 2017 at 5:35

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.