Ruby Numbers


Ruby provides a number of built-in number classes.

Notice: "built-in" in this case means you cannot use them explicitly.

Ruby's number objects keep the same object ID no matter the number class they fit at the momment.

Integer Class: The base class from which the following number classes are derived.

Fixnum Class: A Fixnum holds Integer values that can be represented in a native machine word (minus 1 bit). This effectively means that the maximum range of a Fixnum value depends on the architecture of the system on which the code is executing.

If an operation performed on a Fixnum exceeds the range defined by the system's machine word, the value is automatically converted by the interpreter to a Bignum.

Bignum Class: Bignum objects hold integers that fall outside the range of the Ruby Fixnum class. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted to Fixnum.

BigDecimal Class: BigDecimal objects hold very large or very accurate floating point numbers. Decimal arithmetic is also useful for general calculation, because it provides the correct answers people expect.

Float Class: The Float object represents real numbers based on the native architecture's double-precision floating point representation.

Rational Class: Rational implements a rational class for numbers.

A rational number is a number that can be expressed as a fraction p/q where p and q are integers and q is not equal to zero. A rational number p/q is said to have numerator p and denominator q. Numbers that are not rational are called irrational numbers.

Let's see some of them in action.
      num = 43
      6.times do
        puts "#{num.class} #{num}"
        num *= num
      end
      

This will produce following result:

      Fixnum 43
      Fixnum 1849
      Fixnum 3418801
      Bignum 11688200277601
      Bignum 136614025729312093462315201
      Bignum 18663392025969146670022260524972832947786731075670401
      

 

Here are the ways you write numbers:
      puts 123456                    # Fixnum
      puts 123_456                   # Fixnum
      puts -543                      # Negative Fixnum
      puts 123_456_789_123_345_789   # Bignum
      puts 0xaabb                    # Hexadecimal
      puts 0377                      # Octal
      puts -0b101_010                # Binary (negated)
      

Notice: underscores are ignored.

This will produce following result:

      123456
      123456
      -543
      123456789123345789
      43707
      255
      -42
      

 

Here are some rational numbers:
      puts Rational(5,6)       # -> 5/6
      puts Rational(5)         # -> 5/1
      puts Rational(6,10)      # -> 3/5
      puts Rational.new!(6,10) # -> 6/10
      puts Rational(3,0)       # -> ZeroDivisionError
      

This will produce following result:

      5/6
      5
      3/5
      6/10
      D:/Ruby186-398/lib/ruby/1.8/rational.rb:72:
        in `reduce': denominator is zero (ZeroDivisionError)
      	from D:/Ruby186-398/lib/ruby/1.8/rational.rb:35:in `Rational'
      	from rubynumbers04.rb:5
      

 

Here are some BigDecimal numbers:
      require 'bigdecimal'
      
      puts BigDecimal('1876.8')
      a=BigDecimal('0.123456789123456789')
      b=BigDecimal('123456.78912345678',40)
      puts c = a + b
      

This will produce following result:

      0.18768E4
      0.123456912580245903456789E6
      

 

Converting Numbers in Ruby


Numbers can be converted from one type to another using the Ruby Integer and Float methods. These methods take as an argument the value to be converted. For example:

      # Convert Floating Point Number to an Integer
      puts Integer(10.898)
      
      10
      

 

      # Convert a String to an Integer
      puts Integer("10898")
      
      10898
      

 

      # Convert a Hexadecimal Number to an Integer
      puts Integer(0xA4F5D)
      
      675677
      

 

      # Convert an Octal Number to an Integer
      puts Integer(01231)
      
      665
      

 

      # Convert a Binary Number to an Integer
      puts Integer(0b01110101)
      
      117
      

 

Convert a Character to the ASCII Character Code

For Ruby version 1.8 or earlier:

      puts Integer(?e)
      
      101
      

For Ruby version 1.9 or later:

      puts "e".getbyte(0)               #ASCII to Integer
      
      101
      

 

Similarly, we can perform conversions to floating point using the Float method:

      # Convert an Integer Floating Point
      puts Float(10)
      
      10.0
      

 

      # Convert a String to Floating Point
      puts Float("10.09889")
      
      10.09889
      

 

      # Convert a Hexadecimal Number to Floating Point
      puts Float(0xA4F5D)
      
      675677.0
      

 

      # Convert an Octal Number to a Floating Point
      puts Float(01231)
      
      665.0
      

 

      # Convert a Binary Number to Floating Point
      puts Float(0b01110101)
      
      117
      

 

Convert a Character to a Floating Point ASCII Character Code

For Ruby version 1.8 or earlier:

      puts Float(?e)
      
      101.0
      

For Ruby version 1.9 or later:

      puts "e".getbyte(0)
      
      101.0