What is sys.maxint in Python 3?
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).
http://docs.python.org/3.1/whatsnew/3.0.html#integers
Maximum and Minimum values for ints
Python 3
In Python 3, this question doesn't apply. The plain int
type is unbound.
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.
In Python, what is `sys.maxsize`?
Python can handle arbitrarily large integers in computation. Any integer too big to fit in 64 bits (or whatever the underlying hardware limit is) is handled in software. For that reason, Python 3 doesn't have a sys.maxint
constant.
The value sys.maxsize
, on the other hand, reports the platform's pointer size, and that limits the size of Python's data structures such as strings and lists.
Is sys.maxint safe to use?
sys.maxint
is safe to use as the maximum value of an int
. If you want 2**31-1
regardless of what size int
is, use 2**31-1
(and be aware that this will give you a long
if it equals sys.maxint
, since 2**31
will overflow, so call int
on it if you need it as an int
for some reason).
Max integer value of python3
In Python3, value of an integer is not restricted by the number of bits and can expand to the limit of the available memory
print(100**100)
output:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Why is sys.maxint (sys.maxint - 100 + 0.01) in Python?
On a system with 64-bit longs, sys.maxint
is:
// decimal hexadecimal
9223372036854775807 0x7fffffffffffffff
So sys.maxint - 100
is:
9223372036854775707 0x7fffffffffffff9b
Adding 0.01
forces this value to be rounded to double-precision floating point before the addition. The two closest values that are representable in double-precision are:
9223372036854774784 0x7ffffffffffffc00
9223372036854775808 0x8000000000000000
Because sys.maxint - 100
is closer to the second (larger) value, it rounds up. Adding 0.01
gives:
9223372036854775808.01 0x8000000000000000.028f5c28f5c...
which is not representable in double-precision, so it is rounded again, to:
9223372036854775808 0x8000000000000000
So the value of sys.maxint - 100 + 0.01
is actually larger than the value of sys.maxint
. However, in many modern languages, comparison between an integer and a float forces the integer value to be converted to floating point before the comparison takes place; if this were the case in python, sys.maxint
would be rounded up to the same value, and they would compare equal. It seems that this is not the case in Python. I'm not familiar with the details of python numerics, but this is an interesting curiosity of the language.
Is there something sys.minint in python similar to sys.maxint?
The old documentation specifies that it's -sys.maxint - 1
, so could just use that, or make a constant of your own with that value. Do note that in the current version maxint
has been removed and probably shouldn't be used in new code.
Related Topics
How to Implement Band-Pass Butterworth Filter with Scipy.Signal.Butter
How to Get Element-Wise Matrix Multiplication (Hadamard Product) in Numpy
Managing Dynamic Plotting in Matplotlib Animation Module
Finding Elements Not in a List
Typeerror: 'List' Object Is Not Callable While Trying to Access a List
Python: Use MySQLdb to Import a MySQL Table as a Dictionary
Python Split a String with at Least 2 Whitespaces
How to Upload a File to Google Cloud Storage on Python 3
Create a Main Loop with Tkinter
No Multiline Lambda in Python: Why Not
How to Avoid "Permission Denied" When Using Pip with Virtualenv
Openpyxl 1.8.5: Reading the Result of a Formula Typed in a Cell Using Openpyxl
Nltk Naivebayesclassifier Training for Sentiment Analysis
How to Convert a List to a List of Tuples