-
Notifications
You must be signed in to change notification settings - Fork 432
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
Generic distributions: use custom trait #793
Conversation
A custom trait can be fine-tuned to our needs
/// | ||
/// The bounds and methods are based purely on internal | ||
/// requirements, and will change as needed. | ||
pub trait Float: Copy + Sized + cmp::PartialOrd |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe pub(crate)
is more appropriate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then it would be a private trait used in a public API — however, the compiler doesn't complain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I do get a compiler error if marking this pub(crate)
, though not if it is a pub
item within a private module which is not re-exported (I guess this must be a compiler bug and don't know that we should exploit this).
error[E0445]: crate-visible trait
utils::Float
in public interface
There are discussions on roughly this here and here, but I'm not sure exactly where this got to.
I think using a custom trait is fine, but we should not make it public. |
Aside from the technicalities of whether hiding the Writing generic code is quite common in Rust, and if we do so using an internal trait as a bound it makes writing generic code over FP types significantly more difficult for users. |
I have completed the type transitions for most distributions, with two exceptions: Binomial and Poisson. Both involve integer and float types, while the latter also uses the |
These are required as a side-effect of the previous generalisation.
For some reason I missed some obvious compile errors in the previous push. Fixed, but the new requirements on type specification for |
Regarding the previous question: we could add a |
Regarding the The I suggest we make a new PR for the second change since it's a new topic and the rest of this is ready. (I also want to leave the optimal f32 Ziggurat implementations until later; it's related to #257). |
I think this is the best we can do, unless we get default type parameters in Rust. |
There is a problem with the binomial distribution in I agree that it does not make sense to make the binomial distribution generic, given we don't plan to use a different algorithm for |
The advantage of returning |
Whoops! Perhaps you can make a PR after this is merged.
Except that since the algorithm does not enforce the upper bound, this is a false promise. Probably adding an assert would cover the vast majority of uses, but it would be possible to break by using extremely large rate λ. So lets try doing both in a new PR. Time to merge this one! |
I opened #794. |
Continuing from #785, this replaces
num_traits::Float
with a custom version, then also makesCauchy
generic.Closes #100.
Thoughts (@vks)? The implementation of
Float
in the first commit doesn't look bad, but we see in the second it is growing quickly.One motivation is to avoid a dependency on an unstable lib (there appear to be suggestions the trait should change). Another is to avoid requiring
.unwrap()
onN::from(..)
, although as implemented here we useas
which isn't exactly safe (we could instead restrict input precision and useFrom<f32>
).