FA-97001 / Recipe scaling and nutrition / Open access
Atwater energy calculator: fat energy share · case 01
A snack that is half fat by energy reports 6% of energy from fat.
ROOT CAUSE
The fat share divides fat grams, not fat energy, by total energy.
VERIFIED REPAIR
Use 9*fat/total energy.
Unsuccessful approach: The simplified 4-9-4 denominator ignores fiber, alcohol and polyol energy.
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 = 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(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: fat energy share', ['19', '15', '13', '15', '0', '0'], [241, 1008, 56]],
['repair check: fat energy share', ['10', '17', '0', '3', '14', '0'], [297, 1243, 52]],
['generated control 1', ['36', '11/4', '39', '3', '0', '10'], [303, 1267, 8]],
['generated control 2', ['37', '8', '58', '60', '14', '4'], [448, 1873, 16]],
['generated control 3', ['5', '65/4', '30', '8', '0', '10'], [254, 1064, 58]]],
[['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: fat energy share', ['19', '19/4', '59', '3', '0', '0'], [349, 1459, 12]],
['repair check: fat energy share', ['14', '41/2', '14', '3', '14', '4'], [382, 1599, 48]],
['generated control 1', ['18', '3', '18', '3', '14', '4'], [257, 1074, 11]],
['generated control 2', ['40', '21', '14', '1', '5', '10'], [422, 1766, 45]],
['generated control 3', ['17', '0', '59', '3', '0', '4'], [292, 1220, 0]]],
[['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: fat energy share', ['22', '12', '26', '28', '0', '4'], [262, 1095, 41]],
['repair check: fat energy share', ['20', '29', '32', '5', '5', '0'], [494, 2067, 53]],
['generated control 1', ['11', '13', '49', '0', '0', '0'], [357, 1494, 33]],
['generated control 2', ['6', '23', '9', '0', '0', '0'], [267, 1117, 78]],
['generated control 3', ['3', '23', '4', '8', '0', '0'], [235, 983, 88]]],
[['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: fat energy share', ['24', '15', '48', '5', '14', '10'], [495, 2071, 27]],
['repair check: fat energy share', ['16', '30', '5', '7', '14', '10'], [470, 1966, 57]],
['generated control 1', ['22', '18', '15', '3', '0', '0'], [304, 1272, 53]],
['generated control 2', ['32', '13', '57', '2', '0', '4'], [463, 1936, 25]],
['generated control 3', ['33', '0', '4', '8', '0', '0'], [148, 619, 0]]],
[['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: fat energy share', ['23', '15', '31', '33', '0', '10'], [317, 1326, 43]],
['repair check: fat energy share', ['13', '4', '17', '5', '0', '10'], [130, 544, 28]],
['generated control 1', ['24', '29', '19', '2', '0', '10'], [413, 1728, 63]],
['generated control 2', ['17', '6', '43', '0', '14', '0'], [392, 1640, 14]],
['generated control 3', ['35', '20', '55', '1', '0', '4'], [532, 2224, 34]]]]
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, 11] | [90, 377, 100] | Failed |
| fiber heavier than carb | [41, 172, 2] | [41, 172, 22] | Failed |
| regression: fat energy share | [241, 1008, 6] | [241, 1008, 56] | Failed |
| repair check: fat energy share | [297, 1243, 6] | [297, 1243, 52] | Failed |
| generated control 1 | [303, 1267, 1] | [303, 1267, 8] | Failed |
| generated control 2 | [448, 1873, 2] | [448, 1873, 16] | Failed |
| generated control 3 | [254, 1064, 6] | [254, 1064, 58] | Failed |
SHA-256 / d07d5cd7732a2e51624383454554a5b7ecd6d9cb43a687a0a0c34a1dcfe603a6
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 = 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(fat * 900 / (4 * p + 9 * fat + 4 * carb)) 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: fat energy share', ['19', '15', '13', '15', '0', '0'], [241, 1008, 56]],
['repair check: fat energy share', ['10', '17', '0', '3', '14', '0'], [297, 1243, 52]],
['generated control 1', ['36', '11/4', '39', '3', '0', '10'], [303, 1267, 8]],
['generated control 2', ['37', '8', '58', '60', '14', '4'], [448, 1873, 16]],
['generated control 3', ['5', '65/4', '30', '8', '0', '10'], [254, 1064, 58]]],
[['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: fat energy share', ['19', '19/4', '59', '3', '0', '0'], [349, 1459, 12]],
['repair check: fat energy share', ['14', '41/2', '14', '3', '14', '4'], [382, 1599, 48]],
['generated control 1', ['18', '3', '18', '3', '14', '4'], [257, 1074, 11]],
['generated control 2', ['40', '21', '14', '1', '5', '10'], [422, 1766, 45]],
['generated control 3', ['17', '0', '59', '3', '0', '4'], [292, 1220, 0]]],
[['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: fat energy share', ['22', '12', '26', '28', '0', '4'], [262, 1095, 41]],
['repair check: fat energy share', ['20', '29', '32', '5', '5', '0'], [494, 2067, 53]],
['generated control 1', ['11', '13', '49', '0', '0', '0'], [357, 1494, 33]],
['generated control 2', ['6', '23', '9', '0', '0', '0'], [267, 1117, 78]],
['generated control 3', ['3', '23', '4', '8', '0', '0'], [235, 983, 88]]],
[['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: fat energy share', ['24', '15', '48', '5', '14', '10'], [495, 2071, 27]],
['repair check: fat energy share', ['16', '30', '5', '7', '14', '10'], [470, 1966, 57]],
['generated control 1', ['22', '18', '15', '3', '0', '0'], [304, 1272, 53]],
['generated control 2', ['32', '13', '57', '2', '0', '4'], [463, 1936, 25]],
['generated control 3', ['33', '0', '4', '8', '0', '0'], [148, 619, 0]]],
[['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: fat energy share', ['23', '15', '31', '33', '0', '10'], [317, 1326, 43]],
['repair check: fat energy share', ['13', '4', '17', '5', '0', '10'], [130, 544, 28]],
['generated control 1', ['24', '29', '19', '2', '0', '10'], [413, 1728, 63]],
['generated control 2', ['17', '6', '43', '0', '14', '0'], [392, 1640, 14]],
['generated control 3', ['35', '20', '55', '1', '0', '4'], [532, 2224, 34]]]]
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: fat energy share | [241, 1008, 51] | [241, 1008, 56] | Failed |
| repair check: fat energy share | [297, 1243, 79] | [297, 1243, 52] | Failed |
| generated control 1 | [303, 1267, 8] | [303, 1267, 8] | Passed |
| generated control 2 | [448, 1873, 16] | [448, 1873, 16] | Passed |
| generated control 3 | [254, 1064, 51] | [254, 1064, 58] | Failed |
SHA-256 / cfd40f4f1a6770982d68ec51218e3e4afc7aedc506507069363711b2f96cf83c
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: fat energy share', ['19', '15', '13', '15', '0', '0'], [241, 1008, 56]],
['repair check: fat energy share', ['10', '17', '0', '3', '14', '0'], [297, 1243, 52]],
['generated control 1', ['36', '11/4', '39', '3', '0', '10'], [303, 1267, 8]],
['generated control 2', ['37', '8', '58', '60', '14', '4'], [448, 1873, 16]],
['generated control 3', ['5', '65/4', '30', '8', '0', '10'], [254, 1064, 58]]],
[['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: fat energy share', ['19', '19/4', '59', '3', '0', '0'], [349, 1459, 12]],
['repair check: fat energy share', ['14', '41/2', '14', '3', '14', '4'], [382, 1599, 48]],
['generated control 1', ['18', '3', '18', '3', '14', '4'], [257, 1074, 11]],
['generated control 2', ['40', '21', '14', '1', '5', '10'], [422, 1766, 45]],
['generated control 3', ['17', '0', '59', '3', '0', '4'], [292, 1220, 0]]],
[['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: fat energy share', ['22', '12', '26', '28', '0', '4'], [262, 1095, 41]],
['repair check: fat energy share', ['20', '29', '32', '5', '5', '0'], [494, 2067, 53]],
['generated control 1', ['11', '13', '49', '0', '0', '0'], [357, 1494, 33]],
['generated control 2', ['6', '23', '9', '0', '0', '0'], [267, 1117, 78]],
['generated control 3', ['3', '23', '4', '8', '0', '0'], [235, 983, 88]]],
[['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: fat energy share', ['24', '15', '48', '5', '14', '10'], [495, 2071, 27]],
['repair check: fat energy share', ['16', '30', '5', '7', '14', '10'], [470, 1966, 57]],
['generated control 1', ['22', '18', '15', '3', '0', '0'], [304, 1272, 53]],
['generated control 2', ['32', '13', '57', '2', '0', '4'], [463, 1936, 25]],
['generated control 3', ['33', '0', '4', '8', '0', '0'], [148, 619, 0]]],
[['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: fat energy share', ['23', '15', '31', '33', '0', '10'], [317, 1326, 43]],
['repair check: fat energy share', ['13', '4', '17', '5', '0', '10'], [130, 544, 28]],
['generated control 1', ['24', '29', '19', '2', '0', '10'], [413, 1728, 63]],
['generated control 2', ['17', '6', '43', '0', '14', '0'], [392, 1640, 14]],
['generated control 3', ['35', '20', '55', '1', '0', '4'], [532, 2224, 34]]]]
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: fat energy share | [241, 1008, 56] | [241, 1008, 56] | Passed |
| repair check: fat energy share | [297, 1243, 52] | [297, 1243, 52] | Passed |
| generated control 1 | [303, 1267, 8] | [303, 1267, 8] | Passed |
| generated control 2 | [448, 1873, 16] | [448, 1873, 16] | Passed |
| generated control 3 | [254, 1064, 58] | [254, 1064, 58] | Passed |
SHA-256 / a497ddd4c9e6c9e0d5308294279e8a33aea734664279b65f572a4c9503dd37ad
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.928328+00:00.
Case digest / 9f89981629244a31084901e67a54aed22d1eff2077a05b75bc94c18628519875