fix(core): a tie between shares goes to the earlier part, exactly (held for the owner's decision)

split_by_weights_into's largest-remainder policy ranked the leftover
cents by the rounded quotient's remainder (Decimal keeps 28 significant
digits), so an exact tie was decided by the quotient's last digit, not
given to the earlier part as apportion's docs and bookspec's Lean model
(Apportion.lean: remW = x*w % W, ties to the earlier member) say.
20.00 by 4/1/1 over 6 gave 13.33, 3.34, 3.33 where the exact tie of
remainders (0.02 over 6 each) belongs to the first: 13.34, 3.33, 3.33.

It now ranks the exact integer remainder whole x weight - part x total
(the quantity share_floor_with returns), sign-flipped for a negative
total so the order stays the share's own. The parts still sum to the
whole and each is within one step; only which part takes a tied cent
changes. Stored entries never move (posted is forever); a report
recomputed for a past period can move a cent between parties.

The finding test is flipped into the law (ties_go_to_the_earlier_part),
the bridge's ranking property now includes ties, and the spec block,
k-u3.md and verification.md say ties go to the earlier part, exactly.

This branch is held: the owner decides whether the cents may move
(see tests/kernel_bridge/ties.rs for what it moves).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-09 15:37:06 -07:00
1 parent 4da1c5b30f
commit 294cb99a58
5 files changed
+112 -96

No files matched your search

