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

inaccurate probabilities in IteratorRandom::choose() #1058

Closed
kazcw opened this issue Oct 11, 2020 · 0 comments
Closed

inaccurate probabilities in IteratorRandom::choose() #1058

kazcw opened this issue Oct 11, 2020 · 0 comments

Comments

@kazcw
Copy link
Contributor

kazcw commented Oct 11, 2020

IteratorRandom::choose() uses a floating-point reciprocal to calculate probabilities. f64 reciprocal is an inexact operation with residuals on the order of 1 / 2^54.

For example: the probability of gen_bool(1.0 / 3) == true is: 6004799503160661/18014398509481984, which differs from 1/3 in the 17th digit.
An exact implementation of choose would be possible with gen_range.

kazcw added a commit to kazcw/rand that referenced this issue Oct 11, 2020
f64 reciprocal is inexact, with residuals on the order of 1 / 2^54.

For example: the probability gen_bool(1.0 / 3) == true is:
6004799503160661/18014398509481984
Using gen_range is exact for all values of `consumed`.

NOTE: this is a value stability-breaking change

fixes rust-random#1058
kazcw added a commit to kazcw/rand that referenced this issue Oct 13, 2020
f64 reciprocal is inexact, with residuals on the order of 1 / 2^54.

For example: the probability gen_bool(1.0 / 3) == true is:
6004799503160661/18014398509481984
Using gen_range is exact for all values of `consumed`.

NOTE: this is a value stability-breaking change

fixes rust-random#1058
@dhardy dhardy closed this as completed in a37a599 Oct 17, 2020
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

No branches or pull requests

1 participant