From 89b3f431c087e54441a6e18f435cdd99957d4fba Mon Sep 17 00:00:00 2001 From: "Fabio A. Correa" Date: Wed, 25 Mar 2020 10:11:38 -0400 Subject: [PATCH] Bugfix for panic on WeightedIndex::update_weights WeightedIndex::update_weights() uses subtraction on old_w. Rounding errors, particularly on f32, may bring total_weight to below 0. This PR makes sure this situation results in an error. --- src/distributions/weighted/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/distributions/weighted/mod.rs b/src/distributions/weighted/mod.rs index 357e3a9f024..91779d66167 100644 --- a/src/distributions/weighted/mod.rs +++ b/src/distributions/weighted/mod.rs @@ -183,7 +183,7 @@ impl WeightedIndex { total_weight += w; prev_i = Some(i); } - if total_weight == zero { + if total_weight <= zero { return Err(WeightedError::AllWeightsZero); }