FA-96986 / Recipe scaling and nutrition / Open access
Atwater energy calculator: available carb floor · case 01
Products whose fiber exceeds listed carbohydrate get negative carbohydrate energy.
ROOT CAUSE
Available carbohydrate is not clamped at zero.
VERIFIED REPAIR
Clamp available carbohydrate at zero.
Unsuccessful approach: abs() turns a 3 g deficit into 3 g of phantom available carbohydrate.
Case contract
Gram amounts as strings. Total carbohydrate includes fiber and polyols; available carb = max(0, carb - fiber - polyol). kcal = 4*protein + 9*fat + 4*available + 2*fiber + 7*alcohol + 2.4*polyol. Return [kcal, kJ, fat_energy_pct]: kcal half-up integer of the exact energy, kJ half-up integer of exact kcal * 4.184, fat pct = half-up of 9*fat/exact kcal*100 (0 if energy is 0).
Why this case matters
Recipe energy totals use factor systems where fiber and sugar alcohols are not full carbohydrate.
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(protein, fat, carb, fiber, alcohol, polyol):
p, fat, carb, fib, alc, poly = (Fraction(v) for v in (protein, fat, carb, fiber, alcohol, polyol))
avail = carb - fib - poly
avail = avail
exact = 4 * p + 9 * fat + 4 * avail + 2 * fib + 7 * alc + Fraction(12, 5) * poly
def r(x):
return math.floor(x + Fraction(1, 2))
kj = r(exact * Fraction(4184, 1000))
pct_fat = r(9 * fat * 100 / exact) if exact else 0
return [r(exact), kj, pct_fat]
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['all zero', ['0', '0', '0', '0', '0', '0'], [0, 0, 0]],
['fat only', ['0', '10', '0', '0', '0', '0'], [90, 377, 100]],
['fiber heavier than carb', ['5', '1', '3', '6', '0', '0'], [41, 172, 22]],
['regression: available carb floor', ['13', '12', '7', '3', '0', '10'], [190, 795, 57]],
['repair check: available carb floor', ['30', '35/4', '10', '3', '14', '10'], [327, 1367, 24]],
['generated control 1', ['23', '6', '35', '1', '5', '4'], [313, 1308, 17]],
['generated control 2', ['1', '24', '59', '0', '5', '0'], [491, 2054, 44]],
['generated control 3', ['18', '5/4', '27', '1', '0', '10'], [173, 725, 6]]],
[['fat only', ['0', '10', '0', '0', '0', '0'], [90, 377, 100]],
['fiber heavier than carb', ['5', '1', '3', '6', '0', '0'], [41, 172, 22]],
['sugar-free candy', ['0', '5', '30', '1', '0', '20'], [131, 548, 34]],
['regression: available carb floor', ['21', '28', '0', '5', '0', '0'], [346, 1448, 73]],
['repair check: available carb floor', ['5', '113/4', '13', '15', '0', '10'], [328, 1373, 77]],
['generated control 1', ['5', '13', '34', '5', '5', '0'], [298, 1247, 39]],
['generated control 2', ['0', '1', '23', '0', '5', '10'], [120, 502, 8]],
['generated control 3', ['25', '8', '50', '1', '0', '4'], [364, 1521, 20]]],
[['fiber heavier than carb', ['5', '1', '3', '6', '0', '0'], [41, 172, 22]],
['sugar-free candy', ['0', '5', '30', '1', '0', '20'], [131, 548, 34]],
['cocktail', ['0', '0', '12', '0', '14', '0'], [146, 611, 0]],
['regression: available carb floor', ['5', '10', '0', '5', '0', '4'], [130, 542, 69]],
['repair check: available carb floor', ['19', '10', '1', '2', '14', '0'], [268, 1121, 34]],
['generated control 1', ['16', '0', '32', '8', '5', '0'], [211, 883, 0]],
['generated control 2', ['7', '49/4', '9', '8', '0', '0'], [158, 662, 70]],
['generated control 3', ['20', '27', '39', '5', '14', '10'], [551, 2305, 44]]],
[['sugar-free candy', ['0', '5', '30', '1', '0', '20'], [131, 548, 34]],
['cocktail', ['0', '0', '12', '0', '14', '0'], [146, 611, 0]],
['all zero', ['0', '0', '0', '0', '0', '0'], [0, 0, 0]],
['regression: available carb floor', ['29', '25', '53', '55', '0', '4'], [461, 1927, 49]],
['repair check: available carb floor', ['20', '10', '43', '45', '5', '4'], [305, 1274, 30]],
['generated control 1', ['35', '4', '8', '2', '5', '4'], [233, 973, 15]],
['generated control 2', ['36', '49/4', '59', '5', '0', '0'], [480, 2009, 23]],
['generated control 3', ['3', '21', '12', '2', '0', '10'], [229, 958, 83]]],
[['cocktail', ['0', '0', '12', '0', '14', '0'], [146, 611, 0]],
['all zero', ['0', '0', '0', '0', '0', '0'], [0, 0, 0]],
['fat only', ['0', '10', '0', '0', '0', '0'], [90, 377, 100]],
['regression: available carb floor', ['23', '117/4', '16', '18', '5', '4'], [436, 1824, 60]],
['repair check: available carb floor', ['14', '47/2', '60', '62', '5', '0'], [427, 1784, 50]],
['generated control 1', ['27', '4', '59', '3', '5', '0'], [409, 1711, 9]],
['generated control 2', ['18', '8', '31', '8', '0', '10'], [236, 987, 31]],
['generated control 3', ['39', '21', '14', '16', '0', '0'], [377, 1577, 50]]]]
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 fixture | Actual | Expected | Outcome |
|---|---|---|---|
| all zero | [0, 0, 0] | [0, 0, 0] | Passed |
| fat only | [90, 377, 100] | [90, 377, 100] | Passed |
| fiber heavier than carb | [29, 121, 31] | [41, 172, 22] | Failed |
| regression: available carb floor | [166, 695, 65] | [190, 795, 57] | Failed |
| repair check: available carb floor | [315, 1317, 25] | [327, 1367, 24] | Failed |
| generated control 1 | [313, 1308, 17] | [313, 1308, 17] | Passed |
| generated control 2 | [491, 2054, 44] | [491, 2054, 44] | Passed |
| generated control 3 | [173, 725, 6] | [173, 725, 6] | Passed |
SHA-256 / a531509aa1477023035eff78f2a278047216b7ee82c23df82019541e8733d696
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(protein, fat, carb, fiber, alcohol, polyol):
p, fat, carb, fib, alc, poly = (Fraction(v) for v in (protein, fat, carb, fiber, alcohol, polyol))
avail = carb - fib - poly
avail = abs(avail)
exact = 4 * p + 9 * fat + 4 * avail + 2 * fib + 7 * alc + Fraction(12, 5) * poly
def r(x):
return math.floor(x + Fraction(1, 2))
kj = r(exact * Fraction(4184, 1000))
pct_fat = r(9 * fat * 100 / exact) if exact else 0
return [r(exact), kj, pct_fat]
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['all zero', ['0', '0', '0', '0', '0', '0'], [0, 0, 0]],
['fat only', ['0', '10', '0', '0', '0', '0'], [90, 377, 100]],
['fiber heavier than carb', ['5', '1', '3', '6', '0', '0'], [41, 172, 22]],
['regression: available carb floor', ['13', '12', '7', '3', '0', '10'], [190, 795, 57]],
['repair check: available carb floor', ['30', '35/4', '10', '3', '14', '10'], [327, 1367, 24]],
['generated control 1', ['23', '6', '35', '1', '5', '4'], [313, 1308, 17]],
['generated control 2', ['1', '24', '59', '0', '5', '0'], [491, 2054, 44]],
['generated control 3', ['18', '5/4', '27', '1', '0', '10'], [173, 725, 6]]],
[['fat only', ['0', '10', '0', '0', '0', '0'], [90, 377, 100]],
['fiber heavier than carb', ['5', '1', '3', '6', '0', '0'], [41, 172, 22]],
['sugar-free candy', ['0', '5', '30', '1', '0', '20'], [131, 548, 34]],
['regression: available carb floor', ['21', '28', '0', '5', '0', '0'], [346, 1448, 73]],
['repair check: available carb floor', ['5', '113/4', '13', '15', '0', '10'], [328, 1373, 77]],
['generated control 1', ['5', '13', '34', '5', '5', '0'], [298, 1247, 39]],
['generated control 2', ['0', '1', '23', '0', '5', '10'], [120, 502, 8]],
['generated control 3', ['25', '8', '50', '1', '0', '4'], [364, 1521, 20]]],
[['fiber heavier than carb', ['5', '1', '3', '6', '0', '0'], [41, 172, 22]],
['sugar-free candy', ['0', '5', '30', '1', '0', '20'], [131, 548, 34]],
['cocktail', ['0', '0', '12', '0', '14', '0'], [146, 611, 0]],
['regression: available carb floor', ['5', '10', '0', '5', '0', '4'], [130, 542, 69]],
['repair check: available carb floor', ['19', '10', '1', '2', '14', '0'], [268, 1121, 34]],
['generated control 1', ['16', '0', '32', '8', '5', '0'], [211, 883, 0]],
['generated control 2', ['7', '49/4', '9', '8', '0', '0'], [158, 662, 70]],
['generated control 3', ['20', '27', '39', '5', '14', '10'], [551, 2305, 44]]],
[['sugar-free candy', ['0', '5', '30', '1', '0', '20'], [131, 548, 34]],
['cocktail', ['0', '0', '12', '0', '14', '0'], [146, 611, 0]],
['all zero', ['0', '0', '0', '0', '0', '0'], [0, 0, 0]],
['regression: available carb floor', ['29', '25', '53', '55', '0', '4'], [461, 1927, 49]],
['repair check: available carb floor', ['20', '10', '43', '45', '5', '4'], [305, 1274, 30]],
['generated control 1', ['35', '4', '8', '2', '5', '4'], [233, 973, 15]],
['generated control 2', ['36', '49/4', '59', '5', '0', '0'], [480, 2009, 23]],
['generated control 3', ['3', '21', '12', '2', '0', '10'], [229, 958, 83]]],
[['cocktail', ['0', '0', '12', '0', '14', '0'], [146, 611, 0]],
['all zero', ['0', '0', '0', '0', '0', '0'], [0, 0, 0]],
['fat only', ['0', '10', '0', '0', '0', '0'], [90, 377, 100]],
['regression: available carb floor', ['23', '117/4', '16', '18', '5', '4'], [436, 1824, 60]],
['repair check: available carb floor', ['14', '47/2', '60', '62', '5', '0'], [427, 1784, 50]],
['generated control 1', ['27', '4', '59', '3', '5', '0'], [409, 1711, 9]],
['generated control 2', ['18', '8', '31', '8', '0', '10'], [236, 987, 31]],
['generated control 3', ['39', '21', '14', '16', '0', '0'], [377, 1577, 50]]]]
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 fixture | Actual | Expected | Outcome |
|---|---|---|---|
| all zero | [0, 0, 0] | [0, 0, 0] | Passed |
| fat only | [90, 377, 100] | [90, 377, 100] | Passed |
| fiber heavier than carb | [53, 222, 17] | [41, 172, 22] | Failed |
| regression: available carb floor | [214, 895, 50] | [190, 795, 57] | Failed |
| repair check: available carb floor | [339, 1417, 23] | [327, 1367, 24] | Failed |
| generated control 1 | [313, 1308, 17] | [313, 1308, 17] | Passed |
| generated control 2 | [491, 2054, 44] | [491, 2054, 44] | Passed |
| generated control 3 | [173, 725, 6] | [173, 725, 6] | Passed |
SHA-256 / 3a7f5c6b707c40795b0acc9ebd4b609010a24964fdc1ca24039753fdc4de0457
3 / The verified repair
Exit 0"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(protein, fat, carb, fiber, alcohol, polyol):
p, fat, carb, fib, alc, poly = (Fraction(v) for v in (protein, fat, carb, fiber, alcohol, polyol))
avail = carb - fib - poly
avail = max(avail, Fraction(0))
exact = 4 * p + 9 * fat + 4 * avail + 2 * fib + 7 * alc + Fraction(12, 5) * poly
def r(x):
return math.floor(x + Fraction(1, 2))
kj = r(exact * Fraction(4184, 1000))
pct_fat = r(9 * fat * 100 / exact) if exact else 0
return [r(exact), kj, pct_fat]
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['all zero', ['0', '0', '0', '0', '0', '0'], [0, 0, 0]],
['fat only', ['0', '10', '0', '0', '0', '0'], [90, 377, 100]],
['fiber heavier than carb', ['5', '1', '3', '6', '0', '0'], [41, 172, 22]],
['regression: available carb floor', ['13', '12', '7', '3', '0', '10'], [190, 795, 57]],
['repair check: available carb floor', ['30', '35/4', '10', '3', '14', '10'], [327, 1367, 24]],
['generated control 1', ['23', '6', '35', '1', '5', '4'], [313, 1308, 17]],
['generated control 2', ['1', '24', '59', '0', '5', '0'], [491, 2054, 44]],
['generated control 3', ['18', '5/4', '27', '1', '0', '10'], [173, 725, 6]]],
[['fat only', ['0', '10', '0', '0', '0', '0'], [90, 377, 100]],
['fiber heavier than carb', ['5', '1', '3', '6', '0', '0'], [41, 172, 22]],
['sugar-free candy', ['0', '5', '30', '1', '0', '20'], [131, 548, 34]],
['regression: available carb floor', ['21', '28', '0', '5', '0', '0'], [346, 1448, 73]],
['repair check: available carb floor', ['5', '113/4', '13', '15', '0', '10'], [328, 1373, 77]],
['generated control 1', ['5', '13', '34', '5', '5', '0'], [298, 1247, 39]],
['generated control 2', ['0', '1', '23', '0', '5', '10'], [120, 502, 8]],
['generated control 3', ['25', '8', '50', '1', '0', '4'], [364, 1521, 20]]],
[['fiber heavier than carb', ['5', '1', '3', '6', '0', '0'], [41, 172, 22]],
['sugar-free candy', ['0', '5', '30', '1', '0', '20'], [131, 548, 34]],
['cocktail', ['0', '0', '12', '0', '14', '0'], [146, 611, 0]],
['regression: available carb floor', ['5', '10', '0', '5', '0', '4'], [130, 542, 69]],
['repair check: available carb floor', ['19', '10', '1', '2', '14', '0'], [268, 1121, 34]],
['generated control 1', ['16', '0', '32', '8', '5', '0'], [211, 883, 0]],
['generated control 2', ['7', '49/4', '9', '8', '0', '0'], [158, 662, 70]],
['generated control 3', ['20', '27', '39', '5', '14', '10'], [551, 2305, 44]]],
[['sugar-free candy', ['0', '5', '30', '1', '0', '20'], [131, 548, 34]],
['cocktail', ['0', '0', '12', '0', '14', '0'], [146, 611, 0]],
['all zero', ['0', '0', '0', '0', '0', '0'], [0, 0, 0]],
['regression: available carb floor', ['29', '25', '53', '55', '0', '4'], [461, 1927, 49]],
['repair check: available carb floor', ['20', '10', '43', '45', '5', '4'], [305, 1274, 30]],
['generated control 1', ['35', '4', '8', '2', '5', '4'], [233, 973, 15]],
['generated control 2', ['36', '49/4', '59', '5', '0', '0'], [480, 2009, 23]],
['generated control 3', ['3', '21', '12', '2', '0', '10'], [229, 958, 83]]],
[['cocktail', ['0', '0', '12', '0', '14', '0'], [146, 611, 0]],
['all zero', ['0', '0', '0', '0', '0', '0'], [0, 0, 0]],
['fat only', ['0', '10', '0', '0', '0', '0'], [90, 377, 100]],
['regression: available carb floor', ['23', '117/4', '16', '18', '5', '4'], [436, 1824, 60]],
['repair check: available carb floor', ['14', '47/2', '60', '62', '5', '0'], [427, 1784, 50]],
['generated control 1', ['27', '4', '59', '3', '5', '0'], [409, 1711, 9]],
['generated control 2', ['18', '8', '31', '8', '0', '10'], [236, 987, 31]],
['generated control 3', ['39', '21', '14', '16', '0', '0'], [377, 1577, 50]]]]
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 fixture | Actual | Expected | Outcome |
|---|---|---|---|
| all zero | [0, 0, 0] | [0, 0, 0] | Passed |
| fat only | [90, 377, 100] | [90, 377, 100] | Passed |
| fiber heavier than carb | [41, 172, 22] | [41, 172, 22] | Passed |
| regression: available carb floor | [190, 795, 57] | [190, 795, 57] | Passed |
| repair check: available carb floor | [327, 1367, 24] | [327, 1367, 24] | Passed |
| generated control 1 | [313, 1308, 17] | [313, 1308, 17] | Passed |
| generated control 2 | [491, 2054, 44] | [491, 2054, 44] | Passed |
| generated control 3 | [173, 725, 6] | [173, 725, 6] | Passed |
SHA-256 / 6015883ccdb9982159de3b866f32306d980c819d378eafc6cd2402232f6ea026
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:27.735998+00:00.
Case digest / 9ab23378259e54ed5ff72936c14fbfc721342f0f241243c288deabb431d07455