Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rounding error fix on WeightedIndex::update_weights() #955

Closed
wants to merge 1 commit into from

Conversation

facorread
Copy link
Contributor

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.

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.
@facorread facorread changed the title Bugfix for panic on WeightedIndex::update_weights Rounding error fix on WeightedIndex::update_weights() Mar 25, 2020
Copy link
Collaborator

@vks vks left a comment

Choose a reason for hiding this comment

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

Would it be possible to add a test demonstrating this rounding error?

Copy link
Member

@dhardy dhardy left a comment

Choose a reason for hiding this comment

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

Looks like a sensible change to me. I don't see the need for an extra test.

@vks
Copy link
Collaborator

vks commented Mar 26, 2020

Looks like a sensible change to me. I don't see the need for an extra test.

I think this would be nice to have, but the change certainly can be merged as is.

This needs to be rebased on master, because #945 was merged.

@facorread
Copy link
Contributor Author

facorread commented Mar 26, 2020

If you want, you can just enter the change by hand and commit to master, it might amount to less work than rebasing.

Thanks for your approval!

@dhardy
Copy link
Member

dhardy commented Mar 26, 2020

@facorread you can probably just do:

git fetch origin
git rebase origin/master
# fix if necessary
git push --force

@facorread
Copy link
Contributor Author

I am not authorized, sorry

facorread added a commit to facorread/rand that referenced this pull request Mar 26, 2020
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 rust-random#955 which was invalidated after a merge. Thanks for your consideration!
@facorread facorread closed this Mar 26, 2020
@facorread
Copy link
Contributor Author

Would it be possible to add a test demonstrating this rounding error?

I wanted to leave this example for future reference, for anyone who needs to understand this rounding error. Thank you.

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.

None yet

3 participants