How do languages such as Python overcome C's Integral data limits?

后端 未结 5 639
天命终不由人
天命终不由人 2021-01-15 16:06

While doing some random experimentation with a factorial program in C, Python and Scheme. I came across this fact:

In C, using \'unsigned long long\' data type, the

相关标签:
5条回答
  • 2021-01-15 16:24

    It's called Arbitrary Precision Arithmetic. There's more here: http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic

    0 讨论(0)
  • 2021-01-15 16:29

    Looking at the Python source code, it seems the long type (at least in pre-Python 3 code) is defined in longintrepr.h like this -

    /* 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];
    };
    

    The actual usable interface of the long type is then defined in longobject.h by creating a new type PyLongObject like this -

    typedef struct _longobject PyLongObject;
    

    And so on.

    There is more stuff happening inside longobject.c, you can take a look at those for more details.

    0 讨论(0)
  • 2021-01-15 16:29

    Data types such as int in C are directly mapped (more or less) to the data types supported by the processor. So the limits on C's int are essentially the limits imposed by the processor hardware.

    But one can implement one's own int data type entirely in software. You can for example use an array of digits as your underlying representation. May be like this:

    class MyInt {
        private int [] digits;
        public MyInt(int noOfDigits) {
           digits = new int[noOfDigits];
        }
    }
    

    Once you do that you may use this class and store integers containing as many digits as you want, as long as you don't run out memory.

    Perhaps Python is doing something like this inside its virtual machine. You may want to read this article on Arbitrary Precision Arithmetic to get the details.

    0 讨论(0)
  • 2021-01-15 16:36

    Not octaword. It implemented bignum structure to store arbitary-precision numbers.

    0 讨论(0)
  • 2021-01-15 16:41

    Python assigns to long integers (all ints in Python 3) just as much space as they need -- an array of "digits" (base being a power of 2) allocated as needed.

    0 讨论(0)
提交回复
热议问题