The Math Wizard loves precision. He doesn't like "half a potion" or "half a shelf".
Sometimes you need to round numbers down (floor) and sometimes up (ceil).
Floor and Ceiling
Imagine a room. The floor is at the bottom, the ceiling is at the top.
math.floor(3.9)->3(Rounds down)math.ceil(3.1)->4(Rounds up)
import math # Floor: Removing the decimal part (basically) print(math.floor(5.99)) # 5 # Ceil: Going to the next integer print(math.ceil(5.01)) # 6
1
Max Potions (Floor)
We have ingredients for 29 / 3 = 9.66 potions. Since we can't make 0.66 of a potion, we need to round down. Use math.floor().
2
Shelves Needed (Ceil)
If we have 8 potions and each shelf holds 4, we need 2 shelves. But what if we had 9 potions? Key math: max_potions / potions_per_shelf. Use math.ceil() to make sure we have enough space.
Why not just use round()? Because round(3.5) goes to 4, but round(3.4) goes to 3. Sometimes you forcing direction matters!
Suggested SolutionExpandCollapse
ExpandCollapse
Solution:
import math
# Step 1: Floor (Round down)
potions = 29 / 3
print("Potions we can make: " + str(math.floor(potions))) # 9
# Step 2: Ceil (Round up)
shelves_needed = 9 / 4
print("Shelves needed: " + str(math.ceil(shelves_needed))) # 3Advanced TipsWant more? Click to expandClick to collapse
F. Precision Matters
- In banking app, rounding wrong means losing money.
- In navigation app, rounding wrong means crashing into a wall.
floorandceilgive you control over the direction of the error.
Loading...
Terminal
Terminal
Ready to run...