FAILURE MAP
← Case archive

FA-97011 / Recipe scaling and nutrition / Open access

Percent daily value formatter: micronutrient 10 percent band · case 01

7% calcium prints 5% instead of 8%.

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

ROOT CAUSE

Calcium and vitamin D values at or below 10% skip the 2% increment.

VERIFIED REPAIR

Use 2%, 5% and 10% increments in their bands.

Unsuccessful approach: Dropping the 10% increment above 50% prints 65% instead of 70%.

Case contract

DVs: sodium 2300 mg, fat 78 g, fiber 28 g, calcium 1300 mg, vitamin d 20 mcg, added sugars 50 g, protein 50 g. Names are trimmed, case-insensitive; unknown -> "error: no daily value". Units g, mg, mcg convert exactly; "iu" is allowed only for vitamin d (40 IU = 1 mcg), else "error: unit". pct = amount/DV*100. Zero -> "0%". Calcium and vitamin d: <2 -> "<2%", <=10 -> nearest 2, <=50 -> nearest 5, else nearest 10. Others: <1 -> "<1%", else nearest 1. Half-up.

Why this case matters

Label software prints %DV with different increments for micronutrients and macronutrients.

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(nutrient, amount, unit):
    DV = {'sodium': (2300, 'mg'), 'fat': (78, 'g'), 'fiber': (28, 'g'), 'calcium': (1300, 'mg'),
          'vitamin d': (20, 'mcg'), 'added sugars': (50, 'g'), 'protein': (50, 'g')}
    SCALE = {'g': 1000000, 'mg': 1000, 'mcg': 1}
    key = nutrient.strip().lower()
    if key not in DV:
        return 'error: no daily value'
    amt = Fraction(str(amount))
    if unit == 'iu':
        if key != 'vitamin d':
            return 'error: unit'
        amt, unit = amt / 40, 'mcg'
    if unit not in SCALE:
        return 'error: unit'
    dv, dvu = DV[key]
    p = amt * SCALE[unit] / (dv * SCALE[dvu]) * 100
    def near(v, s):
        return math.floor(v / s + Fraction(1, 2)) * s
    if p == 0:
        return '0%'
    if key in ('calcium', 'vitamin d'):
        if p < 2:
            return '<2%'
        v = near(p, 5) if p <= 50 else near(p, 10)
    else:
        if p < 1:
            return '<1%'
        v = near(p, 1)
    return '%d%%' % v
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['sodium control', ['sodium', 460, 'mg'], '20%'],
  ['calcium exactly two percent', ['calcium', 26, 'mg'], '2%'],
  ['vitamin d in IU', ['vitamin d', 400, 'iu'], '50%'],
  ['repair check: micronutrient 10 percent band', ['CALCIUM', 2300, 'g'], '176920%'],
  ['generated control 1', ['vitamin d', 0.2, 'mcg'], '<2%'],
  ['generated control 2', ['fiber', 0.2, 'g'], '<1%'], ['generated control 3', ['sodium', 5, 'g'], '217%'],
  ['generated control 4', ['protein', 3, 'mcg'], '<1%']],
 [['calcium exactly two percent', ['calcium', 26, 'mg'], '2%'],
  ['vitamin d in IU', ['vitamin d', 400, 'iu'], '50%'], ['fat in grams', ['fat', 3, 'g'], '4%'],
  ['regression: micronutrient 10 percent band', ['CALCIUM', 26, 'mg'], '2%'],
  ['repair check: micronutrient 10 percent band', ['calcium', 40, 'g'], '3080%'],
  ['generated control 1', ['fiber', 10, 'iu'], 'error: unit'],
  ['generated control 2', ['fiber', 0.39, 'g'], '1%'],
  ['generated control 3', ['protein', 300, 'mcg'], '<1%']],
 [['vitamin d in IU', ['vitamin d', 400, 'iu'], '50%'], ['fat in grams', ['fat', 3, 'g'], '4%'],
  ['zero calcium', ['calcium', 0, 'mg'], '0%'],
  ['regression: micronutrient 10 percent band', ['vitamin d', 0.5, 'mcg'], '2%'],
  ['repair check: micronutrient 10 percent band', ['calcium', 300, 'g'], '23080%'],
  ['generated control 1', ['sodium', 1500, 'mcg'], '<1%'],
  ['generated control 2', ['protein', 260, 'mcg'], '<1%'],
  ['generated control 3', ['iron', 0.2, 'mg'], 'error: no daily value']],
 [['fat in grams', ['fat', 3, 'g'], '4%'], ['zero calcium', ['calcium', 0, 'mg'], '0%'],
  ['iu for sodium rejected', ['sodium', 10, 'iu'], 'error: unit'],
  ['regression: micronutrient 10 percent band', ['CALCIUM', 40, 'mg'], '4%'],
  ['repair check: micronutrient 10 percent band', ['CALCIUM', 2, 'g'], '150%'],
  ['generated control 1', ['sodium', 5, 'mg'], '<1%'],
  ['generated control 2', ['iron', 40, 'g'], 'error: no daily value'],
  ['generated control 3', ['calcium', 12, 'mcg'], '<2%']],
 [['zero calcium', ['calcium', 0, 'mg'], '0%'],
  ['iu for sodium rejected', ['sodium', 10, 'iu'], 'error: unit'],
  ['sodium control', ['sodium', 460, 'mg'], '20%'],
  ['regression: micronutrient 10 percent band', ['calcium', 26, 'mg'], '2%'],
  ['repair check: micronutrient 10 percent band', ['CALCIUM', 1500, 'mg'], '120%'],
  ['generated control 1', ['sodium', 300, 'mg'], '13%'], ['generated control 2', ['fat', 0.5, 'g'], '<1%'],
  ['generated control 3', ['calcium', 0.2, 'mcg'], '<2%']]]
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
sodium control20%20%Passed
calcium exactly two percent0%2%Failed
vitamin d in IU50%50%Passed
repair check: micronutrient 10 percent band176920%176920%Passed
generated control 1<2%<2%Passed
generated control 2<1%<1%Passed
generated control 3217%217%Passed
generated control 4<1%<1%Passed

