[Numpy-discussion] Numpy-discussion

Stefan van der Walt stefanv at berkeley.edu
Tue Jan 29 03:29:18 EST 2019


On Tue, 29 Jan 2019 10:15:00 +0200, Matti Picus wrote:
> 2000*2000*2000 overflows a 32 bit signed integer, so it wraps around to a
> negative value.

I was curious about what happens, bit-wise, in this case.  We are dealing
with a signed integer, so I presume of the 33 bits in 2000**3, only 32
are kept:

In [60]: len("{0:b}".format(1999**3))

Out [60]: 33

In [61]: "{0:b}".format(1999**3)

Out [61]: '111011100000111110100110001101111'

In [62]: "{0:b}".format(1999**3)[-32:]

Out [62]: '11011100000111110100110001101111'

The first of the 32 bits indicates sign, so converting back to integer
would give:

1 * np.iinfo(np.int32).min + int('1011100000111110100110001101111', 2) == -601928593


Best regards,
Stéfan


More information about the NumPy-Discussion mailing list