[Numpy-discussion] find_common_type broken?

Ralf Gommers ralf.gommers at googlemail.com
Sun Jul 12 11:54:07 EDT 2009


On Sun, Jul 12, 2009 at 6:54 AM, Citi, Luca <lciti at essex.ac.uk> wrote:

> 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.


That is what I thought at first, but then what is the difference between
array_types and scalar_types? Function signature is:
*find_common_type(array_types, scalar_types)*

np.float64, np.int32 etc are scalar types so I thought they should go in the
second argument. Maybe something else is supposed to go into the array_types
list, but I have no clue what if not actual array types.

>
>
> 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
>

yes, this makes sense.


>
> 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


that is confusing. so I guess the dtype(t) conversion should not happen?

>
>
> 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.


ah, missed that part. thanks, Luca.

Ralf


>
>
> Best,
> Luca
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090712/367f2bb9/attachment.html>


More information about the NumPy-Discussion mailing list