[Numpy-discussion] find_common_type broken?

Citi, Luca lciti@essex.ac...
Sun Jul 12 05:54:47 CDT 2009


Hi,
I am not very confident with types but I will try to give you
my opinion.

As for the first part of the question

> >>> np.find_common_type([np.ndarray, np.ma.MaskedArray, np.recarray], [])
> dtype('object')

I think that the first argument of np.find_common_type should be
the type of an _element_ of the array, not the type of the array.
In your case you are asking np.find_common_type the common type
between an array of arrays, an array of masked arrays, and
an array of record arrays. Therefore the best thing np can do
is to find object as common type.

Correctly:

>>> np.find_common_type([np.float64, np.int32], [])
dtype('float64')



As for the second part of the question np.find_common_type
internally uses np.dtype(t) for each type in input.

While the comparison between types work as expected:

>>> np.complex128 > np.complex64 > np.float64 > np.float32 > np.int64 > np.int32
True

the comparison between dtype(t) gives different results:

>>> np.dtype(np.float64) > np.dtype(np.int64)
True
>>> np.dtype(np.float32) > np.dtype(np.int64)
False
>>> np.dtype(np.float32) > np.dtype(np.int32)
False
>>> np.dtype(np.float32) > np.dtype(np.int16)
True

At first I thought the comparison was made based on the
number of bits in the mantissa or the highest integer
N for which N-1 was still representable.
But then I could not explain the first result.

What is surprising is that

>>> np.dtype(np.float32) > np.dtype(np.int32)
False
>>> np.dtype(np.float32) < np.dtype(np.int32)
False
>>> np.dtype(np.float32) == np.dtype(np.int32)
False

therefore the max() function in np.find_common_type
cannot tell which to return, and returns the first.

In fact:

>>> np.find_common_type([], [np.int64, np.float32])
dtype('int64')

but

>>> np.find_common_type([], [np.float32, np.int64])
dtype('float32')

which is unexpected.

Best,
Luca


More information about the NumPy-Discussion mailing list