1. Computing

Enter Bignum

By

Ruby can use the "native" types of numbers that the computer knows how to use, but it can also use what are called Bignums. Instead of storing a number as a binary number that the computer can readily use, a Bignum stores the number as a series of decimal digits. The Bignum then uses the computer's math functions to do arithmetic as you would do it on paper--digit by digit.

This has a few downsides. Firstly, it's slow. Using the native integer types is like doing a quick calculation on a calculator. Using the Bignum numbers is like doing the problem on paper. It's slow, but just like the problem with the calculator, you won't run out of digits. However, depending on the numbers you're working with, you may have no choice but to do the calculations with Bignums or on paper.

The second downside is that the numbers take a lot more memory. The memory a Bignum takes is directly proportional to the number of digits in the number. For example, if the Bignum library takes 1 byte per digit in the number, storing a 100 digit number would take 100 bytes. The native types of integers are much smaller. A 32-bit integer can store any number within its range in just 4 bytes. Also, aside from being smaller, the size of native integers are known, so they're a bit easier to work with.

But, despite all these downsides, there is one huge upside: no limitations. Bignums have no size limitations. Theoretically, you can work with numbers as large as or larger than the amount of RAM you have in your computer. Those are some very huge numbers, probably more atoms than there are in the universe.

  1. About.com
  2. Computing
  3. Ruby
  4. Advanced Ruby
  5. Enter Bignum

©2014 About.com. All rights reserved.