Maximum value for long integer
PythonPython Problem Overview
How can I assign the maximum value for a long integer to a variable, similar, for example, to C++'s LONG_MAX
.
Python Solutions
Solution 1 - Python
Long integers:
There is no explicitly defined limit. The amount of available address space forms a practical limit.
(Taken from this site). See the docs on Numeric Types where you'll see that Long integers have unlimited precision
. In Python 2, Integers will automatically switch to longs when they grow beyond their limit:
>>> import sys
>>> type(sys.maxsize)
<type 'int'>
>>> type(sys.maxsize+1)
<type 'long'>
for integers we have
maxint and maxsize:
The maximum value of an int can be found in Python 2.x with sys.maxint
. It was removed in Python 3, but sys.maxsize
can often be used instead. From the changelog:
> The sys.maxint constant was removed, since there is no longer a limit > to the value of integers. However, sys.maxsize can be used as an > integer larger than any practical list or string index. It conforms to > the implementation’s “natural” integer size and is typically the same > as sys.maxint in previous releases on the same platform (assuming the > same build options).
and, for anyone interested in the difference (Python 2.x):
> sys.maxint The largest positive integer supported by Python’s regular > integer type. This is at least 2**31-1. The largest negative integer > is -maxint-1 — the asymmetry results from the use of 2’s complement > binary arithmetic.
> sys.maxsize The largest positive integer supported by the platform’s > Py_ssize_t type, and thus the maximum size lists, strings, dicts, and > many other containers can have.
and for completeness, here's the Python 3 version:
> sys.maxsize > An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2^31 - 1 on a 32-bit platform and > 2^63 - 1 on a 64-bit platform.
floats:
There's float("inf")
and float("-inf")
. These can be compared to other numeric types:
>>> import sys
>>> float("inf") > sys.maxsize
True
Solution 2 - Python
Python long
can be arbitrarily large. If you need a value that's greater than any other value, you can use float('inf')
, since Python has no trouble comparing numeric values of different types. Similarly, for a value lesser than any other value, you can use float('-inf')
.
Solution 3 - Python
Direct answer to title question:
Integers are unlimited in size and have no maximum value in Python.
Answer which addresses stated underlying use case:
According to your comment of what you're trying to do, you are currently thinking something along the lines of
minval = MAXINT;
for (i = 1; i < num_elems; i++)
if a[i] < a[i-1]
minval = a[i];
That's not how to think in Python. A better translation to Python (but still not the best) would be
minval = a[0] # Just use the first value
for i in range(1, len(a)):
minval = min(a[i], a[i - 1])
Note that the above doesn't use MAXINT at all. That part of the solution applies to any programming language: You don't need to know the highest possible value just to find the smallest value in a collection.
But anyway, what you really do in Python is just
minval = min(a)
That is, you don't write a loop at all. The built-in min()
function gets the minimum of the whole collection.
Solution 4 - Python
long
type in Python 2.x uses arbitrary precision arithmetic and has no such thing as maximum possible value. It is limited by the available memory. Python 3.x has no special type for values that cannot be represented by the native machine integer — everything is int
and conversion is handled behind the scenes.
Solution 5 - Python
Unlike C/C++ Long in Python have unlimited precision. Refer the section Numeric Types in python for more information.To determine the max value of integer you can just refer sys.maxint
. You can get more details from the documentation of sys.
Solution 6 - Python
You can use: max value of float is
float('inf')
for negative
float('-inf')
Solution 7 - Python
A) For a cheap comparison / arithmetics dummy use math.inf
. Or math.nan
, which compares FALSE
in any direction (including nan == nan
) except identity check (is
) and renders any arithmetics (like nan - nan
) nan
. Or a reasonably high real integer number according to your use case (e.g. sys.maxsize
). For a bitmask dummy (e.g. in mybits & bitmask
) use -1
.
B) To get the platform primitive maximum signed long int (or long long):
>>> 256 ** sys.int_info.sizeof_digit // 2 - 1 # Python’s internal primitive
2147483647
>>> 256 ** ctypes.sizeof(ctypes.c_long) // 2 - 1 # CPython
2147483647
>>> 256 ** ctypes.sizeof(ctypes.c_longlong) // 2 - 1 # CPython
9223372036854775807
>>> 2**63 - 1 # Java / JPython primitive long
9223372036854775807
C) The maximum Python integer could be estimated by a long running loop teasing for a memory overflow (try 256**int(8e9)
- can be stopped by KeyboardInterrupt
). But it cannot not be used reasonably, because its representation already consumes all the memory and its much greater than sys.float_info.max
.
Solution 8 - Python
In python3, you can send the float value into the int function the get that number 1.7976931348623157e+308 in integer representation.
import sys
int(sys.float_info.max)