1. Technology
Send to a Friend via Email
You can opt-out at any time. Please refer to our privacy policy for contact information.

Bignum Quick Reference


Bignum Quick Reference

The Bignum class is a bit of an odd one, many people don't even realize it's there or that they're even using it. A Bignum is part of the Numeric class hierarchy in Ruby, and is used to represent numbers of arbitrary magnitude or precision. Where integers are realistically limited to a magnitude 32 bits, and floating point numbers to a precision 64 bits (using double precision floating point on x86), Bignum numbers have no limitations. They can be millions of digits long and never lose any precision as with floating point numbers. However, where math can be performed on the native numeric types in a single CPU cycle, working with Bignum objects is comparatively very slow.

Creating Bignums

Where many classes would have something more informative in this section, the Bignum class is, as mentioned above, a bit of an odd one. There is no documented way of directly creating a Bignum object. When a native type overflows (such as when computing a rather large exponent), instead of throwing its hands up and announcing it simply cannot continue (by raising an exception), Ruby will automatically convert the number to a Bignum and complete the operation. So, if you really want to create a Bignum manually, try any very large integer literal or expression that would produce such a large number.

> 10.class
 => Fixnum 
 > 1000000000000.class
 => Bignum 
> (10**100).class
 => Bignum 


All of the typical arithmetic operators are implemented for Bignum and work with either other Bignum objects or any other Numeric object. As these work so seamlessly, they're simply not worth listing. The addition, subtraction, multiplication, division, modulo and exponent operators are there of course. As are the typical comparison operators, Bignums can be compared to any other Numerics seamlessly.

One thing worth mentioning is that for all bitwise operations, a Bignum object is treated as a very large integer in two's complement format. In other words, the same way an integer type is represented only not bounded to any particular. This may not be how the number is represented in reality, but for the purpose of bitwise operators, this is assumed. This includes the index operator some_bignum[x], which returns the value of the Xth bit in the Bignum where some_bignum is the number's least significant bit.

Methods and Caveat

Bignum doesn't really have any methods of its own. For some methods you can call on Bignum, see its parent class Integer. And the only caveat for working with Bignums is the performance caveat. Arithmetic that produces very large results will produce Bignum objects, and there is no way I can see to turn this off. Since (for speed reasons) the Integer arithmetic methods are implemented in C, there is no way to simply delete the Bignum class. Doing this (such as with the hack Object.send(:remove_const, :Bignum)) you will no longer be able to refer to the Bignum class by name, but the C code will still be able to generate Bignum objects and work with them just fine. So be careful not to generate Bignum objects when they're not wanted for performance reasons, because Ruby does this silently and there's no way to turn this off.

  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. Bignum Quick Reference

©2014 About.com. All rights reserved.