merge: a tie between shares goes to the earlier part, exactly (K-U10b, D-76)

The rule (BS-partner-1, bookspec's Apportion) is that leftover cents go
to the largest remainders, ties to the earlier part. The engine ranked
the difference of quotients rounded at Decimal's 28th digit, so an exact
tie went where that digit sent it: 20.00 by 4/1/1 came out 13.33 / 3.34 /
3.33. It now ranks the exact remainder, whole x weight - part x total,
and gives 13.34 / 3.33 / 3.33. The owner decided for the exact ranking on
2026-11-09 (D-76, recorded here).

What moves: no golden, nothing between two parts or by equal weights, at
most a cent a part elsewhere (tests/kernel_bridge/ties.rs measures it).
Of the engine's splits only the K-1 worksheet's share of contributed
property divides by a total that is not one; a convention's split and the
frozen close rule already ranked exactly. Posted splits are not
recomputed.

Changed in the merge:
- The ranking's two products are the rounding ones (checked_mul_rounded).
  The kernel's checked_mul is exact or refused since K-U10a, and would
  refuse a split by weights of 27 places; the split is made, ranked by
  the nearest figures Decimal holds. A test splits by thirds, sevenths,
  ninths and elevenths at 27 and 28 places, both signs.
- Kani failed the unit as it came: with the exact ranking a part whose
  share is exact never takes a leftover unit (5 by 2/3/5 is 1, 2, 2, where
  i64's truncated quotients gave 2, 1, 2), so the cover "a part exactly
  one step above its truncated share" could not be reached. The harness
  split_by_weights_within_one_step now asserts the strict bound and that
  an exact share takes no unit; both covers are reached.
- docs/verification.md also takes K-U10a's account (sums and products
  exact or refused), which closes the edges it listed as pinned.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude committed 2026-11-09 22:39:13 -07:00
commit 5e9591725a
9 files changed
+613 -149

No files matched your search

+38 -13
View File
@@ -24,11 +24,10 @@ use super::{Amount, apportion, plug_last};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Leftover {
/// Each part is rounded down; the units left over go one each to the
/// largest remainders. The remainders are the rounded quotients'
/// differences, not exact rationals, so ranking follows the quotient:
/// an exact tie can go where the quotient's 28th digit sends it, and
/// only a tie in the rounded remainders goes to the earlier part. K-U10
/// decides the exact ranking. [`apportion`].
/// largest remainders. The remainders are exact: `whole x weight -
/// part x total`, an integer comparison, never the rounded quotient's
/// difference, so an exact tie goes to the earlier part (the Lean
/// model's rank, bookspec's `Apportion`). [`apportion`].
LargestRemainder,
/// The last part becomes the whole less the others. [`plug_last`].
LastAbsorbs,
@@ -201,11 +200,13 @@ const MAX_CORRECTION_STEPS: usize = 4096;
/// a part more than one step from the exact rational (K-U10, pinned
/// in the bridge). Under the first/last-absorbs policies only the
/// absorbing part may be further, by the rounding of all the others.
/// The largest-remainder ranking compares the rounded quotients'
/// remainders, not exact rationals: an exact tie can go where
/// `Decimal`'s 28th digit sends it, and only a tie in the rounded
/// remainders goes to the earlier part (K-U10 decides the exact
/// rule; the bridge pins today's answer).
/// The largest-remainder ranking compares the exact remainders
/// `whole x weight - part x total` (the quantity `share_floor_with`
/// returns, scaled by total; the sign flipped for a negative total),
/// never the rounded quotients' differences. An exact tie goes to the
/// earlier part, whatever `Decimal`'s 28th digit would have said
/// (K-U10, the owner's decision; exact while the products are, as
/// above).
/// 3. With total not zero, no part exceeds the whole when every weight
/// is in [0, total] and the whole is not negative; no part is
/// negative when the weights and the whole are not.
@@ -219,13 +220,19 @@ const MAX_CORRECTION_STEPS: usize = 4096;
/// promised.
/// SCENARIOS:
/// 1. IF 100 by 1/1/1 to the cent by largest remainder: SHOULD be
/// 33.34, 33.33, 33.33 (the rounded remainders tie, so the cent goes
/// 33.34, 33.33, 33.33 (the exact remainders tie, so the cent goes
/// to the earlier part).
/// 2. IF 0.01 by 1/1 to the cent by largest remainder: SHOULD be
/// 0.01, 0.00.
/// 3. IF 10 by weights 2/3/5 by first-absorbs: SHOULD be the rounded
/// shares with the first taking the whole less the others.
/// 4. IF the total is zero, or every weight is zero: SHOULD be zeros (a
/// 4. IF 20.00 by 4/1/1 over 6 to the cent by largest remainder: SHOULD
/// be 13.34, 3.33, 3.33. The exact remainders (0.02, 0.02, 0.02 over
/// 6, as 80 - 79.98, 20 - 19.98) tie, so the cent goes to the first.
/// The rounded quotients (13.33...3 keeps one fewer fractional digit
/// than 3.33...3 at 28 significant digits) would have put the
/// second's remainder above the first's.
/// 5. IF the total is zero, or every weight is zero: SHOULD be zeros (a
/// zero total) or the largest-remainder handout of the whole (a
/// positive total with zero weights, as the conventions' template
/// split has always done).
@@ -263,8 +270,26 @@ pub fn split_by_weights_into<T: Amount>(
if unit <= T::ZERO {
return None;
}
// Rank by the exact remainder, never the rounded quotient's
// difference: `whole x weight - part x total`, the exact
// rational's fraction scaled by `total`, the same quantity
// `share_floor_with` returns. A negative total flips the
// scale's sign, so the order is the share's own. The two
// products are the rounding ones: each is exact while it fits
// Decimal's mantissa (every split of posted amounts by whole
// weights or basis points), and where a weight of 27 places
// makes a product round, the split is still made, ranked by
// the nearest figures Decimal holds, not refused. Their
// difference is exact: the subtraction refuses otherwise.
for i in 0..weights.len() {
remainders[i] = remainders[i].checked_sub(parts[i])?;
let exact = whole
.checked_mul_rounded(weights[i])?
.checked_sub(parts[i].checked_mul_rounded(total)?)?;
remainders[i] = if total < T::ZERO {
T::ZERO.checked_sub(exact)?
} else {
exact
};
}
apportion(whole, parts, remainders, unit)
}