|
4 | 4 |
|
5 | 5 | *You will learn:* |
6 | 6 | - how to use basic arithmetic operators on numbers |
| 7 | +- about the inaccuracy of *Float* values |
7 | 8 |
|
8 | 9 | --- |
9 | 10 |
|
10 | | -You can apply |
| 11 | +All the numbers shown so far (*Fixnum*, *Bignum*, *Float*, *Rational*, and *Complex*) |
| 12 | +share the following basic operator methods: |
| 13 | + |
| 14 | +` +` (addition) |
| 15 | +` -` (subtraction) |
| 16 | +` \*` (multiplication) |
| 17 | +` /` (division) |
| 18 | +` \*\*` (exponentiation) |
| 19 | + |
| 20 | +You can apply these arithmetic operators by writing one of them between two |
| 21 | +numbers, e.g.: |
| 22 | + |
| 23 | +` 3.2 + 7` # => 10.2 |
| 24 | +` Rational(2, 3) - Rational(1, 2)` # => (1/6) |
| 25 | +` 3 \* 5` # => 15 |
| 26 | +` Complex(2.0, 4) / Complex(4.0, 2.0)` # => (0.8+0.6000000000000001i) |
| 27 | +` 2 \*\* 3` # => 8 |
| 28 | + |
| 29 | +## "Strange" behaviours |
| 30 | + |
| 31 | +When you try some combinations of numbers and operators in the interactive Ruby |
| 32 | +console (irb), you might think that sometimes strange things are going on. |
| 33 | + |
| 34 | +For instance, why will `3 / 7` always equal *0*? And why does `0.6 + 0.3 + 0.1` equal |
| 35 | +*0.9999999999999999* and not *1.0*? (Try it, if you don't believe it!) |
| 36 | + |
| 37 | +The explanation can be found in how Ruby handles numbers. |
| 38 | +If you divide an *Integer* by another *Integer*, then the result will be the rounded |
| 39 | +down *Integer* value (`5 / 2` equals *2*, `7 / 2` equals *3*). |
| 40 | +To get a floating point number you have to use a *Float* for either the numerator |
| 41 | +or the denominator or for both of them, i.e.: |
| 42 | + |
| 43 | +` 3.0 / 7` # => 0.42857142857142855 |
| 44 | +` 3 / 7.0` # => 0.42857142857142855 |
| 45 | +` 3.0 / 7.0` # => 0.42857142857142855 |
| 46 | + |
| 47 | +The inaccuracy when summing up *Floats* appears beacause a *Float* is an approximation |
| 48 | +of a decimal number. Since floating point numbers are stored in binary in your |
| 49 | +computer, small rounding errors are likely to occur. |
| 50 | + |
| 51 | +(if you really want to know what's going on here, you can read more about it on |
| 52 | +http://docs.sun.com/source/806-3568/ncg_goldberg.html!) |
| 53 | + |
| 54 | +This binary representation of *Floats* is also the explanation for the inaccuracy |
| 55 | +when calculating the division of two *Complex* numbers (as in the example above, |
| 56 | +where the maginary part is *0.6000000000000001* instead of *0.6*). |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +Connect numbers by each of the learned operators: |
| 61 | + |
| 62 | +1. Sum up two *Rational* numbers |
| 63 | +2. Subtract a *Float* from an *Bignum* |
| 64 | +3. Build the product of three *Float* numbers |
| 65 | +4. Divide a *Complex* number by a *Fixnum* |
| 66 | +5. Calculate a *Rational* to the power of a *Float* |
| 67 | + |
| 68 | +Store each result in a variable that is named like the result of the operation, e.g. |
| 69 | +for 1. *sum*, |
| 70 | +for 2. *difference*, |
| 71 | +and so on. |
| 72 | + |
| 73 | +--- |
0 commit comments