Skip to content

Commit 79f1b0f

Browse files
authored
Document known issues: #1378, #1312, #1476.
1 parent 2833c78 commit 79f1b0f

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
2020
- Fix portability of `rand::distributions::Slice` (#1469)
2121
- Rename `rand::distributions` to `rand::distr` (#1470)
2222
- The `serde1` feature has been renamed `serde` (#1477)
23+
- Mark `WeightError`, `PoissonError`, `BinomialError` as `#[non_exhaustive]` (#1480).
2324

2425
## [0.9.0-alpha.1] - 2024-03-18
2526
- Add the `Slice::num_choices` method to the Slice distribution (#1402)

rand_distr/src/binomial.rs

+13
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ use rand::Rng;
2626
///
2727
/// `f(k) = n!/(k! (n-k)!) p^k (1-p)^(n-k)` for `k >= 0`.
2828
///
29+
/// # Known issues
30+
///
31+
/// See documentation of [`Binomial::new`].
32+
///
2933
/// # Plot
3034
///
3135
/// The following plot of the binomial distribution illustrates the
@@ -54,6 +58,8 @@ pub struct Binomial {
5458

5559
/// Error type returned from [`Binomial::new`].
5660
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
61+
// Marked non_exhaustive to allow a new error code in the solution to #1378.
62+
#[non_exhaustive]
5763
pub enum Error {
5864
/// `p < 0` or `nan`.
5965
ProbabilityTooSmall,
@@ -76,6 +82,13 @@ impl std::error::Error for Error {}
7682
impl Binomial {
7783
/// Construct a new `Binomial` with the given shape parameters `n` (number
7884
/// of trials) and `p` (probability of success).
85+
///
86+
/// # Known issues
87+
///
88+
/// Although this method should return an [`Error`] on invalid parameters,
89+
/// some (extreme) parameter combinations are known to return a [`Binomial`]
90+
/// object which panics when [sampled](Distribution::sample).
91+
/// See [#1378](https://github.com/rust-random/rand/issues/1378).
7992
pub fn new(n: u64, p: f64) -> Result<Binomial, Error> {
8093
if !(p >= 0.0) {
8194
return Err(Error::ProbabilityTooSmall);

rand_distr/src/poisson.rs

+15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ use rand::Rng;
2323
/// This distribution has density function:
2424
/// `f(k) = λ^k * exp(-λ) / k!` for `k >= 0`.
2525
///
26+
/// # Known issues
27+
///
28+
/// See documentation of [`Poisson::new`].
29+
///
2630
/// # Plot
2731
///
2832
/// The following plot shows the Poisson distribution with various values of `λ`.
@@ -56,6 +60,8 @@ where
5660

5761
/// Error type returned from [`Poisson::new`].
5862
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
63+
// Marked non_exhaustive to allow a new error code in the solution to #1312.
64+
#[non_exhaustive]
5965
pub enum Error {
6066
/// `lambda <= 0`
6167
ShapeTooSmall,
@@ -82,6 +88,15 @@ where
8288
{
8389
/// Construct a new `Poisson` with the given shape parameter
8490
/// `lambda`.
91+
///
92+
/// # Known issues
93+
///
94+
/// Although this method should return an [`Error`] on invalid parameters,
95+
/// some (extreme) values of `lambda` are known to return a [`Poisson`]
96+
/// object which hangs when [sampled](Distribution::sample).
97+
/// Large (less extreme) values of `lambda` may result in successful
98+
/// sampling but with reduced precision.
99+
/// See [#1312](https://github.com/rust-random/rand/issues/1312).
85100
pub fn new(lambda: F) -> Result<Poisson<F>, Error> {
86101
if !lambda.is_finite() {
87102
return Err(Error::NonFinite);

src/distr/weighted_index.rs

+2
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ mod test {
704704

705705
/// Errors returned by [`WeightedIndex::new`], [`WeightedIndex::update_weights`] and other weighted distributions
706706
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
707+
// Marked non_exhaustive to allow a new error code in the solution to #1476.
708+
#[non_exhaustive]
707709
pub enum WeightError {
708710
/// The input weight sequence is empty, too long, or wrongly ordered
709711
InvalidInput,

src/seq/slice.rs

+6
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ pub trait IndexedRandom: Index<usize> {
189189
/// if the "nightly" feature is enabled, or `O(length)` space and
190190
/// `O(length + amount * log length)` time otherwise.
191191
///
192+
/// # Known issues
193+
///
194+
/// The algorithm currently used to implement this method loses accuracy
195+
/// when small values are used for weights.
196+
/// See [#1476](https://github.com/rust-random/rand/issues/1476).
197+
///
192198
/// # Example
193199
///
194200
/// ```

0 commit comments

Comments
 (0)