[Numpy-discussion] Overloading numpy's ufuncs for better typecoercion?
Hans Meine
meine@informatik.uni-hamburg...
Wed Jul 22 08:34:52 CDT 2009
On Wednesday 22 July 2009 15:14:32 Citi, Luca wrote:
> In [2]: a + a
> Out[2]: array([144], dtype=uint8)
>
> Please do not "fix" this, that IS the correct output.
No, I did not mean to fix this. (Although it should be noted that in C/C++,
the result of uint8+uint8 is int.)
> If instead, you refer to
> In [3]: numpy.add(a, a, numpy.empty((1, ), dtype = numpy.uint32))
> Out[3]: array([144], dtype=uint32)
> in this case I agree with you, the expected result should be 400.
Yes, that's what I meant.
> The inputs could be casted to the output type before performing the
> operation. I do not think performing the operations with the output dtype
> would break something.
> Even in borderline cases like the following:
> >>> b = numpy.array([400], numpy.int16)
> >>> c = numpy.array([200], numpy.int16)
> >>> numpy.subtract(b.astype(numpy.int8), c.astype(numpy.int8),
> >>> numpy.empty((1, ), dtype = numpy.int8))
Indeed - I thought we had to take more care, but will this also work with
int<->float conversions? No:
In [22]: a, b = numpy.array([[8.6,4.9]]).T
In [23]: numpy.subtract(a, b, numpy.empty((1, ), dtype = numpy.uint8))
Out[23]: array([3], dtype=uint8)
In [24]: numpy.subtract(a.astype(numpy.uint8), b.astype(numpy.uint8))
Out[24]: array([4], dtype=uint8)
However, I admit that this is a contrived example. ;-)
Greetings,
Hans
More information about the NumPy-Discussion
mailing list