DEV Community

Cover image for Numbers in Ruby
Andressa
Andressa

Posted on • Edited on

Numbers in Ruby

Quick index

  1. Numeric
    1. Integer
    2. Float
    3. BigDecimal
    4. Complex
    5. Rational
  2. Important Notes
  3. Summary from wikibooks.org
  4. References

Numeric

Numeric is the parent class for all numeric classes existing in ruby and it inherits directly from the Object class.

The Ruby Programming Language by David Flanagan, Yukihiro Matsumoto

As you might know, when we create an object, it is stored in the Heap designated memory space. It is the Numeric class that allows the allocation of a physical address in the heap for that numerical instance.

Each other child class is a single immutable object passed by value. There can only ever be one instance of a number.

a = 1  
a.object_id == 1.object_id #=> true
Enter fullscreen mode Exit fullscreen mode

In Ruby, negative numbers can be represented by placing a minus sign in front of it. Just like in -404.

An underscore can be placed to separate the thousands if you wish. As it is in 1_234.56. At the end of the day, that underscore will be ignored by the compiler.

Integer

Integers are whole numbers, used when we don't care about precision.

Parent class for Fixnum and Bignum in the past. Nowadays, Fixnum and Bignum are considered deprecated, but under the hood they function the same way.

Fixnum represents all whole numbers that fits into 32 or 64 bits.

Bignum represents whole numbers that are outside that range.

1.class #=> Fixnum
100000000000.class #=> Bignum
Enter fullscreen mode Exit fullscreen mode

Float

Float point numbers are the ones with decimal places. Although they are often used to have a more exact result, they are quite imprecise because of rounding errors. So you could see something like this:

(2.0-1.1) == 0.9 #=> false
Enter fullscreen mode Exit fullscreen mode

This is not a Ruby particularity, it is like that because of the way floats are stored, and it would only be a concern if you need a very precise results.

2.0-1.1 #=> 0.8999999999999999
Enter fullscreen mode Exit fullscreen mode

BigDecimal

That is the one to go if you need that extreme precise result.

The down side: it is 12x slower than a float and that is the reason float is the default.

Complex

This type is used when pairing real number with an imaginary number.

2+1i                 #=> (2+1i)
Complex(1)           #=> (1+0i)
Complex(2, 3)        #=> (2+3i)
3.to_c               #=> (3+0i)
Enter fullscreen mode Exit fullscreen mode

Rational

They are used for fractions. They have the numerator and denominator. To represent one, we just need to add r to the fraction:

1/4r + 2/4r #=> 3/4
Enter fullscreen mode Exit fullscreen mode

Important notes

  • Calculations with two integers, will always result in an integer. 5/2 #=> 2

  • To get a more precise result, use one of the operands as a float. 5/2.0 #=> 2.5

  • There is a bunch of methods available for Numerics in general, for Integers, Floats, BigDecimals, Complexes, and Rationals as well.


Summary from wikibooks.org

numbers Its type
123 Fixnum
-123 Fixnum (signed)
1_123 Fixnum (underscore is ignored)
-543 Negative Fixnum
123_456_789_123_456_789 Bignum
123.45 Float
1.2e-3 Float
123.45r Rational, introduced in ruby 2.1
0xaabb (Hexadecimal) Fixnum
0377 (Octal) Fixnum
-0b1010 (Binary [negated]) Fixnum
0b001_001 (Binary) Fixnum
?a ASCII character code for 'a'(97)

References

Top comments (0)