13

The fact that Python is written in C and is actually a C program made me wonder about how decimal numbers assignment are handled.

How does a C program implement the Python variable assignment of a very large decimal number (bigger than int or long)?

For example:

a=10000...  # a=(10^1000)

when running in python I know that the value is so big that it takes many words in the memory, so the C program obviously does that, but how?

Every variable in C has a type, but the C compiled code does not know how big the number will be.

How does the (python) C program handles that assignment? (and operations on such variables)

NullUserException
  • 83,810
  • 28
  • 209
  • 234
izac89
  • 3,790
  • 7
  • 30
  • 46
  • 1
    What makes you think the Python runtime assigns the value to a C variable? – Wooble Oct 15 '13 at 12:52
  • Interasting question but i'm not sure that's the case, Please see Daenyth [post](http://stackoverflow.com/a/6101453/1982962) and Duncan [post](http://stackoverflow.com/a/6101687/1982962) – Kobi K Oct 15 '13 at 12:56
  • please, be more specific about large decimal numbers on your question title – milton Oct 15 '13 at 12:58
  • The other post does not refer to a very large numbers that exceeds long and ints, and this is not the same as I am asking. – izac89 Oct 15 '13 at 13:02
  • Just to be annoying: **CPython** is written in C. Other implementations are written in other languages. – Daniel Roseman Oct 15 '13 at 13:10
  • 1
    @user2162550: Python longs are not C longs, and it is talking about very large numbers. OTOH, that question has nothing to do with the C implementation behind it, so I agree it's not a duplicate. – Wooble Oct 15 '13 at 13:28
  • Like @Wooble said, Python long aren't the same as C longs. Python longs are more like `BigInteger`s in Java or C#, in that they have unlimited precision. You can read more about them in the [manual](http://docs.python.org/2/library/stdtypes.html#numeric-types-int-float-long-complex). But I agree this isn't a duplicate of the other question. – NullUserException Oct 25 '13 at 15:59

2 Answers2

5

Here is the C struct that is used in CPython 2.7.5 to represent a long integer:

/* 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.

   CAUTION:  Generic code manipulating subtypes of PyVarObject has to
   aware that longs abuse  ob_size's sign bit.
*/

struct _longobject {
        PyObject_VAR_HEAD
        digit ob_digit[1];
};

If you'd like to explore this further, download the source code and take a look at the following files:

./Include/longintrepr.h
./Include/longobject.h
./Objects/longobject.c

This will tell you every detail you could possibly wish to know. :)

NPE
  • 486,780
  • 108
  • 951
  • 1,012
-1

Python may have a big number implementation of its own for performance reasons, but it could use any third-party arbitrary precision library, such as GMP.

milton
  • 988
  • 6
  • 19