Coder Perfect

Integers’ maximum and minimum values

Problem

In Python, I’m seeking for the lowest and maximum values for integers. In Java, we have Integer.MIN VALUE and Integer.MAX VALUE, for example. Is there anything similar in Python?

Asked by bdhar

Solution #1

This question isn’t relevant in Python 3. The unbounded int type is the most basic type.

You may, however, be seeking for information about the current interpreter’s word size, which will, in most cases, be the same as the machine’s word size. In Python 3, the information is still available as sys.maxsize, which is the maximum value that a signed word can represent. It’s the size of the largest feasible list or in-memory sequence, in other words.

The maximum value that an unsigned word can represent is usually sys.maxsize * 2 + 1, and the amount of bits in a word is usually math.log2(sys.maxsize * 2 + 2). For further information, see this answer.

sys.maxint: sys.maxint: sys.maxint: sys.maxint: sys.maxint: sys.maxint

>>> sys.maxint
9223372036854775807

As seen here, -sys.maxint – 1 can be used to calculate the minimum value.

Once you reach this value, Python effortlessly changes from ordinary to long integers. As a result, you won’t need to know it very often.

Answered by senderle

Solution #2

You can use this if you merely need a number that is larger than all others.

float('inf')

Similarly, a number that is lower than all others:

float('-inf')

This works in both python 2 and 3.

Answered by Melle

Solution #3

From Python 3.0 onwards, the sys.maxint constant has been deprecated; instead, use sys.maxsize.

Refer : https://docs.python.org/3/whatsnew/3.0.html#integers

Answered by Akash Rana

Solution #4

Once you pass the value sys.maxint, which is either 231 – 1 or 263 – 1 depending on your platform, integers in Python will automatically transition from a fixed-size int representation to a variable width long representation. Take note of the L that is added here:

>>> 9223372036854775807
9223372036854775807
>>> 9223372036854775808
9223372036854775808L

According to the Python manual:

Python makes a concerted effort to make its integers appear to be mathematical integers that are unbounded. It can easily calculate a googol, for example:

>>> 10**100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L

Answered by John Kugelman

Solution #5

It is for Python 3.

import sys
maxSize = sys.maxsize
minSize = -sys.maxsize - 1

Answered by netskink

Post is based on https://stackoverflow.com/questions/7604966/maximum-and-minimum-values-for-ints