SHA-256 / bc7ebf207b6e77859bf2e8b25ff1050b23b2434a00c191aba2cf12cbf6bccc3d

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(nutrient, amount, unit):
    DV = {'sodium': (2300, 'mg'), 'fat': (78, 'g'), 'fiber': (28, 'g'), 'calcium': (1300, 'mg'),
          'vitamin d': (20, 'mcg'), 'added sugars': (50, 'g'), 'protein': (50, 'g')}
    SCALE = {'g': 1000000, 'mg': 1000, 'mcg': 1}
    key = nutrient.strip().lower()
    if key not in DV:
        return 'error: no daily value'
    amt = Fraction(str(amount))
    if unit == 'iu':
        if key != 'vitamin d':
            return 'error: unit'
        amt, unit = amt / 40, 'mcg'
    if unit not in SCALE:
        return 'error: unit'
    dv, dvu = DV[key]
    p = amt * SCALE[unit] / (dv * SCALE[dvu]) * 100
    def near(v, s):
        return math.floor(v / s + Fraction(1, 2)) * s
    if p == 0:
        return '0%'
    if key in ('calcium', 'vitamin d'):
        if p < 2:
            return '<2%'
        v = near(p, 2) if p <= 10 else near(p, 5)
    else:
        if p < 1:
            return '<1%'
        v = near(p, 1)
    return '%d%%' % v
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['sodium control', ['sodium', 460, 'mg'], '20%'],
  ['calcium exactly two percent', ['calcium', 26, 'mg'], '2%'],
  ['vitamin d in IU', ['vitamin d', 400, 'iu'], '50%'],
  ['repair check: micronutrient 10 percent band', ['CALCIUM', 2300, 'g'], '176920%'],
  ['generated control 1', ['vitamin d', 0.2, 'mcg'], '<2%'],
  ['generated control 2', ['fiber', 0.2, 'g'], '<1%'], ['generated control 3', ['sodium', 5, 'g'], '217%'],
  ['generated control 4', ['protein', 3, 'mcg'], '<1%']],
 [['calcium exactly two percent', ['calcium', 26, 'mg'], '2%'],
  ['vitamin d in IU', ['vitamin d', 400, 'iu'], '50%'], ['fat in grams', ['fat', 3, 'g'], '4%'],
  ['regression: micronutrient 10 percent band', ['CALCIUM', 26, 'mg'], '2%'],
  ['repair check: micronutrient 10 percent band', ['calcium', 40, 'g'], '3080%'],
  ['generated control 1', ['fiber', 10, 'iu'], 'error: unit'],
  ['generated control 2', ['fiber', 0.39, 'g'], '1%'],
  ['generated control 3', ['protein', 300, 'mcg'], '<1%']],
 [['vitamin d in IU', ['vitamin d', 400, 'iu'], '50%'], ['fat in grams', ['fat', 3, 'g'], '4%'],
  ['zero calcium', ['calcium', 0, 'mg'], '0%'],
  ['regression: micronutrient 10 percent band', ['vitamin d', 0.5, 'mcg'], '2%'],
  ['repair check: micronutrient 10 percent band', ['calcium', 300, 'g'], '23080%'],
  ['generated control 1', ['sodium', 1500, 'mcg'], '<1%'],
  ['generated control 2', ['protein', 260, 'mcg'], '<1%'],
  ['generated control 3', ['iron', 0.2, 'mg'], 'error: no daily value']],
 [['fat in grams', ['fat', 3, 'g'], '4%'], ['zero calcium', ['calcium', 0, 'mg'], '0%'],
  ['iu for sodium rejected', ['sodium', 10, 'iu'], 'error: unit'],
  ['regression: micronutrient 10 percent band', ['CALCIUM', 40, 'mg'], '4%'],
  ['repair check: micronutrient 10 percent band', ['CALCIUM', 2, 'g'], '150%'],
  ['generated control 1', ['sodium', 5, 'mg'], '<1%'],
  ['generated control 2', ['iron', 40, 'g'], 'error: no daily value'],
  ['generated control 3', ['calcium', 12, 'mcg'], '<2%']],
 [['zero calcium', ['calcium', 0, 'mg'], '0%'],
  ['iu for sodium rejected', ['sodium', 10, 'iu'], 'error: unit'],
  ['sodium control', ['sodium', 460, 'mg'], '20%'],
  ['regression: micronutrient 10 percent band', ['calcium', 26, 'mg'], '2%'],
  ['repair check: micronutrient 10 percent band', ['CALCIUM', 1500, 'mg'], '120%'],
  ['generated control 1', ['sodium', 300, 'mg'], '13%'], ['generated control 2', ['fat', 0.5, 'g'], '<1%'],
  ['generated control 3', ['calcium', 0.2, 'mcg'], '<2%']]]
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
sodium control20%20%Passed
calcium exactly two percent2%2%Passed
vitamin d in IU50%50%Passed
repair check: micronutrient 10 percent band176925%176920%Failed
generated control 1<2%<2%Passed
generated control 2<1%<1%Passed
generated control 3217%217%Passed
generated control 4<1%<1%Passed