+2 -3
View File
@@ -34,9 +34,8 @@
//! it) is shared among the members credited in proportion to their //! it) is shared among the members credited in proportion to their
//! credits, to the base currency's minor unit (cents, whole yen), //! credits, to the base currency's minor unit (cents, whole yen),
//! leftover units by largest remainder //! leftover units by largest remainder
//! (kernel::apportion, a tie in the rounded remainders to the //! (kernel::apportion over the exact remainders, a tie to the
//! earlier member; an exact tie follows the quotient's 28th digit, //! earlier member, K-U10): each takes at
//! K-U10): each takes at
//! most its own credit, and the split depends on the order the //! most its own credit, and the split depends on the order the
//! members are given only for a tied leftover cent (never on line //! members are given only for a tied leftover cent (never on line
//! order). Joint property is joint (BS-partner-1's //! order). Joint property is joint (BS-partner-1's
+32 -13
View File
@@ -21,11 +21,10 @@ use super::{Amount, apportion, plug_last};
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Leftover { pub enum Leftover {
/// Each part is rounded down; the units left over go one each to the /// Each part is rounded down; the units left over go one each to the
/// largest remainders. The remainders are the rounded quotients' /// largest remainders. The remainders are exact: `whole x weight -
/// differences, not exact rationals, so ranking follows the quotient: /// part x total`, an integer comparison, never the rounded quotient's
/// an exact tie can go where the quotient's 28th digit sends it, and /// difference, so an exact tie goes to the earlier part (the Lean
/// only a tie in the rounded remainders goes to the earlier part. K-U10 /// model's rank, bookspec's `Apportion`). [`apportion`].
/// decides the exact ranking. [`apportion`].
LargestRemainder, LargestRemainder,
/// The last part becomes the whole less the others. [`plug_last`]. /// The last part becomes the whole less the others. [`plug_last`].
LastAbsorbs, LastAbsorbs,
@@ -195,11 +194,13 @@ const MAX_CORRECTION_STEPS: usize = 4096;
/// a part more than one step from the exact rational (K-U10, pinned /// a part more than one step from the exact rational (K-U10, pinned
/// in the bridge). Under the first/last-absorbs policies only the /// in the bridge). Under the first/last-absorbs policies only the
/// absorbing part may be further, by the rounding of all the others. /// absorbing part may be further, by the rounding of all the others.
/// The largest-remainder ranking compares the rounded quotients' /// The largest-remainder ranking compares the exact remainders
/// remainders, not exact rationals: an exact tie can go where /// `whole x weight - part x total` (the quantity `share_floor_with`
/// `Decimal`'s 28th digit sends it, and only a tie in the rounded /// returns, scaled by total; the sign flipped for a negative total),
/// remainders goes to the earlier part (K-U10 decides the exact /// never the rounded quotients' differences. An exact tie goes to the
/// rule; the bridge pins today's answer). /// 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 /// 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 /// is in [0, total] and the whole is not negative; no part is
/// negative when the weights and the whole are not. /// negative when the weights and the whole are not.
@@ -213,13 +214,19 @@ const MAX_CORRECTION_STEPS: usize = 4096;
/// promised. /// promised.
/// SCENARIOS: /// SCENARIOS:
/// 1. IF 100 by 1/1/1 to the cent by largest remainder: SHOULD be /// 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). /// to the earlier part).
/// 2. IF 0.01 by 1/1 to the cent by largest remainder: SHOULD be /// 2. IF 0.01 by 1/1 to the cent by largest remainder: SHOULD be
/// 0.01, 0.00. /// 0.01, 0.00.
/// 3. IF 10 by weights 2/3/5 by first-absorbs: SHOULD be the rounded /// 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. /// 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 /// zero total) or the largest-remainder handout of the whole (a
/// positive total with zero weights, as the conventions' template /// positive total with zero weights, as the conventions' template
/// split has always done). /// split has always done).
@@ -257,8 +264,20 @@ pub fn split_by_weights_into<T: Amount>(
if unit <= T::ZERO { if unit <= T::ZERO {
return None; 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.
for i in 0..weights.len() { for i in 0..weights.len() {
remainders[i] = remainders[i].checked_sub(parts[i])?; let exact = whole
.checked_mul(weights[i])?
.checked_sub(parts[i].checked_mul(total)?)?;
remainders[i] = if total < T::ZERO {
T::ZERO.checked_sub(exact)?
} else {
exact
};
} }
apportion(whole, parts, remainders, unit) apportion(whole, parts, remainders, unit)
} }
@@ -291,58 +291,31 @@ proptest! {
prop_assert_eq!(cents(&got), want); prop_assert_eq!(cents(&got), want);
} }
/// The exact reference ranks the exact remainders; the kernel ranks /// The exact reference ranks the exact remainders, and so does the
/// the rounded quotients' remainders. With a small non-terminating /// kernel (K-U10): with any weights, ties included (equal weights,
/// total the two orders agree except at an exact tie, which this /// the 4/1/1 shape, a whole that leaves equal remainders), the split
/// property excludes and `finding_ties_follow_the_rounded_quotient` /// is the reference's, at whole units and at the cent.
/// pins.
#[test] #[test]
fn largest_remainder_ranks_like_the_exact_reference( fn largest_remainder_ranks_like_the_exact_reference(
whole_start in 0i64..=2_000, whole in 0i64..=100_000,
weights in prop::collection::vec(1i64..=9, 2..=4), weights in prop::collection::vec(1i64..=9, 2..=5),
) { ) {
// Distinct weights, and a total with a factor of 3 or 7 (a
// non-terminating division), by construction: no case is
// rejected, and no exact tie is left to the property.
let mut weights = weights;
weights.sort_unstable();
for (i, w) in weights.iter_mut().enumerate() {
*w += i as i64;
}
let plain: i64 = weights.iter().sum();
if plain % 3 != 0 && plain % 7 != 0 {
*weights.last_mut().unwrap() += 3 - plain % 3;
}
let total: i64 = weights.iter().sum(); let total: i64 = weights.iter().sum();
let whole = (whole_start..whole_start + total)
.find(|&w| {
let mut rems: Vec<i128> = weights
.iter()
.map(|&x| (i128::from(w) * i128::from(x)) % i128::from(total))
.collect();
rems.sort_unstable();
rems.dedup();
rems.len() == weights.len()
})
.expect("whole ≡ 1 mod total leaves distinct remainders");
let decimal_weights: Vec<Decimal> = weights.iter().copied().map(Decimal::from).collect(); let decimal_weights: Vec<Decimal> = weights.iter().copied().map(Decimal::from).collect();
let got = split_by_weights_with( let wide: Vec<i128> = weights.iter().map(|&w| i128::from(w)).collect();
Decimal::from(whole), for places in [0u32, 2] {
&decimal_weights, let got = decimal_split(
Decimal::from(total), Decimal::new(whole, places),
Decimal::ONE, &decimal_weights,
|x| round_step(x, Decimal::ONE, Rounding::TowardZero), Decimal::from(total),
Leftover::LargestRemainder, places,
) Leftover::LargestRemainder,
.unwrap(); )
let want = reference( .unwrap();
i128::from(whole), let want = reference(i128::from(whole), &wide, i128::from(total), 1).unwrap();
&weights.iter().map(|&w| i128::from(w)).collect::<Vec<_>>(), let got = if places == 0 { got.iter().map(|&p| integer(p)).collect() } else { cents(&got) };
i128::from(total), prop_assert_eq!(got, want);
1, }
)
.unwrap();
prop_assert_eq!(got.iter().map(|&p| integer(p)).collect::<Vec<_>>(), want);
} }
/// `share_floor_with` is the exact floor of the rational, even when /// `share_floor_with` is the exact floor of the rational, even when
@@ -571,14 +544,13 @@ fn split_into_matches_the_allocating_split() {
); );
} }
/// The EM's finding, pinned rather than fixed: a split ranks the rounded /// K-U10 (the owner's decision): a split ranks the exact remainders, so an
/// quotients' remainders, so an exact tie goes where the 28th digit sends /// exact tie goes to the earlier part, as the docs and the Lean model say.
/// it, not to the earlier part. 20.00 split 4/1/1 over 6 gives /// 20.00 split 4/1/1 over 6 is 13.34/3.33/3.33 (the rounded quotients'
/// 13.33/3.34/3.33 (exact ranking would give 13.34/3.33/3.33), and 20 at /// ranking gave 13.33/3.34/3.33), and 20 at whole units is 14/3/3 (it gave
/// whole units gives 13/4/3 (exact would give 14/3/3). The pre-U3 sites /// 13/4/3). Equal weights tie on every odd cent and give it to the first.
/// did the same. The owner decides the exact ranking in K-U10.
#[test] #[test]
fn finding_ties_follow_the_rounded_quotient() { fn ties_go_to_the_earlier_part() {
let weights = [Decimal::from(4), Decimal::ONE, Decimal::ONE]; let weights = [Decimal::from(4), Decimal::ONE, Decimal::ONE];
let got = decimal_split( let got = decimal_split(
parse("20.00"), parse("20.00"),
@@ -590,7 +562,7 @@ fn finding_ties_follow_the_rounded_quotient() {
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
got.iter().map(Decimal::to_string).collect::<Vec<_>>(), got.iter().map(Decimal::to_string).collect::<Vec<_>>(),
["13.33", "3.34", "3.33"] ["13.34", "3.33", "3.33"]
); );
let got = decimal_split( let got = decimal_split(
Decimal::from(20), Decimal::from(20),
@@ -602,7 +574,21 @@ fn finding_ties_follow_the_rounded_quotient() {
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
got.iter().map(Decimal::to_string).collect::<Vec<_>>(), got.iter().map(Decimal::to_string).collect::<Vec<_>>(),
["13", "4", "3"] ["14", "3", "3"]
);
// A negative total keeps the share's own order: ties still go first.
let neg = [Decimal::from(-4), Decimal::from(-1), Decimal::from(-1)];
let got = decimal_split(
parse("20.00"),
&neg,
Decimal::from(-6),
2,
Leftover::LargestRemainder,
)
.unwrap();
assert_eq!(
got.iter().map(Decimal::to_string).collect::<Vec<_>>(),
["13.34", "3.33", "3.33"]
); );
} }
+24 -16
View File
@@ -73,7 +73,9 @@ split when the whole divides), the step is 1 (or 10 for the floor
harness), and the bounds are `whole` 0 to 1,000 (100 for the floor harness), and the bounds are `whole` 0 to 1,000 (100 for the floor
harness). The laws are about the shape of the split, not the magnitude; harness). The laws are about the shape of the split, not the magnitude;
Lean proves them of the exact-share model at any size, and the bridge Lean proves them of the exact-share model at any size, and the bridge
checks the `Decimal` engine, whose ranking uses the rounded quotient. checks the `Decimal` engine, whose ranking is the exact remainder (K-U10:
`whole × weight − part × total`, ties to the earlier part, the same
quantity `share_floor_with` returns).
| Harness | Law | Verification | | Harness | Law | Verification |
|---|---|---| |---|---|---|
@@ -143,10 +145,9 @@ harness 500.8 s, the run inside its 3600-second cap). Lean:
no-rounding split, at the scales the call sites produce. no-rounding split, at the scales the call sites produce.
- `decimal_edges_are_none_or_exact`: `Decimal::MAX`, a product past it, a - `decimal_edges_are_none_or_exact`: `Decimal::MAX`, a product past it, a
zero total, and scale 28: each `None` or exact, never a panic. zero total, and scale 28: each `None` or exact, never a panic.
- `largest_remainder_ranks_like_the_exact_reference`: random distinct - `largest_remainder_ranks_like_the_exact_reference`: random weights (ties
weights over a non-terminating total (a factor of 3 or 7), against the included, since K-U10) at whole units and at the cent, against the exact
exact `i128` reference's ranking; exact ties are pinned below, not left `i128` reference's ranking.
to fail this property.
- `the_absorbing_parts_keep_the_old_scale`: the strings, scale included, - `the_absorbing_parts_keep_the_old_scale`: the strings, scale included,
for 4860 by 6/0.7/0.5 (first `4050`, as the old `split_first` gave) and for 4860 by 6/0.7/0.5 (first `4050`, as the old `split_first` gave) and
902 by 1/0.000001/0.000001 (first `902.00`); last absorbs against the 902 by 1/0.000001/0.000001 (first `902.00`); last absorbs against the
@@ -154,9 +155,10 @@ harness 500.8 s, the run inside its 3600-second cap). Lean:
- `split_into_matches_the_allocating_split`: the fixed-array - `split_into_matches_the_allocating_split`: the fixed-array
`split_by_weights_into` and `split_by_weights_with` agree, scale `split_by_weights_into` and `split_by_weights_with` agree, scale
included (the gate requires a bridge that calls the fixed-array form). included (the gate requires a bridge that calls the fixed-array form).
- `finding_ties_follow_the_rounded_quotient`: 20.00 split 4/1/1 over 6 is - `ties_go_to_the_earlier_part`: 20.00 split 4/1/1 over 6 is
13.33/3.34/3.33, and 20 whole is 13/4/3, not the exact tie's earlier 13.34/3.33/3.33, and 20 whole is 14/3/3 (K-U10: the ranking is exact;
part (K-U10 decides). the rounded quotient gave 13.33/3.34/3.33 and 13/4/3). A negative total
keeps the share's own order.
- `share_floor_refuses_past_the_correction_cap`: the review's - `share_floor_refuses_past_the_correction_cap`: the review's
0.1113 × 5909471247797156928749568911.8 ÷ 0.11134381908633486049673 0.1113 × 5909471247797156928749568911.8 ÷ 0.11134381908633486049673
returns `None` promptly instead of hanging. returns `None` promptly instead of hanging.
@@ -249,11 +251,17 @@ the `allocW_*` theorems were reused, not restated.
checked arithmetic), and `the_absorbing_parts_keep_the_old_scale` checked arithmetic), and `the_absorbing_parts_keep_the_old_scale`
compares strings. `LastAbsorbs` was already the old `plug_last` and the compares strings. `LastAbsorbs` was already the old `plug_last` and the
no-places conventions split, and the same property checks it. no-places conventions split, and the same property checks it.
7. **Ties follow the rounded quotient.** `split_by_weights_into` ranks the 7. **Ties go to the earlier part, exactly (K-U10).** The first version of
rounded quotients' remainders, not exact rationals, so an exact tie goes `split_by_weights_into` (and the pre-U3 call sites) ranked the leftover
where the 28th digit sends it, not to the earlier part: units by the rounded quotients' remainders, so an exact tie went where
20.00 split 4/1/1 over 6 is `13.33, 3.34, 3.33` (exact ranking would be the quotient's 28th digit sent it: 20.00 split 4/1/1 over 6 was
`13.34, 3.33, 3.33`), and 20 whole is `13, 4, 3` (exact would be `13.33, 3.34, 3.33` and 20 whole was `13, 4, 3`. It now ranks the exact
`14, 3, 3`). The pre-U3 sites did the same. Pinned by integer remainder `whole × weight − part × total` (the quantity
`finding_ties_follow_the_rounded_quotient`; the owner decides the exact `share_floor_with` returns; the sign flipped for a negative total), so
ranking in K-U10, and the ranking is unchanged here. a tie goes to the earlier part, as `apportion`'s docs and bookspec's
`Apportion.lean` (`remW`, `allocNat`: ties to the earlier member) say:
`13.34, 3.33, 3.33` and `14, 3, 3`. Pinned by
`ties_go_to_the_earlier_part`; `tests/kernel_bridge/ties.rs` keeps the
old ranking as a reference and measures what the change moves. A
recomputed past report can differ from what it showed before by a cent
between parties (a posted entry never moves).
+12 -8
View File
@@ -885,7 +885,7 @@ replay rules.
`largest_remainder_ranks_like_the_exact_reference`, `largest_remainder_ranks_like_the_exact_reference`,
`the_absorbing_parts_keep_the_old_scale`, `the_absorbing_parts_keep_the_old_scale`,
`split_into_matches_the_allocating_split`, `split_into_matches_the_allocating_split`,
`finding_ties_follow_the_rounded_quotient`, `ties_go_to_the_earlier_part`,
`share_floor_refuses_past_the_correction_cap`, `share_floor_refuses_past_the_correction_cap`,
`finding_the_quotients_rounding_can_cross_a_step` `finding_the_quotients_rounding_can_cross_a_step`
- `split_by_weights_with` is an `EXCEPTIONS` wrapper (it allocates two - `split_by_weights_with` is an `EXCEPTIONS` wrapper (it allocates two
@@ -1024,13 +1024,17 @@ kernel. What the proofs and their reviews found, beyond the tidying:
337.50`, the kernel `4050.00, …`), and that scale reaches `Part.amount`. 337.50`, the kernel `4050.00, …`), and that scale reaches `Part.amount`.
The kernel computes the old expression, pinned by The kernel computes the old expression, pinned by
`the_absorbing_parts_keep_the_old_scale`. `the_absorbing_parts_keep_the_old_scale`.
- **Ties follow the rounded quotient.** The split ranks the rounded - **Ties go to the earlier part, exactly (K-U10).** The split ranked the
quotients' remainders, not exact rationals, so an exact tie goes where rounded quotients' remainders, so an exact tie went where the 28th digit
the 28th digit sends it: 20.00 split 4/1/1 over 6 is `13.33, 3.34, sent it: 20.00 split 4/1/1 over 6 was `13.33, 3.34, 3.33` and 20 whole
3.33` (exact ranking `13.34, 3.33, 3.33`), and 20 whole is `13, 4, 3` was `13, 4, 3`. The pre-sweep sites did the same, and K-U3 kept it and
(exact `14, 3, 3`). The pre-sweep sites did the same; pinned it. It now ranks the exact remainder `whole x weight - part x
`finding_ties_follow_the_rounded_quotient` pins it, and the owner decides total` (as `share_floor_with` does), so a tie goes to the earlier part:
the exact ranking in K-U10, unchanged here. `13.34, 3.33, 3.33` and `14, 3, 3`, which is what bookspec's
`Apportion.lean` models (`remW`, ties to the earlier member).
`ties_go_to_the_earlier_part` pins it, and `tests/kernel_bridge/ties.rs`
measures what it moves against the old ranking (held for the owner's
decision).
- **The sweep changed no posted figure.** Every golden and existing test - **The sweep changed no posted figure.** Every golden and existing test
passed unchanged. `round_places` reproduces `round_dp_with_strategy` to passed unchanged. `round_places` reproduces `round_dp_with_strategy` to
the character except a negative-zero input (which no call site can the character except a negative-zero input (which no call site can