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

Added a Distribution<usize> for Poisson #958

Closed
wants to merge 2 commits into from

Conversation

CGMossa
Copy link
Contributor

@CGMossa CGMossa commented Apr 2, 2020

I just needed this and couldn't implement it in my own crate.

Using the casting from f64 -> u64 -> usize.

I'd like to hear suggestions and concerns, as I am unsure this is the best thing to do for this type of conversation.

Using the casting from f64 -> u64 -> usize.
rand_distr/src/poisson.rs Outdated Show resolved Hide resolved
Mimiced the approach for to_u64.
@CGMossa
Copy link
Contributor Author

CGMossa commented Apr 4, 2020

Hey, am I supposed to do something here or just wait for another review?

@bjorn3
Copy link
Contributor

bjorn3 commented Apr 4, 2020

Probably just wait. I am not a member of rust-random.

@CGMossa
Copy link
Contributor Author

CGMossa commented Apr 4, 2020 via email

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.

Thanks for the PR @CGMossa.

It seems fine, even if it is somewhat redundant functionality.

@vks?

Comment on lines +89 to +90
fn to_usize(self) -> Option<usize> {
if self >= 0. && self <= ::core::usize::MAX as f32 {
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 incorrect for 32-bit platforms. Check this out.

This isn't your fault; to_u64 for f64 makes the same mistake. Hmm, even the TryFrom in Rust 1.34 doesn't handle this conversion. I'm starting to hate this topic; seems simple but isn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ohh this issue. I don't know how to solve this aswell. The link didn't suggest a solution. I think this belongs in another PR, or something?

Copy link
Member

Choose a reason for hiding this comment

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

Yes: #973

@vks
Copy link
Collaborator

vks commented Apr 5, 2020

I'm a bit wary of sampling usize because of portability hazards: the results for 32 bit and 64 bit platforms may be different. However, this probably does not apply here, because it converts from floats.

What is the motivation for adding this instead of letting the user deal with the conversion from a float? Should we also add support for the other integer types?

@burdges
Copy link
Contributor

burdges commented Apr 5, 2020

Why is this useful? If you do want this then I'd suspect you want usize inside another type, in which case this works:

struct MyUsize(size);
impl Distribution<MyUsize> for Poisson { }

@CGMossa
Copy link
Contributor Author

CGMossa commented Apr 5, 2020

I just thought that this was just a little missing detail to add.
It is openning up another issue with casting f64 to usize, which is fine. If
the right way to this is to be found somewhere, I'd be happy to write it.

I don't think there should be implementation for all integer types, but for count-distributions, it
is not a stretch at all, to expect their realisations to be count-compatible types. So I still think this
is very-nice-to-have, and welcome more suggestions to improve it.

@burdges
Copy link
Contributor

burdges commented Apr 5, 2020

As a rule, implementations for usize should use the u32 implementation and panic if usize is smaller than u32.

@CGMossa
Copy link
Contributor Author

CGMossa commented May 1, 2020

As a rule, implementations for usize should use the u32 implementation and panic if usize is smaller than u32.

I think this does that. Do you mind checking?

@CGMossa
Copy link
Contributor Author

CGMossa commented May 1, 2020

I think the API should be consistent with what it is supposed to reflect. A Poisson distribution have realisations that are usize, u32, u64 and thus I think it should yield that.

I'd love to carry argue more for this, or even do more necessary work to make that happen.

@dhardy
Copy link
Member

dhardy commented May 1, 2020

Actually, this seems fine as-is to me, except for the issue I raised. But maybe we should solve that separately. I'll open an issue.

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.

LGTM, aside from #973. @vks?

let result: u64 = self.sample(rng);
result as usize
let result: N = self.sample(rng);
result.to_usize().unwrap()
Copy link
Collaborator

@vks vks May 1, 2020

Choose a reason for hiding this comment

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

This may panic on 32-bit platforms. Maybe we should document that somewhere?

@vks
Copy link
Collaborator

vks commented Aug 1, 2020

Unfortunately, this is incompatible with the choices in #987. We now require the user to perform the conversion from f64 or f32.

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.

5 participants