Mixed Numbers
A mixed number combines a whole number and a proper fraction, like ("two and three quarters"). This lesson converts between mixed numbers and improper fractions, connects the conversion to division with remainder, and shows how to do it in Python with divmod.
What a mixed number means
The mixed number means 2 wholes plus more:
Recipes, tape measures and timetables use mixed numbers because they are easy to picture: " cups of flour" is clearer than " cups". For calculations, though, improper fractions are easier, so you will convert back and forth a lot.
Mixed number → improper fraction
Multiply the whole part by the denominator, add the numerator, and keep the denominator:
Why: 2 wholes are quarters, plus 3 quarters makes 11 quarters.
Improper fraction → mixed number
Divide the numerator by the denominator. The quotient is the whole part, the remainder is the new numerator:
That is division with remainder, and Python's divmod gives you both parts at once:
def to_mixed(top, bottom):
whole, rest = divmod(top, bottom)
if rest == 0:
return f"{whole}"
return f"{whole} {rest}/{bottom}" if whole else f"{rest}/{bottom}"
def to_improper(whole, top, bottom):
return whole * bottom + top, bottom
for top, bottom in [(17, 5), (22, 7), (12, 4), (3, 8)]:
print(f"{top}/{bottom} = {to_mixed(top, bottom)}")
print("2 3/4 =", "%d/%d" % to_improper(2, 3, 4))
print("5 1/3 =", "%d/%d" % to_improper(5, 1, 3))17/5 = 3 2/5
22/7 = 3 1/7
12/4 = 3
3/8 = 3/8
2 3/4 = 11/4
5 1/3 = 16/3
Tip: Simplify the fraction part when you can: . Simplifying first () gives the same .
Negative mixed numbers
A negative mixed number like means . The minus sign applies to the whole thing, not only to the 2. This is a classic trap when converting by hand, and also in code: Python's divmod rounds toward minus infinity, so divmod(-9, 4) gives (-3, 3), which means . That is the same number, but not the form people write.
from fractions import Fraction
def mixed(fr):
sign = "-" if fr < 0 else ""
whole, rest = divmod(abs(fr.numerator), fr.denominator)
return f"{sign}{whole} {rest}/{fr.denominator}"
x = Fraction(-9, 4)
print("divmod(-9, 4) =", divmod(-9, 4))
print("human form:", mixed(x))
print("check:", -(2 + Fraction(1, 4)) == x)divmod(-9, 4) = (-3, 3)
human form: -2 1/4
check: True
Working with the absolute value and putting the sign back at the end gives the usual written form.
Mixed numbers in measurements
US customary measurements are full of mixed numbers. A carpentry helper might turn a decimal length in inches into the nearest sixteenth:
from fractions import Fraction
def to_sixteenths(inches):
f = Fraction(round(inches * 16), 16) # nearest 1/16 inch
whole, rest = divmod(f.numerator, f.denominator)
part = Fraction(rest, f.denominator)
return f"{whole} {part}\"" if part else f"{whole}\""
for length in [2.75, 5.3125, 7.9, 3.0]:
print(length, "->", to_sixteenths(length))2.75 -> 2 3/4"
5.3125 -> 5 5/16"
7.9 -> 7 7/8"
3.0 -> 3"
, which rounds to inches. Fraction simplified for us.
In software
- Time is the everyday mixed number: 135 minutes is
divmod(135, 60) = (2, 15), so 2 h 15 min. The same pattern formats durations, file sizes ("3 GB 512 MB") and pagination (item 47 with 10 per page is on page 4, position 7). - Recipe and DIY apps display mixed numbers (" cups") but compute with improper fractions internally.
Exercises
Exercise 1: To improper
Write and as improper fractions.
Answer
and .
Solution
Exercise 2: To mixed
Write and as mixed numbers in lowest terms.
Answer
and .
Solution
Exercise 3: Minutes to hours
A film lasts 167 minutes. Write its length as a mixed number of hours in lowest terms, and as "h min".
Answer
hours, which is 2 h 47 min.
Solution
remainder 47, so . 47 and 60 share no factor (47 is prime), so it is in lowest terms. The remainder is the minutes: 2 h 47 min.
h, m = divmod(167, 60)
print(f"{h} {m}/60 hours = {h} h {m} min")2 47/60 hours = 2 h 47 min
Exercise 4: A mixed-number formatter
Write fmt(fr) that turns any Fraction into a human mixed number: Fraction(-17, 6) → -2 5/6, Fraction(8, 4) → 2, Fraction(3, 7) → 3/7.
Answer
-2 5/6, 2, 3/7.
Solution
Work on the absolute value so divmod behaves, drop the zero parts, then put the sign back.
from fractions import Fraction
def fmt(fr):
sign = "-" if fr < 0 else ""
whole, rest = divmod(abs(fr.numerator), fr.denominator)
if rest == 0:
return f"{sign}{whole}"
if whole == 0:
return f"{sign}{rest}/{fr.denominator}"
return f"{sign}{whole} {rest}/{fr.denominator}"
for f in [Fraction(-17, 6), Fraction(8, 4), Fraction(3, 7), Fraction(-1, 2)]:
print(fmt(f))-2 5/6
2
3/7
-1/2
Test yourself
What is as an improper fraction?
What is as a mixed number?
Which improper fraction equals ?