Search code examples
pythonfloating-pointuint8t

what does numpy if the value in an array exceeds int8


Consider the following piece of code:

test = np.ones((200,200))
test = test*20000
test_int8 = test.astype(np.int8)

When I implement this, I get that test_int8 is filled with 32, why? and in general, if there's an array whose values exceed the maximum value that dtype I am converting into accepts, how does python handle that?


Solution

  • 20000 in 32-bit binary is 0000 0000 0000 0000 0100 1110 0010 0000.

    As you can see, the last eight bits are 0010 0000, which is 32.

    If you cast an int with a higher number of bits to a lower number, the top bits are just cut off (you could say it almost is... A stack overflow? (not quite by the way)).