Fractions and whole numbers
In lesson 2, 7 / 2 gave 3: dividing two whole numbers drops the remainder. That is no flaw – it is a tool. / says how many times, % what is left over, and divmod gives both at once; two variables on the left of the = take its answer apart. 135 minutes are 2 hours and 15 minutes, and 5 hours after 22 o'clock the clock says 3:
minutes = 135
p minutes / 60 # whole hours
p minutes % 60 # minutes left over
hours, rest = minutes.divmod(60)
puts "#{hours} h #{rest} min"
(22 + 5) % 24 # 5 hours after 22 o'clockTwo more whole-number tools come from school. The greatest common divisor, gcd, is the biggest number that divides both – divide the top and the bottom of a fraction by it, and the fraction is fully cancelled. The least common multiple, lcm, is the smallest number that both go into: if one bus leaves every 12 minutes and another every 18, both at 8:00, they next leave together at 8:36:
g = 12.gcd(18)
p g
p [12 / g, 18 / g] # 12/18 cancelled: 2/3
12.lcm(18) # minutes until both buses leave againBut Ruby can also keep a fraction as a fraction: Rational(3, 4) is three quarters, and an r after a number is the short way to write one – 3/4r. A Rational cancels itself and calculates exactly, the way you learned at school: a half plus a third is five sixths:
p Rational(3, 4)
p Rational(12, 18) # cancels itself
half = 1/2r
p half + Rational(1, 3)
p half * 3
Rational(6, 3)(2/1) is a fraction that happens to be whole. But why fractions at all, when there are decimals? Because a decimal like 0.1 is stored in binary – and in binary a tenth never ends, just as 1/3 never ends in decimal, so the computer has to round it. One tiny error is invisible, but add up ten tenths and it shows. Step through the cell with ⏯ and watch the two sums grow:
float_sum = 0
exact_sum = 0
10.times do
float_sum += 0.1
exact_sum += 1/10r
end
p float_sum
p exact_sum
float_sum == 1Ten tenths make 0.9999999999999999 as floats, but exactly (1/1) as fractions. A fraction knows its parts, numerator (the top) and denominator (the bottom), and to_f turns it into a decimal. The other way round, to_r turns a decimal into a fraction – and shows what the computer really stored: 0.75 is exact (1/2 + 1/4), 0.1 is not. rationalize finds the simple fraction behind it:
q = Rational(9, 8)
p [q.numerator, q.denominator]
p q.to_f
p 0.75.to_r
p 0.1.to_r
0.1.rationalizeSooner or later a result has to be a whole number. round rounds to the nearest one (a half goes up), floor always down, ceil always up – and ceildiv divides and rounds up, just right for “how many buses for 23 foxes, at 8 seats a bus?”. Given a negative number, round rounds to tens or hundreds, and Integer.sqrt is the whole-number square root:
p Rational(7, 2).round # 3.5: a half goes up
p 3.7.floor
p 3.2.ceil
p 23.ceildiv(8) # buses for 23 foxes
p 1234.round(-2)
Integer.sqrt(50) # 7, because 7 * 7 = 49A rule of thumb: whole numbers with /, % and divmod for things that don't come in halves – minutes, buses, foxes; Rational when a fraction must stay exact; a Float for what is measured anyway. And for money there is BigDecimal, in lesson 64.
scale_recipe(recipe, from, to), which turns the amounts for from foxes into amounts for to foxes – exactly, as fractions, not as decimals: 3/4 litre of milk for 4 becomes 9/8 litre for 6.pancakes = { milk: 3/4r, flour: 1/2r, eggs: 3 } # for 4 foxes
# def scale_recipe(recipe, from, to)
# ... # every amount * to / from, as an exact fraction
# end
# scale_recipe(pancakes, 4, 6)
1 1/8. Write mixed_number(q), which writes a fraction as a mixed number: the whole part, a space, then the fraction that is left over. mixed_number(9/8r) is "1 1/8", mixed_number(3/4r) just "3/4" and mixed_number(4/2r) just "2".# def mixed_number(q)
# ... # 9/8r -> "1 1/8", 3/4r -> "3/4", 4/2r -> "2"
# end
# mixed_number(9/8r)