How can I assign the maximum value for a long integer to a variable, similar, for example, to C++'s LONG_MAX
.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with 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
That's not how to think in Python. A better translation to Python (but still not the best) would be
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
That is, you don't write a loop at all. The built-in
min()
function gets the minimum of the whole collection.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.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: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, butsys.maxsize
can often be used instead. From the changelog:and, for anyone interested in the difference (Python 2.x):
and for completeness, here's the Python 3 version:
floats:
There's
float("inf")
andfloat("-inf")
. These can be compared to other numeric types: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 isint
and conversion is handled behind the scenes.Python
long
can be arbitrarily large. If you need a value that's greater than any other value, you can usefloat('inf')
, since Python has no trouble comparing numeric values of different types. Similarly, for a value lesser than any other value, you can usefloat('-inf')
.In python3, you can send the float value into the int function the get that number 1.7976931348623157e+308 in integer representation.