FAILURE MAP
← Case archive

FA-61946 / Currency rounding and FX conversion / Open access

The converted control total is rebuilt from the already rounded lines · case 01

Invoice totals in the target currency drift from round(sum*rate) and no plug is ever posted.

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

ROOT CAUSE

The control total is computed as the sum of the individually rounded converted lines.

VERIFIED REPAIR

Compute the control total from the exact source sum times the rate, rounded once.

Unsuccessful approach: Rounding the control total half-up while lines are rounded half-even leaves a mismatched plug on ties.

Case contract

solve(lines, rate, exp): lines are source-currency decimal strings (credits negative), rate a decimal string, exp the target exponent. The converted total is round_half_even(sum(lines)*rate) at exp. Each line is converted and rounded half-even individually; the difference between the converted total and the sum of converted lines is posted in full to the line with the largest absolute source amount (earliest on ties). Return the converted lines as plain strings; zero lines are unsigned. An empty invoice returns [].

Why this case matters

Currency amounts must be rounded at the right stage and in the right unit, or ledgers, quotes and settlements drift by minor units.

1 / The failure

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json
from decimal import Decimal, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_FLOOR, ROUND_CEILING, ROUND_DOWN, ROUND_UP, ROUND_HALF_DOWN
N = 1
observations = []
def solve(lines, rate, exp):
    r = Decimal(rate)
    qt = Decimal(1).scaleb(-exp)
    def q(v): return v.quantize(qt, rounding=ROUND_HALF_EVEN)
    src = [Decimal(l) for l in lines]
    if not src: return []
    conv = [q(v * r) for v in src]
    total = sum(conv, Decimal(0))
    diff = total - sum(conv, Decimal(0))
    i = max(range(len(conv)), key=lambda k: (abs(src[k]), -k))
    conv[i] += diff
    return [format(abs(c) if c == 0 else c, 'f') for c in conv]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression control-total 1', (['10.01', '10.01', '10.01'], '0.5', 2), ['5.02', '5.00', '5.00']),
  ('regression control-total 2', (['135.46', '1241.19', '543.10'], '121.0106', 2),
   ['16392.10', '150197.14', '65720.86']),
  ('partial repair guard 1', (['93.13', '2587.99', '484.24', '2232.33', '1229.90'], '0.95000', 3),
   ['88.474', '2458.589', '460.028', '2120.714', '1168.405']),
  ('control: credit and debit', (['-3.33', '3.33'], '1.5', 2), ['-5.00', '5.00']),
  ('control: tiny credit', (['100.00', '-0.01'], '0.3', 2), ['30.00', '0.00']),
  ('control: empty invoice', ([], '1.1', 2), []),
  ('control: yen target', (['19.99', '5.01', '0.99'], '151.37', 0), ['3026', '758', '150'])],
 [('regression control-total 1', (['2009.44', '2296.55', '1393.27', '1770.57', '2944.58'], '81.6625', 2),
   ['164095.89', '187542.01', '113777.91', '144589.17', '240461.78']),
  ('regression control-total 2', (['1729.48', '-260.26', '1061.77', '2756.95'], '151.6104', 2),
   ['262207.15', '-39458.12', '160975.37', '417982.30']),
  ('partial repair guard 1', (['93.13', '2587.99', '484.24', '2232.33', '1229.90'], '0.95000', 3),
   ['88.474', '2458.589', '460.028', '2120.714', '1168.405']),
  ('control: empty invoice', ([], '1.1', 2), []),
  ('control: yen target', (['19.99', '5.01', '0.99'], '151.37', 0), ['3026', '758', '150']),
  ('control: dinar target', (['1.25', '2.50'], '0.3071', 3), ['0.384', '0.768']),
  ('control: single line', (['123.45'], '0.9215', 2), ['113.76'])],
 [('regression control-total 1', (['607.45', '-205.45', '116.41', '195.72', '309.63'], '1991.467', 2),
   ['1209716.64', '-409146.90', '231826.67', '389769.92', '616617.93']),
  ('regression control-total 2', (['2340.71', '566.48', '2486.21', '1232.14'], '11.46450', 3),
   ['26835.070', '6494.410', '28503.154', '14125.869']),
  ('partial repair guard 1', (['93.13', '2587.99', '484.24', '2232.33', '1229.90'], '0.95000', 3),
   ['88.474', '2458.589', '460.028', '2120.714', '1168.405']),
  ('control: single line', (['123.45'], '0.9215', 2), ['113.76']),
  ('control: largest is credit', (['-50.05', '20.03', '20.03'], '1.0833', 2), ['-54.22', '21.70', '21.70']),
  ('control: three equal lines', (['10.01', '10.01', '10.01'], '0.5', 2), ['5.02', '5.00', '5.00']),
  ('control: credit and debit', (['-3.33', '3.33'], '1.5', 2), ['-5.00', '5.00'])],
 [('regression control-total 1', (['2564.41', '147.72', '2843.10'], '11.81884', 3),
   ['30308.351', '1745.879', '33602.145']),
  ('regression control-total 2', (['2885.93', '241.41'], '10.35728', 0), ['29891', '2500']),
  ('partial repair guard 1', (['93.13', '2587.99', '484.24', '2232.33', '1229.90'], '0.95000', 3),
   ['88.474', '2458.589', '460.028', '2120.714', '1168.405']),
  ('control: credit and debit', (['-3.33', '3.33'], '1.5', 2), ['-5.00', '5.00']),
  ('control: tiny credit', (['100.00', '-0.01'], '0.3', 2), ['30.00', '0.00']),
  ('control: empty invoice', ([], '1.1', 2), []),
  ('control: yen target', (['19.99', '5.01', '0.99'], '151.37', 0), ['3026', '758', '150'])],
 [('regression control-total 1', (['474.23', '2013.40'], '2.07137', 2), ['982.31', '4170.49']),
  ('regression control-total 2', (['1616.46', '-55.89', '2559.69'], '35.6985', 3),
   ['57705.197', '-1995.189', '91377.094']),
  ('partial repair guard 1', (['93.13', '2587.99', '484.24', '2232.33', '1229.90'], '0.95000', 3),
   ['88.474', '2458.589', '460.028', '2120.714', '1168.405']),
  ('control: yen target', (['19.99', '5.01', '0.99'], '151.37', 0), ['3026', '758', '150']),
  ('control: dinar target', (['1.25', '2.50'], '0.3071', 3), ['0.384', '0.768']),
  ('control: single line', (['123.45'], '0.9215', 2), ['113.76']),
  ('control: largest is credit', (['-50.05', '20.03', '20.03'], '1.0833', 2), ['-54.22', '21.70', '21.70'])]]
