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

impl std::error::Error for rand_distr::*Error. #919

Merged
merged 4 commits into from Dec 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions rand_distr/src/binomial.rs
Expand Up @@ -11,6 +11,7 @@

use rand::Rng;
use crate::{Distribution, Uniform};
use std::{error, fmt};

/// The binomial distribution `Binomial(n, p)`.
///
Expand Down Expand Up @@ -43,6 +44,17 @@ pub enum Error {
ProbabilityTooLarge,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Error::ProbabilityTooSmall => "p < 0 or is NaN in binomial distribution",
Error::ProbabilityTooLarge => "p > 1 in binomial distribution",
})
}
}

impl error::Error for Error {}

impl Binomial {
/// Construct a new `Binomial` with the given shape parameters `n` (number
/// of trials) and `p` (probability of success).
Expand Down
11 changes: 11 additions & 0 deletions rand_distr/src/cauchy.rs
Expand Up @@ -12,6 +12,7 @@
use rand::Rng;
use crate::{Distribution, Standard};
use crate::utils::Float;
use std::{error, fmt};

/// The Cauchy distribution `Cauchy(median, scale)`.
///
Expand Down Expand Up @@ -43,6 +44,16 @@ pub enum Error {
ScaleTooSmall,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Error::ScaleTooSmall => "scale is not positive in Cauchy distribution",
})
}
}

impl error::Error for Error {}

impl<N: Float> Cauchy<N>
where Standard: Distribution<N>
{
Expand Down
16 changes: 15 additions & 1 deletion rand_distr/src/dirichlet.rs
Expand Up @@ -12,8 +12,9 @@
use rand::Rng;
use crate::{Distribution, Gamma, StandardNormal, Exp1, Open01};
use crate::utils::Float;
use std::{error, fmt};

/// The dirichelet distribution `Dirichlet(alpha)`.
/// The Dirichlet distribution `Dirichlet(alpha)`.
///
/// The Dirichlet distribution is a family of continuous multivariate
/// probability distributions parameterized by a vector alpha of positive reals.
Expand Down Expand Up @@ -46,6 +47,19 @@ pub enum Error {
SizeTooSmall,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Error::AlphaTooShort | Error::SizeTooSmall => {
"less than 2 dimensions in Dirichlet distribution"
}
Error::AlphaTooSmall => "alpha is not positive in Dirichlet distribution",
dhardy marked this conversation as resolved.
Show resolved Hide resolved
})
}
}

impl error::Error for Error {}

impl<N: Float> Dirichlet<N>
where StandardNormal: Distribution<N>, Exp1: Distribution<N>, Open01: Distribution<N>
{
Expand Down
11 changes: 11 additions & 0 deletions rand_distr/src/exponential.rs
Expand Up @@ -12,6 +12,7 @@
use rand::Rng;
use crate::{ziggurat_tables, Distribution};
use crate::utils::{ziggurat, Float};
use std::{error, fmt};

/// Samples floating-point numbers according to the exponential distribution,
/// with rate parameter `λ = 1`. This is equivalent to `Exp::new(1.0)` or
Expand Down Expand Up @@ -97,6 +98,16 @@ pub enum Error {
LambdaTooSmall,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Error::LambdaTooSmall => "lambda is not positive in exponential distribution",
})
}
}

impl error::Error for Error {}

impl<N: Float> Exp<N>
where Exp1: Distribution<N>
{
Expand Down
47 changes: 47 additions & 0 deletions rand_distr/src/gamma.rs
Expand Up @@ -16,6 +16,7 @@ use rand::Rng;
use crate::normal::StandardNormal;
use crate::{Distribution, Exp1, Exp, Open01};
use crate::utils::Float;
use std::{error, fmt};

/// The Gamma distribution `Gamma(shape, scale)` distribution.
///
Expand Down Expand Up @@ -63,6 +64,18 @@ pub enum Error {
ScaleTooLarge,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Error::ShapeTooSmall => "shape is not positive in gamma distribution",
Error::ScaleTooSmall => "scale is not positive in gamma distribution",
Error::ScaleTooLarge => "scale is infinity in gamma distribution",
})
}
}

