Skip to content

Prevent random floats from occasionally being greater than 1. #2939

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

Merged
merged 1 commit into from
Jul 20, 2012

Conversation

jld
Copy link
Contributor

@jld jld commented Jul 17, 2012

Previously, gen_f64 could generate numbers as high as 1.0000000002328306
in the case that u3 was 4294967295.0f64 and u2 was nonzero. This change
divides the random numbers by 2**32 instead, effectively concatenating
their bits as apparently intended. (Bonus fix: const.)

The comments are updated to be more specific than "random float"; note
that this can still generate 1.0f64 (P = 2**-54) due to rounding.

See also: https://gist.github.com/3127268

Previously, gen_f64 could generate numbers as high as 1.0000000002328306
in the case that u3 was 4294967295.0f64 and u2 was nonzero.  This change
divides the random numbers by 2**32 instead, effectively concatenating
their bits as apparently intended.  (Bonus fix: const.)

The comments are updated to be more specific than "random float"; note
that this can still generate 1.0f64 (P = 2**-54) due to rounding.
@jld
Copy link
Contributor Author

jld commented Jul 17, 2012

Passes make check, and it's not really the kind of thing that lends itself to adding a regression test.

@bblum
Copy link
Contributor

bblum commented Jul 17, 2012

I shudder to think the sort of bug this might have manifested in.

@jld
Copy link
Contributor Author

jld commented Jul 18, 2012

Not actually encountered outside the microbenchmark thing I wrote in response to finding it, thankfully; I found it when reading the library source to see if the vague “a random float” meant [0,1] or what. (Although there was a can't-happen case that I handled instead of failing, out of general FP paranoia, and a random float >1 would be sufficientish to reach it.)

catamorphism added a commit that referenced this pull request Jul 20, 2012
Prevent random floats from occasionally being greater than 1.
@catamorphism catamorphism merged commit ed62f44 into rust-lang:incoming Jul 20, 2012
@catamorphism
Copy link
Contributor

Merged, thanks!

celinval added a commit to celinval/rust-dev that referenced this pull request Jun 4, 2024
This one was fairly straight forward. It still uses a few internal APIs
for user friendly errors, for type layout, SIMD information and
intrinsic validity.
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