Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum and Minimum values for ints

Tags:

python

integer

I am looking for minimum and maximum values for integers in python. For eg., in Java, we have Integer.MIN_VALUE and Integer.MAX_VALUE. Is there something like this in python?

like image 790
bdhar Avatar asked Sep 30 '11 01:09

bdhar


People also ask

What is the minimum and maximum value for an int data type?

Minimum value is –2147483648. Maximum value is 2147483647. Minimum value is –9223372036854775808.

What is the maximum value for int?

The int type in Java can be used to represent any whole number from -2147483648 to 2147483647.

What is integer Max_value in Java?

Integer.MAX_VALUE is a constant in the Integer class of java.lang package that specifies that stores the maximum possible value for any integer variable in Java. The actual value of this is. 2^31-1 = 2147483647.


2 Answers

If you just need a number that's bigger than all others, you can use

float('inf') 

in similar fashion, a number smaller than all others:

float('-inf') 

This works in both python 2 and 3.

like image 40
Melle Avatar answered Oct 02 '22 10:10

Melle


Python 3

In Python 3, this question doesn't apply. The plain int type is unbounded.

However, you might actually be looking for information about the current interpreter's word size, which will be the same as the machine's word size in most cases. That information is still available in Python 3 as sys.maxsize, which is the maximum value representable by a signed word. Equivalently, it's the size of the largest possible list or in-memory sequence.

Generally, the maximum value representable by an unsigned word will be sys.maxsize * 2 + 1, and the number of bits in a word will be math.log2(sys.maxsize * 2 + 2). See this answer for more information.

Python 2

In Python 2, the maximum value for plain int values is available as sys.maxint:

>>> sys.maxint 9223372036854775807 

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. So most of the time, you won't need to know it.

like image 171
senderle Avatar answered Oct 02 '22 10:10

senderle