Caffeine Calculator
Track daily caffeine intake from beverages and food against the 400mg recommended limit.
Frequently Asked Questions
Code Implementation
def caffeine_remaining(initial_mg: float, elapsed_hours: float, half_life_hours: float = 5.5) -> float:
"""
Calculate remaining caffeine using half-life decay formula.
C(t) = C0 * (0.5)^(t / tΒ½)
Default half-life: 5.5 hours (adult average).
"""
return initial_mg * (0.5 ** (elapsed_hours / half_life_hours))
def hours_until_threshold(initial_mg: float, threshold_mg: float, half_life: float = 5.5) -> float:
"""
Calculate hours until caffeine drops below threshold.
t = tΒ½ * log2(C0 / threshold)
"""
import math
if threshold_mg <= 0 or initial_mg <= threshold_mg:
return 0.0
return half_life * math.log2(initial_mg / threshold_mg)
# Example: Morning coffee at 07:00
initial_mg = 200 # large drip coffee
half_life = 5.5
print("Caffeine decay after a 200 mg coffee:")
print(f"{'Time':>8} {'Remaining (mg)':>16} {'% Left':>8}")
print("-" * 38)
for h in range(0, 14):
remaining = caffeine_remaining(initial_mg, h, half_life)
pct = remaining / initial_mg * 100
print(f"{h:>7}h {remaining:>14.1f} {pct:>7.1f}%")
# Bedtime estimate
bedtime_h = hours_until_threshold(initial_mg, 25) # <25 mg feels negligible
print(f"\nTime until <25 mg remains: {bedtime_h:.1f} hours after intake")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.