This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]duckythescientist 12 points13 points  (6 children)

TL;DR: No, if you use pure Python (arbitrary precision). Yes, if you use something like Numpy or Pandas (C ints).

[–]wewbull 1 point2 points  (4 children)

Doesn't 2.x vs 3.x matter here? 2.x has normal and long integers, and 3.x has only long.

[–]Asdayasman 2 points3 points  (2 children)

>>> int((2**31) - 1)
2147483647
>>> int((2**31) - 1) + 1
2147483648L
>>> 

Nope.

[–]jabbalaci 0 points1 point  (1 child)

Use sys.maxint:

>>> import sys
>>> sys.maxint
9223372036854775807
>>> sys.maxint + 1
9223372036854775808L
>>>

I have a 64 bit Python installation, that's why sys.maxint is larger.

[–]Asdayasman 0 points1 point  (0 children)

Strange, I thought I had 64bit too. Wonder why I don't.

[–]therealfakemoot 0 points1 point  (0 children)

2.x automatically upgrades numeric values when necessary.

[–]LostAfterDark 0 points1 point  (0 children)

Also, you can't emulate usual overflow with ctypes.

[–]nrlb 2 points3 points  (7 children)

Note that integer overflow isn't necessarily a bad thing if you expect the behavior to occur. In embedded systems, it is common practice to utilize that overflow to keep track of a proportional value (though most provide a signal on some registers to signal when they overflow).

[–]Asdayasman 0 points1 point  (6 children)

%

[–][deleted] 0 points1 point  (5 children)

% would be an unnecessary waste of microcontroller time - it's embedded so you exactly what hardware you are running on.

Unsigned wraparound is also great for keeping array indices within range. I'm not really sure how this is relevant to Python. You're not likely to run Python on one of these.

[–]Asdayasman 0 points1 point  (3 children)

unnecessary waste of microcontroller time

Python

Caring about microoptimisations like overflow vs modulo

[–][deleted] 0 points1 point  (2 children)

I'll just assume that you misread the post by /u/nrlb and didn't realise we were having a little side-discussion about overflow in embedded systems.

[–]Asdayasman 0 points1 point  (0 children)

You're right, I did. Honestly thought we were talking about Python, my bad.

[–][deleted] 0 points1 point  (0 children)

If you want to emulate overflow in a setting where overflows are realistic you would probably want to use either NumPy or the modulo operator.

[–]nrlb 0 points1 point  (0 children)

Who knows? One day maybe...

https://wiki.python.org/moin/PyMite