numpy array, difference between a /= x vs. a = a / x
Warning:
In place operations will perform the calculation using the precision decided by the data type of the two operands, but will silently downcast the result (if necessary) so it can fit back into the array. Therefore, for mixed precision calculations,
A {op}= B
can be different thanA = A {op} B
. For example, supposea = ones((3,3))
. Then,a += 3j
is different thana = a + 3j
: while they both perform the same computation,a += 3
casts the result to fit back ina
, whereasa = a + 3j
re-binds the namea
to the result.
Since your array was an array of integers, when using the in-place operations, the result will be downcasted to integers again.
If you change your array so it stores floats originally, then the results (which are floats) can be stored in the original array, and your code will work fine:
>>> a = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])>>> a /= float(2**16 - 1)>>> aarray([[ 1.52590219e-05, 3.05180438e-05, 4.57770657e-05], [ 6.10360876e-05, 7.62951095e-05, 9.15541314e-05]])