test(core): round_step's directed modes and finer steps are proved and tested
Review found two holes in K-U2's evidence.
1. The spec block's invariants 3, 4 and 6 (toward zero: |r| <= |x| and
|r - x| < step; toward negative infinity: r <= x < r + step; no mode
flips the sign) had no proof: Kani checked those modes only at exact
ties. Three harnesses now assert them for every amount over the fixed
steps (round_step_toward_zero_bounds, round_step_toward_neg_inf_is_floor,
round_step_sign_preserved), each with covers for a non-tie and a negative
non-multiple; bookspec's Lean carries the same laws for any step. With
TowardZero mutated to round 7 at step 10 up to 10, the first harness
fails ("magnitude never grows"); reverted.
2. The bridge never ran checked_round_to_step's finer-step branch (step
with more places than the amount and a mantissa above one). Three
properties compare the kernel with a second, independent i128 reference
that takes any step (mantissa 1..1000 at scales 0..10, amounts at scales
0..28, small amounts, and constructed exact ties). Replacing
.checked_add(remainder / b) with .checked_add(0) in the digit loop fails
all three; reverted.
docs/proofs/k-u2.md also states the one exception to "no result changes":
a negative-zero input (round_dp keeps -0.00; round_places gives 0.00; no
call site can produce one).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f16525fd35
commit
f0a5d10708
3 files changed
+225
-6
No files matched your search
@@ -188,6 +188,72 @@ fn round_step_never_negative_zero() {
|
||||
);
|
||||
}
|
||||
|
||||
/// Toward zero never grows the magnitude and lands less than a step from
|
||||
/// the amount: `|r| <= |x|` and `|r - x| < step`, for every amount (ties
|
||||
/// are not special: both modes pick the same multiple whether or not the
|
||||
/// amount is exactly between two).
|
||||
#[kani::proof]
|
||||
#[kani::unwind(8)]
|
||||
fn round_step_toward_zero_bounds() {
|
||||
let value = any_value();
|
||||
for step in STEPS {
|
||||
let rounded =
|
||||
round_step(value, step, Rounding::TowardZero).expect("no overflow under these bounds");
|
||||
assert!(rounded.abs() <= value.abs(), "magnitude never grows");
|
||||
assert!((rounded - value).abs() < step, "less than a step away");
|
||||
}
|
||||
cover!(value % 10 != 0 && value > 0, "a positive non-multiple");
|
||||
cover!(value % 10 != 0 && value < 0, "a negative non-multiple");
|
||||
cover!(
|
||||
value.abs() % 10 != 0 && value.abs() % 10 != 5,
|
||||
"a non-tie, a non-multiple of ten"
|
||||
);
|
||||
}
|
||||
|
||||
/// Toward negative infinity is the floor: `r <= x < r + step`, for every
|
||||
/// amount of either sign.
|
||||
#[kani::proof]
|
||||
#[kani::unwind(8)]
|
||||
fn round_step_toward_neg_inf_is_floor() {
|
||||
let value = any_value();
|
||||
for step in STEPS {
|
||||
let rounded = round_step(value, step, Rounding::TowardNegativeInfinity)
|
||||
.expect("no overflow under these bounds");
|
||||
assert!(rounded <= value, "never above the amount");
|
||||
assert!(value < rounded + step, "less than a step below it");
|
||||
}
|
||||
cover!(value % 10 != 0 && value > 0, "a positive non-multiple");
|
||||
cover!(
|
||||
value % 10 != 0 && value < 0,
|
||||
"a negative non-multiple (the floor moves away from zero)"
|
||||
);
|
||||
cover!(
|
||||
value.abs() % 10 != 0 && value.abs() % 10 != 5,
|
||||
"a non-tie, a non-multiple of ten"
|
||||
);
|
||||
}
|
||||
|
||||
/// No mode flips the sign: a non-negative amount rounds to a non-negative
|
||||
/// one, and a non-positive amount to a non-positive one.
|
||||
#[kani::proof]
|
||||
#[kani::unwind(8)]
|
||||
fn round_step_sign_preserved() {
|
||||
let value = any_value();
|
||||
let mode = any_mode();
|
||||
for step in STEPS {
|
||||
let rounded = round_step(value, step, mode).expect("no overflow under these bounds");
|
||||
if value >= 0 {
|
||||
assert!(rounded >= 0, "a non-negative amount stays non-negative");
|
||||
}
|
||||
if value <= 0 {
|
||||
assert!(rounded <= 0, "a non-positive amount stays non-positive");
|
||||
}
|
||||
}
|
||||
cover!(value > 0, "a positive amount");
|
||||
cover!(value < 0, "a negative amount");
|
||||
cover!(value == 0, "zero");
|
||||
}
|
||||
|
||||
/// A step of zero or less is refused, never guessed at.
|
||||
#[kani::proof]
|
||||
fn round_step_refuses_nonpositive_step() {
|
||||
|
||||
Reference in new issue
Block a user