SHA-256 / 3c6bd20baf5921fee157e80ccb5f0958c7721bd5334b3575880fbc3bda5ef3d8

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(nutrient, amount, unit):
    DV = {'sodium': (2300, 'mg'), 'fat': (78, 'g'), 'fiber': (28, 'g'), 'calcium': (1300, 'mg'),
          'vitamin d': (20, 'mcg'), 'added sugars': (50, 'g'), 'protein': (50, 'g')}
    SCALE = {'g': 1000000, 'mg': 1000, 'mcg': 1}
    key = nutrient.strip().lower()
    if key not in DV:
        return 'error: no daily value'
    amt = Fraction(str(amount))
    if unit == 'iu':
        if key != 'vitamin d':
            return 'error: unit'
        amt, unit = amt / 40, 'mcg'
    if unit not in SCALE:
        return 'error: unit'
    dv, dvu = DV[key]
    p = amt * SCALE[unit] / (dv * SCALE[dvu]) * 100
    def near(v, s):
        return math.floor(v / s + Fraction(1, 2)) * s
    if p == 0:
        return '0%'
    if key in ('calcium', 'vitamin d'):
        if p < 2:
            return '<2%'
        v = near(p, 2) if p <= 10 else near(p, 5) if p <= 50 else near(p, 10)
    else:
        if p < 1:
            return '<1%'
        v = near(p, 1)
    return '%d%%' % v
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['sodium control', ['sodium', 460, 'mg'], '20%'],
  ['calcium exactly two percent', ['calcium', 26, 'mg'], '2%'],
  ['vitamin d in IU', ['vitamin d', 400, 'iu'], '50%'],
  ['repair check: micronutrient 10 percent band', ['CALCIUM', 2300, 'g'], '176920%'],
  ['generated control 1', ['vitamin d', 0.2, 'mcg'], '<2%'],
  ['generated control 2', ['fiber', 0.2, 'g'], '<1%'], ['generated control 3', ['sodium', 5, 'g'], '217%'],
  ['generated control 4', ['protein', 3, 'mcg'], '<1%']],
 [['calcium exactly two percent', ['calcium', 26, 'mg'], '2%'],
  ['vitamin d in IU', ['vitamin d', 400, 'iu'], '50%'], ['fat in grams', ['fat', 3, 'g'], '4%'],
  ['regression: micronutrient 10 percent band', ['CALCIUM', 26, 'mg'], '2%'],
  ['repair check: micronutrient 10 percent band', ['calcium', 40, 'g'], '3080%'],
  ['generated control 1', ['fiber', 10, 'iu'], 'error: unit'],
  ['generated control 2', ['fiber', 0.39, 'g'], '1%'],
  ['generated control 3', ['protein', 300, 'mcg'], '<1%']],
 [['vitamin d in IU', ['vitamin d', 400, 'iu'], '50%'], ['fat in grams', ['fat', 3, 'g'], '4%'],
  ['zero calcium', ['calcium', 0, 'mg'], '0%'],
  ['regression: micronutrient 10 percent band', ['vitamin d', 0.5, 'mcg'], '2%'],
  ['repair check: micronutrient 10 percent band', ['calcium', 300, 'g'], '23080%'],
  ['generated control 1', ['sodium', 1500, 'mcg'], '<1%'],
  ['generated control 2', ['protein', 260, 'mcg'], '<1%'],
  ['generated control 3', ['iron', 0.2, 'mg'], 'error: no daily value']],
 [['fat in grams', ['fat', 3, 'g'], '4%'], ['zero calcium', ['calcium', 0, 'mg'], '0%'],
  ['iu for sodium rejected', ['sodium', 10, 'iu'], 'error: unit'],
  ['regression: micronutrient 10 percent band', ['CALCIUM', 40, 'mg'], '4%'],
  ['repair check: micronutrient 10 percent band', ['CALCIUM', 2, 'g'], '150%'],
  ['generated control 1', ['sodium', 5, 'mg'], '<1%'],
  ['generated control 2', ['iron', 40, 'g'], 'error: no daily value'],
  ['generated control 3', ['calcium', 12, 'mcg'], '<2%']],
 [['zero calcium', ['calcium', 0, 'mg'], '0%'],
  ['iu for sodium rejected', ['sodium', 10, 'iu'], 'error: unit'],
  ['sodium control', ['sodium', 460, 'mg'], '20%'],
  ['regression: micronutrient 10 percent band', ['calcium', 26, 'mg'], '2%'],
  ['repair check: micronutrient 10 percent band', ['CALCIUM', 1500, 'mg'], '120%'],
  ['generated control 1', ['sodium', 300, 'mg'], '13%'], ['generated control 2', ['fat', 0.5, 'g'], '<1%'],
  ['generated control 3', ['calcium', 0.2, 'mcg'], '<2%']]]
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
sodium control20%20%Passed
calcium exactly two percent2%2%Passed
vitamin d in IU50%50%Passed
repair check: micronutrient 10 percent band176920%176920%Passed
generated control 1<2%<2%Passed
generated control 2<1%<1%Passed
generated control 3217%217%Passed
generated control 4<1%<1%Passed

SHA-256 / 5c812030af0f412d478274e0b5a67fca04cfac8ed476a57ae45cceeeed9e9bf1

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

Case digest / c7cbbc06d77fc23485ce1e3ccf3f050e3bab970d9517a16ea9c0a15de7cffbdf