for label, args, expected in cases[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
regression control-total 1['5.00', '5.00', '5.00']['5.02', '5.00', '5.00']Failed
regression control-total 2['16392.10', '150197.15', '65720.86']['16392.10', '150197.14', '65720.86']Failed
partial repair guard 1['88.474', '2458.590', '460.028', '2120.714', '1168.405']['88.474', '2458.589', '460.028', '2120.714', '1168.405']Failed
control: credit and debit['-5.00', '5.00']['-5.00', '5.00']Passed
control: tiny credit['30.00', '0.00']['30.00', '0.00']Passed
control: empty invoice[][]Passed
control: yen target['3026', '758', '150']['3026', '758', '150']Passed

SHA-256 / 9bc40823133ad6f25edfa3cd4c2504af2799eb3f4dd06951a6259d7da69d33b6

2 / The unsuccessful fix

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json
from decimal import Decimal, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_FLOOR, ROUND_CEILING, ROUND_DOWN, ROUND_UP, ROUND_HALF_DOWN
N = 1
observations = []
def solve(lines, rate, exp):
    r = Decimal(rate)
    qt = Decimal(1).scaleb(-exp)
    def q(v): return v.quantize(qt, rounding=ROUND_HALF_EVEN)
    src = [Decimal(l) for l in lines]
    if not src: return []
    conv = [q(v * r) for v in src]
    total = (sum(src, Decimal(0)) * r).quantize(qt, rounding=ROUND_HALF_UP)
    diff = total - sum(conv, Decimal(0))
    i = max(range(len(conv)), key=lambda k: (abs(src[k]), -k))
    conv[i] += diff
    return [format(abs(c) if c == 0 else c, 'f') for c in conv]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression control-total 1', (['10.01', '10.01', '10.01'], '0.5', 2), ['5.02', '5.00', '5.00']),
  ('regression control-total 2', (['135.46', '1241.19', '543.10'], '121.0106', 2),
   ['16392.10', '150197.14', '65720.86']),
  ('partial repair guard 1', (['93.13', '2587.99', '484.24', '2232.33', '1229.90'], '0.95000', 3),
   ['88.474', '2458.589', '460.028', '2120.714', '1168.405']),
  ('control: credit and debit', (['-3.33', '3.33'], '1.5', 2), ['-5.00', '5.00']),
  ('control: tiny credit', (['100.00', '-0.01'], '0.3', 2), ['30.00', '0.00']),
  ('control: empty invoice', ([], '1.1', 2), []),
  ('control: yen target', (['19.99', '5.01', '0.99'], '151.37', 0), ['3026', '758', '150'])],
 [('regression control-total 1', (['2009.44', '2296.55', '1393.27', '1770.57', '2944.58'], '81.6625', 2),
   ['164095.89', '187542.01', '113777.91', '144589.17', '240461.78']),
  ('regression control-total 2', (['1729.48', '-260.26', '1061.77', '2756.95'], '151.6104', 2),
   ['262207.15', '-39458.12', '160975.37', '417982.30']),
  ('partial repair guard 1', (['93.13', '2587.99', '484.24', '2232.33', '1229.90'], '0.95000', 3),
   ['88.474', '2458.589', '460.028', '2120.714', '1168.405']),
  ('control: empty invoice', ([], '1.1', 2), []),
  ('control: yen target', (['19.99', '5.01', '0.99'], '151.37', 0), ['3026', '758', '150']),
  ('control: dinar target', (['1.25', '2.50'], '0.3071', 3), ['0.384', '0.768']),
  ('control: single line', (['123.45'], '0.9215', 2), ['113.76'])],
 [('regression control-total 1', (['607.45', '-205.45', '116.41', '195.72', '309.63'], '1991.467', 2),
   ['1209716.64', '-409146.90', '231826.67', '389769.92', '616617.93']),
  ('regression control-total 2', (['2340.71', '566.48', '2486.21', '1232.14'], '11.46450', 3),
   ['26835.070', '6494.410', '28503.154', '14125.869']),
  ('partial repair guard 1', (['93.13', '2587.99', '484.24', '2232.33', '1229.90'], '0.95000', 3),
   ['88.474', '2458.589', '460.028', '2120.714', '1168.405']),
  ('control: single line', (['123.45'], '0.9215', 2), ['113.76']),
  ('control: largest is credit', (['-50.05', '20.03', '20.03'], '1.0833', 2), ['-54.22', '21.70', '21.70']),
  ('control: three equal lines', (['10.01', '10.01', '10.01'], '0.5', 2), ['5.02', '5.00', '5.00']),
  ('control: credit and debit', (['-3.33', '3.33'], '1.5', 2), ['-5.00', '5.00'])],
 [('regression control-total 1', (['2564.41', '147.72', '2843.10'], '11.81884', 3),
   ['30308.351', '1745.879', '33602.145']),
  ('regression control-total 2', (['2885.93', '241.41'], '10.35728', 0), ['29891', '2500']),
  ('partial repair guard 1', (['93.13', '2587.99', '484.24', '2232.33', '1229.90'], '0.95000', 3),
   ['88.474', '2458.589', '460.028', '2120.714', '1168.405']),
  ('control: credit and debit', (['-3.33', '3.33'], '1.5', 2), ['-5.00', '5.00']),
  ('control: tiny credit', (['100.00', '-0.01'], '0.3', 2), ['30.00', '0.00']),
  ('control: empty invoice', ([], '1.1', 2), []),
  ('control: yen target', (['19.99', '5.01', '0.99'], '151.37', 0), ['3026', '758', '150'])],
 [('regression control-total 1', (['474.23', '2013.40'], '2.07137', 2), ['982.31', '4170.49']),
  ('regression control-total 2', (['1616.46', '-55.89', '2559.69'], '35.6985', 3),
   ['57705.197', '-1995.189', '91377.094']),
  ('partial repair guard 1', (['93.13', '2587.99', '484.24', '2232.33', '1229.90'], '0.95000', 3),
   ['88.474', '2458.589', '460.028', '2120.714', '1168.405']),
  ('control: yen target', (['19.99', '5.01', '0.99'], '151.37', 0), ['3026', '758', '150']),
  ('control: dinar target', (['1.25', '2.50'], '0.3071', 3), ['0.384', '0.768']),
  ('control: single line', (['123.45'], '0.9215', 2), ['113.76']),
  ('control: largest is credit', (['-50.05', '20.03', '20.03'], '1.0833', 2), ['-54.22', '21.70', '21.70'])]]
