Numbers in Ruby
Tags: ruby, programming, codenewbie, beginners
Published: 2022-06-07 02:08:46

**_Quick index_**
1. [Numeric](#numeric)
  1. [Integer](#integer)
  2. [Float](#float)
  3. [BigDecimal](#bigdecimal)
  4. [Complex](#complex)
  5. [Rational](#rational)
2. [Important Notes] (#important-notes)
3. [Summary from wikibooks.org](#summary-from-wikibooksorg)
4. [References] (#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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3sx8itkgo6l8o77duigq.png)

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. 

```ruby
a = 1  
a.object_id == 1.object_id #=> true
```

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.

```ruby
1.class #=> Fixnum
100000000000.class #=> Bignum
```

### 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: 

```ruby
(2.0-1.1) == 0.9 #=> false
```

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. 

```ruby
2.0-1.1 #=> 0.8999999999999999
```

### 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. 

```ruby
2+1i                 #=> (2+1i)
Complex(1)           #=> (1+0i)
Complex(2, 3)        #=> (2+3i)
3.to_c               #=> (3+0i)
```

### Rational

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

```ruby
1/4r + 2/4r #=> 3/4
```

_____

## 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](https://ruby-doc.org/core-3.1.1/Numeric.html) in general, for [Integers](https://ruby-doc.org/core-3.1.0/Integer.html), [Floats](https://ruby-doc.org/core-3.1.1/Float.html), [BigDecimals](https://ruby-doc.org/stdlib-3.1.0/libdoc/bigdecimal/rdoc/BigDecimal.html), [Complexes](https://ruby-doc.org/stdlib-3.1.1/libdoc/bigdecimal/rdoc/Complex.html), and [Rationals](https://ruby-doc.org/core-2.5.0/Rational.html) as well.
_____

## Summary from [wikibooks.org] (https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Numerics)

| 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

+ [Ruby Documentation] (https://ruby-doc.org/core-3.1.1/Numeric.html)

+ [The Ruby Programming Language] (https://www.oreilly.com/library/view/the-ruby-programming/9780596516178/)

+ [How numbers work in Ruby: Understanding Integers, Floats & Bigdecimal] (https://www.rubyguides.com/2016/07/numbers-in-ruby/)

+ [Ruby Programming/Syntax/Literals](https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Numerics)