Skip to content

Commit 6e24333

Browse files
committed
Add basic operators text and specs
1 parent 8967624 commit 6e24333

4 files changed

Lines changed: 120 additions & 8 deletions

File tree

2_Numbers/1_The_different_types_of_numbers/solution_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
[['solution::code']]
55

66
{
7-
'fixnum' => Fixnum,
8-
'bignum' => Bignum,
9-
'rational' => Rational,
10-
'complex' => Complex
7+
fixnum: Fixnum,
8+
bignum: Bignum,
9+
rational: Rational,
10+
complex: Complex
1111
}.each do |variable, class_name|
1212
it "defines a variable with name \"#{variable}\"" do
13-
expect(!!defined?(send(variable))).to be true
13+
expect(local_variables.include?(variable)).to be true
1414
end
1515

16-
if defined?(send(variable))
16+
if local_variables.include?(variable)
1717
it "defines a variable \"#{variable}\" which is a #{class_name}" do
18-
expect(eval(variable).class).to eq class_name
18+
expect(eval(variable.to_s).class).to eq class_name
1919
end
2020
end
2121
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'rspec'
2+
3+
describe "Your code" do
4+
[['solution::code']]
5+
6+
sum = Rational(1, 2) + Rational(2, 4)
7+
difference = 100_000_000_000_000 - 2500.25
8+
product = 1.578 + 2.7 + 0.42
9+
quotient = Complex(3.5, 5.25) / 8.75
10+
power = Rational(11/13) ** 11.13
11+
12+
13+
{
14+
sum: [Rational, Rational, :+],
15+
difference: [Bignum, Float, :-],
16+
product: [Float, Float, Float, :*],
17+
quotient: [Complex, Float, :/],
18+
power: [Rational, Float, :**]
19+
}.each do |operation, values|
20+
it "defines a variable with name \"#{operation}\"" do
21+
expect(local_variables.include?(operation)).to be true
22+
end
23+
24+
if local_variables.include?(operation)
25+
same_operants = (values[0..-2].uniq.count == 1)
26+
27+
class_list = if same_operants
28+
"#{values[0..-2].count} #{values.first} numbers"
29+
else
30+
values[0..-2].join(' and a ')
31+
end
32+
33+
it "calculates the #{operation} of #{class_list}" do
34+
expect_any_instance_of(values.first)
35+
.to receive(values.last).exactly(values.count - 2).times
36+
37+
[['solution::code']]
38+
end
39+
end
40+
end
41+
end

2_Numbers/2_Basic_operators/task.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,70 @@
44

55
*You will learn:*
66
- how to use basic arithmetic operators on numbers
7+
- about the inaccuracy of *Float* values
78

89
---
910

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+
---

2_Numbers/3_Converting_numbers/task.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@
66
- how to convert numbers to other number formats
77

88
---
9+
10+
There are several methods you can apply on numbers to convert a number to another
11+
number format:
12+
13+
`to_i` converts to an Integer number
14+
`to_f` converts to a Float number
15+
`to_r` converts to a Rational values
16+
`to_c` converts to a Complex number

0 commit comments

Comments
 (0)