Ratios and Rates
A ratio compares two quantities, like 3 cups of flour to 2 cups of milk (). A rate is a ratio of two different kinds of quantity, like 60 kilometres per hour or 144 frames per second. This lesson writes and simplifies ratios, splits amounts in a given ratio, computes unit rates, and meets the ratios programmers use every day: aspect ratios, frame rates and pixel densities.
Writing a ratio
A ratio of to can be written three ways:
A bag has 12 red and 8 blue marbles. The ratio of red to blue is . The order matters: blue to red is . A ratio can also compare part to whole: red to all marbles is .
Simplifying ratios
Like fractions, ratios simplify by dividing both sides by their greatest common factor:
A ratio with three parts works the same way: .
Splitting in a ratio
Share 150 in the ratio . There are parts, so one part is :
from fractions import Fraction
import math
def simplify(*parts):
g = math.gcd(*parts)
return tuple(p // g for p in parts)
def split(total, *ratio):
whole = sum(ratio)
return [Fraction(total) * r / whole for r in ratio]
print(simplify(12, 8), simplify(45, 60), simplify(10, 15, 25))
print([str(x) for x in split(150, 2, 3, 5)])
print([str(x) for x in split(100, 1, 2)]) # exact thirds(3, 2) (3, 4) (2, 3, 5)
['30', '45', '75']
['100/3', '200/3']
Rates and unit rates
A rate compares different units: 240 km in 3 hours, 18.00 for 4 kg. A unit rate has 1 in the denominator ("per one"):
Unit rates make comparison easy. Which is the better buy, 500 g for 3.40 or 750 g for 4.95?
Ratios and rates in software
Aspect ratio is . A video in a 1280-pixel-wide player must be pixels tall.
Frame rate is frames per second, and frame time is its reciprocal, seconds per frame. Monitors and games quote both.
Pixel density (PPI, pixels per inch) is pixels along the diagonal divided by the diagonal in inches.
import math
# frame rate <-> frame time
for fps in [30, 60, 144, 240]:
print(f"{fps:3} fps -> {1000 / fps:6.2f} ms per frame")
# height for a given width and aspect ratio
w, (aw, ah) = 1280, (16, 9)
print("height for 1280 at 16:9 =", w * ah // aw)
# pixel density of a 27-inch 2560x1440 monitor and a 6.1-inch 2532x1170 phone
for name, px_w, px_h, inches in [("monitor", 2560, 1440, 27), ("phone", 2532, 1170, 6.1)]:
ppi = math.hypot(px_w, px_h) / inches
print(f"{name}: {ppi:.0f} PPI") 30 fps -> 33.33 ms per frame
60 fps -> 16.67 ms per frame
144 fps -> 6.94 ms per frame
240 fps -> 4.17 ms per frame
height for 1280 at 16:9 = 720
monitor: 109 PPI
phone: 457 PPI
math.hypot computes the diagonal with the Pythagorean theorem.
More rates you will meet:
| Rate | Unit | Where |
|---|---|---|
| bandwidth | megabits per second | networking, video streaming |
| throughput | requests per second | web servers, load tests |
| sample rate | samples per second (Hz) | audio: 44 100 or 48 000 Hz |
| tick rate | updates per second | game servers (e.g. 64 tick) |
| exchange rate | currency A per currency B | payment and travel apps |
Note: Watch the direction of a rate. 0.92 EUR per USD and 1.087 USD per EUR describe the same exchange rate; one is the reciprocal of the other.
Exercises
Exercise 1: Simplify
Simplify and .
Answer
and
Solution
Exercise 2: Share the prize
Three players share 360 coins in the ratio . How many coins does each get?
Answer
40, 120 and 200
Solution
parts; . Shares: 40, , . Check: .
Exercise 3: Better buy
Pack A: 6 batteries for 4.20. Pack B: 16 batteries for 10.40. Which is cheaper per battery?
Answer
Pack B: 0.65 per battery vs 0.70.
Solution
A: . B: . B is cheaper per battery.
Exercise 4: Letterboxing
A film is shown on a screen, filling the full width. Write Python that computes the film's height in pixels (rounded) and the height of each black bar above and below.
Answer
Film height 803 px; bars of about 138.5 px each (138 and 139 in practice).
Solution
, so 803 px. The leftover px is split into two bars.
width, height, aspect = 1920, 1080, 2.39
film_h = round(width / aspect)
bars = height - film_h
print("film height:", film_h, " bars:", bars // 2, "and", bars - bars // 2)film height: 803 bars: 138 and 139
Test yourself
In a class of 14 girls and 21 boys, what is the ratio of girls to boys in simplest form?
A game runs at 120 frames per second. How long is one frame?
- 8.33 ms
- 12 ms
- 0.12 s
- 120 ms
A image is 900 pixels tall. How wide is it?
- 675
- 1200
- 1600
- 1080