Adding and Subtracting Fractions
You can only add parts of the same size, so adding fractions is all about finding a common denominator. This lesson covers like denominators, unlike denominators with the least common denominator, mixed numbers, and exact sums in Python with Fraction.
Same denominator
When the parts are the same size, just count them. Add (or subtract) the numerators and keep the denominator:
Note: Never add the denominators. is , not . Two halves make a whole, not a half.
Different denominators
adds thirds and quarters, which are different sizes. First rewrite both as equivalent fractions with a common denominator. The smallest one is the least common denominator (LCD), which is the LCM of the denominators.
A worked subtraction with a less obvious LCD:
You may also use the product of the denominators () as a common denominator. It always works, but the numbers get bigger and you must simplify more at the end: .
| Problem | LCD | Rewritten | Result |
|---|---|---|---|
| 6 | |||
| 12 | |||
| 10 | |||
| 24 |
The general formula
For any :
This is what a program does, and simplifying with the GCD keeps the numbers small:
import math
def add(a, b, c, d):
top = a * d + c * b
bottom = b * d
g = math.gcd(top, bottom)
return top // g, bottom // g
print("1/3 + 1/4 =", add(1, 3, 1, 4))
print("5/6 - 3/8 =", add(5, 6, -3, 8))
print("3/4 + 1/4 =", add(3, 4, 1, 4))1/3 + 1/4 = (7, 12)
5/6 - 3/8 = (11, 24)
3/4 + 1/4 = (1, 1)
Mixed numbers
Turn mixed numbers into improper fractions first; that avoids "borrowing" mistakes:
With Fraction
Fraction finds the common denominator and simplifies for you. A recipe needs cup of milk, cup of water and cups of stock; how much liquid is that, and how much is left from a 4-cup jug?
from fractions import Fraction as F
liquids = [F(3, 4), F(2, 3), 1 + F(1, 2)]
total = sum(liquids)
print("total:", total, "cups =", float(total))
print("left in a 4-cup jug:", 4 - total)
# three tenths: exact with Fraction, slightly off with float
print(F(1, 10) + F(1, 10) + F(1, 10), 0.1 + 0.1 + 0.1)total: 35/12 cups = 2.9166666666666665
left in a 4-cup jug: 13/12
3/10 0.30000000000000004
is cups, and cups are left. The last line shows why exact fractions matter: adding three times as a float does not give exactly (more in Floating Point).
Many small fractions
A sum like gets closer and closer to 1 without reaching it. With Fraction you can watch the exact gap shrink:
from fractions import Fraction as F
total = F(0)
for k in range(1, 9):
total += F(1, 2**k)
print(f"after {k} terms: {total} gap to 1: {1 - total}")after 1 terms: 1/2 gap to 1: 1/2
after 2 terms: 3/4 gap to 1: 1/4
after 3 terms: 7/8 gap to 1: 1/8
after 4 terms: 15/16 gap to 1: 1/16
after 5 terms: 31/32 gap to 1: 1/32
after 6 terms: 63/64 gap to 1: 1/64
after 7 terms: 127/128 gap to 1: 1/128
after 8 terms: 255/256 gap to 1: 1/256
Each step halves the gap. You will meet sums like this again as geometric series in calculus.
In software
- Music timing. A DAW checks that the note lengths in a bar add up exactly: . With floats, triplet lengths like of a bar would leave tiny errors that accumulate.
- Animation timelines add fractional durations of frames at rates like 24 or fps; keeping them as fractions avoids audio/video drift.
- Probability: the chances of separate outcomes add (see First Probability), and exact fractions make tests easy to write.
Exercises
Exercise 1: Like denominators
Compute and write the answer as a mixed number in lowest terms.
Answer
Solution
Exercise 2: Unlike denominators
Compute .
Answer
Solution
Exercise 3: Mixed-number subtraction
A plank is feet long. You cut off feet. How long is the rest?
Answer
feet
Solution
from fractions import Fraction as F
print(F(21, 4) - F(8, 3))31/12
Exercise 4: Does the bar add up?
A bar in 4/4 time holds notes of length , , , then an eighth-note triplet (three notes of each) and one more note. Using Fraction, find the length of the last note so the bar adds up to exactly 1.
Answer
(a quarter note)
Solution
from fractions import Fraction as F
notes = [F(1, 4), F(1, 8), F(1, 8), F(1, 12), F(1, 12), F(1, 12)]
print("so far:", sum(notes), " missing:", 1 - sum(notes))so far: 3/4 missing: 1/4
Test yourself
What is ?
What is the least common denominator of and ?
- 54
- 3
- 18
- 15
What is ?