From 4679f0567305341aee9d4563e7626d19859d77ee Mon Sep 17 00:00:00 2001 From: "Fabio A. Correa" Date: Thu, 26 Mar 2020 16:50:29 -0400 Subject: [PATCH] Rounding error fix 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 a negative total_weight results in an error. This replaces PR #955 which was invalidated after a merge. Thanks for your consideration! --- src/distributions/weighted_index.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/distributions/weighted_index.rs b/src/distributions/weighted_index.rs index 5a3ecaa2099..b73bc43af02 100644 --- a/src/distributions/weighted_index.rs +++ b/src/distributions/weighted_index.rs @@ -174,7 +174,7 @@ impl WeightedIndex { total_weight += w; prev_i = Some(i); } - if total_weight == zero { + if total_weight <= zero { return Err(WeightedError::AllWeightsZero); }