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

后端 未结 5 637
天命终不由人
天命终不由人 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: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.

提交回复
热议问题