Half of a third. Three quarters of the signs, and two thirds of those. A share of a share comes up constantly, and it's a multiplication. In AI it's how chances combine: the chance of several things all happening is their chances multiplied, and it gets small fast. This lesson shows why multiplying fractions is easier than adding them, and what dividing by a fraction really asks.
What you'll be able to do
Read "a fraction of a fraction" as a multiplication, and multiply tops and bottoms
Divide by a fraction by multiplying by its flip
Multiply and divide Fractions in Python, and chain several shares
See why chances of things happening one after another multiply and shrink
Quick check
Have a guess before you start. What is half of a third of a trail bar?
1/6
1/5
5/6
2/3
Show the answer
1/6
Cut the bar into thirds, then cut one third in half: that piece is one sixth of the bar. Half of a third is 1/2 × 1/3 = 1/6.
The math: a share of a share
Purpose. "Of" turns up whenever you take a part of a part: half of a third, three quarters of the signs, two thirds of those. In math, "of" means multiply.
Picture a square trail map. Cut it into 3 columns and shade 2: that's 2/3 of the map. Now cut the same map into 4 rows, and keep 3 of the rows within the shaded columns. The piece that's shaded both ways is 3/4 of 2/3.
Count the small pieces: the map is now cut into 3 × 4 = 12 of them. The doubly shaded part is 2 × 3 = 6 of them. So 3/4 of 2/3 is 6/12, which is 1/2.
Notice what happened: the tops multiplied (2 × 3 = 6) and the bottoms multiplied (3 × 4 = 12). Unlike adding, there's no need to make the pieces the same size first. Cutting both ways makes the pieces for you.
The picture. The flow draws the map as a square, 1 wide and 1 tall. The first cut lights 2/3 of its width. The second keeps 3/4 of its height, and only the piece shaded both ways stays lit: a rectangle 2/3 wide and 3/4 tall. Under the picture, the number cells count its small pieces.
Try it now
The first cut makes 2/3. The second keeps 3 of 4 rows within it. Count the pieces shaded both ways. Then set the first fraction to 1/3 and the second to 1/2, and press Use these numbers: half of a third.
Symbols and formula.
ba×dc=b×da×c
In plain words: multiply the tops, multiply the bottoms. Then simplify.
Multiplying by less than one shrinks. Half of something is smaller than the thing. So 3/4 × 2/3 is smaller than both 3/4 and 2/3. This surprises people, because with whole numbers multiplying makes things bigger. With fractions under 1, it takes a part, so the answer is smaller.
A shortcut: cancel first. In 2/3 × 3/4, the 3 on top and the 3 on the bottom divide out before you multiply: 2/3 × 3/4 = 2/4 = 1/2. Any top can cancel with any bottom, which keeps the numbers small.
Whole numbers are fractions too. 5 is 5/1, so 5 × 2/3 = 10/3, which is 3 and 1/3.
Multiplying by 1/2 takes half: 2/3 × 1/2 = 1/3, smaller than 2/3. Multiplying by anything under 1 shrinks.
Dividing by a fraction. Dividing asks "how many of these fit in that?" How many quarters fit in 3? Each whole holds 4 quarters, so 3 wholes hold 12: 3 ÷ 1/4 = 12. Dividing by 1/4 gave the same answer as multiplying by 4.
That's the rule: to divide by a fraction, multiply by its flip. The flip of 1/4 is 4/1. The flip of 2/3 is 3/2.
ba÷dc=ba×cd
In plain words: turn the second fraction upside down, then multiply.
How many eighths fit in three quarters? 3/4 ÷ 1/8 = 3/4 × 8/1 = 24/4 = 6. Check with a picture: three quarters of a bar is six eighths of it.
In your own words
In your own words: why does dividing by 1/4 give the same answer as multiplying by 4?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: products and quotients that stay exact
Purpose. A chain of shares is easy to slip on by hand. Python checks it exactly.
First the rule written out: multiply the tops, multiply the bottoms.
a = 2
b = 3
c = 3
d = 4print(f"{a}/{b} x {c}/{d} = {a * c}/{b * d}")
It runs right here in your browser, with nothing to install.
Then with Fraction, which multiplies and divides exactly and simplifies for you:
from fractions import Fraction
print(Fraction(2, 3) * Fraction(3, 4))
print(Fraction(3, 4) / Fraction(1, 8)) # how many eighths in three quartersprint(5 * Fraction(2, 3)) # a whole number times a fraction
The same calculations in decimals give close, but not always exact, answers: 2/3 is 0.6666…, which a computer can't store completely.
print(2 / 3 * 3 / 4)
print(0.75 / 0.125)
Worked example
Worked example: a chain of shares
Three quarters of the trail club's signs are on the north loop. Two thirds of those are arrows. A fifth of those arrows are faded. What share of all the signs are faded north-loop arrows?
from fractions import Fraction
north = Fraction(3, 4)
arrows_of_north = Fraction(2, 3)
faded_of_those = Fraction(1, 5)
share = north * arrows_of_north * faded_of_those
print(share)
print(float(share))
print(share * 120) # out of 120 signs
In the lesson you can change this and run it.
Each "of those" multiplies: 3/4 × 2/3 × 1/5. The tops make 6, the bottoms 60, so the share is 6/60, which Fraction shows as 1/10. float(share) turns it into a decimal, 0.1, if you want one. Out of 120 signs, that's 12. Each share in the chain was large, but the product is small: three steps took it from 3/4 down to 1/10.
Code flow. Here is the same chain one share at a time. This time a single variable, share, starts as all the signs, 1/1, and each line takes a share of whatever it holds. Watch it shrink.
Mixed numbers. A fraction with a bigger top than bottom, like 10/3, is more than one whole. People often write it as a mixed number: a whole part and a fraction part, 3 and 1/3. To convert, divide the top by the bottom: the quotient is the whole part and the remainder is the new top. That's // and % again:
print(10 // 3, "and", 10 % 3, "/ 3") # 10/3 is 3 and 1/3print(3 * 3 + 1) # back again: 3 wholes of 3 thirds, plus 1
Before multiplying or dividing, turn mixed numbers back into one fraction (3 and 1/3 is 10/3). Multiplying the whole parts and the fraction parts separately gives the wrong answer.
1/2 × 4/5 = 4/10, shown simplified as 2/5. 1/2 ÷ 1/4 = 1/2 × 4 = 2: two quarters fit in a half.
Quick check
Which line works out how many eighths fit in three quarters?
Fraction(3, 4) / Fraction(1, 8)
Fraction(1, 8) / Fraction(3, 4)
Fraction(3, 4) * Fraction(1, 8)
Fraction(3, 4) - Fraction(1, 8)
Show the answer
Fraction(3, 4) / Fraction(1, 8)
"How many eighths fit in three quarters" is three quarters divided by an eighth: 6.
Drill
Scale a trail-mix recipe
A trail-mix recipe uses 3/4 of a cup of nuts and 2/3 of a cup of raisins for one batch. Set half_nuts to the nuts for half a batch, batches to how many half-cup scoops fit in the raisins for 3 batches, and nuts_per_raisin to nuts ÷ raisins for one batch. Use Fractions.
In the lesson, your code is checked as you write it.
In your own words
Why does Fraction give an exact answer where decimals sometimes don't?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: chances in a row multiply
Purpose. A chance is a fraction: how often something happens, out of all the times. When several things must all happen, one after another, their chances multiply, just like shares of shares.
Say the sign reader sees a sign clearly 3/4 of the time, and when it does see it clearly, it reads it right 2/3 of the time. The chance it sees a sign clearly and reads it right is:
P(clear and right)=43×32=21
In plain words: the chance of a chain of things all happening is their chances multiplied.
The picture. Add a step in front: before it can look, the reader has to find the sign in the photo, which it does 9 times in 10. A chance tree draws the three steps from left to right. Each step splits two ways, with its chance on each branch, and the two chances out of a step add to 1. Press Next to follow the path where all three go right.
Here is the path in code, one step at a time:
from fractions import Fraction
found = Fraction(9, 10)
clear = found * Fraction(3, 4) # found, then seen clearly
right = clear * Fraction(2, 3) # found, seen clearly, then read rightprint(found, clear, right)
print(float(right))
The chance shrinks at every step: 9/10, then 27/40, then 9/20, which is 0.45 as a decimal.
Every step in a chain is a fraction under 1, and multiplying by less than 1 shrinks. So long chains make small chances, even when each step is likely:
from fractions import Fraction
step = Fraction(9, 10) # each step goes right 9 times in 10print(step * step) # two stepsprint(step * step * step * step * step) # five stepsprint(float(step * step * step * step * step))
Five steps that each succeed 90% of the time succeed together only about 59% of the time. That's why a long chain of steps, like an AI agent doing a task in many moves, fails more often than any single step suggests.
3/4 × 2/3 = 6/12, shown simplified as 1/2: half of all the signs are sharp arrows.
Quick check
A task has 10 steps, each 95% reliable. What happens to the chance all 10 go right as you add steps?
It keeps shrinking
It stays at 95%
It drops to 0 after 10 steps
It grows toward 100%
Show the answer
It keeps shrinking
Each step multiplies by 19/20, which is under 1, so the chance shrinks with every step: after 10 steps it's about 60%.
In the real tools. In PyTorch, the library most models are built with, a model's chances sit in a tensor, a list of numbers PyTorch works on all at once. Here are the tree's three chances in one tensor, and the chance of the whole path:
Drill
A three-step chance
A second sign reader, still in training, finds the sign in the photo 4/5 of the time, crops it well 5/6 of the time after that, and reads a well-cropped sign right 9/10 of the time. Set all_right to the chance all three steps go right, and fails to the chance something goes wrong (1 minus that).
31Formulas with fractions: clear them firstFreeClearing denominators, cross-multiplying, a letter in the bottom, and reciprocal formulas like 1/R = 1/R₁ + 1/R₂.35 min
37Inverse functions, lines and scalingFreeUndoing a function by making x the subject, domains that allow it, line forms, and unscaling predictions.40 min
38Sequences and sums: find n, a, d and rFreeEvery letter of the arithmetic and geometric formulas as the subject, and an average's window from its decay.40 min
40Review: rearrange anything (algebra)FreeBroken rearrangements to fix, mixed practice from the whole algebra block, a strategy chart, and its formula sheet.35 min
41Coordinates and distanceFreePoints as (x, y), Pythagoras, distance in any number of coordinates, and nearest neighbors.42 min
42Angles, degrees and radiansFreeMeasuring a turn, converting units, arcs, and the angle between two arrows.36 min
43Sine and cosineFreeAcross and up: right triangles, the unit circle, and directions and rotations.42 min
44Waves: height, speed and shiftFreeAmplitude, period and phase, sampling a wave, and position codes in transformers.37 min
68Vector equations: rearranging with arrowsFreeSolving for a vector or a scale, recovering a gradient from two weight snapshots, and an angle from a dot product.40 min
87Counting: choices, orders and groupsFreeThe counting principle, factorials, ordered picks and groups, and why attention's cost grows with the square of the length.43 min
88Probability: from counts to chancesFreeEqually likely outcomes, the complement, distributions, seeded simulation, and a next-word model.43 min
89Combining events: and, or, independentFreeThe or rule, independence, probability trees, drawing with and without replacement, and chains of steps.42 min
100Maximum likelihoodFreeLikelihoods as products, log-likelihoods as sums, grid search, underflow, and the negative log-likelihood loss.45 min
101Linear regression: the best lineFreeResiduals, least squares by formula, three roads to one line, the matrix form, and train versus test error.45 min
109One neuron, start to finishFreeWeighted sum, activation, loss, slopes and update, by hand and in code, and what one straight line can and can't split.45 min
111Training a small networkFreeThe backward pass in matrix form, gradient checks, a full training loop, and reading loss curves.45 min
112The same network in PyTorchFreenn.Sequential with the NumPy weights loaded, the same loss and slopes, then SGD and mini-batches.45 min
113Attention as matrix mathFreeScaled dot-product scores, the causal mask, a row-by-row softmax, and blending value vectors.45 min
114Residual connections and layer normFreeAdding the input back and why its slope keeps learning alive, layer norm by hand, and add and norm.40 min
115A transformer block, start to finishFreeA sentence through embeddings, attention, add and norm, a small network and next-word scores, every shape named.45 min
117Final test: transpose any formulaFreeSlips to fix from every topic, mixed practice of every kind of rearranging, the strategy chart and the full sheet.45 min
Learn the math behind AI from arithmetic up: numbers, algebra, vectors and matrices, calculus, probability and statistics, each shown as numbers moving and checked in Python, ending with a network you build and train from the formulas.
See the course →
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write