Averages, Explained: The Math Behind Mean, Median, Mode & Weighted Averages
Every calculator that averages numbers runs the same three lines of math: add the values, count them, divide. The mean, the median, the mode, and the weighted average are four different answers to one question — "what is typical here?" — and building a calculator that gets them right means understanding exactly where each one breaks. Here is the math, with worked examples you can check by hand.
The Mean: Add, Count, Divide
The arithmetic mean is the sum of the values divided by how many there are. For test scores 78, 85, 85, 90, 92: the sum is 430, the count is 5, and 430 ÷ 5 = 86. That is the whole algorithm — and the full guide on how to calculate average walks through it step by step, including decimals and the two-number shortcut (the average of 40 and 60 is 50, exactly halfway between them).
One invariant every implementation should test: the mean always lies between the smallest and largest value. If your function returns 86 for inputs between 78 and 92, it is plausible; if it returns 95, something is wrong. Another: the sum of the differences from the mean is always zero — (78−86) + (85−86) + (85−86) + (90−86) + (92−86) = −8 −1 −1 +4 +6 = 0. That is the "balance point" property, and it makes a good unit test.
Median and Mode: Sort, Then Count
The median needs a sort: order the values and take the middle one (or the average of the two middle ones for an even count). For 78, 85, 85, 90, 92 the median is 85. The mode needs a frequency count: 85 appears twice, everything else once, so the mode is 85. The range is max − min: 92 − 78 = 14.
The interesting engineering question is which one your tool should surface. The answer: all of them, side by side — because they disagree exactly when the data is interesting. Take 10, 12, 13, 14, 101: the mean is 30, the median is 13, and there is no mode. One outlier drags the mean up by 17 points while the median barely notices. Salaries show the same effect at scale: 30, 32, 35, 38, 40, 250 (thousands) gives a mean of 70.83 and a median of 36.5. Any calculator that reports only the mean is hiding the story.
The Weighted Average: When Values Count Differently
A weighted average multiplies each value by its weight, adds the products, and divides by the total weight: Σ(value × weight) ÷ Σ(weights). For scores 90 (40%), 80 (30%), 70 (30%): 90 × 0.40 + 80 × 0.30 + 70 × 0.30 = 36 + 24 + 21 = 81. The plain average is 80 — wrong, because the 90 counted most.
The classic implementation bug is forgetting the final division: when weights are 3, 2, 1 (scores 90, 80, 70), the products sum to 500, and the answer is 500 ÷ 6 = 83.33 — not 500. And when weights are percentages that sum to 100%, the division is by 1, which tempts people to skip it; keep it in the code anyway, because a caller will eventually pass weights that do not sum to 1. The full walkthrough, with grade and investment examples, is in the guide to weighted average.
The Traps Worth Testing Against
Three cases break naive average implementations, and all three make good test fixtures:
- Percentages from different-sized groups. 90% of 100 students and 70% of 10 students do not average to 80%. Combine the totals: (90 + 7) ÷ 110 ≈ 88.18%. Any tool that averages percentages directly without asking about group sizes is wrong by construction.
- Two speeds. A car at 60 mph for 1 hour then 30 mph for 2 hours does not average 45 mph. Total distance (120 miles) ÷ total time (3 hours) = 40 mph. Averaging the speeds ignores how long each lasted.
- Equal distances, different speeds. Two equal halves at 20 and 60 km/h average to 2 × 20 × 60 ÷ 80 = 30 km/h — the harmonic-mean shortcut — not 40. The slow half takes three times longer.
The Bottom Line
Mean, median, mode, and weighted average are four small algorithms with four different failure modes: the mean is fooled by outliers, the median needs a sort, the mode needs a frequency count, and the weighted average needs the final division by the total weight. Build them together, show all four answers for one data set, and test against the fixtures above — 78, 85, 85, 90, 92 → mean 86, median 85, mode 85, range 14 — and your calculator will be right where it matters.
Top comments (0)