From 9dd45bae143442aed66c06adec31d77d93a99607 Mon Sep 17 00:00:00 2001 From: Gabriel Comte Date: Mon, 30 Mar 2026 10:56:12 +0200 Subject: [PATCH] Fix flaky round_value property test The original test compared round_value output against a recomputation that could differ by 1 ULP for large values due to f64 precision. Replace with a bounded-error test: rounding must not move the value by more than half a unit of the last decimal place. --- src/currency/btc.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/currency/btc.rs b/src/currency/btc.rs index 57a0fc2..4d17f74 100644 --- a/src/currency/btc.rs +++ b/src/currency/btc.rs @@ -89,17 +89,18 @@ mod tests { } #[test] - fn round_value_has_correct_decimal_places( + fn round_value_error_within_half_unit( amount in 0.0_f64..1.0e8, unit in arb_btc_unit(), ) { let rounded = unit.round_value(amount); - let factor = 10_f64.powi(unit.decimal_places().into()); - let check = (rounded * factor).round() / factor; - let diff = (rounded - check).abs(); + let half_unit = 0.5 / 10_f64.powi(unit.decimal_places().into()); + // Allow a small extra tolerance for f64 representation at large magnitudes + let tolerance = half_unit + amount.abs() * f64::EPSILON; + let error = (rounded - amount).abs(); prop_assert!( - diff < 1.0e-10, - "round_value produced too many decimal places: {rounded} (expected {check})" + error <= tolerance, + "round_value({amount}) = {rounded}, error {error} exceeds half unit {half_unit}" ); } }