Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do programming languages handle huge number arithmetic

For a computer working with a 64 bit processor, the largest number that it can handle would be 264 = 18,446,744,073,709,551,616. How does programming languages, say Java or be it C, C++ handle arithmetic of numbers higher than this value. Any register cannot hold it as a single piece. How was this issue tackled?

like image 723
Ajay Avatar asked Sep 17 '09 13:09

Ajay


2 Answers

There are lots of specialized techniques for doing calculations on numbers larger than the register size. Some of them are outlined in this wikipedia article on arbitrary precision arithmetic

Low level languages, like C and C++, leave large number calculations to the library of your choice. One notable one is the GNU Multi-Precision library. High level languages like Python, and others, integrate this into the core of the language, so normal numbers and very large numbers are identical to the programmer.

like image 142
Christian Oudard Avatar answered Oct 04 '22 05:10

Christian Oudard


Programming languages that handle truly massive numbers use custom number primitives that go beyond normal operations optimized for 32, 64, or 128 bit CPUs. These numbers are especially useful in computer security and mathematical research.

The GNU Multiple Precision Library is probably the most complete example of these approaches.

You can handle larger numbers by using arrays. Try this out in your web browser. Type the following code in the JavaScript console of your web browser:

The point at which JavaScript fails

console.log(9999999999999998 + 1)
// expected 9999999999999999
// actual  10000000000000000  oops!

JavaScript does not handle plain integers above 9999999999999998. But writing your own number primitive is to make this calculation work is simple enough. Here is an example using a custom number adder class in JavaScript.

Passing the test using a custom number class

// Require a custom number primative class
const {Num} = require('./bases')

// Create a massive number that JavaScript will not add to (correctly)
const num = new Num(9999999999999998, 10)

// Add to the massive number
num.add(1)

// The result is correct (where plain JavaScript Math would fail)
console.log(num.val)  // 9999999999999999

How it Works

You can look in the code at class Num { ... } to see details of what is happening; but here is a basic outline of the logic in use:

Classes:

  • The Num class contains an array of single Digit classes.
  • The Digit class contains the value of a single digit, and the logic to handle the Carry flag

Steps:

  1. The chosen number is turned into a string
  2. Each digit is turned into a Digit class and stored in the Num class as an array of digits
  3. When the Num is incremented, it gets carried to the first Digit in the array (the right-most number)
  4. If the Digit value plus the Carry flag are equal to the Base, then the next Digit to the left is called to be incremented, and the current number is reset to 0
  5. ... Repeat all the way to the left-most digit of the array

Logistically it is very similar to what is happening at the machine level, but here it is unbounded. You can read more about about how digits are carried here; this can be applied to numbers of any base.

like image 31
f1lt3r Avatar answered Oct 04 '22 05:10

f1lt3r