FA-64216 / Medication dosing calculations / Open access
Concentration and electrolyte unit conversions: mole to equivalent sign · case 01
Phosphate-style divalent anions produce negative mEq amounts.
ROOT CAUSE
The signed valence is used, so anions yield negative equivalents.
VERIFIED REPAIR
Multiply mmol by the absolute valence.
Unsuccessful approach: Dividing by the valence inverts the mmol-to-mEq relationship.
Case contract
Input {kind, value, mw?, valence?}. kind "pct": w/v percent to mg/mL (x10). kind "ratio": "a:b" ratio strength (a g in b mL) to mg/mL = a*1000/b, whitespace around parts allowed. kind "meq_to_mg": mg = mEq*mw/|valence|. kind "mmol_to_meq": mEq = mmol*|valence|. Any other kind returns {error: UNKNOWN_KIND}. Otherwise {result} rounded to 4 decimals.
Why this case matters
Strength labels mix percent, ratio and equivalent units; each conversion has its own orientation and sign rule.
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(x):
k, v = x['kind'], x['value']
if k == 'pct':
r = Fraction(str(v)) * 10
elif k == 'ratio':
a, b = [Fraction(p.strip()) for p in v.split(':')]
r = a * 1000 / b
elif k == 'meq_to_mg':
r = Fraction(str(v)) * Fraction(str(x['mw'])) / abs(x['valence'])
elif k == 'mmol_to_meq':
r = Fraction(str(v)) * x['valence']
else:
return {'error': 'UNKNOWN_KIND'}
return {'result': round(float(r), 4)}
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('percent solution', {'kind': 'pct', 'value': '0.9'}, {'result': 9.0}),
('one in ten thousand', {'kind': 'ratio', 'value': '1:10000'}, {'result': 0.1}),
('ratio with numerator above one', {'kind': 'ratio', 'value': '2 : 1000'}, {'result': 2.0}),
('divalent cation mEq to mg',
{'kind': 'meq_to_mg', 'value': 10, 'mw': '40.08', 'valence': 2},
{'result': 200.4}),
('anion mEq to mg', {'kind': 'meq_to_mg', 'value': 20, 'mw': '35.45', 'valence': -1}, {'result': 709.0}),
('mmol of anion to mEq', {'kind': 'mmol_to_meq', 'value': 5, 'valence': -2}, {'result': 10.0}),
('mmol of monovalent cation', {'kind': 'mmol_to_meq', 'value': 8, 'valence': 1}, {'result': 8.0}),
('unknown conversion kind', {'kind': 'mcg', 'value': 1}, {'error': 'UNKNOWN_KIND'})],
[('percent solution', {'kind': 'pct', 'value': '5'}, {'result': 50.0}),
('one in ten thousand', {'kind': 'ratio', 'value': '1:20000'}, {'result': 0.05}),
('ratio with numerator above one', {'kind': 'ratio', 'value': '3 : 1000'}, {'result': 3.0}),
('divalent cation mEq to mg',
{'kind': 'meq_to_mg', 'value': 20, 'mw': '40.08', 'valence': 2},
{'result': 400.8}),
('anion mEq to mg', {'kind': 'meq_to_mg', 'value': 20, 'mw': '35.45', 'valence': -2}, {'result': 354.5}),
('mmol of anion to mEq', {'kind': 'mmol_to_meq', 'value': 10, 'valence': -2}, {'result': 20.0}),
('mmol of monovalent cation', {'kind': 'mmol_to_meq', 'value': 9, 'valence': 1}, {'result': 9.0}),
('unknown conversion kind', {'kind': 'iu', 'value': 1}, {'error': 'UNKNOWN_KIND'})],
[('percent solution', {'kind': 'pct', 'value': '2'}, {'result': 20.0}),
('one in ten thousand', {'kind': 'ratio', 'value': '1:30000'}, {'result': 0.0333}),
('ratio with numerator above one', {'kind': 'ratio', 'value': '4 : 1000'}, {'result': 4.0}),
('divalent cation mEq to mg',
{'kind': 'meq_to_mg', 'value': 30, 'mw': '40.08', 'valence': 2},
{'result': 601.2}),
('anion mEq to mg', {'kind': 'meq_to_mg', 'value': 20, 'mw': '35.45', 'valence': -3}, {'result': 236.3333}),
('mmol of anion to mEq', {'kind': 'mmol_to_meq', 'value': 15, 'valence': -2}, {'result': 30.0}),
('mmol of monovalent cation', {'kind': 'mmol_to_meq', 'value': 10, 'valence': 1}, {'result': 10.0}),
('unknown conversion kind', {'kind': 'pct ', 'value': 1}, {'error': 'UNKNOWN_KIND'})],
[('percent solution', {'kind': 'pct', 'value': '0.45'}, {'result': 4.5}),
('one in ten thousand', {'kind': 'ratio', 'value': '1:40000'}, {'result': 0.025}),
('ratio with numerator above one', {'kind': 'ratio', 'value': '5 : 1000'}, {'result': 5.0}),
('divalent cation mEq to mg',
{'kind': 'meq_to_mg', 'value': 40, 'mw': '40.08', 'valence': 2},
{'result': 801.6}),
('anion mEq to mg', {'kind': 'meq_to_mg', 'value': 20, 'mw': '35.45', 'valence': -4}, {'result': 177.25}),
('mmol of anion to mEq', {'kind': 'mmol_to_meq', 'value': 20, 'valence': -2}, {'result': 40.0}),
('mmol of monovalent cation', {'kind': 'mmol_to_meq', 'value': 11, 'valence': 1}, {'result': 11.0}),
('unknown conversion kind', {'kind': 'Ratio', 'value': 1}, {'error': 'UNKNOWN_KIND'})],
[('percent solution', {'kind': 'pct', 'value': '10'}, {'result': 100.0}),
('one in ten thousand', {'kind': 'ratio', 'value': '1:50000'}, {'result': 0.02}),
('ratio with numerator above one', {'kind': 'ratio', 'value': '6 : 1000'}, {'result': 6.0}),
('divalent cation mEq to mg',
{'kind': 'meq_to_mg', 'value': 50, 'mw': '40.08', 'valence': 2},
{'result': 1002.0}),
('anion mEq to mg', {'kind': 'meq_to_mg', 'value': 20, 'mw': '35.45', 'valence': -5}, {'result': 141.8}),
('mmol of anion to mEq', {'kind': 'mmol_to_meq', 'value': 25, 'valence': -2}, {'result': 50.0}),
('mmol of monovalent cation', {'kind': 'mmol_to_meq', 'value': 12, 'valence': 1}, {'result': 12.0}),
('unknown conversion kind', {'kind': '', 'value': 1}, {'error': 'UNKNOWN_KIND'})]]
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 |
|---|---|---|---|
| percent solution | {'result': 9.0} | {'result': 9.0} | Passed |
| one in ten thousand | {'result': 0.1} | {'result': 0.1} | Passed |
| ratio with numerator above one | {'result': 2.0} | {'result': 2.0} | Passed |
| divalent cation mEq to mg | {'result': 200.4} | {'result': 200.4} | Passed |
| anion mEq to mg | {'result': 709.0} | {'result': 709.0} | Passed |
| mmol of anion to mEq | {'result': -10.0} | {'result': 10.0} | Failed |
| mmol of monovalent cation | {'result': 8.0} | {'result': 8.0} | Passed |
| unknown conversion kind | {'error': 'UNKNOWN_KIND'} | {'error': 'UNKNOWN_KIND'} | Passed |
SHA-256 / 61f1269bdfbecf44b3a7e04f54c441efe481d2d1cace57e8c0a106b0c4e764c9
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(x):
k, v = x['kind'], x['value']
if k == 'pct':
r = Fraction(str(v)) * 10
elif k == 'ratio':
a, b = [Fraction(p.strip()) for p in v.split(':')]
r = a * 1000 / b
elif k == 'meq_to_mg':
r = Fraction(str(v)) * Fraction(str(x['mw'])) / abs(x['valence'])
elif k == 'mmol_to_meq':
r = Fraction(str(v)) / abs(x['valence'])
else:
return {'error': 'UNKNOWN_KIND'}
return {'result': round(float(r), 4)}
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('percent solution', {'kind': 'pct', 'value': '0.9'}, {'result': 9.0}),
('one in ten thousand', {'kind': 'ratio', 'value': '1:10000'}, {'result': 0.1}),
('ratio with numerator above one', {'kind': 'ratio', 'value': '2 : 1000'}, {'result': 2.0}),
('divalent cation mEq to mg',
{'kind': 'meq_to_mg', 'value': 10, 'mw': '40.08', 'valence': 2},
{'result': 200.4}),
('anion mEq to mg', {'kind': 'meq_to_mg', 'value': 20, 'mw': '35.45', 'valence': -1}, {'result': 709.0}),
('mmol of anion to mEq', {'kind': 'mmol_to_meq', 'value': 5, 'valence': -2}, {'result': 10.0}),
('mmol of monovalent cation', {'kind': 'mmol_to_meq', 'value': 8, 'valence': 1}, {'result': 8.0}),
('unknown conversion kind', {'kind': 'mcg', 'value': 1}, {'error': 'UNKNOWN_KIND'})],
[('percent solution', {'kind': 'pct', 'value': '5'}, {'result': 50.0}),
('one in ten thousand', {'kind': 'ratio', 'value': '1:20000'}, {'result': 0.05}),
('ratio with numerator above one', {'kind': 'ratio', 'value': '3 : 1000'}, {'result': 3.0}),
('divalent cation mEq to mg',
{'kind': 'meq_to_mg', 'value': 20, 'mw': '40.08', 'valence': 2},
{'result': 400.8}),
('anion mEq to mg', {'kind': 'meq_to_mg', 'value': 20, 'mw': '35.45', 'valence': -2}, {'result': 354.5}),
('mmol of anion to mEq', {'kind': 'mmol_to_meq', 'value': 10, 'valence': -2}, {'result': 20.0}),
('mmol of monovalent cation', {'kind': 'mmol_to_meq', 'value': 9, 'valence': 1}, {'result': 9.0}),
('unknown conversion kind', {'kind': 'iu', 'value': 1}, {'error': 'UNKNOWN_KIND'})],
[('percent solution', {'kind': 'pct', 'value': '2'}, {'result': 20.0}),
('one in ten thousand', {'kind': 'ratio', 'value': '1:30000'}, {'result': 0.0333}),
('ratio with numerator above one', {'kind': 'ratio', 'value': '4 : 1000'}, {'result': 4.0}),
('divalent cation mEq to mg',
{'kind': 'meq_to_mg', 'value': 30, 'mw': '40.08', 'valence': 2},
{'result': 601.2}),
('anion mEq to mg', {'kind': 'meq_to_mg', 'value': 20, 'mw': '35.45', 'valence': -3}, {'result': 236.3333}),
('mmol of anion to mEq', {'kind': 'mmol_to_meq', 'value': 15, 'valence': -2}, {'result': 30.0}),
('mmol of monovalent cation', {'kind': 'mmol_to_meq', 'value': 10, 'valence': 1}, {'result': 10.0}),
('unknown conversion kind', {'kind': 'pct ', 'value': 1}, {'error': 'UNKNOWN_KIND'})],
[('percent solution', {'kind': 'pct', 'value': '0.45'}, {'result': 4.5}),
('one in ten thousand', {'kind': 'ratio', 'value': '1:40000'}, {'result': 0.025}),
('ratio with numerator above one', {'kind': 'ratio', 'value': '5 : 1000'}, {'result': 5.0}),
('divalent cation mEq to mg',
{'kind': 'meq_to_mg', 'value': 40, 'mw': '40.08', 'valence': 2},
{'result': 801.6}),
('anion mEq to mg', {'kind': 'meq_to_mg', 'value': 20, 'mw': '35.45', 'valence': -4}, {'result': 177.25}),
('mmol of anion to mEq', {'kind': 'mmol_to_meq', 'value': 20, 'valence': -2}, {'result': 40.0}),
('mmol of monovalent cation', {'kind': 'mmol_to_meq', 'value': 11, 'valence': 1}, {'result': 11.0}),
('unknown conversion kind', {'kind': 'Ratio', 'value': 1}, {'error': 'UNKNOWN_KIND'})],
[('percent solution', {'kind': 'pct', 'value': '10'}, {'result': 100.0}),
('one in ten thousand', {'kind': 'ratio', 'value': '1:50000'}, {'result': 0.02}),
('ratio with numerator above one', {'kind': 'ratio', 'value': '6 : 1000'}, {'result': 6.0}),
('divalent cation mEq to mg',
{'kind': 'meq_to_mg', 'value': 50, 'mw': '40.08', 'valence': 2},
{'result': 1002.0}),
('anion mEq to mg', {'kind': 'meq_to_mg', 'value': 20, 'mw': '35.45', 'valence': -5}, {'result': 141.8}),
('mmol of anion to mEq', {'kind': 'mmol_to_meq', 'value': 25, 'valence': -2}, {'result': 50.0}),
('mmol of monovalent cation', {'kind': 'mmol_to_meq', 'value': 12, 'valence': 1}, {'result': 12.0}),
('unknown conversion kind', {'kind': '', 'value': 1}, {'error': 'UNKNOWN_KIND'})]]
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 |
|---|---|---|---|
| percent solution | {'result': 9.0} | {'result': 9.0} | Passed |
| one in ten thousand | {'result': 0.1} | {'result': 0.1} | Passed |
| ratio with numerator above one | {'result': 2.0} | {'result': 2.0} | Passed |
| divalent cation mEq to mg | {'result': 200.4} | {'result': 200.4} | Passed |
| anion mEq to mg | {'result': 709.0} | {'result': 709.0} | Passed |
| mmol of anion to mEq | {'result': 2.5} | {'result': 10.0} | Failed |
| mmol of monovalent cation | {'result': 8.0} | {'result': 8.0} | Passed |
| unknown conversion kind | {'error': 'UNKNOWN_KIND'} | {'error': 'UNKNOWN_KIND'} | Passed |
SHA-256 / a8b2d9bcaac710ba613d7e3a0408a95061a861866b50fb0737c05c7c7e6b6dd2
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(x):
k, v = x['kind'], x['value']
if k == 'pct':
r = Fraction(str(v)) * 10
elif k == 'ratio':
a, b = [Fraction(p.strip()) for p in v.split(':')]
r = a * 1000 / b
elif k == 'meq_to_mg':
r = Fraction(str(v)) * Fraction(str(x['mw'])) / abs(x['valence'])
elif k == 'mmol_to_meq':
r = Fraction(str(v)) * abs(x['valence'])
else:
return {'error': 'UNKNOWN_KIND'}
return {'result': round(float(r), 4)}
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('percent solution', {'kind': 'pct', 'value': '0.9'}, {'result': 9.0}),
('one in ten thousand', {'kind': 'ratio', 'value': '1:10000'}, {'result': 0.1}),
('ratio with numerator above one', {'kind': 'ratio', 'value': '2 : 1000'}, {'result': 2.0}),
('divalent cation mEq to mg',
{'kind': 'meq_to_mg', 'value': 10, 'mw': '40.08', 'valence': 2},
{'result': 200.4}),
('anion mEq to mg', {'kind': 'meq_to_mg', 'value': 20, 'mw': '35.45', 'valence': -1}, {'result': 709.0}),
('mmol of anion to mEq', {'kind': 'mmol_to_meq', 'value': 5, 'valence': -2}, {'result': 10.0}),
('mmol of monovalent cation', {'kind': 'mmol_to_meq', 'value': 8, 'valence': 1}, {'result': 8.0}),
('unknown conversion kind', {'kind': 'mcg', 'value': 1}, {'error': 'UNKNOWN_KIND'})],
[('percent solution', {'kind': 'pct', 'value': '5'}, {'result': 50.0}),
('one in ten thousand', {'kind': 'ratio', 'value': '1:20000'}, {'result': 0.05}),
('ratio with numerator above one', {'kind': 'ratio', 'value': '3 : 1000'}, {'result': 3.0}),
('divalent cation mEq to mg',
{'kind': 'meq_to_mg', 'value': 20, 'mw': '40.08', 'valence': 2},
{'result': 400.8}),
('anion mEq to mg', {'kind': 'meq_to_mg', 'value': 20, 'mw': '35.45', 'valence': -2}, {'result': 354.5}),
('mmol of anion to mEq', {'kind': 'mmol_to_meq', 'value': 10, 'valence': -2}, {'result': 20.0}),
('mmol of monovalent cation', {'kind': 'mmol_to_meq', 'value': 9, 'valence': 1}, {'result': 9.0}),
('unknown conversion kind', {'kind': 'iu', 'value': 1}, {'error': 'UNKNOWN_KIND'})],
[('percent solution', {'kind': 'pct', 'value': '2'}, {'result': 20.0}),
('one in ten thousand', {'kind': 'ratio', 'value': '1:30000'}, {'result': 0.0333}),
('ratio with numerator above one', {'kind': 'ratio', 'value': '4 : 1000'}, {'result': 4.0}),
('divalent cation mEq to mg',
{'kind': 'meq_to_mg', 'value': 30, 'mw': '40.08', 'valence': 2},
{'result': 601.2}),
('anion mEq to mg', {'kind': 'meq_to_mg', 'value': 20, 'mw': '35.45', 'valence': -3}, {'result': 236.3333}),
('mmol of anion to mEq', {'kind': 'mmol_to_meq', 'value': 15, 'valence': -2}, {'result': 30.0}),
('mmol of monovalent cation', {'kind': 'mmol_to_meq', 'value': 10, 'valence': 1}, {'result': 10.0}),
('unknown conversion kind', {'kind': 'pct ', 'value': 1}, {'error': 'UNKNOWN_KIND'})],
[('percent solution', {'kind': 'pct', 'value': '0.45'}, {'result': 4.5}),
('one in ten thousand', {'kind': 'ratio', 'value': '1:40000'}, {'result': 0.025}),
('ratio with numerator above one', {'kind': 'ratio', 'value': '5 : 1000'}, {'result': 5.0}),
('divalent cation mEq to mg',
{'kind': 'meq_to_mg', 'value': 40, 'mw': '40.08', 'valence': 2},
{'result': 801.6}),
('anion mEq to mg', {'kind': 'meq_to_mg', 'value': 20, 'mw': '35.45', 'valence': -4}, {'result': 177.25}),
('mmol of anion to mEq', {'kind': 'mmol_to_meq', 'value': 20, 'valence': -2}, {'result': 40.0}),
('mmol of monovalent cation', {'kind': 'mmol_to_meq', 'value': 11, 'valence': 1}, {'result': 11.0}),
('unknown conversion kind', {'kind': 'Ratio', 'value': 1}, {'error': 'UNKNOWN_KIND'})],
[('percent solution', {'kind': 'pct', 'value': '10'}, {'result': 100.0}),
('one in ten thousand', {'kind': 'ratio', 'value': '1:50000'}, {'result': 0.02}),
('ratio with numerator above one', {'kind': 'ratio', 'value': '6 : 1000'}, {'result': 6.0}),
('divalent cation mEq to mg',
{'kind': 'meq_to_mg', 'value': 50, 'mw': '40.08', 'valence': 2},
{'result': 1002.0}),
('anion mEq to mg', {'kind': 'meq_to_mg', 'value': 20, 'mw': '35.45', 'valence': -5}, {'result': 141.8}),
('mmol of anion to mEq', {'kind': 'mmol_to_meq', 'value': 25, 'valence': -2}, {'result': 50.0}),
('mmol of monovalent cation', {'kind': 'mmol_to_meq', 'value': 12, 'valence': 1}, {'result': 12.0}),
('unknown conversion kind', {'kind': '', 'value': 1}, {'error': 'UNKNOWN_KIND'})]]
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 |
|---|---|---|---|
| percent solution | {'result': 9.0} | {'result': 9.0} | Passed |
| one in ten thousand | {'result': 0.1} | {'result': 0.1} | Passed |
| ratio with numerator above one | {'result': 2.0} | {'result': 2.0} | Passed |
| divalent cation mEq to mg | {'result': 200.4} | {'result': 200.4} | Passed |
| anion mEq to mg | {'result': 709.0} | {'result': 709.0} | Passed |
| mmol of anion to mEq | {'result': 10.0} | {'result': 10.0} | Passed |
| mmol of monovalent cation | {'result': 8.0} | {'result': 8.0} | Passed |
| unknown conversion kind | {'error': 'UNKNOWN_KIND'} | {'error': 'UNKNOWN_KIND'} | Passed |
SHA-256 / 9583d0428d501b313196ff6113812df9e04cb2fae429b6f9b0f02e1e1a6de5ed
Verification & scope
A deterministic toy software model with explicitly stipulated thresholds and formulas; it is not clinical guidance, not a validated medical calculator, and makes no claim of conformance to any published protocol. Numbered variants vary patient and order inputs. 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:47:22.319911+00:00.
Case digest / b417ed84517fffab16e18517e4dacf3a34c6b74c733ec7561d7561ff0031ac98