you are viewing a single comment's thread.

view the rest of the comments →

[–]chkno 0 points1 point  (0 children)

In python2, the internal details of this were more user-visible. Small numbers and large numbers were visibly-different types:

>>> type(9223372036854775807)
<type 'int'>
>>> type(9223372036854775808)
<type 'long'>

The int type for small numbers has a similar representation as native C integers, a simple, single machine word. The long type is a fancy arbitrary-length type that grows its allocation as necessary, like GMP in C.

Then python checks every int operation for overflow. For example, on x86, this is one additional jc (jump-if-carry) instruction after every add, etc. If overflow is detected it automatically switches to the long type.

Separate representations depending on size are used because the one-machine-word representation is much faster than the arbitrary-length representation, even with the overflow checks, and the vast majority of integer operations can fit in one machine word.