Why This Matters
Not everything divides evenly. When you split a pizza among three people, each person gets one-third. A fraction represents this idea: a part of a whole. The numerator tells you how many parts you have, and the denominator tells you how many equal parts make up the whole. Fractions are division frozen in place: 3/4 means "3 divided by 4" but expressed as a ratio rather than a decimal.
Fractions appear everywhere in computing. Probability is expressed as a fraction. Ratios describe aspect ratios in displays (16/9), compression ratios, and sampling rates. When you need exact arithmetic -- avoiding the rounding errors that plague floating-point decimals -- fractions are the answer.
Adding fractions requires a common denominator. You cannot add 1/3 and 1/4 directly because the pieces are different sizes. You must first convert them to equivalent fractions with the same denominator (4/12 and 3/12), then add the numerators (7/12). This process of finding common ground before combining is a pattern you will see again and again in mathematics and programming.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
Fractions are parts of a whole. To add fractions, find a common denominator, convert, then add numerators.
Code Example
// Representing fractions as [numerator, denominator]
const half = [1, 2];
const third = [1, 3];
// Evaluate a fraction as a decimal
function toDecimal(frac) {
return frac[0] / frac[1];
}
console.log(toDecimal(half)); // 0.5
console.log(toDecimal(third)); // 0.333...
// Greatest Common Divisor (for simplifying)
function gcd(a, b) {
a = Math.abs(a);
b = Math.abs(b);
while (b !== 0) {
[a, b] = [b, a % b];
}
return a;
}
// Simplify a fraction
function simplify(frac) {
const divisor = gcd(frac[0], frac[1]);
return [frac[0] / divisor, frac[1] / divisor];
}
console.log(simplify([4, 8])); // [1, 2]
console.log(simplify([6, 9])); // [2, 3]
// Add two fractions
function addFractions(f1, f2) {
const num = f1[0] * f2[1] + f2[0] * f1[1];
const den = f1[1] * f2[1];
return simplify([num, den]);
}
console.log(addFractions([1, 3], [1, 4])); // [7, 12]
console.log(addFractions([1, 2], [1, 2])); // [1, 1]Interactive Experiment
Try these exercises:
- Simplify 12/18 by hand. What is the GCD of 12 and 18? Verify with the
simplifyfunction. - Add 2/5 and 3/7. What common denominator do you use? Check your answer with the
addFractionsfunction. - What is 1/2 + 1/3 + 1/6? Add them step by step, or find the LCD of all three at once.
- Express 0.75 as a fraction. Then simplify it. What do you get?
- What fraction is equivalent to 3/5 with a denominator of 100? This connects fractions to percentages.
Quick Quiz
Coding Challenge
Write a function called `addAndSimplify` that takes four integers: numerator1, denominator1, numerator2, denominator2. It should add the two fractions and return the result as a simplified string in the format 'numerator/denominator'. For example, addAndSimplify(1, 3, 1, 6) should return '1/2' because 1/3 + 1/6 = 2/6 + 1/6 = 3/6 = 1/2. You will need to implement GCD to simplify.
Real-World Usage
Fractions power precise computation across many domains:
- Exact arithmetic: Financial calculations use fractions to avoid rounding errors. One-third of a dollar is exactly 1/3, not 0.333333.
- Music and rhythm: Time signatures are fractions. A measure in 3/4 time has three quarter-note beats. Subdivisions of rhythm are all fractional.
- Probability: The probability of rolling a 6 on a fair die is 1/6. Combining probabilities requires fraction addition and multiplication.
- Screen ratios: Display aspect ratios (16/9, 4/3, 21/9) are fractions. Calculating how content fits a screen uses fraction arithmetic.
- Cooking and recipes: Scaling recipes up or down means multiplying fractions. Half of 3/4 cup is 3/8 cup.