Section 5.8. Using BigDecimal


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 take more memory and are obviously somewhat slower, but number-crunching with very large integers (hundreds of digits) is still reasonably practical.




The Ruby Way(c) Solutions and Techniques in Ruby Programming
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2004
Pages: 269
Authors: Hal Fulton

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net