Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does python represent such large integers?

In C, C++, and Java, an integer has a certain range. One thing I realized in Python is that I can calculate really large integers such as pow(2, 100). The same equivalent code, in C, pow(2, 100) would clearly cause an overflow since in 32-bit architecture, the unsigned integer type ranges from 0 to 2^32-1. How is it possible for Python to calculate these large numbers?

like image 788
chanpkr Avatar asked Apr 05 '14 00:04

chanpkr


People also ask

What is a large integer in Python called?

Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate. In Python 3.0+, the int type has been dropped completely.

What are Python integers and why are they interesting?

Yet, Python integers are interesting because they are not just 32-bit or 64-bit integers that CPUs work with natively. Python integers are arbitrary-precision integers, also known as bignums. This means that they can be as large as we want, and their sizes are only limited by the amount of available memory.

Is it possible to write large numbers in Python?

Yes - that Jake. Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate.

How to get number of bytes of an integer in Python?

In Python, all integers are instances of the class int. Use the getsizeof () function of the sys module to get the number of bytes of an integer. Python integers support all standard operations including addition, subtraction, multiplication, division, and exponent. Did you find this tutorial helpful ?


2 Answers

Basically, big numbers in Python are stored in arrays of 'digits'. That's quoted, right, because each 'digit' could actually be quite a big number on its own. )

You can check the details of implementation in longintrepr.h and longobject.c:

There are two different sets of parameters: one set for 30-bit digits, stored in an unsigned 32-bit integer type, and one set for 15-bit digits with each digit stored in an unsigned short. The value of PYLONG_BITS_IN_DIGIT, defined either at configure time or in pyport.h, is used to decide which digit size to use.

/* Long integer representation.
    The absolute value of a number is equal to
    SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i)
    Negative numbers are represented with ob_size < 0; 
      zero is represented by ob_size == 0.

    In a normalized number, ob_digit[abs(ob_size)-1] (the most significant
      digit) is never zero.  Also, in all cases, for all valid i,
        0 <= ob_digit[i] <= MASK.

    The allocation function takes care of allocating extra memory
    so that ob_digit[0] ... ob_digit[abs(ob_size)-1] are actually available.

*/

struct _longobject {
   PyObject_VAR_HEAD
   digit ob_digit[1];
};
like image 163
raina77ow Avatar answered Nov 08 '22 15:11

raina77ow


How is it possible for Python to calculate these large numbers?

How is it possible for you to calculate these large numbers if you only have the 10 digits 0-9? Well, you use more than one digit!

Bignum arithmetic works the same way, except the individual "digits" are not 0-9 but 0-4294967296 or 0-18446744073709551616.

like image 39
Jörg W Mittag Avatar answered Nov 08 '22 17:11

Jörg W Mittag