Skip to content

Commit

Permalink
Rounding error fix on WeightedIndex::update_weights()
Browse files Browse the repository at this point in the history
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!
  • Loading branch information
facorread committed Mar 26, 2020
1 parent d0a584c commit 4679f05
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/distributions/weighted_index.rs
Expand Up @@ -174,7 +174,7 @@ impl<X: SampleUniform + PartialOrd> WeightedIndex<X> {
total_weight += w;
prev_i = Some(i);
}
if total_weight == zero {
if total_weight <= zero {
return Err(WeightedError::AllWeightsZero);
}

Expand Down

0 comments on commit 4679f05

Please sign in to comment.