FA-96871 / Recipe scaling and nutrition / Open access
Nutrition label rounding: calorie insignificance test · case 01
A 4.6 kcal serving is labelled 5 calories instead of 0.
ROOT CAUSE
The below-5 insignificance test runs on the already rounded value, so 2.5-4.9 kcal escapes to 5.
VERIFIED REPAIR
Test the raw calorie amount against 5 before rounding.
Unsuccessful approach: Using <= 5 on the raw value zeroes an exact 5 kcal serving, which must display 5.
Case contract
Round a panel dict to displayed strings (toy label rules, half-up): calories <5 -> 0, <=50 -> nearest 5, else nearest 10; fat g <0.5 -> 0, <5 -> nearest 0.5, else nearest 1; sodium mg <5 -> 0, <=140 -> nearest 5, else nearest 10; cholesterol mg <2 -> "0 mg", <5 -> "less than 5 mg", else nearest 5; carbs g <0.5 -> "0 g", <1 -> "less than 1 g", else nearest 1. Absent keys are omitted. Values print with %g formatting.
Why this case matters
Packaged food and meal-kit labels are generated from recipe nutrition totals with banded rounding rules that differ per nutrient.
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(panel):
def near(x, step):
return math.floor(x / step + Fraction(1, 2)) * step
def show(v, unit):
t = format(float(v), 'g')
return t + ' ' + unit if unit else t
out = {}
if 'calories' in panel:
c = Fraction(str(panel['calories']))
out['calories'] = show(0 if near(c, 5) < 5 else near(c, 5) if c <= 50 else near(c, 10), '')
if 'fat' in panel:
x = Fraction(str(panel['fat']))
out['fat'] = show(0 if x < Fraction(1, 2) else near(x, Fraction(1, 2)) if x < 5 else near(x, 1), 'g')
if 'sodium' in panel:
x = Fraction(str(panel['sodium']))
out['sodium'] = show(0 if x < 5 else near(x, 5) if x <= 140 else near(x, 10), 'mg')
if 'cholesterol' in panel:
x = Fraction(str(panel['cholesterol']))
out['cholesterol'] = '0 mg' if x < 2 else 'less than 5 mg' if x < 5 else show(near(x, 5), 'mg')
if 'carbs' in panel:
x = Fraction(str(panel['carbs']))
out['carbs'] = '0 g' if x < Fraction(1, 2) else 'less than 1 g' if x < 1 else show(near(x, 1), 'g')
return out
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['empty panel', [{}], {}],
['insignificant calories', [{'calories': 4.9, 'fat': 0.4}], {'calories': '0', 'fat': '0 g'}],
['mid-band sodium', [{'carbs': 0.9, 'sodium': 137.5}], {'carbs': 'less than 1 g', 'sodium': '140 mg'}],
['regression: calorie insignificance test', [{'calories': 2.5, 'cholesterol': 3, 'sodium': 4.5}],
{'calories': '0', 'cholesterol': 'less than 5 mg', 'sodium': '0 mg'}],
['repair check: calorie insignificance test', [{'calories': 5, 'cholesterol': 12, 'fat': 2.75}],
{'calories': '5', 'cholesterol': '10 mg', 'fat': '3 g'}],
['generated control 1', [{'calories': 54.5, 'carbs': 0.5, 'cholesterol': 2.2, 'sodium': 192.5}],
{'calories': '50', 'carbs': 'less than 1 g', 'cholesterol': 'less than 5 mg', 'sodium': '190 mg'}],
['generated control 2', [{'calories': 47, 'carbs': 1, 'cholesterol': 1.5, 'fat': 17, 'sodium': 199}],
{'calories': '45', 'carbs': '1 g', 'cholesterol': '0 mg', 'fat': '17 g', 'sodium': '200 mg'}],
['generated control 3', [{'cholesterol': 2.2, 'fat': 0.4, 'sodium': 190.5}],
{'cholesterol': 'less than 5 mg', 'fat': '0 g', 'sodium': '190 mg'}]],
[['insignificant calories', [{'calories': 4.9, 'fat': 0.4}], {'calories': '0', 'fat': '0 g'}],
['mid-band sodium', [{'carbs': 0.9, 'sodium': 137.5}], {'carbs': 'less than 1 g', 'sodium': '140 mg'}],
['large values', [{'calories': 245, 'cholesterol': 22.5, 'fat': 12.5, 'sodium': 455}],
{'calories': '250', 'cholesterol': '25 mg', 'fat': '13 g', 'sodium': '460 mg'}],
['regression: calorie insignificance test', [{'calories': 2.5, 'carbs': 0.5, 'fat': 1.25}],
{'calories': '0', 'carbs': 'less than 1 g', 'fat': '1.5 g'}],
['repair check: calorie insignificance test',
[{'calories': 5, 'cholesterol': 22.5, 'fat': 4.75, 'sodium': 1.75}],
{'calories': '5', 'cholesterol': '25 mg', 'fat': '5 g', 'sodium': '0 mg'}],
['generated control 1', [{'calories': 7.25, 'fat': 8.75, 'sodium': 0.25}],
{'calories': '5', 'fat': '9 g', 'sodium': '0 mg'}],
['generated control 2', [{'calories': 267.5, 'carbs': 1.5, 'cholesterol': 5, 'fat': 1.25, 'sodium': 0.25}],
{'calories': '270', 'carbs': '2 g', 'cholesterol': '5 mg', 'fat': '1.5 g', 'sodium': '0 mg'}],
['generated control 3', [{'calories': 333, 'cholesterol': 1.5, 'fat': 19.25, 'sodium': 5.25}],
{'calories': '330', 'cholesterol': '0 mg', 'fat': '19 g', 'sodium': '5 mg'}]],
[['mid-band sodium', [{'carbs': 0.9, 'sodium': 137.5}], {'carbs': 'less than 1 g', 'sodium': '140 mg'}],
['large values', [{'calories': 245, 'cholesterol': 22.5, 'fat': 12.5, 'sodium': 455}],
{'calories': '250', 'cholesterol': '25 mg', 'fat': '13 g', 'sodium': '460 mg'}],
['small cholesterol', [{'carbs': 12.5, 'cholesterol': 3}],
{'carbs': '13 g', 'cholesterol': 'less than 5 mg'}],
['regression: calorie insignificance test',
[{'calories': 2.5, 'cholesterol': 4.9, 'fat': 10.5, 'sodium': 133}],
{'calories': '0', 'cholesterol': 'less than 5 mg', 'fat': '11 g', 'sodium': '135 mg'}],
['repair check: calorie insignificance test', [{'calories': 5, 'carbs': 0.2, 'fat': 0.5, 'sodium': 175}],
{'calories': '5', 'carbs': '0 g', 'fat': '0.5 g', 'sodium': '180 mg'}],
['generated control 1', [{'carbs': 30.4, 'fat': 0.5, 'sodium': 1.25}],
{'carbs': '30 g', 'fat': '0.5 g', 'sodium': '0 mg'}],
['generated control 2', [{'calories': 45.25, 'carbs': 0.2, 'fat': 20.25, 'sodium': 0}],
{'calories': '45', 'carbs': '0 g', 'fat': '20 g', 'sodium': '0 mg'}],
['generated control 3', [{'calories': 54, 'fat': 4, 'sodium': 6}],
{'calories': '50', 'fat': '4 g', 'sodium': '5 mg'}]],
[['large values', [{'calories': 245, 'cholesterol': 22.5, 'fat': 12.5, 'sodium': 455}],
{'calories': '250', 'cholesterol': '25 mg', 'fat': '13 g', 'sodium': '460 mg'}],
['small cholesterol', [{'carbs': 12.5, 'cholesterol': 3}],
{'carbs': '13 g', 'cholesterol': 'less than 5 mg'}],
['empty panel', [{}], {}],
['regression: calorie insignificance test',
[{'calories': 4.6, 'carbs': 0.2, 'cholesterol': 12, 'sodium': 1.5}],
{'calories': '0', 'carbs': '0 g', 'cholesterol': '10 mg', 'sodium': '0 mg'}],
['repair check: calorie insignificance test', [{'calories': 5, 'carbs': 0.2, 'fat': 2.75}],
{'calories': '5', 'carbs': '0 g', 'fat': '3 g'}],
['generated control 1', [{'calories': 65, 'carbs': 0.9, 'cholesterol': 5, 'sodium': 151.5}],
{'calories': '70', 'carbs': 'less than 1 g', 'cholesterol': '5 mg', 'sodium': '150 mg'}],
['generated control 2', [{'carbs': 0.2, 'fat': 11.5}], {'carbs': '0 g', 'fat': '12 g'}],
['generated control 3', [{'calories': 56, 'cholesterol': 4.9, 'fat': 2.75, 'sodium': 10.75}],
{'calories': '60', 'cholesterol': 'less than 5 mg', 'fat': '3 g', 'sodium': '10 mg'}]],
[['small cholesterol', [{'carbs': 12.5, 'cholesterol': 3}],
{'carbs': '13 g', 'cholesterol': 'less than 5 mg'}],
['empty panel', [{}], {}],
['insignificant calories', [{'calories': 4.9, 'fat': 0.4}], {'calories': '0', 'fat': '0 g'}],
['regression: calorie insignificance test', [{'calories': 3.75, 'cholesterol': 5, 'fat': 3.25}],
{'calories': '0', 'cholesterol': '5 mg', 'fat': '3.5 g'}],
['repair check: calorie insignificance test', [{'calories': 5, 'carbs': 12.5, 'fat': 2.75, 'sodium': 93}],
{'calories': '5', 'carbs': '13 g', 'fat': '3 g', 'sodium': '95 mg'}],
['generated control 1', [{'cholesterol': 5, 'fat': 0, 'sodium': 145}],
{'cholesterol': '5 mg', 'fat': '0 g', 'sodium': '150 mg'}],
['generated control 2', [{'calories': 214, 'carbs': 1, 'cholesterol': 2.2}],
{'calories': '210', 'carbs': '1 g', 'cholesterol': 'less than 5 mg'}],
['generated control 3', [{'calories': 306, 'cholesterol': 22.5, 'sodium': 10.25}],
{'calories': '310', 'cholesterol': '25 mg', 'sodium': '10 mg'}]]]
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 |
|---|---|---|---|
| empty panel | {} | {} | Passed |
| insignificant calories | {'calories': '5', 'fat': '0 g'} | {'calories': '0', 'fat': '0 g'} | Failed |
| mid-band sodium | {'carbs': 'less than 1 g', 'sodium': '140 mg'} | {'carbs': 'less than 1 g', 'sodium': '140 mg'} | Passed |
| regression: calorie insignificance test | {'calories': '5', 'cholesterol': 'less than 5 mg', 'sodium': '0 mg'} | {'calories': '0', 'cholesterol': 'less than 5 mg', 'sodium': '0 mg'} | Failed |
| repair check: calorie insignificance test | {'calories': '5', 'cholesterol': '10 mg', 'fat': '3 g'} | {'calories': '5', 'cholesterol': '10 mg', 'fat': '3 g'} | Passed |
| generated control 1 | {'calories': '50', 'carbs': 'less than 1 g', 'cholesterol': 'less than 5 mg', 'sodium': '190 mg'} | {'calories': '50', 'carbs': 'less than 1 g', 'cholesterol': 'less than 5 mg', 'sodium': '190 mg'} | Passed |
| generated control 2 | {'calories': '45', 'carbs': '1 g', 'cholesterol': '0 mg', 'fat': '17 g', 'sodium': '200 mg'} | {'calories': '45', 'carbs': '1 g', 'cholesterol': '0 mg', 'fat': '17 g', 'sodium': '200 mg'} | Passed |
| generated control 3 | {'cholesterol': 'less than 5 mg', 'fat': '0 g', 'sodium': '190 mg'} | {'cholesterol': 'less than 5 mg', 'fat': '0 g', 'sodium': '190 mg'} | Passed |
SHA-256 / a5da4b5e9ba187dd15e38f2744429d0dfb96ace05f918f8b1b6e943bf5f6a4e2
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(panel):
def near(x, step):
return math.floor(x / step + Fraction(1, 2)) * step
def show(v, unit):
t = format(float(v), 'g')
return t + ' ' + unit if unit else t
out = {}
if 'calories' in panel:
c = Fraction(str(panel['calories']))
out['calories'] = show(0 if c <= 5 else near(c, 5) if c <= 50 else near(c, 10), '')
if 'fat' in panel:
x = Fraction(str(panel['fat']))
out['fat'] = show(0 if x < Fraction(1, 2) else near(x, Fraction(1, 2)) if x < 5 else near(x, 1), 'g')
if 'sodium' in panel:
x = Fraction(str(panel['sodium']))
out['sodium'] = show(0 if x < 5 else near(x, 5) if x <= 140 else near(x, 10), 'mg')
if 'cholesterol' in panel:
x = Fraction(str(panel['cholesterol']))
out['cholesterol'] = '0 mg' if x < 2 else 'less than 5 mg' if x < 5 else show(near(x, 5), 'mg')
if 'carbs' in panel:
x = Fraction(str(panel['carbs']))
out['carbs'] = '0 g' if x < Fraction(1, 2) else 'less than 1 g' if x < 1 else show(near(x, 1), 'g')
return out
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['empty panel', [{}], {}],
['insignificant calories', [{'calories': 4.9, 'fat': 0.4}], {'calories': '0', 'fat': '0 g'}],
['mid-band sodium', [{'carbs': 0.9, 'sodium': 137.5}], {'carbs': 'less than 1 g', 'sodium': '140 mg'}],
['regression: calorie insignificance test', [{'calories': 2.5, 'cholesterol': 3, 'sodium': 4.5}],
{'calories': '0', 'cholesterol': 'less than 5 mg', 'sodium': '0 mg'}],
['repair check: calorie insignificance test', [{'calories': 5, 'cholesterol': 12, 'fat': 2.75}],
{'calories': '5', 'cholesterol': '10 mg', 'fat': '3 g'}],
['generated control 1', [{'calories': 54.5, 'carbs': 0.5, 'cholesterol': 2.2, 'sodium': 192.5}],
{'calories': '50', 'carbs': 'less than 1 g', 'cholesterol': 'less than 5 mg', 'sodium': '190 mg'}],
['generated control 2', [{'calories': 47, 'carbs': 1, 'cholesterol': 1.5, 'fat': 17, 'sodium': 199}],
{'calories': '45', 'carbs': '1 g', 'cholesterol': '0 mg', 'fat': '17 g', 'sodium': '200 mg'}],
['generated control 3', [{'cholesterol': 2.2, 'fat': 0.4, 'sodium': 190.5}],
{'cholesterol': 'less than 5 mg', 'fat': '0 g', 'sodium': '190 mg'}]],
[['insignificant calories', [{'calories': 4.9, 'fat': 0.4}], {'calories': '0', 'fat': '0 g'}],
['mid-band sodium', [{'carbs': 0.9, 'sodium': 137.5}], {'carbs': 'less than 1 g', 'sodium': '140 mg'}],
['large values', [{'calories': 245, 'cholesterol': 22.5, 'fat': 12.5, 'sodium': 455}],
{'calories': '250', 'cholesterol': '25 mg', 'fat': '13 g', 'sodium': '460 mg'}],
['regression: calorie insignificance test', [{'calories': 2.5, 'carbs': 0.5, 'fat': 1.25}],
{'calories': '0', 'carbs': 'less than 1 g', 'fat': '1.5 g'}],
['repair check: calorie insignificance test',
[{'calories': 5, 'cholesterol': 22.5, 'fat': 4.75, 'sodium': 1.75}],
{'calories': '5', 'cholesterol': '25 mg', 'fat': '5 g', 'sodium': '0 mg'}],
['generated control 1', [{'calories': 7.25, 'fat': 8.75, 'sodium': 0.25}],
{'calories': '5', 'fat': '9 g', 'sodium': '0 mg'}],
['generated control 2', [{'calories': 267.5, 'carbs': 1.5, 'cholesterol': 5, 'fat': 1.25, 'sodium': 0.25}],
{'calories': '270', 'carbs': '2 g', 'cholesterol': '5 mg', 'fat': '1.5 g', 'sodium': '0 mg'}],
['generated control 3', [{'calories': 333, 'cholesterol': 1.5, 'fat': 19.25, 'sodium': 5.25}],
{'calories': '330', 'cholesterol': '0 mg', 'fat': '19 g', 'sodium': '5 mg'}]],
[['mid-band sodium', [{'carbs': 0.9, 'sodium': 137.5}], {'carbs': 'less than 1 g', 'sodium': '140 mg'}],
['large values', [{'calories': 245, 'cholesterol': 22.5, 'fat': 12.5, 'sodium': 455}],
{'calories': '250', 'cholesterol': '25 mg', 'fat': '13 g', 'sodium': '460 mg'}],
['small cholesterol', [{'carbs': 12.5, 'cholesterol': 3}],
{'carbs': '13 g', 'cholesterol': 'less than 5 mg'}],
['regression: calorie insignificance test',
[{'calories': 2.5, 'cholesterol': 4.9, 'fat': 10.5, 'sodium': 133}],
{'calories': '0', 'cholesterol': 'less than 5 mg', 'fat': '11 g', 'sodium': '135 mg'}],
['repair check: calorie insignificance test', [{'calories': 5, 'carbs': 0.2, 'fat': 0.5, 'sodium': 175}],
{'calories': '5', 'carbs': '0 g', 'fat': '0.5 g', 'sodium': '180 mg'}],
['generated control 1', [{'carbs': 30.4, 'fat': 0.5, 'sodium': 1.25}],
{'carbs': '30 g', 'fat': '0.5 g', 'sodium': '0 mg'}],
['generated control 2', [{'calories': 45.25, 'carbs': 0.2, 'fat': 20.25, 'sodium': 0}],
{'calories': '45', 'carbs': '0 g', 'fat': '20 g', 'sodium': '0 mg'}],
['generated control 3', [{'calories': 54, 'fat': 4, 'sodium': 6}],
{'calories': '50', 'fat': '4 g', 'sodium': '5 mg'}]],
[['large values', [{'calories': 245, 'cholesterol': 22.5, 'fat': 12.5, 'sodium': 455}],
{'calories': '250', 'cholesterol': '25 mg', 'fat': '13 g', 'sodium': '460 mg'}],
['small cholesterol', [{'carbs': 12.5, 'cholesterol': 3}],
{'carbs': '13 g', 'cholesterol': 'less than 5 mg'}],
['empty panel', [{}], {}],
['regression: calorie insignificance test',
[{'calories': 4.6, 'carbs': 0.2, 'cholesterol': 12, 'sodium': 1.5}],
{'calories': '0', 'carbs': '0 g', 'cholesterol': '10 mg', 'sodium': '0 mg'}],
['repair check: calorie insignificance test', [{'calories': 5, 'carbs': 0.2, 'fat': 2.75}],
{'calories': '5', 'carbs': '0 g', 'fat': '3 g'}],
['generated control 1', [{'calories': 65, 'carbs': 0.9, 'cholesterol': 5, 'sodium': 151.5}],
{'calories': '70', 'carbs': 'less than 1 g', 'cholesterol': '5 mg', 'sodium': '150 mg'}],
['generated control 2', [{'carbs': 0.2, 'fat': 11.5}], {'carbs': '0 g', 'fat': '12 g'}],
['generated control 3', [{'calories': 56, 'cholesterol': 4.9, 'fat': 2.75, 'sodium': 10.75}],
{'calories': '60', 'cholesterol': 'less than 5 mg', 'fat': '3 g', 'sodium': '10 mg'}]],
[['small cholesterol', [{'carbs': 12.5, 'cholesterol': 3}],
{'carbs': '13 g', 'cholesterol': 'less than 5 mg'}],
['empty panel', [{}], {}],
['insignificant calories', [{'calories': 4.9, 'fat': 0.4}], {'calories': '0', 'fat': '0 g'}],
['regression: calorie insignificance test', [{'calories': 3.75, 'cholesterol': 5, 'fat': 3.25}],
{'calories': '0', 'cholesterol': '5 mg', 'fat': '3.5 g'}],
['repair check: calorie insignificance test', [{'calories': 5, 'carbs': 12.5, 'fat': 2.75, 'sodium': 93}],
{'calories': '5', 'carbs': '13 g', 'fat': '3 g', 'sodium': '95 mg'}],
['generated control 1', [{'cholesterol': 5, 'fat': 0, 'sodium': 145}],
{'cholesterol': '5 mg', 'fat': '0 g', 'sodium': '150 mg'}],
['generated control 2', [{'calories': 214, 'carbs': 1, 'cholesterol': 2.2}],
{'calories': '210', 'carbs': '1 g', 'cholesterol': 'less than 5 mg'}],
['generated control 3', [{'calories': 306, 'cholesterol': 22.5, 'sodium': 10.25}],
{'calories': '310', 'cholesterol': '25 mg', 'sodium': '10 mg'}]]]
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 |
|---|---|---|---|
| empty panel | {} | {} | Passed |
| insignificant calories | {'calories': '0', 'fat': '0 g'} | {'calories': '0', 'fat': '0 g'} | Passed |
| mid-band sodium | {'carbs': 'less than 1 g', 'sodium': '140 mg'} | {'carbs': 'less than 1 g', 'sodium': '140 mg'} | Passed |
| regression: calorie insignificance test | {'calories': '0', 'cholesterol': 'less than 5 mg', 'sodium': '0 mg'} | {'calories': '0', 'cholesterol': 'less than 5 mg', 'sodium': '0 mg'} | Passed |
| repair check: calorie insignificance test | {'calories': '0', 'cholesterol': '10 mg', 'fat': '3 g'} | {'calories': '5', 'cholesterol': '10 mg', 'fat': '3 g'} | Failed |
| generated control 1 | {'calories': '50', 'carbs': 'less than 1 g', 'cholesterol': 'less than 5 mg', 'sodium': '190 mg'} | {'calories': '50', 'carbs': 'less than 1 g', 'cholesterol': 'less than 5 mg', 'sodium': '190 mg'} | Passed |
| generated control 2 | {'calories': '45', 'carbs': '1 g', 'cholesterol': '0 mg', 'fat': '17 g', 'sodium': '200 mg'} | {'calories': '45', 'carbs': '1 g', 'cholesterol': '0 mg', 'fat': '17 g', 'sodium': '200 mg'} | Passed |
| generated control 3 | {'cholesterol': 'less than 5 mg', 'fat': '0 g', 'sodium': '190 mg'} | {'cholesterol': 'less than 5 mg', 'fat': '0 g', 'sodium': '190 mg'} | Passed |
SHA-256 / 118594e368663c23aabd25c445d1ff2def48fea32d499fbc96d1b4d26249ea0f
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(panel):
def near(x, step):
return math.floor(x / step + Fraction(1, 2)) * step
def show(v, unit):
t = format(float(v), 'g')
return t + ' ' + unit if unit else t
out = {}
if 'calories' in panel:
c = Fraction(str(panel['calories']))
out['calories'] = show(0 if c < 5 else near(c, 5) if c <= 50 else near(c, 10), '')
if 'fat' in panel:
x = Fraction(str(panel['fat']))
out['fat'] = show(0 if x < Fraction(1, 2) else near(x, Fraction(1, 2)) if x < 5 else near(x, 1), 'g')
if 'sodium' in panel:
x = Fraction(str(panel['sodium']))
out['sodium'] = show(0 if x < 5 else near(x, 5) if x <= 140 else near(x, 10), 'mg')
if 'cholesterol' in panel:
x = Fraction(str(panel['cholesterol']))
out['cholesterol'] = '0 mg' if x < 2 else 'less than 5 mg' if x < 5 else show(near(x, 5), 'mg')
if 'carbs' in panel:
x = Fraction(str(panel['carbs']))
out['carbs'] = '0 g' if x < Fraction(1, 2) else 'less than 1 g' if x < 1 else show(near(x, 1), 'g')
return out
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['empty panel', [{}], {}],
['insignificant calories', [{'calories': 4.9, 'fat': 0.4}], {'calories': '0', 'fat': '0 g'}],
['mid-band sodium', [{'carbs': 0.9, 'sodium': 137.5}], {'carbs': 'less than 1 g', 'sodium': '140 mg'}],
['regression: calorie insignificance test', [{'calories': 2.5, 'cholesterol': 3, 'sodium': 4.5}],
{'calories': '0', 'cholesterol': 'less than 5 mg', 'sodium': '0 mg'}],
['repair check: calorie insignificance test', [{'calories': 5, 'cholesterol': 12, 'fat': 2.75}],
{'calories': '5', 'cholesterol': '10 mg', 'fat': '3 g'}],
['generated control 1', [{'calories': 54.5, 'carbs': 0.5, 'cholesterol': 2.2, 'sodium': 192.5}],
{'calories': '50', 'carbs': 'less than 1 g', 'cholesterol': 'less than 5 mg', 'sodium': '190 mg'}],
['generated control 2', [{'calories': 47, 'carbs': 1, 'cholesterol': 1.5, 'fat': 17, 'sodium': 199}],
{'calories': '45', 'carbs': '1 g', 'cholesterol': '0 mg', 'fat': '17 g', 'sodium': '200 mg'}],
['generated control 3', [{'cholesterol': 2.2, 'fat': 0.4, 'sodium': 190.5}],
{'cholesterol': 'less than 5 mg', 'fat': '0 g', 'sodium': '190 mg'}]],
[['insignificant calories', [{'calories': 4.9, 'fat': 0.4}], {'calories': '0', 'fat': '0 g'}],
['mid-band sodium', [{'carbs': 0.9, 'sodium': 137.5}], {'carbs': 'less than 1 g', 'sodium': '140 mg'}],
['large values', [{'calories': 245, 'cholesterol': 22.5, 'fat': 12.5, 'sodium': 455}],
{'calories': '250', 'cholesterol': '25 mg', 'fat': '13 g', 'sodium': '460 mg'}],
['regression: calorie insignificance test', [{'calories': 2.5, 'carbs': 0.5, 'fat': 1.25}],
{'calories': '0', 'carbs': 'less than 1 g', 'fat': '1.5 g'}],
['repair check: calorie insignificance test',
[{'calories': 5, 'cholesterol': 22.5, 'fat': 4.75, 'sodium': 1.75}],
{'calories': '5', 'cholesterol': '25 mg', 'fat': '5 g', 'sodium': '0 mg'}],
['generated control 1', [{'calories': 7.25, 'fat': 8.75, 'sodium': 0.25}],
{'calories': '5', 'fat': '9 g', 'sodium': '0 mg'}],
['generated control 2', [{'calories': 267.5, 'carbs': 1.5, 'cholesterol': 5, 'fat': 1.25, 'sodium': 0.25}],
{'calories': '270', 'carbs': '2 g', 'cholesterol': '5 mg', 'fat': '1.5 g', 'sodium': '0 mg'}],
['generated control 3', [{'calories': 333, 'cholesterol': 1.5, 'fat': 19.25, 'sodium': 5.25}],
{'calories': '330', 'cholesterol': '0 mg', 'fat': '19 g', 'sodium': '5 mg'}]],
[['mid-band sodium', [{'carbs': 0.9, 'sodium': 137.5}], {'carbs': 'less than 1 g', 'sodium': '140 mg'}],
['large values', [{'calories': 245, 'cholesterol': 22.5, 'fat': 12.5, 'sodium': 455}],
{'calories': '250', 'cholesterol': '25 mg', 'fat': '13 g', 'sodium': '460 mg'}],
['small cholesterol', [{'carbs': 12.5, 'cholesterol': 3}],
{'carbs': '13 g', 'cholesterol': 'less than 5 mg'}],
['regression: calorie insignificance test',
[{'calories': 2.5, 'cholesterol': 4.9, 'fat': 10.5, 'sodium': 133}],
{'calories': '0', 'cholesterol': 'less than 5 mg', 'fat': '11 g', 'sodium': '135 mg'}],
['repair check: calorie insignificance test', [{'calories': 5, 'carbs': 0.2, 'fat': 0.5, 'sodium': 175}],
{'calories': '5', 'carbs': '0 g', 'fat': '0.5 g', 'sodium': '180 mg'}],
['generated control 1', [{'carbs': 30.4, 'fat': 0.5, 'sodium': 1.25}],
{'carbs': '30 g', 'fat': '0.5 g', 'sodium': '0 mg'}],
['generated control 2', [{'calories': 45.25, 'carbs': 0.2, 'fat': 20.25, 'sodium': 0}],
{'calories': '45', 'carbs': '0 g', 'fat': '20 g', 'sodium': '0 mg'}],
['generated control 3', [{'calories': 54, 'fat': 4, 'sodium': 6}],
{'calories': '50', 'fat': '4 g', 'sodium': '5 mg'}]],
[['large values', [{'calories': 245, 'cholesterol': 22.5, 'fat': 12.5, 'sodium': 455}],
{'calories': '250', 'cholesterol': '25 mg', 'fat': '13 g', 'sodium': '460 mg'}],
['small cholesterol', [{'carbs': 12.5, 'cholesterol': 3}],
{'carbs': '13 g', 'cholesterol': 'less than 5 mg'}],
['empty panel', [{}], {}],
['regression: calorie insignificance test',
[{'calories': 4.6, 'carbs': 0.2, 'cholesterol': 12, 'sodium': 1.5}],
{'calories': '0', 'carbs': '0 g', 'cholesterol': '10 mg', 'sodium': '0 mg'}],
['repair check: calorie insignificance test', [{'calories': 5, 'carbs': 0.2, 'fat': 2.75}],
{'calories': '5', 'carbs': '0 g', 'fat': '3 g'}],
['generated control 1', [{'calories': 65, 'carbs': 0.9, 'cholesterol': 5, 'sodium': 151.5}],
{'calories': '70', 'carbs': 'less than 1 g', 'cholesterol': '5 mg', 'sodium': '150 mg'}],
['generated control 2', [{'carbs': 0.2, 'fat': 11.5}], {'carbs': '0 g', 'fat': '12 g'}],
['generated control 3', [{'calories': 56, 'cholesterol': 4.9, 'fat': 2.75, 'sodium': 10.75}],
{'calories': '60', 'cholesterol': 'less than 5 mg', 'fat': '3 g', 'sodium': '10 mg'}]],
[['small cholesterol', [{'carbs': 12.5, 'cholesterol': 3}],
{'carbs': '13 g', 'cholesterol': 'less than 5 mg'}],
['empty panel', [{}], {}],
['insignificant calories', [{'calories': 4.9, 'fat': 0.4}], {'calories': '0', 'fat': '0 g'}],
['regression: calorie insignificance test', [{'calories': 3.75, 'cholesterol': 5, 'fat': 3.25}],
{'calories': '0', 'cholesterol': '5 mg', 'fat': '3.5 g'}],
['repair check: calorie insignificance test', [{'calories': 5, 'carbs': 12.5, 'fat': 2.75, 'sodium': 93}],
{'calories': '5', 'carbs': '13 g', 'fat': '3 g', 'sodium': '95 mg'}],
['generated control 1', [{'cholesterol': 5, 'fat': 0, 'sodium': 145}],
{'cholesterol': '5 mg', 'fat': '0 g', 'sodium': '150 mg'}],
['generated control 2', [{'calories': 214, 'carbs': 1, 'cholesterol': 2.2}],
{'calories': '210', 'carbs': '1 g', 'cholesterol': 'less than 5 mg'}],
['generated control 3', [{'calories': 306, 'cholesterol': 22.5, 'sodium': 10.25}],
{'calories': '310', 'cholesterol': '25 mg', 'sodium': '10 mg'}]]]
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 |
|---|---|---|---|
| empty panel | {} | {} | Passed |
| insignificant calories | {'calories': '0', 'fat': '0 g'} | {'calories': '0', 'fat': '0 g'} | Passed |
| mid-band sodium | {'carbs': 'less than 1 g', 'sodium': '140 mg'} | {'carbs': 'less than 1 g', 'sodium': '140 mg'} | Passed |
| regression: calorie insignificance test | {'calories': '0', 'cholesterol': 'less than 5 mg', 'sodium': '0 mg'} | {'calories': '0', 'cholesterol': 'less than 5 mg', 'sodium': '0 mg'} | Passed |
| repair check: calorie insignificance test | {'calories': '5', 'cholesterol': '10 mg', 'fat': '3 g'} | {'calories': '5', 'cholesterol': '10 mg', 'fat': '3 g'} | Passed |
| generated control 1 | {'calories': '50', 'carbs': 'less than 1 g', 'cholesterol': 'less than 5 mg', 'sodium': '190 mg'} | {'calories': '50', 'carbs': 'less than 1 g', 'cholesterol': 'less than 5 mg', 'sodium': '190 mg'} | Passed |
| generated control 2 | {'calories': '45', 'carbs': '1 g', 'cholesterol': '0 mg', 'fat': '17 g', 'sodium': '200 mg'} | {'calories': '45', 'carbs': '1 g', 'cholesterol': '0 mg', 'fat': '17 g', 'sodium': '200 mg'} | Passed |
| generated control 3 | {'cholesterol': 'less than 5 mg', 'fat': '0 g', 'sodium': '190 mg'} | {'cholesterol': 'less than 5 mg', 'fat': '0 g', 'sodium': '190 mg'} | Passed |
SHA-256 / a2054f15cd02f482f3ad78be1bd179a1787e433826cf842d774583201100bbff
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:26.663779+00:00.
Case digest / ef0e6039cacb6cd18a581918f85d92a19c8c965f2fe9f57b2f262d763502e118