[Numpy-discussion] __iadd__(ndarray<int>, ndarray<float>)

Stéfan van der Walt stefan at sun.ac.za
Mon Mar 24 20:26:09 EDT 2008


Hi Andreas

On Mon, Mar 24, 2008 at 7:28 PM, Andreas Klöckner
<lists at informa.tiker.net> wrote:
>  I just got tripped up by this behavior in Numpy 1.0.4:
>
>  >>> u = numpy.array([1,3])
>  >>> v = numpy.array([0.2,0.1])
>  >>> u+=v
>  >>> u
>  array([1, 3])
>  >>>
>
>  I think this is highly undesirable and should be fixed, or at least warned
>  about. Opinions?

I know the result is surprising, but it follows logically.  You have
created two integers in memory, and now you add 0.2 and 0.1 to both --
not enough to flip them over to the next value.  The equivalent in C
is roughly:

#include <stdio.h>

int
main()
{
  int i;
  int x[2] = {1,3};
  x[0] += 0.2;
  x[1] += 0.1;

  printf("[%d %d]\n", x[0], x[1]);
}

Which results in the same answer.

Regards
Stéfan



More information about the NumPy-Discussion mailing list