Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

smallest negative int in Python [duplicate]

Tags:

python

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

like image 576
Lin Ma Avatar asked Oct 21 '15 01:10

Lin Ma


People also ask

What is the smallest negative integer?

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.

What is the most negative number in Python?

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 .

How do you set a minimum value in Python?

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.

Can a Python int be negative?

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.


2 Answers

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
like image 79
Sharon Dwilif K Avatar answered Oct 14 '22 09:10

Sharon Dwilif K


-sys.maxint - 1

Negative limits are always one off from positive limits, due to how integers are represented in binary.

like image 42
Amadan Avatar answered Oct 14 '22 11:10

Amadan