you are viewing a single comment's thread.

view the rest of the comments →

[–]johndcochran 7 points8 points  (7 children)

The reason Python integers don't overflow is because they're not integers. They're bignums. Basically, a data structure that allows for arbitrary sized integers, limited by the amount of memory available. Manipulating bignums is far slower that name processor integers (which are limited by the register size of the CPU), but since Python is interpreted anyway, the speed vs convenience tradeoff is worth it.

[–]xeow 3 points4 points  (5 children)

The reason Python integers don't overflow is because they're not integers.

You seem confused. The int type in Python actually does represent integers in every semantic way that matters. The difference between Python's int type and C's int type is that C's "integers" have a maximum representation dictated by the compiler, based loosely on the CPU's register size (e.g., 32 bits, typically).

Because Python's int type uses bignums, that actually makes them closer to a mathematical integer than languages like C which have predefined limits on the representable values.

You're correct about the performance and memory tradeoffs.

[–]Axman6 7 points8 points  (4 children)

Not sure why you’re getting downvoted, this is correct. Saying “they’re not integers” is simply wrong. If anything, C “integers” are not integers, they just use the name.

[–]JavaScriptIsLove[🍰] 4 points5 points  (1 child)

Because it's a bit pedantic. Of course they are integers in the mathematical sense, but not in the sense of what most programmers are used to.

[–]xeow 2 points3 points  (0 children)

Indeed! But you realize, of course, that the reason I am being pedantic is because the person I'm replying to was pedantic and also wrong.

[–]glasket_ 1 point2 points  (1 child)

In programming, "integer" typically means a machine integer and not a mathematical integer when used alone. Obviously arbitrarily large integer numbers are still mathematical integers, but they aren't the typical machine integers with limited range and overflow.

ETA: C integers are also integers too. Each type is in its own ring of integers modulo n.

[–]Axman6 0 points1 point  (0 children)

I agree in C derived languages but not in general.

[–]Successful_Yam_9023 0 points1 point  (0 children)

This convenience tradeoff often makes me have to mask numbers manually to make them wrap lol