5.7. Working with Very Large Integers

The control of large numbers is possible, and like unto that of small numbers, if we subdivide them.

Sun Tze

In the event it becomes necessary, Ruby programmers can work with integers of arbitrary size. The transition from a Fixnum to a Bignum is handled automatically and transparently. In this following piece of code, notice how a result that is large enough is promoted from Fixnum to Bignum:

num1 = 1000000           # One million (10**6)
num2 = num1*num1         # One trillion (10**12)
puts num1                # 1000000
puts num1.class          # Fixnum
puts num2                # 1000000000000
puts num2.class          # Bignum

The size of a Fixnum varies from one architecture to another. Calculations with Bignums are limited only by memory and processor speed. They ...

Get The Ruby Way: Solutions and Techniques in Ruby Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.