|
| 1 | +# Types of numbers |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +*You will learn:* |
| 6 | +- which types of numbers are available in Ruby |
| 7 | +- how to create a certain type of number |
| 8 | +- different ways to create a number |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +Ruby has following types of numbers: *Integer*, *Float*, *Rational*, and *Complex*. |
| 13 | + |
| 14 | +## Integers |
| 15 | + |
| 16 | +An *Integer* represents a *whole number*. |
| 17 | +You can create an Integer by declaring the number, like `2000` or `-42`. |
| 18 | +Ruby allows you to write more readable numbers by using underscores, e.g. `1_000_000`. |
| 19 | +Using the underscore style does not change the value of an integer, so `2_000` is |
| 20 | +perfectly the same as `2000`. |
| 21 | + |
| 22 | +Another way to create an Integer is by calling `Integer()` with a parameter, e.g.: |
| 23 | + |
| 24 | +` Integer(3)` *# => 3* |
| 25 | +` Integer('3')` *# => 3* |
| 26 | + |
| 27 | +Internally, Ruby uses two different classes for Integers. Dependent on how big the |
| 28 | +Integer is it uses a *Fixnum* or a *Bignum*. You don't have to worry about which class to |
| 29 | +use, Ruby is taking care of this for you! |
| 30 | + |
| 31 | +However, you can check which class Ruby uses for your number by calling the `class` |
| 32 | +method on your number. Open a new terminal tab and open the interactive Ruby console |
| 33 | +with `irb`. Then check the class of different numbers: |
| 34 | + |
| 35 | +` 1000.class` *# => Fixnum* |
| 36 | +` 4_611_686_018_427_387_904.class` *# => Bignum* |
| 37 | + |
| 38 | +If you want to learn more, read about Fixnum and Bignum here: |
| 39 | +- http://patshaughnessy.net/2014/1/9/how-big-is-a-bignum |
| 40 | +- (ruby-doc core: Integer) |
| 41 | + |
| 42 | +## Floats |
| 43 | + |
| 44 | +A *Float* represents an *inexact real number*. |
| 45 | +You can create a Float by declaring a number with a decimal place, like `3.0` |
| 46 | +or `-2_000.25`. |
| 47 | +Another way to create a Float is to call `Float()` with a parameter, e.g.: |
| 48 | + |
| 49 | +` Float(3)` *# => 3.0* |
| 50 | +` Float('3')` *# => 3.0* |
| 51 | + |
| 52 | +Read more about Float here: (ruby-doc core: Float). |
| 53 | + |
| 54 | +## Rationals |
| 55 | + |
| 56 | +A *Rational* represents a rational number (a paired integer number a/b, where b > 0). |
| 57 | +You can create a Rational by calling `Rational()` with one or two parameters, e.g.: |
| 58 | + |
| 59 | +` Rational(2)` *# => (2/1)* |
| 60 | +` Rational(2, 3)` *# => (2/3)* |
| 61 | +` Rarional('2/3')` *# => (2/3)* |
| 62 | + |
| 63 | +Read more about Rational here: (ruby-doc core: Rational). |
| 64 | + |
| 65 | +## Complex numbers |
| 66 | + |
| 67 | +A *Complex* represents a complex number. |
| 68 | +You can create a complex number by calling `Complex()` with one or two parameters, e.g.: |
| 69 | + |
| 70 | +` Complex(2)` *# => (2+0i)* |
| 71 | +` Complex(2.1, 0.2)` *# => (2.1+0.2i)* |
| 72 | +` Complex('2.1+0.2i')` *# => (2.1+0.2i)* |
| 73 | +` Complex('2.1@0.2')` *# => (2.0581398134666076+0.4172055946696286i)* - polar form |
| 74 | + |
| 75 | +Read more about Complex here: (ruby-doc core: Complex). |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +Create a number of each described type and store it into a variable with the |
| 80 | +lowercased name of the number's class (e.g. *rational* for Rational). |
| 81 | +Create both types of Integers (*Fixnum* and *Bignum*)! |
| 82 | + |
| 83 | +--- |
0 commit comments