FAILURE MAP
← Case archive

FA-97286 / Recipe scaling and nutrition / Open access

Macronutrient target splitter: carb from remainder · case 01

Totals drift a few calories from the target because fat rounding is not absorbed.

Verified by executionVariant 1 · 8 checks per implementationDownload source bundle ↓JSON ↗

ROOT CAUSE

Carbs are computed from the ratio independently rather than from energy left after rounded fat.

THE FAILURE

Carbs are computed from the ratio independently rather than from energy left after rounded fat.

Unsuccessful approach: Subtracting 4 kcal per fat gram leaves too much energy for carbs.

Case contract

split = [protein%, carb%, fat%] must sum to 100 else "error: split". protein_g = max(half-up kcal*p/400, ceiling(1.6*weight_kg)). Remaining kcal = kcal - 4*protein_g is divided between carb and fat in the ratio c:f; fat_g = half-up(rem*f/(c+f)/9); carb_g = half-up((rem - 9*fat_g)/4). Return [protein_g, carb_g, fat_g].

Why this case matters

Meal-plan generators turn calorie targets into gram targets with a protein floor by body weight.

1 / The failure

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(kcal, weight_kg, split):
    p, c, f = split
    if p + c + f != 100:
        return 'error: split'
    def r(x):
        return math.floor(x + Fraction(1, 2))
    protein_g = max(r(Fraction(kcal * p, 400)), math.ceil(Fraction(16, 10) * weight_kg))
    rem = kcal - 4 * protein_g
    fat_g = r(Fraction(rem * f, c + f) / 9)
    carb_g = r(Fraction(rem * c, c + f) / 4)
    return [protein_g, carb_g, fat_g]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['balanced', [2000, 70, [30, 40, 30]], [150, 199, 67]],
  ['protein floor binds', [1500, 120, [15, 55, 30]], [192, 118, 29]],
  ['bad split', [2000, 70, [30, 40, 31]], 'error: split'],
  ['regression: carb from remainder', [2500, 83, [25, 45, 30]], [156, 282, 83]],
  ['repair check: carb from remainder', [1800, 100, [40, 30, 30]], [180, 135, 60]],
  ['generated control 1', [3000, 91, [30, 40, 29]], 'error: split'],
  ['generated control 2', [1800, 118, [30, 40, 29]], 'error: split'],
  ['generated control 3', [2000, 60, [20, 50, 25]], 'error: split']],
 [['protein floor binds', [1500, 120, [15, 55, 30]], [192, 118, 29]],
  ['bad split', [2000, 70, [30, 40, 31]], 'error: split'],
  ['split under 100', [2000, 70, [30, 40, 20]], 'error: split'],
  ['regression: carb from remainder', [2000, 120, [15, 55, 30]], [192, 200, 48]],
  ['repair check: carb from remainder', [2500, 60, [40, 30, 30]], [250, 188, 83]],
  ['generated control 1', [2500, 120, [35, 35, 30]], [219, 219, 83]],
  ['generated control 2', [2200, 60, [10, 60, 30]], [96, 303, 67]],
  ['generated control 3', [2200, 120, [30, 40, 30]], [192, 205, 68]]],
 [['bad split', [2000, 70, [30, 40, 31]], 'error: split'],
  ['split under 100', [2000, 70, [30, 40, 20]], 'error: split'],
  ['fractional floor', [1200, 83, [20, 50, 30]], [133, 104, 28]],
  ['regression: carb from remainder', [2500, 60, [10, 60, 30]], [96, 354, 78]],
  ['repair check: carb from remainder', [2500, 83, [35, 35, 30]], [219, 219, 83]],
  ['generated control 1', [3000, 60, [15, 55, 30]], [113, 412, 100]],
  ['generated control 2', [2500, 100, [30, 40, 31]], 'error: split'],
  ['generated control 3', [2000, 118, [20, 50, 25]], 'error: split']],
 [['split under 100', [2000, 70, [30, 40, 20]], 'error: split'],
  ['fractional floor', [1200, 83, [20, 50, 30]], [133, 104, 28]],
  ['balanced', [2000, 70, [30, 40, 30]], [150, 199, 67]],
  ['regression: carb from remainder', [2000, 72, [30, 40, 30]], [150, 199, 67]],
  ['repair check: carb from remainder', [1500, 120, [40, 30, 30]], [192, 91, 41]],
  ['generated control 1', [1800, 72, [15, 55, 30]], [116, 217, 52]],
  ['generated control 2', [2000, 91, [30, 40, 31]], 'error: split'],
  ['generated control 3', [2200, 100, [10, 60, 30]], [160, 260, 58]]],
 [['fractional floor', [1200, 83, [20, 50, 30]], [133, 104, 28]],
  ['balanced', [2000, 70, [30, 40, 30]], [150, 199, 67]],
  ['protein floor binds', [1500, 120, [15, 55, 30]], [192, 118, 29]],
  ['regression: carb from remainder', [1800, 100, [20, 50, 30]], [160, 182, 48]],
  ['repair check: carb from remainder', [1500, 118, [20, 50, 30]], [189, 116, 31]],
  ['generated control 1', [2200, 100, [20, 50, 25]], 'error: split'],
  ['generated control 2', [3000, 91, [35, 35, 30]], [263, 262, 100]],
  ['generated control 3', [2500, 72, [25, 45, 30]], [156, 282, 83]]]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
