FAILURE MAP
← Case archive

FA-96981 / Recipe scaling and nutrition / Open access

Atwater energy calculator: fiber subtraction · case 01

High-fiber breads are credited 6 kcal per gram of fiber.

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

ROOT CAUSE

Fiber is not removed from total carbohydrate before the 4 kcal factor, yet also earns its own 2 kcal.

VERIFIED REPAIR

Subtract both fiber and polyols from total carbohydrate.

Unsuccessful approach: Subtracting fiber but not polyols double counts sugar alcohols.

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 - 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: fiber subtraction', ['8', '73/4', '14', '1', '0', '4'], [244, 1020, 67]],
  ['repair check: fiber subtraction', ['6', '26', '35', '1', '14', '4'], [488, 2040, 48]],
  ['generated control 1', ['9', '29/2', '40', '3', '14', '0'], [419, 1751, 31]],
  ['generated control 2', ['20', '51/4', '0', '2', '0', '0'], [199, 832, 58]],
  ['generated control 3', ['36', '27', '5', '1', '0', '4'], [399, 1668, 61]]],
 [['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: fiber subtraction', ['23', '113/4', '48', '1', '0', '4'], [530, 2217, 48]],
  ['repair check: fiber subtraction', ['27', '49/2', '34', '0', '14', '10'], [547, 2287, 40]],
  ['generated control 1', ['28', '73/4', '5', '0', '5', '0'], [331, 1386, 50]],
  ['generated control 2', ['30', '8', '52', '54', '0', '0'], [300, 1255, 24]],
  ['generated control 3', ['25', '4', '60', '62', '0', '10'], [284, 1188, 13]]],
 [['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: fiber subtraction', ['13', '8', '60', '3', '0', '0'], [358, 1498, 20]],
  ['repair check: fiber subtraction', ['23', '26', '30', '0', '5', '4'], [475, 1986, 49]],
  ['generated control 1', ['7', '6', '26', '3', '14', '0'], [278, 1163, 19]],
  ['generated control 2', ['33', '30', '19', '3', '0', '0'], [472, 1975, 57]],
  ['generated control 3', ['14', '29', '46', '2', '0', '0'], [497, 2079, 53]]],
 [['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: fiber subtraction', ['28', '14', '21', '8', '14', '0'], [404, 1690, 31]],
  ['repair check: fiber subtraction', ['1', '24', '39', '2', '0', '4'], [366, 1530, 59]],
  ['generated control 1', ['18', '14', '14', '0', '14', '4'], [346, 1446, 36]],
  ['generated control 2', ['31', '113/4', '13', '2', '0', '0'], [426, 1783, 60]],
  ['generated control 3', ['22', '57/2', '29', '8', '0', '0'], [445, 1860, 58]]],
 [['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: fiber subtraction', ['7', '10', '11', '8', '0', '0'], [146, 611, 62]],
  ['repair check: fiber subtraction', ['10', '29/2', '53', '0', '0', '10'], [367, 1533, 36]],
  ['generated control 1', ['38', '53/2', '9', '0', '14', '0'], [525, 2195, 45]],
  ['generated control 2', ['2', '27', '52', '2', '0', '4'], [449, 1877, 54]],
  ['generated control 3', ['21', '15', '37', '0', '5', '0'], [402, 1682, 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 fixtureActualExpectedOutcome
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: fiber subtraction[248, 1037, 66][244, 1020, 67]Failed
repair check: fiber subtraction[492, 2057, 48][488, 2040, 48]Failed
generated control 1[431, 1801, 30][419, 1751, 31]Failed
generated control 2[199, 832, 58][199, 832, 58]Passed
generated control 3[403, 1684, 60][399, 1668, 61]Failed

SHA-256 / a3abcd5807d2dee625a15620ffabe2a4859d6900b8f6df3fb21a629373aaf28c

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
    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: fiber subtraction', ['8', '73/4', '14', '1', '0', '4'], [244, 1020, 67]],
  ['repair check: fiber subtraction', ['6', '26', '35', '1', '14', '4'], [488, 2040, 48]],
  ['generated control 1', ['9', '29/2', '40', '3', '14', '0'], [419, 1751, 31]],
  ['generated control 2', ['20', '51/4', '0', '2', '0', '0'], [199, 832, 58]],
  ['generated control 3', ['36', '27', '5', '1', '0', '4'], [399, 1668, 61]]],
 [['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: fiber subtraction', ['23', '113/4', '48', '1', '0', '4'], [530, 2217, 48]],
  ['repair check: fiber subtraction', ['27', '49/2', '34', '0', '14', '10'], [547, 2287, 40]],
  ['generated control 1', ['28', '73/4', '5', '0', '5', '0'], [331, 1386, 50]],
  ['generated control 2', ['30', '8', '52', '54', '0', '0'], [300, 1255, 24]],
  ['generated control 3', ['25', '4', '60', '62', '0', '10'], [284, 1188, 13]]],
 [['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: fiber subtraction', ['13', '8', '60', '3', '0', '0'], [358, 1498, 20]],
  ['repair check: fiber subtraction', ['23', '26', '30', '0', '5', '4'], [475, 1986, 49]],
  ['generated control 1', ['7', '6', '26', '3', '14', '0'], [278, 1163, 19]],
  ['generated control 2', ['33', '30', '19', '3', '0', '0'], [472, 1975, 57]],
  ['generated control 3', ['14', '29', '46', '2', '0', '0'], [497, 2079, 53]]],
 [['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: fiber subtraction', ['28', '14', '21', '8', '14', '0'], [404, 1690, 31]],
  ['repair check: fiber subtraction', ['1', '24', '39', '2', '0', '4'], [366, 1530, 59]],
  ['generated control 1', ['18', '14', '14', '0', '14', '4'], [346, 1446, 36]],
  ['generated control 2', ['31', '113/4', '13', '2', '0', '0'], [426, 1783, 60]],
  ['generated control 3', ['22', '57/2', '29', '8', '0', '0'], [445, 1860, 58]]],
 [['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: fiber subtraction', ['7', '10', '11', '8', '0', '0'], [146, 611, 62]],
  ['repair check: fiber subtraction', ['10', '29/2', '53', '0', '0', '10'], [367, 1533, 36]],
  ['generated control 1', ['38', '53/2', '9', '0', '14', '0'], [525, 2195, 45]],
  ['generated control 2', ['2', '27', '52', '2', '0', '4'], [449, 1877, 54]],
  ['generated control 3', ['21', '15', '37', '0', '5', '0'], [402, 1682, 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 fixtureActualExpectedOutcome
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: fiber subtraction[260, 1087, 63][244, 1020, 67]Failed
repair check: fiber subtraction[504, 2107, 46][488, 2040, 48]Failed
generated control 1[419, 1751, 31][419, 1751, 31]Passed
generated control 2[199, 832, 58][199, 832, 58]Passed
generated control 3[415, 1735, 59][399, 1668, 61]Failed

SHA-256 / 40eb7872c02f948712bf3f574ab23ae88e78fa2a21b714c26bd9ba19d579b65e

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: fiber subtraction', ['8', '73/4', '14', '1', '0', '4'], [244, 1020, 67]],
  ['repair check: fiber subtraction', ['6', '26', '35', '1', '14', '4'], [488, 2040, 48]],
  ['generated control 1', ['9', '29/2', '40', '3', '14', '0'], [419, 1751, 31]],
  ['generated control 2', ['20', '51/4', '0', '2', '0', '0'], [199, 832, 58]],
  ['generated control 3', ['36', '27', '5', '1', '0', '4'], [399, 1668, 61]]],
 [['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: fiber subtraction', ['23', '113/4', '48', '1', '0', '4'], [530, 2217, 48]],
  ['repair check: fiber subtraction', ['27', '49/2', '34', '0', '14', '10'], [547, 2287, 40]],
  ['generated control 1', ['28', '73/4', '5', '0', '5', '0'], [331, 1386, 50]],
  ['generated control 2', ['30', '8', '52', '54', '0', '0'], [300, 1255, 24]],
  ['generated control 3', ['25', '4', '60', '62', '0', '10'], [284, 1188, 13]]],
 [['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: fiber subtraction', ['13', '8', '60', '3', '0', '0'], [358, 1498, 20]],
  ['repair check: fiber subtraction', ['23', '26', '30', '0', '5', '4'], [475, 1986, 49]],
  ['generated control 1', ['7', '6', '26', '3', '14', '0'], [278, 1163, 19]],
  ['generated control 2', ['33', '30', '19', '3', '0', '0'], [472, 1975, 57]],
  ['generated control 3', ['14', '29', '46', '2', '0', '0'], [497, 2079, 53]]],
 [['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: fiber subtraction', ['28', '14', '21', '8', '14', '0'], [404, 1690, 31]],
  ['repair check: fiber subtraction', ['1', '24', '39', '2', '0', '4'], [366, 1530, 59]],
  ['generated control 1', ['18', '14', '14', '0', '14', '4'], [346, 1446, 36]],
  ['generated control 2', ['31', '113/4', '13', '2', '0', '0'], [426, 1783, 60]],
  ['generated control 3', ['22', '57/2', '29', '8', '0', '0'], [445, 1860, 58]]],
 [['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: fiber subtraction', ['7', '10', '11', '8', '0', '0'], [146, 611, 62]],
  ['repair check: fiber subtraction', ['10', '29/2', '53', '0', '0', '10'], [367, 1533, 36]],
  ['generated control 1', ['38', '53/2', '9', '0', '14', '0'], [525, 2195, 45]],
  ['generated control 2', ['2', '27', '52', '2', '0', '4'], [449, 1877, 54]],
  ['generated control 3', ['21', '15', '37', '0', '5', '0'], [402, 1682, 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 fixtureActualExpectedOutcome
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: fiber subtraction[244, 1020, 67][244, 1020, 67]Passed
repair check: fiber subtraction[488, 2040, 48][488, 2040, 48]Passed
generated control 1[419, 1751, 31][419, 1751, 31]Passed
generated control 2[199, 832, 58][199, 832, 58]Passed
generated control 3[399, 1668, 61][399, 1668, 61]Passed

SHA-256 / c250f661e976249babcbd46f595fefcd39cd22d5cb34143db252b6883ed6c78a

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.690610+00:00.

Case digest / fc3edad44438f277ea655d74b726c19750990a03e55d8df7a2bd971813cfe847