Understanding integer limit on python3 using Fibonacci series

Question:

I need to better understand the max integer number that can be used in python3.
I used the following instructions to get the max integer size.

>> import sys
>> print(sys.maxsize)
9223372036854775807

So, here I got some integer which I thought was the maximum integer value that python3 will process.
Then, I wrote a small code to get numbers in the Fibonacci series. It looks like this.

fib=[0,1]
file=open('fib.txt','a')
file.write(str(fib[0]))
for i in range(1000):
    fib.append(fib[-1]+fib[-2])
    file.write('n'+ str(fib[-1]))
file.close()

So, I got my series in the list ‘fib’ and got the series in a text file named ‘fib.txt’. Here, I got the first 1001 numbers in the series (including zero).
Now, when I see the last entry in this series, it shows a very big number.

70330367711422815821835254877183549770181269836358732742604905087154537118196933579742249494562611733487750449241765991088186363265450223647106012053374121273867339111198139373125598767690091902245245323403501

This number is clearly larger than what ‘sys.maxsize’ showed.
And if I wish I can still do simple mathematical operations on the number so it is not like it is just a string. (I tried only addition and subtraction.)
I do not understand what the max integer is that python can process. Please clarify this.

Thank you.

PS: I am new so please explain accordingly.

Asked By: Arya

||

Answers:

Integers

In Python, an integer is an int and is unbounded and should not be confused with integer types (int, long etc) in languages like C++.

For example, C++ have a limits.h library to see the constrains, you can test this by trying to compile a code that makes an int major than the actual limit:

// numeric_limits example
// credit : https://cplusplus.com/reference/limits/numeric_limits/
#include <iostream>     // std::cout
#include <limits>       // std::numeric_limits

int main () {
  std::cout << std::boolalpha;
  std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << 'n';
  std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << 'n';
  std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << 'n';
  std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << 'n';
  std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << 'n';
  return 0;
}

What python can handle?

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.

so what does sys.maxsize do?

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.

You can test this trying to create a list range bigger than that, in a python shell terminal:

kali@kali:~$ python3
Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> list(range(sys.maxsize+1))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C ssize_t

Answered By: XxJames07-

In Python3, int has no max limit.

maxsize attribute of the sys module fetches the largest value a variable of data type Py_ssize_t can store. It is the Python platform’s pointer that dictates the maximum size of lists and strings in Python. The size value returned by maxsize depends on the platform architecture.

You can also see it Here for more clarifications.

The sys.maxint constant was removed, since there is no longer a limit to the value of integers.

Answered By: Nafisa Alam
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.