for label, args, expected in cases[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
regression control-total 1['5.02', '5.00', '5.00']['5.02', '5.00', '5.00']Passed
regression control-total 2['16392.10', '150197.14', '65720.86']['16392.10', '150197.14', '65720.86']Passed
partial repair guard 1['88.474', '2458.590', '460.028', '2120.714', '1168.405']['88.474', '2458.589', '460.028', '2120.714', '1168.405']Failed
control: credit and debit['-5.00', '5.00']['-5.00', '5.00']Passed
control: tiny credit['30.00', '0.00']['30.00', '0.00']Passed
control: empty invoice[][]Passed
control: yen target['3026', '758', '150']['3026', '758', '150']Passed

SHA-256 / 93d0bf9ce9da746971da583c601d071adc5883bdcaee213ef5bf23919dc44c55

3 / The verified repair

Exit 0
"""Failure Map reference implementation. Python standard library only."""
import json
from decimal import Decimal, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_FLOOR, ROUND_CEILING, ROUND_DOWN, ROUND_UP, ROUND_HALF_DOWN
N = 1
observations = []
def solve(lines, rate, exp):
    r = Decimal(rate)
    qt = Decimal(1).scaleb(-exp)
    def q(v): return v.quantize(qt, rounding=ROUND_HALF_EVEN)
    src = [Decimal(l) for l in lines]
    if not src: return []
    conv = [q(v * r) for v in src]
    total = q(sum(src, Decimal(0)) * r)
    diff = total - sum(conv, Decimal(0))
    i = max(range(len(conv)), key=lambda k: (abs(src[k]), -k))
    conv[i] += diff
    return [format(abs(c) if c == 0 else c, 'f') for c in conv]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression control-total 1', (['10.01', '10.01', '10.01'], '0.5', 2), ['5.02', '5.00', '5.00']),
  ('regression control-total 2', (['135.46', '1241.19', '543.10'], '121.0106', 2),
   ['16392.10', '150197.14', '65720.86']),
  ('partial repair guard 1', (['93.13', '2587.99', '484.24', '2232.33', '1229.90'], '0.95000', 3),
   ['88.474', '2458.589', '460.028', '2120.714', '1168.405']),
  ('control: credit and debit', (['-3.33', '3.33'], '1.5', 2), ['-5.00', '5.00']),
  ('control: tiny credit', (['100.00', '-0.01'], '0.3', 2), ['30.00', '0.00']),
  ('control: empty invoice', ([], '1.1', 2), []),
  ('control: yen target', (['19.99', '5.01', '0.99'], '151.37', 0), ['3026', '758', '150'])],
 [('regression control-total 1', (['2009.44', '2296.55', '1393.27', '1770.57', '2944.58'], '81.6625', 2),
   ['164095.89', '187542.01', '113777.91', '144589.17', '240461.78']),
  ('regression control-total 2', (['1729.48', '-260.26', '1061.77', '2756.95'], '151.6104', 2),
   ['262207.15', '-39458.12', '160975.37', '417982.30']),
  ('partial repair guard 1', (['93.13', '2587.99', '484.24', '2232.33', '1229.90'], '0.95000', 3),
   ['88.474', '2458.589', '460.028', '2120.714', '1168.405']),
  ('control: empty invoice', ([], '1.1', 2), []),
  ('control: yen target', (['19.99', '5.01', '0.99'], '151.37', 0), ['3026', '758', '150']),
  ('control: dinar target', (['1.25', '2.50'], '0.3071', 3), ['0.384', '0.768']),
  ('control: single line', (['123.45'], '0.9215', 2), ['113.76'])],
 [('regression control-total 1', (['607.45', '-205.45', '116.41', '195.72', '309.63'], '1991.467', 2),
   ['1209716.64', '-409146.90', '231826.67', '389769.92', '616617.93']),
  ('regression control-total 2', (['2340.71', '566.48', '2486.21', '1232.14'], '11.46450', 3),
   ['26835.070', '6494.410', '28503.154', '14125.869']),
  ('partial repair guard 1', (['93.13', '2587.99', '484.24', '2232.33', '1229.90'], '0.95000', 3),
   ['88.474', '2458.589', '460.028', '2120.714', '1168.405']),
  ('control: single line', (['123.45'], '0.9215', 2), ['113.76']),
  ('control: largest is credit', (['-50.05', '20.03', '20.03'], '1.0833', 2), ['-54.22', '21.70', '21.70']),
  ('control: three equal lines', (['10.01', '10.01', '10.01'], '0.5', 2), ['5.02', '5.00', '5.00']),
  ('control: credit and debit', (['-3.33', '3.33'], '1.5', 2), ['-5.00', '5.00'])],
 [('regression control-total 1', (['2564.41', '147.72', '2843.10'], '11.81884', 3),
   ['30308.351', '1745.879', '33602.145']),
  ('regression control-total 2', (['2885.93', '241.41'], '10.35728', 0), ['29891', '2500']),
  ('partial repair guard 1', (['93.13', '2587.99', '484.24', '2232.33', '1229.90'], '0.95000', 3),
   ['88.474', '2458.589', '460.028', '2120.714', '1168.405']),
  ('control: credit and debit', (['-3.33', '3.33'], '1.5', 2), ['-5.00', '5.00']),
  ('control: tiny credit', (['100.00', '-0.01'], '0.3', 2), ['30.00', '0.00']),
  ('control: empty invoice', ([], '1.1', 2), []),
  ('control: yen target', (['19.99', '5.01', '0.99'], '151.37', 0), ['3026', '758', '150'])],
 [('regression control-total 1', (['474.23', '2013.40'], '2.07137', 2), ['982.31', '4170.49']),
  ('regression control-total 2', (['1616.46', '-55.89', '2559.69'], '35.6985', 3),
   ['57705.197', '-1995.189', '91377.094']),
  ('partial repair guard 1', (['93.13', '2587.99', '484.24', '2232.33', '1229.90'], '0.95000', 3),
   ['88.474', '2458.589', '460.028', '2120.714', '1168.405']),
  ('control: yen target', (['19.99', '5.01', '0.99'], '151.37', 0), ['3026', '758', '150']),
  ('control: dinar target', (['1.25', '2.50'], '0.3071', 3), ['0.384', '0.768']),
  ('control: single line', (['123.45'], '0.9215', 2), ['113.76']),
  ('control: largest is credit', (['-50.05', '20.03', '20.03'], '1.0833', 2), ['-54.22', '21.70', '21.70'])]]
for label, args, expected in cases[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
regression control-total 1['5.02', '5.00', '5.00']['5.02', '5.00', '5.00']Passed
regression control-total 2['16392.10', '150197.14', '65720.86']['16392.10', '150197.14', '65720.86']Passed
partial repair guard 1['88.474', '2458.589', '460.028', '2120.714', '1168.405']['88.474', '2458.589', '460.028', '2120.714', '1168.405']Passed
control: credit and debit['-5.00', '5.00']['-5.00', '5.00']Passed
control: tiny credit['30.00', '0.00']['30.00', '0.00']Passed
control: empty invoice[][]Passed
control: yen target['3026', '758', '150']['3026', '758', '150']Passed

SHA-256 / ffd696af8cd834593941b68fda30329e201c3aa781ce41df3710b56a3af96c41

Verification & scope

A deterministic, bounded teaching model with a stipulated toy contract; it makes no claim of conformance to any real regulation, standard, or institution's rules. 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:00.023945+00:00.

Case digest / 86d79c04530023e605733ecf7c03729a9a52b2cbb54e8ac7807ad84587a1cbbd