views:

540

answers:

9

For a computer working with a 64 bit processor, the largest number that it can handle would be 2^64 = 18446744073709551616. 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?

A: 

In general, the language itself doesn't handle high-precision, high-accuracy large number arithmetic. It's far more likely that a library is written that uses alternate numerical methods to perform the desired operations.

For example (I'm just making this up right now), such a library might emulate the actual techniques that you might use to perform that large number arithmetic by hand. Such libraries are generally much slower than using the built-in arithmetic, but occasionally the additional precision and accuracy is called for.

Greg D
Ok, however it be, when it comes down to arithmetic operations, say adding the two big numbers, both of them have to be in registers, for which the size of the register is not enough. How is this problem tackled?
Ajay
You can perform a single large arithmetic operation by performing lots of smaller ones. I think I said it pretty clearly in the second paragraph...
Greg D
You don't add 123098712380714091823 to 120497235897345089273403 on a piece of paper all at once, do you? You perform many smaller operations one at a time.
Greg D
A: 

You assume the wrong thing. The biggest number it can handle in a single register is a 64-bits number. However, with some smart programming techniques, you could just combined a few dozens of those 64-bits numbers in a row to generate a huge 6400 bit number and use that to do more calculations. It's just not as fast as having the number fit in one register.

Even the old 8 and 16 bits processors used this trick, where they would just let the number overflow to other registers. It makes the math more complex but it doesn't put an end to the possibilities.

However, such high-precision math is extremely unusual. Even if you want to calculate the whole national debt of the USA and store the outcome in Zimbabwean Dollars, a 64-bits integer would still be big enough, I think. It's definitely big enough to contain the amount of my savings account, though.

Workshop Alex
not true.... It's especially the fractional part which they don't want to round off to fast (for instance when using normal floating point numbersd)
Toad
@reinier, Donald Knuth has written several good books and articles about this subject and computers and math in general. I suggest you start reading some of those. Many languages have a special BigNum library, which can be implemented in many ways. (See http://en.wikipedia.org/wiki/Bignum)
Workshop Alex
Not unusual.. hmm what about.. say i want 35!.. which crosses the limit easily
Ajay
A: 

As a thought experiment, imagine the numbers stored as a string. With functions to add, multiply, etc these arbitrarily long numbers.

In reality these numbers are probably stored in a more space efficient manner.

Toad
A: 

Think of one machine-size number as a digit and apply the algorithm for multi-digit multiplication from primary school. Then you don't need to keep the whole numbers in registers, just the digits as they are worked on.

starblue
+3  A: 

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.

Christian Oudard
A: 

Most languages store them as array of integers. If you add/subtract two to of these big numbers the library adds/subtracts all integer elements in the array separately and handles the carries/borrows. It's like manual addition/subtraction in school because this is how it works internally.

Some languages use real text strings instead of integer arrays which is less efficient but simpler to transform into text representation.

codymanix
A: 

Ada actually supports this natively, but only for its typeless constants ("named numbers"). For actual variables, you need to go find an arbitrary-length package. See http://stackoverflow.com/questions/1309195/arbitrary-length-integer-in-ada

T.E.D.
A: 

I asked a similar question and eventually found my way here;

http://stackoverflow.com/questions/1218149/arbitrary-precision-arithmetic-explanation/1218185#1218185

Good hunting!

deau
A: 

More-or-less the same way that you do. In school, you memorized single-digit addition, multiplication, subtraction, and division. Then, you learned how to do multiple-digit problems as a sequence of single-digit problems.

If you wanted to, you could multiply two twenty-digit numbers together using nothing more than knowledge of a simple algorithm, and the single-digit times tables.

Aric TenEyck