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

Equidistant sampling of uniform FP values #1270

Open
Tracked by #1165
cmpute opened this issue Nov 26, 2022 · 2 comments
Open
Tracked by #1165

Equidistant sampling of uniform FP values #1270

cmpute opened this issue Nov 26, 2022 · 2 comments
Labels
A-new Propose usage of a new algorithm

Comments

@cmpute
Copy link

cmpute commented Nov 26, 2022

I just noticed a paper recently, which reports that the implementation of drawing uniform floating point numbers in rand has some imperfections. Specifically, according to the paper rng.gen_range(-f64::MAX..=f64::MAX) will only generate negative floating point numbers.

After a quick search over the issues I didn't find any issued raised with regard to this paper. I just want to post it in case it's not mentioned here.

@dhardy
Copy link
Member

dhardy commented Nov 26, 2022

According to the paper, the output is 0 positive values. According to my quick test, it's 0 values total:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e8809619518c745617f2509154df2cc6

Result: range overflow. Not really surprising given that range = f64::MAX - -f64::MAX is not representable.

Maybe there's more to take away from a proper reading, but usage of that range should draw alarm bells (it has no practical use and skirts the limits of the type).

@dhardy
Copy link
Member

dhardy commented Feb 20, 2023

A possible take-away from that paper is an alternative method of implementing Uniform for floats which does much of the arithmetic using integers, with better equidistributivity of samples and good performance. We should try implementing this, comparing to the current method (b - a)*x01 + a, possibly also comparing to #531.

@dhardy dhardy changed the title Corner case when the range is too large for floats Equidistant sampling of uniform FP values Feb 20, 2023
This was referenced Feb 20, 2023
@dhardy dhardy added the A-new Propose usage of a new algorithm label Jul 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-new Propose usage of a new algorithm
Projects
None yet
Development

No branches or pull requests

2 participants