FAILURE MAP
← Case archive

FA-64211 / Medication dosing calculations / Open access

Concentration and electrolyte unit conversions: equivalent to mass valence · case 01

Calcium mEq convert to four times the correct milligram amount.

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

ROOT CAUSE

The molecular weight is multiplied by the valence instead of divided.

VERIFIED REPAIR

mg = mEq x MW / |valence|.

Unsuccessful approach: Dividing by the signed valence makes every anion conversion negative.

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)) * 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 fixtureActualExpectedOutcome
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': 801.6}{'result': 200.4}Failed
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 / c1823f49eeea6fb560bb43f1628be9cbf5a9360035e8ab7ca45fee4d38bf37b7

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'])) / 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 fixtureActualExpectedOutcome
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}Failed
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 / 7491c3474797fea10525c8d4cdb923c96810d4214e2b6c414ff33f4c66eac6b1

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

Case digest / ec83f6713ccf178e703a31b691fee84d615f2cf6458d4f2e56f597556e340271