The math module in Python: round up, and solve for time
520.83 tees to break even. Is that 520 or 521? And how long until sales double at 15% a month? Both need a little more than basic arithmetic, and math supplies it.
The big idea
The math module provides precise mathematical functions: ceil, floor, sqrt, log, and more.
See it in code
Three ways to turn 520.83 into a whole number, and they don't agree:
import math
exact = 12500 / 24
print("round():", round(exact))
print("int():", int(exact))
print("math.floor():", math.floor(exact))
print("math.ceil():", math.ceil(exact))round(): 521 int(): 520 math.floor(): 520 math.ceil(): 521
round happens to give 521 here, but it would give 520 for 520.4. Only math.ceil always rounds up, and "at least enough to cover costs" always means up.
ceil stacks naturally. Round up the tees needed, then round up the months it takes to sell them:
import math
launch_costs = 12500
profit_per_tee = 24
monthly_sales = 300
tees_needed = math.ceil(launch_costs / profit_per_tee)
months = math.ceil(tees_needed / monthly_sales)
print("Tees to sell:", tees_needed)
print("Months to break even:", months)Tees to sell: 521 Months to break even: 2
521 tees at 300 a month is 1.74 months, which rounds up to 2. You aren't out of the red until the second month closes.
Logarithms answer "how long?" for anything that grows by a percentage. Compare the exact doubling time with the rule-of-72 shortcut:
import math
growth = 0.15
rule_of_72 = 72 / (growth * 100)
exact = math.log(2) / math.log(1 + growth)
print(f"Rule of 72 estimate: {rule_of_72:.2f} months")
print(f"Exact doubling time: {exact:.2f} months")Rule of 72 estimate: 4.80 months Exact doubling time: 4.96 months
The shortcut says 4.8 months; the exact figure is 4.96. The rule of 72 is a good mental estimate, and math.log is how you check it.
The same functions carry the sciences. sqrt gives a distance or a standard deviation, log linearizes anything exponential, and ceil and floor turn measurements into counts.
Try it yourself
Change growth to 0.05 and compare the two doubling times again. The rule of 72 fits better at low rates: about 1% off at 5%, against about 3% off at 15%. Then use math.sqrt(1000) to find the side of a square that holds 1,000 tees laid flat.
The common mistake
Using int() or round() where you need ceil. int(520.83) is 520, which leaves Evergreen $20 short of covering its costs. When the answer must be "enough," round up.
What it unlocks
math arrives through an import and extends the basic operators. For chance, not certainty, the companion module is random.