[Numpy-discussion] inconsistent dtype promotion in dimensionless arrays

Matthew Koichi Grimes mkg at cs.nyu.edu
Thu Apr 5 18:00:24 EDT 2007


I've noticed two dtype promotion behaviors that are surprising to me. 
I'm hoping to get people's input as to whether I should file bug tickets 
on them.

First weirdness:
When you do an operation between a float32 Numpy array and a python or 
Numpy float64, the result is a float32 array, not a float64 array:

 >>> import numpy as N
 >>> vec32 = N.ones(2, N.float32)
 >>> vec32
array([ 1.,  1.], dtype=float32)
 >>> result = vec32 - 1.0
 >>> result.dtype
dtype('float32')
 >>> result = vec32 - N.float64(1.0)
 >>> result.dtype
dtype('float32')


This is of course not the case if the float64 is replaced with a 
1-dimensional numpy array:
 >>> result = vec32 - N.array([1.0], N.float64)
 >>> result.dtype
dtype('float64')


Second weirdness:
Type promotion doesn't happen if you replace the 1-d, 1-element array 
with a 0-d array:

 >>> result = vec32 - N.array(1.0, N.float64)
 >>> result.dtype
dtype('float32')


The second weirdness in particular strikes me as inconsistent when 
compared to operations between vec32 and the 1-d array.

I didn't see these listed on the trac site as bugs, but then again a 
bunch of tickets seem to have been removed/fixed recently. Sorry if 
these have been mentioned, filed, and fixed already. Otherwise, should I 
file a ticket on the above behaviors?

-- Matt



More information about the NumPy-Discussion mailing list