balanced[150, 200, 67][150, 199, 67]Failed
protein floor binds[192, 118, 29][192, 118, 29]Passed
bad spliterror: spliterror: splitPassed
regression: carb from remainder[156, 281, 83][156, 282, 83]Failed
repair check: carb from remainder[180, 135, 60][180, 135, 60]Passed
generated control 1error: spliterror: splitPassed
generated control 2error: spliterror: splitPassed
generated control 3error: spliterror: splitPassed

SHA-256 / 0eaaa659ac9561d8a8f9abf6c255c63ee78d4ea8b0173b628c47434a2f150192

2 / The unsuccessful fix

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(kcal, weight_kg, split):
    p, c, f = split
    if p + c + f != 100:
        return 'error: split'
    def r(x):
        return math.floor(x + Fraction(1, 2))
    protein_g = max(r(Fraction(kcal * p, 400)), math.ceil(Fraction(16, 10) * weight_kg))
    rem = kcal - 4 * protein_g
    fat_g = r(Fraction(rem * f, c + f) / 9)
    carb_g = r(Fraction(rem - 4 * fat_g, 4))
    return [protein_g, carb_g, fat_g]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['balanced', [2000, 70, [30, 40, 30]], [150, 199, 67]],
  ['protein floor binds', [1500, 120, [15, 55, 30]], [192, 118, 29]],
  ['bad split', [2000, 70, [30, 40, 31]], 'error: split'],
  ['regression: carb from remainder', [2500, 83, [25, 45, 30]], [156, 282, 83]],
  ['repair check: carb from remainder', [1800, 100, [40, 30, 30]], [180, 135, 60]],
  ['generated control 1', [3000, 91, [30, 40, 29]], 'error: split'],
  ['generated control 2', [1800, 118, [30, 40, 29]], 'error: split'],
  ['generated control 3', [2000, 60, [20, 50, 25]], 'error: split']],
 [['protein floor binds', [1500, 120, [15, 55, 30]], [192, 118, 29]],
  ['bad split', [2000, 70, [30, 40, 31]], 'error: split'],
  ['split under 100', [2000, 70, [30, 40, 20]], 'error: split'],
  ['regression: carb from remainder', [2000, 120, [15, 55, 30]], [192, 200, 48]],
  ['repair check: carb from remainder', [2500, 60, [40, 30, 30]], [250, 188, 83]],
  ['generated control 1', [2500, 120, [35, 35, 30]], [219, 219, 83]],
  ['generated control 2', [2200, 60, [10, 60, 30]], [96, 303, 67]],
  ['generated control 3', [2200, 120, [30, 40, 30]], [192, 205, 68]]],
 [['bad split', [2000, 70, [30, 40, 31]], 'error: split'],
  ['split under 100', [2000, 70, [30, 40, 20]], 'error: split'],
  ['fractional floor', [1200, 83, [20, 50, 30]], [133, 104, 28]],
  ['regression: carb from remainder', [2500, 60, [10, 60, 30]], [96, 354, 78]],
  ['repair check: carb from remainder', [2500, 83, [35, 35, 30]], [219, 219, 83]],
  ['generated control 1', [3000, 60, [15, 55, 30]], [113, 412, 100]],
  ['generated control 2', [2500, 100, [30, 40, 31]], 'error: split'],
  ['generated control 3', [2000, 118, [20, 50, 25]], 'error: split']],
 [['split under 100', [2000, 70, [30, 40, 20]], 'error: split'],
  ['fractional floor', [1200, 83, [20, 50, 30]], [133, 104, 28]],
  ['balanced', [2000, 70, [30, 40, 30]], [150, 199, 67]],
  ['regression: carb from remainder', [2000, 72, [30, 40, 30]], [150, 199, 67]],
  ['repair check: carb from remainder', [1500, 120, [40, 30, 30]], [192, 91, 41]],
  ['generated control 1', [1800, 72, [15, 55, 30]], [116, 217, 52]],
  ['generated control 2', [2000, 91, [30, 40, 31]], 'error: split'],
  ['generated control 3', [2200, 100, [10, 60, 30]], [160, 260, 58]]],
 [['fractional floor', [1200, 83, [20, 50, 30]], [133, 104, 28]],
  ['balanced', [2000, 70, [30, 40, 30]], [150, 199, 67]],
  ['protein floor binds', [1500, 120, [15, 55, 30]], [192, 118, 29]],
  ['regression: carb from remainder', [1800, 100, [20, 50, 30]], [160, 182, 48]],
  ['repair check: carb from remainder', [1500, 118, [20, 50, 30]], [189, 116, 31]],
  ['generated control 1', [2200, 100, [20, 50, 25]], 'error: split'],
  ['generated control 2', [3000, 91, [35, 35, 30]], [263, 262, 100]],
  ['generated control 3', [2500, 72, [25, 45, 30]], [156, 282, 83]]]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
