I am using Python 2.7.x Stack. And using sys.maxint for positive max integer (if it is wrong usage, please feel free to correct me). Sometimes I also need to initialize an integer to be smallest negative integer value. Wondering what is the most elegant/correct way in Python 2.7.x Stack?
Thanks in advance, Lin
There are an infinite number of negative integers as they approach negative infinity. Therefore, there is no smallest negative integer. Hence the smallest negative integer does not exist.
int and long in Python2 The min value (the largest negative value) is -sys. maxint-1 . sys. maxint is at least 2**31-1 , and on a 64-bit environment, it is 2**63-1 .
Python 2. You can calculate the minimum value with -sys. maxint - 1 as shown here. Python seamlessly switches from plain to long integers once you exceed this value.
An integer, commonly abbreviated to int, is a whole number (positive, negative, or zero). So 7 , 0 , -11 , 2 , and 5 are integers. 3.14159 , 0.0001 , 11.11111 , and even 2.0 are not integers, they are floats in Python.
For comparisons (like to find out smallest value, etc) I normally use float('-inf')
, this is not an int, but is smaller than any int, and can be used for commparison with ints. Example -
>>> float('-inf')
-inf
>>> -sys.maxint -1 > float('-inf')
True
-sys.maxint - 1
Negative limits are always one off from positive limits, due to how integers are represented in binary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With