```
/** Exact per-reagent totals of a pour log, counted in thousandths. */
export function doseTotals(log: [string, string][]): Record<string, string> {
  const thousandths: Record<string, number> = {};
  const places: Record<string, number> = {};
  for (const [reagent, amount] of log) {
    const written = /^-?\d+(?:\.(\d{1,3}))?$/.exec(amount);
    if (written === null) {
      throw new Error("amount is not a decimal of at most three places: " + amount);
    }
    const digits = written[1] ?? "";
    const sign = amount.startsWith("-") ? -1 : 1;
    const bare = amount.replace("-", "").replace(".", "");
    thousandths[reagent] = (thousandths[reagent] ?? 0) + sign * Number(bare) * Math.pow(10, 3 - digits.length);
    places[reagent] = Math.max(places[reagent] ?? 0, digits.length);
  }
  const totals: Record<string, string> = {};
  for (const [reagent, total] of Object.entries(thousandths)) {
    const placeCount = places[reagent];
    if (placeCount === 0) {
      totals[reagent] = "" + Math.round(total);
    } else {
      totals[reagent] = `${total.toFixed(placeCount)}${placeCount > 1 ? "." : ""}`;
    }
  }
  return totals;
}
```