balanced[150, 283, 67][150, 199, 67]Failed
protein floor binds[192, 154, 29][192, 118, 29]Failed
bad spliterror: spliterror: splitPassed
regression: carb from remainder[156, 386, 83][156, 282, 83]Failed
repair check: carb from remainder[180, 210, 60][180, 135, 60]Failed
generated control 1error: spliterror: splitPassed
generated control 2error: spliterror: splitPassed
generated control 3error: spliterror: splitPassed

SHA-256 / 7a744a3753cd1ef5f299b67eebc5c52e0be4583511a6d8f5d291f94e5611c8a9

HELD IN THE MEMBER ARCHIVE

The verified repair and its recorded checks are member-only.

This mechanism has 8 recorded checks per implementation. The open-access tier publishes the failure and the unsuccessful fix; the repaired source that passes every check, and the observations that prove it, are available to members.

Every case sharing this mechanism uses the same contract and the same repair, so this one record is held back for all of them.

Member access is invitation-based. Sign in with your invited account to inspect the repair.

Sign in to the archive ↗

Verification & scope

A deterministic toy contract stated in full here; it is a bounded teaching model, not an authoritative reference or standards implementation. This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.

Observations recorded using Python 3.12.14 at 2026-09-29T14:52:30.723097+00:00.

Case digest / 181441ba20fbcd382b9571ebbf9033223ec3f5a6dd1fe27e1323280fc99a59c7