Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get the largest integer one can use in Python? [duplicate]

Tags:

python

integer

Is there some pre-defined constant like INT_MAX?

like image 878
nos Avatar asked Jan 03 '11 03:01

nos


People also ask

How do you find the maximum int in python?

If you are using Python 2, you can find the maximum value of the integer using sys. maxint . There is a max int limit in Python 2, and if you cross the max limit, it would automatically switch from int to long .

Which one is the maximum possible value in python?

In the approach above we assume that 999999 is the maximum possible value in our list and compare it with other elements to update when a value lesser than it is found.

Is integer overflow possible in python?

Thus, there's no integer overflow, like how C's int works. But of course the memory can't store infinite data. I think that's why the result of n+1 can be the same as n : Python can't allocate more memory to preform the summation, so it is skipped, and n == n is true.

What is bigger than integer in python?

maxint constant returns the maximum possible plain integer value in python2 which is “9223372036854775807”. Anything higher than this value will be automatically converted to a long type. However, the sys. maxint constant has been removed in python3 since there is no longer a limit to the value of integers.


1 Answers

Python has arbitrary precision integers so there is no true fixed maximum. You're only limited by available memory.

In Python 2, there are two types, int and long. ints use a C type, while longs are arbitrary precision. You can use sys.maxint to find the maximum int. But ints are automatically promoted to long, so you usually don't need to worry about it:

sys.maxint + 1 

works fine and returns a long.

sys.maxint does not even exist in Python 3, since int and long were unified into a single arbitrary precision int type.

like image 199
Matthew Flaschen Avatar answered Sep 20 '22 00:09

Matthew Flaschen