Greetings,

I spent a couple hours today tracking down a bug in one of my programs. I
was getting different answers depending on whether I passed in a numpy
array or a single number. Ultimately, I tracked it down to something I
would consider a bug, but I'm not sure if others do. The case comes from
taking a numpy integer array and adding a float to it.  When doing var =
np.array(ints) + float, var is cast to an array of floats, which is what I
would expect. However, if I do np.array(ints) += float, the result is an
array of integers. I can understand why this happens -- you are shoving the
sum back into an integer array -- but without thinking through that I would
expect the behavior of the two additions to be equal...or at least be
consistent with what occurs with numbers, instead of arrays.  Here's a
trivial example demonstrating this


import numpy as np
a = np.arange(10)
print a.dtype
b = a + 0.5
print b.dtype
a += 0.5
print a.dtype

>> int64
>> float64
>> int64
>> <type 'int'>
>> <type 'float'>
>> <type 'float'>


An implication of this arrises from a simple function that "does math". The
function returns different values depending on whether a number or array
was passed in.


def add_n_multiply(var):
    var += 0.5
    var *= 10
    return var

aaa = np.arange(5)
print aaa
print add_n_multiply(aaa.copy())
print [add_n_multiply(x) for x in aaa.copy()]


>> [0 1 2 3 4]
>> [ 0 10 20 30 40]
>> [5.0, 15.0, 25.0, 35.0, 45.0]




Am I alone in thinking this is a bug? Or is this the behavior that others
would have expected?



Cheers,
Patrick
---
Patrick Marsh
Ph.D. Candidate / Liaison to the HWT
School of Meteorology / University of Oklahoma
Cooperative Institute for Mesoscale Meteorological Studies
National Severe Storms Laboratory
http://www.patricktmarsh.com
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to