impl error::Error for Error {}

#[derive(Clone, Copy, Debug)]
enum GammaRepr<N> {
Large(GammaLargeShape<N>),
Expand Down Expand Up @@ -224,6 +237,18 @@ pub enum ChiSquaredError {
DoFTooSmall,
}

impl fmt::Display for ChiSquaredError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
ChiSquaredError::DoFTooSmall => {
"degrees-of-freedom k is not positive in chi-squared distribution"
}
})
}
}

impl error::Error for ChiSquaredError {}

#[derive(Clone, Copy, Debug)]
enum ChiSquaredRepr<N> {
// k == 1, Gamma(alpha, ..) is particularly slow for alpha < 1,
Expand Down Expand Up @@ -298,6 +323,17 @@ pub enum FisherFError {
NTooSmall,
}

impl fmt::Display for FisherFError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
FisherFError::MTooSmall => "m is not positive in Fisher F distribution",
FisherFError::NTooSmall => "n is not positive in Fisher F distribution",
})
}
}

impl error::Error for FisherFError {}

impl<N: Float> FisherF<N>
where StandardNormal: Distribution<N>, Exp1: Distribution<N>, Open01: Distribution<N>
{
Expand Down Expand Up @@ -390,6 +426,17 @@ pub enum BetaError {
BetaTooSmall,
}

impl fmt::Display for BetaError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
BetaError::AlphaTooSmall => "alpha is not positive in beta distribution",
BetaError::BetaTooSmall => "beta is not positive in beta distribution",
})
}
}

impl error::Error for BetaError {}

impl<N: Float> Beta<N>
where StandardNormal: Distribution<N>, Exp1: Distribution<N>, Open01: Distribution<N>
{
Expand Down
3 changes: 2 additions & 1 deletion rand_distr/src/lib.rs
Expand Up @@ -64,7 +64,8 @@
//! - [`UnitDisc`] distribution

pub use rand::distributions::{Distribution, DistIter, Standard,
Alphanumeric, Uniform, OpenClosed01, Open01, Bernoulli, uniform, weighted};
Alphanumeric, Uniform, OpenClosed01, Open01, Bernoulli, BernoulliError,
uniform, weighted};

pub use self::unit_sphere::UnitSphere;
pub use self::unit_ball::UnitBall;
Expand Down
11 changes: 11 additions & 0 deletions rand_distr/src/normal.rs
Expand Up @@ -12,6 +12,7 @@
use rand::Rng;
use crate::{ziggurat_tables, Distribution, Open01};
use crate::utils::{ziggurat, Float};
use std::{error, fmt};

/// Samples floating-point numbers according to the normal distribution
/// `N(0, 1)` (a.k.a. a standard normal, or Gaussian). This is equivalent to
Expand Down Expand Up @@ -114,6 +115,16 @@ pub enum Error {
StdDevTooSmall,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Error::StdDevTooSmall => "std_dev < 0 or is NaN in normal distribution",
})
}
}

impl error::Error for Error {}

impl<N: Float> Normal<N>
where StandardNormal: Distribution<N>
{
Expand Down
12 changes: 12 additions & 0 deletions rand_distr/src/pareto.rs
Expand Up @@ -11,6 +11,7 @@
use rand::Rng;
use crate::{Distribution, OpenClosed01};
use crate::utils::Float;
use std::{error, fmt};

/// Samples floating-point numbers according to the Pareto distribution
///
Expand All @@ -37,6 +38,17 @@ pub enum Error {
ShapeTooSmall,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Error::ScaleTooSmall => "scale is not positive in Pareto distribution",
Error::ShapeTooSmall => "shape is not positive in Pareto distribution",
})
}
}

impl error::Error for Error {}

