123

How can I assign the maximum value for a long integer to a variable, similar, for example, to C++'s LONG_MAX.

Chris
  • 44,602
  • 16
  • 137
  • 156
Sreevisakh
  • 1,896
  • 2
  • 16
  • 23
  • 8
    I am not sure Python integers are limited at all. At the moment you cross sys.maxint it changes internal representation from int to long, which has unlimited presicion. – tchap Mar 25 '12 at 13:48
  • I asked because i need to find a min value among a group of values one by one So first i need to store a big value to a variable so that i can compare it with others – Sreevisakh Mar 25 '12 at 13:52
  • 2
    To your comment - i recommned to use built-in function min. – Jiri Mar 25 '12 at 13:57
  • for eg: 'for(j=0;j – Sreevisakh Mar 25 '12 at 13:59
  • you can utilize list comprehensions: min(calculate_total(i,j) for i in range(n) for j in range(n)) – Jiri Mar 25 '12 at 14:11
  • 5
    You don't need the maximum value if you are just trying to find the minimum, even if you are programming in C++ or any other language. If you are just going to loop through all the elements anyway, simply use the first element as your starting value. (But better to use the `min` function, if you really want to program in Python!) – John Y Mar 25 '12 at 15:24
  • 3
    @Sreevisakh: for the "big value", use infinity, `float("inf")`. Or better, use the built-in `min` function. – Fred Foo Mar 25 '12 at 15:36

8 Answers8

147

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
keyser
  • 18,829
  • 16
  • 59
  • 101
  • I tried the same code but it still show type as int in python 3 on windows 64 bit machine. Any valued reason for it. – ManojP Jun 29 '15 at 06:34
  • 1
    @ManojP It's because there's no longer a limit to the size of integers in Python 3. See the first block quote. I'll edit to clarify that the example is Python 2 – keyser Jul 20 '15 at 23:49
44

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').

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Taymon
  • 24,950
  • 9
  • 62
  • 84
  • 1
    so it return a very large value does it? – Sreevisakh Mar 25 '12 at 14:03
  • 4
    It returns floating-point infinity, which is greater than any finite number. – Taymon Mar 25 '12 at 14:24
  • 4
    I have to say, this answer is definitely the closest to correct in terms of responding to the OP's title question. That is, "how do you get a Python sentinel value that will be larger than all your input (or at least not smaller than the largest value)?". So I've upvoted this answer, but I think it is better if the OP learns to think in Python instead. – John Y Mar 25 '12 at 15:16
  • Agreed. That said, I have been in a situation where this was the only way to do what I needed. – Taymon Mar 25 '12 at 15:39
23

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.

John Y
  • 14,123
  • 2
  • 48
  • 72
8

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.

rkhayrov
  • 10,040
  • 2
  • 35
  • 40
7

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.

Abhijit
  • 62,056
  • 18
  • 131
  • 204
1

You can use: max value of float is

float('inf')

for negative

float('-inf')
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Ramazan
  • 37
  • 1
  • 2
    The title is *"Maximum value for long integer"*, not max value of float. So, without an explanation how this *answers* the question, I regard this NAA (not an answer). – Sнаđошƒаӽ Nov 19 '16 at 09:20
0

On CPython 3.11 on a 64-bit system, the maximum integer is

2 ** 276701161105643274210 - 1

You will need 35 exabytes of memory to store it, which will cost about $70 billion at today's (May 2023) prices of $65 per 32GB on NewEgg. In practical terms, Python's maximum integer is limited by how much memory you have in your computer.


CPython 3.11 stores integers in this C struct:

struct PyLongObject {
    Py_ssize_t ob_refcnt;
    PyTypeObject* ob_type;
    Py_ssize_t ob_size;
    uint32_t ob_digit[1];
};

So Python integers are implemented as an array of 32-bit integers (uint32_t ob_digit[1]), of which only 30 bits are used to store the absolute value of the integer and a 64-bit signed two's complement integer stores the length of that array (Py_ssize_t ob_size), as well as the sign of the Python integer, so a negative integer has a negative "size". So we have

2 ** ((2 ** 63 - 1) * 30) - 1
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
-1

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.

kxr
  • 4,841
  • 1
  • 49
  • 32