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

Poisson: Fix undefined behavior and support f64 output #795

Merged
merged 9 commits into from
May 16, 2019
2 changes: 2 additions & 0 deletions rand_distr/src/poisson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ where Standard: Distribution<N>
impl<N: Float> Distribution<N> for Poisson<N>
where Standard: Distribution<N>
{
#[inline]
Copy link
Member

Choose a reason for hiding this comment

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

This is a large function — I don't think #[inline] makes any sense here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is not #[inline(always)], so the compiler is still free to make that choice. It might make sense to inline it into the u64 sampling, so that some bound checks can be eliminated.

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> N {
// using the algorithm from Numerical Recipes in C

Expand Down Expand Up @@ -124,6 +125,7 @@ where Standard: Distribution<N>
impl<N: Float> Distribution<u64> for Poisson<N>
where Standard: Distribution<N>
{
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u64 {
vks marked this conversation as resolved.
Show resolved Hide resolved
let result: N = self.sample(rng);
result.to_u64().unwrap()
Expand Down