On Mon, 2014-03-03 at 22:06 +0100, Nicolas Rougier wrote: > Hi all, > > I'm using numpy 1.8.0 (osx 10.9, python 2.7.6) and I can't understand dtype > promotion in the following case: > > >>> Z = np.zeros((2,2),dtype=np.float32) + 1 > >>> print Z.dtype > float32 > > >>> Z = np.zeros((2,2),dtype=np.float32) + (1,1) > >>> print Z.dtype > float64 > > > Is this the expected behavior ? > What it the difference between the two lines ? >
It is intended I guess, scalars (including 0-d arrays such `np.array(1)`) behave differently form normal arrays. Their type is not as important and in many cases with integers even the value gets important. I did not think through this exact case, and there are some funnier corners which have been discussed a lot, but basically you have to expect different casting when scalars are involved (don't trust the scalar dtype to win). (Of course in this context, you always have to imagin an `np.asarray` call) - Sebastian > > > Nicolas > _______________________________________________ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion > _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion