A decimal is a fraction written in place value: 0.75 is 75 hundredths, the same as 3/4. Almost every number inside an AI model is a decimal, and almost every number in a report is rounded. This lesson shows what the digits after the point mean, how to turn fractions into decimals, how to round honestly, and one trap that catches every programmer: 0.1 + 0.2 isn't exactly 0.3 on a computer.
What you'll be able to do
Read decimals as place value: tenths, hundredths, thousandths
Turn a fraction into a decimal by dividing, and spot a repeating decimal
Round to decimal places and to significant figures, by hand and with round() and :.2f
Compare decimals safely in Python, and scale pixel values to between 0 and 1
Quick check
Have a guess before you start. Which is bigger, 0.5 or 0.45?
0.5
0.45
They're equal
It can't be told
Show the answer
0.5
0.5 is 5 tenths, which is 50 hundredths. 0.45 is 45 hundredths. 50 is more than 45, so 0.5 is bigger, even though 45 looks like the bigger number.
The math: place value past the point
Purpose. Whole numbers stop at the ones place. Decimals keep going to the right, into places smaller than one, so you can write any part of a whole in the same digit system.
Each place to the right of the point is worth a tenth of the place before it:
ones
.
tenths
hundredths
thousandths
0
.
3
7
5
So 0.375 means 3 tenths, 7 hundredths and 5 thousandths: 375 thousandths, or 375/1000.
Zooming into the number line helps. Between 0 and 1 there are ten tenths. Between 0.3 and 0.4 there are ten hundredths. Each digit you add zooms in ten times farther.
To compare decimals, line up the points and compare place by place from the left, filling empty places with zeros: 0.5 is 0.50, so it beats 0.45.
Fraction to decimal: divide the top by the bottom. Watch 3/8 become a decimal, one place at a time. The number line in the picture does the zooming for you: each digit lights the gap 3/8 falls in, and the next digit zooms into that gap, ten times closer.
Try it now
Each step turns what's left over into ten times as many of the next smaller place, then sees how many eighths fit. Then set the fraction to 1/3 and press Use these numbers: the same left-over keeps coming back, and 1/3 never lands on a tick.
Symbols and formula. Changing between the two forms:
ba=a÷b0.375=1000375=83
In plain words: to turn a fraction into a decimal, divide. To turn a decimal into a fraction, read its last place (thousandths here), put the digits over that, then simplify.
Some fractions never stop. 1/3 = 0.3333…, with the 3s going on forever. That's a repeating decimal, and it happens whenever the same left-over keeps coming back.
Rounding to decimal places. To round 3.14159 to 2 places, look at the next digit, the third: it's 1, which is under 5, so leave the second digit alone: 3.14. Rounding 2.678 to 1 place: the next digit is 7, which is 5 or more, so round up: 2.7.
On the number line, rounding means moving to the nearest tick. Rounding to 1 place uses the tenths ticks; rounding to 2 places zooms in to the hundredths ticks. The next digit only tells you which half of the gap you're in. See both examples on the line.
Significant figures count digits from the first one that isn't zero. 0.004567 to 2 significant figures is 0.0046: the leading zeros only hold places, so the first two "real" digits are 4 and 5, and the 6 after them rounds the 5 up.
Quick check
What is 0.6 as a fraction in simplest form?
1/6
6/10
6/100
3/5
Show the answer
3/5
0.6 is 6 tenths, 6/10, which simplifies by 2 to 3/5.
Quick check
Round 7.2851 to 2 decimal places.
7.28
7.285
7.29
7.3
Show the answer
7.29
The third decimal is 5, so the second (8) rounds up to 9: 7.29.
Repeating decimals, written short. 1/6 = 0.1666…, where only the 6 repeats. 2/11 = 0.181818…, where the pair 18 repeats. A bar over the repeating part means "forever" without the dots:
61=0.16112=0.18
In plain words: the bar marks the digits that keep repeating. Everything before the bar happens once.
You can tell ahead of time whether a fraction will stop. Simplify it, then break its bottom into factors. If the only factors are 2s and 5s, the decimal stops: 8 is 2 × 2 × 2, so 3/8 stops. Any other factor makes it repeat: 6 is 2 × 3, so 1/6 repeats. The reason is that every place, tenths, hundredths and on, is built from 10, and 10 is 2 × 5.
In your own words
In your own words: what makes a decimal repeat forever instead of stopping?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: floats, rounding and a surprise
Purpose. Python works in decimals all the time. They're called floats, and you've met them already: / always gives one.
It runs right here in your browser, with nothing to install.
round(x, n) rounds x to n decimal places. To show a number rounded without changing it, use a format inside an f-string: :.2f means "2 decimal places, written as a fixed decimal".
loss = 0.4183729print(f"loss {loss:.2f}")
print(f"loss {loss:.4f}")
Now the surprise. Computers store floats in binary, base two, where most decimals repeat forever, like 1/3 does in base ten. So they're stored a hair off:
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
Step through the same sum one line at a time. Watch the variables: each name gets its box when its line runs, and the box for total shows exactly what the computer stored.
A named mistake: testing decimals with ==. Because floats are stored a hair off, two calculations that should agree can differ in the last digit, so == says False. Rounding first only hides the problem: two results a hair apart can still round to different digits. The safe way is to ask whether they're close:
math.isclose(a, b) is True when a and b agree to about nine significant figures, which is plenty for everyday numbers. Use it whenever you compare results that came from decimal arithmetic.
Worked example
Worked example: a tidy table of results
Three runs of the sign reader gave long decimal accuracies. Report each to 3 decimal places, then check whether runs 1 and 3 agree.
The :.3f format shows each accuracy to 3 places: 0.913, 0.887, 0.913. Formatting only changes how the number is shown; the stored value keeps all its digits. Runs 1 and 3 differ only in the tenth decimal place, so == says False while math.isclose says True. For results that come out of decimal arithmetic, "close" is almost always the question you mean to ask.
Quick check
What does this print?
x = 2.71828print(round(x, 2), f"{x:.1f}")
2.72 2.7
2.71 2.7
2.72 2.71828
2.7 2.72
Show the answer
2.72 2.7
round(x, 2) rounds the 1 up because the next digit is 8: 2.72. The :.1f format shows one place: 2.7.
Quick check
Which line is the safe way to check that a calculated total equals 0.3?
round(total) == 0.3
total == 0.3
math.isclose(total, 0.3)
total = 0.3
Show the answer
math.isclose(total, 0.3)
isclose allows for the tiny storage error in floats, so 0.1 + 0.2 counts as 0.3.
Drill
Report results honestly
Set pct to share as a percentage rounded to 1 decimal place (use round), shown to the text of loss with 3 decimal places (an f-string with :.3f), and agree to whether a and b are close.
import math
share = 0.83749
loss = 0.0619483
a = 0.1 + 0.7
b = 0.8
pct = round(share, 1)
shown = f"{loss}"
agree = a == b
print(pct, shown, agree)
In the lesson, your code is checked as you write it.
One more surprise: ties. By hand, a 5 rounds up. Python's round does something different when a number sits exactly halfway: it picks the even neighbor.
This is called round half to even. In a long table, always rounding halves up nudges the total upward a little each time. Rounding to the even neighbor sends half the ties up and half down, so the errors cancel out. It rarely changes a result, but now it won't surprise you.
In your own words
Why does 0.1 + 0.2 == 0.3 print False?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: decimals everywhere, rounded for people
Purpose. Inside a model, nearly every number is a float. And when people read results, those numbers are rounded.
Two everyday cases from the sign reader.
Scaling pixels. A gray pixel is a whole number from 0 to 255. Models learn better from small numbers, so pixels are usually scaled to between 0 and 1 by dividing by 255:
scaled=255pixel
In plain words: divide each pixel by the biggest possible value, so black is 0, white is 1, and everything else is a decimal between.
The last line scales the four gray levels in a 5 × 5 patch of an arrow sign, cut around the arrow's tip. Here is that patch, first as stored, then scaled. The colors show the size of each number, so the brightest pixels, 255, which are white on the sign, get the deepest color.
Quick check
What does this print?
pixel = 51print(round(pixel / 255, 2))
0.51
0.2
0.20
5.0
Show the answer
0.2
51 / 255 is exactly 0.2 (51 is a fifth of 255). round keeps it as 0.2; Python drops a trailing zero when printing a float.
Reporting losses. Training prints a loss, a number for how wrong the model is, usually to 4 places. Two runs whose losses differ only in the fifth place are, for practical purposes, the same: the difference is noise, not progress.
Quick check
Two training runs end with losses 0.41837 and 0.41842. What's the honest report?
Run 1 is better, by 0.00005
Run 2 is worse, since its loss is higher
Both about 0.418, the same
They can't be compared without more runs
Show the answer
Both about 0.418, the same
They agree to 3 significant figures, 0.418. A difference in the fifth place is far smaller than run-to-run noise.
Model outputs are decimals too. When the sign reader looks at a picture, it gives each kind of sign a decimal between 0 and 1. Say it gives 0.97 to stop, 0.02 to yield and 0.01 to speed limit. These add up to 1, so they share out one whole, like slices of a cake: the model is very sure it's a stop sign.
Checks like this appear in real code, and they use isclose. A sum of many floats can land a hair away from 1, and == would wrongly report a problem.
In the real tools. In PyTorch, the library most models are built with, a whole row of pixels is scaled in one line, and torch.isclose is its math.isclose. Here are the patch's four gray levels in one tensor:
Drill
Scale a row of pixels
Another row of a sign has pixels 0, 64, 128 and 255. Set s0 to s3 to each pixel divided by 255, rounded to 2 places, and brightest to the scaled value of 255.
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