Skip to content

Commit bfa9966

Browse files
authored
Merge pull request #510 from gcomte/fix/property-test-floating-point-v2
Fix flaky round_value property test
2 parents 4eebf6a + 9dd45ba commit bfa9966

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

src/currency/btc.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,18 @@ mod tests {
8989
}
9090

9191
#[test]
92-
fn round_value_has_correct_decimal_places(
92+
fn round_value_error_within_half_unit(
9393
amount in 0.0_f64..1.0e8,
9494
unit in arb_btc_unit(),
9595
) {
9696
let rounded = unit.round_value(amount);
97-
let factor = 10_f64.powi(unit.decimal_places().into());
98-
let check = (rounded * factor).round() / factor;
99-
let diff = (rounded - check).abs();
97+
let half_unit = 0.5 / 10_f64.powi(unit.decimal_places().into());
98+
// Allow a small extra tolerance for f64 representation at large magnitudes
99+
let tolerance = half_unit + amount.abs() * f64::EPSILON;
100+
let error = (rounded - amount).abs();
100101
prop_assert!(
101-
diff < 1.0e-10,
102-
"round_value produced too many decimal places: {rounded} (expected {check})"
102+
error <= tolerance,
103+
"round_value({amount}) = {rounded}, error {error} exceeds half unit {half_unit}"
103104
);
104105
}
105106
}

0 commit comments

Comments
 (0)