[Numpy-discussion] Comparing the precision of dtypes?

Ralf Gommers ralf.gommers@googlemail....
Wed Jul 22 10:16:31 CDT 2009


2009/7/22 Hans Meine <meine@informatik.uni-hamburg.de>

> Hi,
>
> Ullrich Köthe found an interesting way to compute a promoted dtype, given
> two
> arrays a and b:
>
>  type = min(float32, a.dtype.type, b.dtype.type)
>

Are you looking for the type to cast to? In that case I guess you meant
max() not min().

>
> How hackish is this?  Is this likely to break on other platforms/numpy
> versions?  Is there a better API for type promotion?


That could give unexpected results. Useful functions are find_common_type,
mintypecode, common_type and can_cast; see
http://docs.scipy.org/numpy/docs/numpy-docs/reference/routines.dtype.rst

Examples where min/max probably does not do what you want, and
find_common_type does:

In [49]: max(float, float32)
Out[49]: <type 'numpy.float32'>
In [50]: find_common_type([], [float, float32])
Out[50]: dtype('float64')

In [40]: max(int8, uint8)
Out[40]: <type 'numpy.uint8'>
In [41]: find_common_type([int8, uint8], [])
Out[41]: dtype('int16')

Note that the second example only works with latest trunk (Travis fixed this
last week) and the updated docs have not yet appeared in trunk (but see link
above).

Cheers,
Ralf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090722/0d4611fa/attachment.html 


More information about the NumPy-Discussion mailing list