[Numpy-discussion] Int casting different across platforms

Charles R Harris charlesr.harris@gmail....
Sat Nov 5 13:24:20 CDT 2011

On Fri, Nov 4, 2011 at 5:21 PM, Matthew Brett <matthew.brett@gmail.com>wrote:

> Hi,
> I noticed this:
> (Intel Mac):
> In [2]: np.int32(np.float32(2**31))
> Out[2]: -2147483648
> (PPC):
> In [3]: np.int32(np.float32(2**31))
> Out[3]: 2147483647
> I assume what is happening is that the casting is handing off to the c
> library, and that behavior of the c library differs on these
> platforms?  Should we expect or hope that this behavior would be the
> same across platforms?

Heh. I think the conversion is basically undefined because 2**31 won't fit
in int32. The Intel example just takes the bottom 32 bits of 2**31
expressed as a binary integer, the PPC throws up its hands and returns the
maximum value supported by int32. Numpy supports casts from unsigned to
signed 32 bit numbers by using the same bits, as does C, and that would
comport with the Intel example. It would probably be useful to have a Numpy
convention for this so that the behavior was consistent across platforms.
Maybe for float types we should raise an error if the value is out of

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20111105/eb3822f5/attachment.html 

More information about the NumPy-Discussion mailing list