fix(core): a ratio split's zero rest is +0, never -0

`split_ratio(0.00, 1, 2, 0.01, half-to-even)` handed the rest back as -0:
the generic `checked_sub` negates the subtrahend, and a zero negated
keeps its sign bit. Decimal equality hides it, but a printed "-0.00" is
not money and a sign test saw it. Map a zero rest to T::ZERO, which is
generic and always +0.

The bridge's edge test now asserts the sign (`!is_sign_negative()`) for
a zero rest and a zero percent, including a negative-zero amount, instead
of trusting equality. Its refusal test also covers a negative step for
both functions, which the kernel already refuses.

Co-Authored-By: DeepSeek V4.1 Flash <noreply@opencode.ai>
This commit is contained in:
swaitsandDeepSeek V4.1 Flash committed 2026-11-09 09:38:09 -07:00
1 parent c9d6c8f59a
commit 803dfc5de0
3 files changed
+72 -12

No files matched your search

+3
View File
@@ -115,5 +115,8 @@ pub fn split_ratio<T: Amount>(amount: T, a: T, b: T, step: T, mode: Rounding) ->
}
let part = round_step(share_of(amount, a, whole)?, step, mode)?;
let rest = amount.checked_sub(part)?;
// A checked subtraction can hand back a negative zero (the negated
// subtrahend keeps the sign bit); money's zero is always +0.
let rest = if rest == T::ZERO { T::ZERO } else { rest };
Some((part, rest))
}
+63 -10
View File
@@ -191,6 +191,9 @@ proptest! {
kernel::split_ratio(amount, Decimal::from(a), Decimal::from(b), step, mode).unwrap();
prop_assert_eq!(part + rest, amount);
prop_assert!((part % step).is_zero(), "the rounded side is a whole multiple");
if rest.is_zero() {
prop_assert!(!rest.is_sign_negative(), "a zero rest is +0");
}
if a == 0 {
prop_assert!(part.is_zero());
prop_assert_eq!(rest, amount);
@@ -267,10 +270,18 @@ fn percent_of_refuses_no_whole() {
kernel::percent_of(Decimal::ONE, Decimal::ONE, Decimal::ZERO, mode),
None
);
assert_eq!(
kernel::percent_of(Decimal::ONE, Decimal::ONE, -step(1), mode),
None
);
assert_eq!(
kernel::split_ratio(Decimal::ONE, Decimal::ZERO, Decimal::ZERO, step(2), mode),
None
);
assert_eq!(
kernel::split_ratio(Decimal::ONE, Decimal::ONE, Decimal::from(2), -step(2), mode),
None
);
assert_eq!(
kernel::split_ratio(Decimal::ONE, -Decimal::ONE, Decimal::from(2), step(2), mode),
None
@@ -324,15 +335,57 @@ fn decimal_ratio_at_the_edges() {
assert_eq!(part + rest, Decimal::MAX);
}
}
// A zero amount splits into two zero parts.
assert_eq!(
kernel::split_ratio(
Decimal::ZERO,
Decimal::ONE,
Decimal::from(2),
step(2),
Rounding::HalfToEven
),
Some((Decimal::ZERO, Decimal::ZERO))
// A zero amount splits into two zero parts, and both are +0: a
// checked subtraction used to hand the rest back as -0, which equality
// hides but a sign test does not.
let (part, rest) = kernel::split_ratio(
Decimal::ZERO,
Decimal::ONE,
Decimal::from(2),
step(2),
Rounding::HalfToEven,
)
.expect("a zero amount splits");
assert_eq!((part, rest), (Decimal::ZERO, Decimal::ZERO));
assert!(part.is_zero() && !part.is_sign_negative());
assert!(
rest.is_zero() && !rest.is_sign_negative(),
"a zero rest is +0"
);
// The same for a negative-zero amount, whose sign bit a subtraction can
// carry: both parts come back +0.
let negative_zero = -Decimal::ZERO;
assert!(negative_zero.is_sign_negative());
let (part, rest) = kernel::split_ratio(
negative_zero,
Decimal::ONE,
Decimal::from(2),
step(2),
Rounding::HalfToEven,
)
.expect("a negative-zero amount splits");
assert!(part.is_zero() && !part.is_sign_negative());
assert!(
rest.is_zero() && !rest.is_sign_negative(),
"a zero rest is +0"
);
// A zero part over a positive whole is a zero percent, never -0.
let zero = kernel::percent_of(
Decimal::ZERO,
Decimal::from(2),
step(1),
Rounding::HalfToEven,
)
.expect("a zero part has a percent");
assert!(zero.is_zero() && !zero.is_sign_negative());
let zero = kernel::percent_of(
negative_zero,
Decimal::from(2),
step(1),
Rounding::HalfToEven,
)
.expect("a negative-zero part has a percent");
assert!(zero.is_zero() && !zero.is_sign_negative());
}
+6 -2
View File
@@ -113,11 +113,15 @@ traceability gate, `crates/ledger-core/tests/bookspec/traceability.rs`):
rounded side is a whole multiple of the step, and a zero `a` share
leaves the whole to the rest.
- `percent_of_refuses_no_whole`: a zero or negative whole, a zero ratio
and a non-positive step are `None`, never a division by zero.
and a non-positive step are `None`, never a division by zero. A
negative step refuses both functions.
- `decimal_ratio_at_the_edges`: `Decimal::MAX` (a part past the range
refuses; a split either refuses or sums), scale 28 (a tiny part is
zero, no negative zero; a ratio of one is 100), a whole past the range
(a percent under a unit is zero), and a zero amount split.
(a percent under a unit is zero), and a zero amount split whose two
parts are asserted to be `+0` (a checked subtraction once handed the
rest back as `-0`, which equality hides and a sign test does not). A
zero part over a positive whole is likewise a `+0` percent.
All pass with the rest of `kernel_bridge`.