Skip to content

Return Error::Overflow from WeightedIndex on float weight overflow#1808

Open
teddytennant wants to merge 1 commit into
rust-random:masterfrom
teddytennant:weighted-index-float-overflow
Open

Return Error::Overflow from WeightedIndex on float weight overflow#1808
teddytennant wants to merge 1 commit into
rust-random:masterfrom
teddytennant:weighted-index-float-overflow

Conversation

@teddytennant

Copy link
Copy Markdown
Contributor
  • Added a CHANGELOG.md entry

Summary

WeightedIndex::new and update_weights panic when the sum of float weights reaches infinity. Return Error::Overflow instead, as already documented and as done for integer weights.

Motivation

WeightedIndex::new documents Error::Overflow when the sum of weights overflows, but Weight::checked_add_assign never errors for floats — the sum just becomes infinite. UniformFloat::new then rejects the non-finite bound and the .unwrap() on X::Sampler::new panics:

WeightedIndex::new([f64::MAX, f64::MAX]); // panics: NonFinite

#1353 fixed this for integer weights; float weights were missed.

update_weights has the same unwrap, and worse: it panics after the cumulative weights have already been mutated, violating the documented "in case of error, self is not modified" contract.

Details

  • In new, map the sampler error to Error::Overflow.
  • In update_weights, construct the new sampler during the pre-mutation check phase (mapping the error the same way) so a failure leaves self untouched, and document Error::Overflow in its error list.
  • Added an overflow_float test covering f64::MAX + f64::MAX, f32, an explicitly infinite weight, and that a failed update_weights leaves the distribution unmodified.

Tested with cargo test --lib weighted (14 tests pass); cargo fmt --check and cargo clippy --lib are clean.

WeightedIndex::new documents Error::Overflow when the sum of weights
overflows, but Weight::checked_add_assign never errors for floats, so a
sum reaching infinity hit the unwrap on Sampler::new and panicked with
NonFinite. The same unwrap in update_weights also panicked after the
cumulative weights were already mutated, violating the documented
"in case of error, self is not modified" contract.

Map the sampler error to Error::Overflow in new, and in update_weights
construct the new sampler during the pre-mutation check phase.
rust-random#1353 fixed this for integer weights; floats were missed.
Copilot AI review requested due to automatic review settings July 11, 2026 12:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@dhardy dhardy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue here is that Weight::checked_add_assign was relied on to avoid overflow in fn new, yet it does no such thing for floats. Can you add a note to its docs clarifying that the implementation for floats does not trap overflow-to-inf or nan?

Otherwise, I think this approach is reasonable. Fn update_weights still has a potential issue with integer overflows but lets not address that here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants