fix(core): the shares keep the old scale, and the floor's correction is bounded
The review's kernel findings, with the bridge properties that hold them. FirstAbsorbs computed the first part as whole minus the rest, which is algebraically the old split_first's parts[0] + (whole minus the sum of parts) but not the same scale: 4860 by 6/0.7/0.5 gave 4050.00 where the old code gave 4050, and 902 by 1/0.000001/0.000001 gave 902 where the old code gave 902.00. That scale reaches Part.amount, so a printed share moved. The kernel computes the old expression now, with checked ops; LastAbsorbs was already the old plug_last and the no-places conventions split. The bridge compares the strings. share_floor_with's correction loop could walk without end on a rounded product far from the exact floor: 0.1113 x 5909471247797156928749568911.8 / 0.11134381908633486049673 hangs. Both directions now stop at MAX_CORRECTION_STEPS and refuse, so an input past the cap is None, never a hang; no figure produced within the cap changes. The bridge pins the input. Ranking needed a property of its own: on i64 every remainder is zero, so the harnesses never rank. A random-weight bridge checks the split against the independent exact reference over non-terminating totals, excluding exact ties because the kernel ranks the rounded quotients' remainders; finding_ties_follow_the_rounded_quotient pins those ties (20.00 by 4/1/1 over 6 is 13.33/3.34/3.33, 20 whole is 13/4/3), today's behaviour and the pre-U3 sites' alike, for K-U10 to decide. share_floor and split_by_weights had no engine caller (only the bridge), so they are gone; split_by_weights_with and share_floor_with are the public API, and the docs say both checked_mul and checked_div can round at Decimal's edge. Co-Authored-By: DeepSeek V4.1 Flash <noreply@opencode.ai>
This commit is contained in:
1 parent
d4440b6a4e
commit
8edc2e85dd
6 files changed
+313
-82
No files matched your search
@@ -7,24 +7,31 @@
|
||||
//!
|
||||
//! Division is not exact: [`share_of`] is the amount type's checked
|
||||
//! division, so a `Decimal` quotient is rounded at 28 significant digits
|
||||
//! (and a machine integer's is truncated), never an exact rational. Every
|
||||
//! invariant below is stated of that computation, and of the parts after
|
||||
//! the named leftover policy, which do sum to the whole exactly. See
|
||||
//! `docs/proofs/k-u3.md` for the one place the quotient's rounding can
|
||||
//! change a rounded part (Decimal's edge, K-U10).
|
||||
//! (and a machine integer's is truncated), never an exact rational.
|
||||
//! Multiplication is not exact either at `Decimal`'s 96-bit mantissa:
|
||||
//! `checked_mul` rounds the product there. Every invariant below is
|
||||
//! stated of that computation, and of the parts after the named leftover
|
||||
//! policy, which do sum to the whole exactly. See `docs/proofs/k-u3.md`
|
||||
//! for the edges where a product's or a quotient's rounding can change a
|
||||
//! rounded part (Decimal's edge, K-U10).
|
||||
|
||||
use super::round::Rounding;
|
||||
use super::{Amount, apportion, plug_last, round_step};
|
||||
use super::{Amount, apportion, plug_last};
|
||||
|
||||
/// What a split does with the units its rounded parts leave over.
|
||||
#[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 (ties to the earlier part). [`apportion`].
|
||||
/// 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`].
|
||||
LargestRemainder,
|
||||
/// The last part becomes the whole less the others. [`plug_last`].
|
||||
LastAbsorbs,
|
||||
/// The first part becomes the whole less the others.
|
||||
/// The first part becomes its rounded share plus the shortfall (the
|
||||
/// whole less every rounded part), so its scale is the shortfall
|
||||
/// addition's; the value is the whole less the others.
|
||||
FirstAbsorbs,
|
||||
}
|
||||
|
||||
@@ -49,8 +56,9 @@ pub enum Leftover {
|
||||
/// 2. None when total is zero, or when the product or quotient is not
|
||||
/// representable. Never a panic, never a wrapped amount.
|
||||
/// 3. When total is not zero and whole x weight is representable, the
|
||||
/// division is the only step that can round; a `Some` is always the
|
||||
/// amount type's quotient, never a guessed one.
|
||||
/// product and the division are the two steps that can round: either
|
||||
/// `Decimal` operation rounds at its own edge. A `Some` is always the
|
||||
/// amount type's quotient of its product, never a guessed one.
|
||||
/// SCENARIOS:
|
||||
/// 1. IF 100 by 40 of 100: SHOULD be 40.
|
||||
/// 2. IF 1 by 1 of 3: SHOULD be 0.333... (28 significant digits), not an
|
||||
@@ -70,9 +78,10 @@ pub fn share_of<T: Amount>(whole: T, weight: T, total: T) -> Option<T> {
|
||||
/// GOAL: the exact floor of a proportional share to a step, without
|
||||
/// trusting a rounded quotient, and the exact remainder that ranks
|
||||
/// the parts of a largest-remainder split.
|
||||
/// SOURCE: BS-1099-1 (a payment split into its buckets to the cent),
|
||||
/// SOURCE: BS-partner-1, BS-1099-1 (a payment split into its buckets to
|
||||
/// the cent; partners share by the agreement),
|
||||
/// BS-entry-3 (amounts are exact; round only where a rule says
|
||||
/// to), BS-partner-1 (largest remainder).
|
||||
/// to).
|
||||
/// INVARIANTS:
|
||||
/// 1. whole and weight are non-negative (the callers pass magnitudes).
|
||||
/// Provided `part x total` and `(part + step) x total` are exact
|
||||
@@ -90,6 +99,10 @@ pub fn share_of<T: Amount>(whole: T, weight: T, total: T) -> Option<T> {
|
||||
/// the product's rounding).
|
||||
/// 3. None when step or total is not positive, a product is not
|
||||
/// representable, or the correction leaves the range; never a panic.
|
||||
/// The correction takes at most [`MAX_CORRECTION_STEPS`] steps in
|
||||
/// each direction and returns None past the cap: a malformed input
|
||||
/// (one whose product's rounding put the estimate many steps away)
|
||||
/// refuses rather than runs without end.
|
||||
/// SCENARIOS:
|
||||
/// 1. IF 60 x 33.33 / 100 to the cent: SHOULD be (19.99, 80), since
|
||||
/// 60 x 33.33 = 1999.80 and 19.99 x 100 = 1999.
|
||||
@@ -114,34 +127,39 @@ pub fn share_floor_with<T: Amount>(
|
||||
// steps back until part x total <= num, then forward while one more
|
||||
// step still fits. The comparisons multiply, never divide; they are
|
||||
// exact while the product fits Decimal's mantissa (the edge is
|
||||
// K-U10, pinned in the bridge).
|
||||
// K-U10, pinned in the bridge). Both directions are bounded so a
|
||||
// quotient rounded far across the floor refuses instead of hanging.
|
||||
let mut steps = 0;
|
||||
while part.checked_mul(total)? > num {
|
||||
if steps == MAX_CORRECTION_STEPS {
|
||||
return None;
|
||||
}
|
||||
steps += 1;
|
||||
part = part.checked_sub(step)?;
|
||||
}
|
||||
steps = 0;
|
||||
loop {
|
||||
if steps == MAX_CORRECTION_STEPS {
|
||||
return None;
|
||||
}
|
||||
let next = part.checked_add(step)?;
|
||||
if next.checked_mul(total)? > num {
|
||||
break;
|
||||
}
|
||||
part = next;
|
||||
steps += 1;
|
||||
}
|
||||
let remainder = num.checked_sub(part.checked_mul(total)?)?;
|
||||
Some((part, remainder))
|
||||
}
|
||||
|
||||
/// [`share_floor_with`] with [`round_step`] as the first estimate: the
|
||||
/// exact floor of `whole × weight ÷ total` to `step`, in `mode`'s
|
||||
/// direction (only `TowardZero` is the floor; the correction makes the
|
||||
/// result exact either way).
|
||||
pub fn share_floor<T: Amount>(
|
||||
whole: T,
|
||||
weight: T,
|
||||
total: T,
|
||||
step: T,
|
||||
mode: Rounding,
|
||||
) -> Option<(T, T)> {
|
||||
share_floor_with(whole, weight, total, step, |x| round_step(x, step, mode))
|
||||
}
|
||||
/// The most correction steps [`share_floor_with`] takes in each direction.
|
||||
/// The first estimate is at most one step off in practice (half-away's
|
||||
/// rounding of a quotient whose product is exact); a few thousand leaves
|
||||
/// room for any quotient rounding that still describes the same figure.
|
||||
/// A share further away is a malformed input, and the function refuses it
|
||||
/// rather than loop without end.
|
||||
const MAX_CORRECTION_STEPS: usize = 4096;
|
||||
|
||||
/// Split `whole` by `weights` over `total`, each part the exact share
|
||||
/// rounded by `round`, the leftover by `policy`, written into `parts` (and
|
||||
@@ -177,6 +195,11 @@ pub fn share_floor<T: Amount>(
|
||||
/// 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).
|
||||
/// 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.
|
||||
@@ -190,7 +213,8 @@ pub fn share_floor<T: Amount>(
|
||||
/// promised.
|
||||
/// SCENARIOS:
|
||||
/// 1. IF 100 by 1/1/1 to the cent by largest remainder: SHOULD be
|
||||
/// 33.34, 33.33, 33.33 (the tied cent to the first).
|
||||
/// 33.34, 33.33, 33.33 (the rounded 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
|
||||
@@ -240,14 +264,19 @@ pub fn split_by_weights_into<T: Amount>(
|
||||
}
|
||||
Leftover::LastAbsorbs => plug_last(whole, parts),
|
||||
Leftover::FirstAbsorbs => {
|
||||
let Some((first, rest)) = parts.split_first_mut() else {
|
||||
// The old `split_first`: the first part takes its rounded share
|
||||
// plus the shortfall (the whole less every rounded part). The
|
||||
// value is the whole less the others, but its scale is the sum
|
||||
// and the addition's, as the call site has always produced.
|
||||
let mut sum = T::ZERO;
|
||||
for &p in parts.iter() {
|
||||
sum = sum.checked_add(p)?;
|
||||
}
|
||||
let Some(first) = parts.first_mut() else {
|
||||
return Some(());
|
||||
};
|
||||
let mut others = T::ZERO;
|
||||
for &p in rest.iter() {
|
||||
others = others.checked_add(p)?;
|
||||
}
|
||||
*first = whole.checked_sub(others)?;
|
||||
let left = whole.checked_sub(sum)?;
|
||||
*first = first.checked_add(left)?;
|
||||
Some(())
|
||||
}
|
||||
}
|
||||
@@ -277,24 +306,3 @@ pub fn split_by_weights_with<T: Amount>(
|
||||
)?;
|
||||
Some(parts)
|
||||
}
|
||||
|
||||
/// [`split_by_weights_with`] with [`round_step`] as the rounding: the
|
||||
/// generic spelling for a caller whose parts carry a whole number of
|
||||
/// `step`s.
|
||||
pub fn split_by_weights<T: Amount>(
|
||||
whole: T,
|
||||
weights: &[T],
|
||||
total: T,
|
||||
step: T,
|
||||
mode: Rounding,
|
||||
policy: Leftover,
|
||||
) -> Option<Vec<T>> {
|
||||
split_by_weights_with(
|
||||
whole,
|
||||
weights,
|
||||
total,
|
||||
step,
|
||||
|x| round_step(x, step, mode),
|
||||
policy,
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user