![](https://secure.gravatar.com/avatar/05fc6835e821894b1ff75c46391eed7b.jpg?s=120&d=mm&r=g)
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 ? Nicolas
![](https://secure.gravatar.com/avatar/b4f6d4f8b501cb05fd054944a166a121.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/09cd8fb3ecae7d982c5bb0e1081dcf69.jpg?s=120&d=mm&r=g)
The tuple gets cast to an ndarray; which invokes a different codepath than the scalar addition. Somehow, numpy has gotten more aggressive at upcasting to float64 as of 1.8, but I havnt been able to discover the logic behind it either. On Mon, Mar 3, 2014 at 10:06 PM, Nicolas Rougier <Nicolas.Rougier@inria.fr>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 ?
Nicolas _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/b4f6d4f8b501cb05fd054944a166a121.jpg?s=120&d=mm&r=g)
On Mon, 2014-03-03 at 22:26 +0100, Eelco Hoogendoorn wrote:
The tuple gets cast to an ndarray; which invokes a different codepath than the scalar addition.
Somehow, numpy has gotten more aggressive at upcasting to float64 as of 1.8, but I havnt been able to discover the logic behind it either
There were changes in the casting logic in 1.7, I think. I can't really remember changes after that, so if there is a change, we might want to check it out. (Or I am just missing something completly :)) - Sebastian
On Mon, Mar 3, 2014 at 10:06 PM, Nicolas Rougier <Nicolas.Rougier@inria.fr> 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 ?
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
![](https://secure.gravatar.com/avatar/09939f25b639512a537ce2c90f77f958.jpg?s=120&d=mm&r=g)
IIRC, this is dependent on whether you are using 32bit versus 64bit numpy. All regular integer numbers can fit in 32 bits (is that right?), but the 1.1 is treated as a float32 if on a 32 bit NumPy or as float64 if on a 64 bit NumPy. That's my stab at it. Ben Root On Mon, Mar 3, 2014 at 4:06 PM, Nicolas Rougier <Nicolas.Rougier@inria.fr>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 ?
Nicolas _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/05fc6835e821894b1ff75c46391eed7b.jpg?s=120&d=mm&r=g)
I never noticed this kind of cast before (1.8.0), it's just a bit surprising. It was convenient to write translations (for a bunch of points) such as: Z = np.ones((n,2),dtype=np.float32) + (300,300) but I can live with Z += 300,300 Nicolas On 03 Mar 2014, at 23:02, Benjamin Root <ben.root@ou.edu> wrote:
IIRC, this is dependent on whether you are using 32bit versus 64bit numpy. All regular integer numbers can fit in 32 bits (is that right?), but the 1.1 is treated as a float32 if on a 32 bit NumPy or as float64 if on a 64 bit NumPy.
That's my stab at it.
Ben Root
On Mon, Mar 3, 2014 at 4:06 PM, Nicolas Rougier <Nicolas.Rougier@inria.fr> 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 ?
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
![](https://secure.gravatar.com/avatar/09939f25b639512a537ce2c90f77f958.jpg?s=120&d=mm&r=g)
Oops, I just now noticed that it was (1,1) and not (1.1). I really need to set a better font that makes the period and the comma more different... Ben Root On Mon, Mar 3, 2014 at 5:12 PM, Nicolas Rougier <Nicolas.Rougier@inria.fr>wrote:
I never noticed this kind of cast before (1.8.0), it's just a bit surprising.
It was convenient to write translations (for a bunch of points) such as:
Z = np.ones((n,2),dtype=np.float32) + (300,300)
but I can live with Z += 300,300
Nicolas
On 03 Mar 2014, at 23:02, Benjamin Root <ben.root@ou.edu> wrote:
IIRC, this is dependent on whether you are using 32bit versus 64bit numpy. All regular integer numbers can fit in 32 bits (is that right?), but the 1.1 is treated as a float32 if on a 32 bit NumPy or as float64 if on a 64 bit NumPy.
That's my stab at it.
Ben Root
On Mon, Mar 3, 2014 at 4:06 PM, Nicolas Rougier < Nicolas.Rougier@inria.fr> 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 ?
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
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/b4f6d4f8b501cb05fd054944a166a121.jpg?s=120&d=mm&r=g)
On Mon, 2014-03-03 at 23:12 +0100, Nicolas Rougier wrote:
I never noticed this kind of cast before (1.8.0), it's just a bit surprising.
It was convenient to write translations (for a bunch of points) such as:
Z = np.ones((n,2),dtype=np.float32) + (300,300)
but I can live with Z += 300,300
Just to note. That actually does the temporary cast anyway doing the calculation in double precision and then casting the result. If you want to make sure it stays in single precision you will need to make that an array with float32 dtype. - Sebastian
Nicolas
On 03 Mar 2014, at 23:02, Benjamin Root <ben.root@ou.edu> wrote:
IIRC, this is dependent on whether you are using 32bit versus 64bit numpy. All regular integer numbers can fit in 32 bits (is that right?), but the 1.1 is treated as a float32 if on a 32 bit NumPy or as float64 if on a 64 bit NumPy.
That's my stab at it.
Ben Root
On Mon, Mar 3, 2014 at 4:06 PM, Nicolas Rougier <Nicolas.Rougier@inria.fr> 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 ?
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
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (4)
-
Benjamin Root
-
Eelco Hoogendoorn
-
Nicolas Rougier
-
Sebastian Berg