Hi Patrick:
I think it is the behavior I have come to expect. The only "gotcha" here
might be the difference between "var = var + 0.5" and "var += 0.5"
For example:
>>> import numpy as np
>>> x = np.arange(5); x += 0.5; x
array([0, 1, 2, 3, 4])
>>> x = np.arange(5); x = x + 0.5; x
array([ 0.5, 1.5, 2.5, 3.5, 4.5])
The first line is definitely what I expect. The second, the automatic
casting from int64 -> double, is documented and generally desirable.
It's hard to avoid these casting issues without making code unnecessarily
complex or allowing only one data type (e.g., as MATLAB does).
If you worry about standardizing behavior you can always use `var =
np.array(var, dtype=np.double, copy=True)` or similar at the start of your
function.
-Brad
On Wed, Jan 16, 2013 at 4:16 PM, Patrick Marsh <patrickmarshwx@gmail.com>wrote:
> 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>>-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20130116/cc56463e/attachment.html