impl<N: Float> Pareto<N>
where OpenClosed01: Distribution<N>
{
Expand Down
13 changes: 13 additions & 0 deletions rand_distr/src/pert.rs
Expand Up @@ -10,6 +10,7 @@
use rand::Rng;
use crate::{Distribution, Beta, StandardNormal, Exp1, Open01};
use crate::utils::Float;
use std::{error, fmt};

/// The PERT distribution.
///
Expand Down Expand Up @@ -47,6 +48,18 @@ pub enum PertError {
ShapeTooSmall,
}

impl fmt::Display for PertError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
PertError::RangeTooSmall => "min..=max is not proper range in PERT distribution",
dhardy marked this conversation as resolved.
Show resolved Hide resolved
PertError::ModeRange => "mode is outside min..=max in PERT distribution",
PertError::ShapeTooSmall => "shape < 0 or is NaN in PERT distribution",
})
}
}

impl error::Error for PertError {}

impl<N: Float> Pert<N>
where StandardNormal: Distribution<N>, Exp1: Distribution<N>, Open01: Distribution<N>
{
Expand Down
11 changes: 11 additions & 0 deletions rand_distr/src/poisson.rs
Expand Up @@ -12,6 +12,7 @@
use rand::Rng;
use crate::{Distribution, Cauchy, Standard};
use crate::utils::Float;
use std::{error, fmt};

/// The Poisson distribution `Poisson(lambda)`.
///
Expand Down Expand Up @@ -44,6 +45,16 @@ pub enum Error {
ShapeTooSmall,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Error::ShapeTooSmall => "lambda is not positive in Poisson distribution",
})
}
}

impl error::Error for Error {}

impl<N: Float> Poisson<N>
where Standard: Distribution<N>
{
Expand Down
14 changes: 14 additions & 0 deletions rand_distr/src/triangular.rs
Expand Up @@ -10,6 +10,7 @@
use rand::Rng;
use crate::{Distribution, Standard};
use crate::utils::Float;
use std::{error, fmt};

/// The triangular distribution.
///
Expand Down Expand Up @@ -46,6 +47,19 @@ pub enum TriangularError {
ModeRange,
}

impl fmt::Display for TriangularError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
TriangularError::RangeTooSmall => {
"min..=max is not proper range in triangular distribution"
}
TriangularError::ModeRange => "mode is outside min..=max in triangular distribution",
})
}
}

impl error::Error for TriangularError {}

impl<N: Float> Triangular<N>
where Standard: Distribution<N>
{
Expand Down
12 changes: 12 additions & 0 deletions rand_distr/src/weibull.rs
Expand Up @@ -11,6 +11,7 @@
use rand::Rng;
use crate::{Distribution, OpenClosed01};
use crate::utils::Float;
use std::{error, fmt};

/// Samples floating-point numbers according to the Weibull distribution
///
Expand All @@ -37,6 +38,17 @@ pub enum Error {
ShapeTooSmall,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Error::ScaleTooSmall => "scale is not positive in Weibull distribution",
Error::ShapeTooSmall => "shape is not positive in Weibull distribution",
})
}
}

impl error::Error for Error {}

impl<N: Float> Weibull<N>
where OpenClosed01: Distribution<N>
{
Expand Down
14 changes: 13 additions & 1 deletion src/distributions/bernoulli.rs
Expand Up @@ -10,6 +10,7 @@

use crate::Rng;
use crate::distributions::Distribution;
use core::{fmt, u64};

/// The Bernoulli distribution.
///
Expand Down Expand Up @@ -55,7 +56,7 @@ pub struct Bernoulli {
// the RNG, and pay the performance price for all uses that *are* reasonable.
// Luckily, if `new()` and `sample` are close, the compiler can optimize out the
// extra check.
const ALWAYS_TRUE: u64 = ::core::u64::MAX;
const ALWAYS_TRUE: u64 = u64::MAX;

// This is just `2.0.powi(64)`, but written this way because it is not available
// in `no_std` mode.
Expand All @@ -68,6 +69,17 @@ pub enum BernoulliError {
InvalidProbability,
}

impl fmt::Display for BernoulliError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
BernoulliError::InvalidProbability => "p is outside 0..=1 in Bernoulli distribution",
dhardy marked this conversation as resolved.
Show resolved Hide resolved
})
}
}

#[cfg(feature = "std")]
impl ::std::error::Error for BernoulliError {}

impl Bernoulli {
/// Construct a new `Bernoulli` with the given probability of success `p`.
///
Expand Down