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

rand_distr: Use Result instead of panics #770

Merged
merged 6 commits into from
Apr 20, 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
8 changes: 4 additions & 4 deletions rand_distr/src/binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ pub struct Binomial {
/// Error type returned from `Binomial::new`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error {
/// `p` < 0.
/// `p < 0` or `nan`.
ProbabilityTooSmall,
/// `p` > 1.
/// `p > 1`.
ProbabilityTooLarge,
}

impl Binomial {
/// Construct a new `Binomial` with the given shape parameters `n` (number
/// of trials) and `p` (probability of success).
pub fn new(n: u64, p: f64) -> Result<Binomial, Error> {
if p < 0.0 {
if !(p >= 0.0) {
return Err(Error::ProbabilityTooSmall);
}
if p > 1.0 {
if !(p <= 1.0) {
return Err(Error::ProbabilityTooLarge);
}
Ok(Binomial { n, p })
Expand Down
4 changes: 2 additions & 2 deletions rand_distr/src/cauchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ pub struct Cauchy {
/// Error type returned from `Cauchy::new`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error {
/// `scale <= 0`.
/// `scale <= 0` or `nan`.
ScaleTooSmall,
}

impl Cauchy {
/// Construct a new `Cauchy` with the given shape parameters
/// `median` the peak location and `scale` the scale factor.
pub fn new(median: f64, scale: f64) -> Result<Cauchy, Error> {
if scale <= 0.0 {
if !(scale > 0.0) {
return Err(Error::ScaleTooSmall);
}
Ok(Cauchy {
Expand Down
4 changes: 2 additions & 2 deletions rand_distr/src/dirichlet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct Dirichlet {
pub enum Error {
/// `alpha.len() < 2`.
AlphaTooShort,
/// `alpha <= 0.0`.
/// `alpha <= 0.0` or `nan`.
AlphaTooSmall,
/// `size < 2`.
SizeTooSmall,
Expand Down Expand Up @@ -70,7 +70,7 @@ impl Dirichlet {
/// Requires `size >= 2`.
#[inline]
pub fn new_with_param(alpha: f64, size: usize) -> Result<Dirichlet, Error> {
if alpha <= 0.0 {
if !(alpha > 0.0) {
return Err(Error::AlphaTooSmall);
}
if size < 2 {
Expand Down
4 changes: 2 additions & 2 deletions rand_distr/src/exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub struct Exp {
/// Error type returned from `Exp::new`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error {
/// `lambda <= 0`.
/// `lambda <= 0` or `nan`.
LambdaTooSmall,
}

Expand All @@ -93,7 +93,7 @@ impl Exp {
/// `lambda`.
#[inline]
pub fn new(lambda: f64) -> Result<Exp, Error> {
if lambda <= 0.0 {
if !(lambda > 0.0) {
return Err(Error::LambdaTooSmall);
}
Ok(Exp { lambda_inverse: 1.0 / lambda })
Expand Down
24 changes: 12 additions & 12 deletions rand_distr/src/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ pub struct Gamma {
/// Error type returned from `Gamma::new`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error {
/// `shape <= 0`.
/// `shape <= 0` or `nan`.
ShapeTooSmall,
/// `scale <= 0`.
/// `scale <= 0` or `nan`.
ScaleTooSmall,
/// `1 / scale == 0`.
ScaleTooLarge,
Expand Down Expand Up @@ -105,10 +105,10 @@ impl Gamma {
/// distribution.
#[inline]
pub fn new(shape: f64, scale: f64) -> Result<Gamma, Error> {
if shape <= 0.0 {
if !(shape > 0.0) {
return Err(Error::ShapeTooSmall);
}
if scale <= 0.0 {
if !(scale > 0.0) {
return Err(Error::ScaleTooSmall);
}

Expand Down Expand Up @@ -205,7 +205,7 @@ pub struct ChiSquared {
/// Error type returned from `ChiSquared::new`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ChiSquaredError {
/// `0.5 * k <= 0`.
/// `0.5 * k <= 0` or `nan`.
DoFTooSmall,
}

Expand All @@ -225,7 +225,7 @@ impl ChiSquared {
let repr = if k == 1.0 {
DoFExactlyOne
} else {
if 0.5 * k <= 0.0 {
if !(0.5 * k > 0.0) {
return Err(ChiSquaredError::DoFTooSmall);
}
DoFAnythingElse(Gamma::new(0.5 * k, 2.0).unwrap())
dhardy marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -273,19 +273,19 @@ pub struct FisherF {
/// Error type returned from `FisherF::new`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FisherFError {
/// `m <= 0`.
/// `m <= 0` or `nan`.
MTooSmall,
/// `n <= 0`.
/// `n <= 0` or `nan`.
NTooSmall,
}

impl FisherF {
/// Create a new `FisherF` distribution, with the given parameter.
pub fn new(m: f64, n: f64) -> Result<FisherF, FisherFError> {
if m <= 0.0 {
if !(m > 0.0) {
return Err(FisherFError::MTooSmall);
}
if n <= 0.0 {
if !(n > 0.0) {
return Err(FisherFError::NTooSmall);
}

Expand Down Expand Up @@ -357,9 +357,9 @@ pub struct Beta {
/// Error type returned from `Beta::new`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BetaError {
/// `alpha <= 0`.
/// `alpha <= 0` or `nan`.
AlphaTooSmall,
/// `beta <= 0`.
/// `beta <= 0` or `nan`.
BetaTooSmall,
}

Expand Down
6 changes: 3 additions & 3 deletions rand_distr/src/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub struct Normal {
/// Error type returned from `Normal::new` and `LogNormal::new`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error {
/// `std_dev < 0`.
/// `std_dev < 0` or `nan`.
StdDevTooSmall,
}

Expand All @@ -110,7 +110,7 @@ impl Normal {
/// standard deviation.
#[inline]
pub fn new(mean: f64, std_dev: f64) -> Result<Normal, Error> {
if std_dev < 0.0 {
if !(std_dev >= 0.0) {
return Err(Error::StdDevTooSmall);
dhardy marked this conversation as resolved.
Show resolved Hide resolved
}
Ok(Normal {
Expand Down Expand Up @@ -152,7 +152,7 @@ impl LogNormal {
/// and standard deviation of the logarithm of the distribution.
#[inline]
pub fn new(mean: f64, std_dev: f64) -> Result<LogNormal, Error> {
dhardy marked this conversation as resolved.
Show resolved Hide resolved
if std_dev < 0.0 {
if !(std_dev >= 0.0) {
return Err(Error::StdDevTooSmall);
}
Ok(LogNormal { norm: Normal::new(mean, std_dev).unwrap() })
Expand Down
8 changes: 4 additions & 4 deletions rand_distr/src/pareto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ pub struct Pareto {
/// Error type returned from `Pareto::new`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error {
/// `scale <= 0`.
/// `scale <= 0` or `nan`.
ScaleTooSmall,
/// `shape <= 0`.
/// `shape <= 0` or `nan`.
ShapeTooSmall,
}

Expand All @@ -42,10 +42,10 @@ impl Pareto {
/// In the literature, `scale` is commonly written as x<sub>m</sub> or k and
/// `shape` is often written as α.
pub fn new(scale: f64, shape: f64) -> Result<Pareto, Error> {
if scale <= 0.0 {
if !(scale > 0.0) {
return Err(Error::ScaleTooSmall);
}
if shape <= 0.0 {
if !(shape > 0.0) {
return Err(Error::ShapeTooSmall);
}
Ok(Pareto { scale, inv_neg_shape: -1.0 / shape })
Expand Down
4 changes: 2 additions & 2 deletions rand_distr/src/poisson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ pub struct Poisson {
/// Error type returned from `Poisson::new`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error {
/// `lambda <= 0`.
/// `lambda <= 0` or `nan`.
ShapeTooSmall,
}

impl Poisson {
/// Construct a new `Poisson` with the given shape parameter
/// `lambda`.
pub fn new(lambda: f64) -> Result<Poisson, Error> {
if lambda <= 0.0 {
if !(lambda > 0.0) {
return Err(Error::ShapeTooSmall);
}
let log_lambda = lambda.ln();
Expand Down
12 changes: 6 additions & 6 deletions rand_distr/src/triangular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ pub struct Triangular {
/// Error type returned from `Triangular::new`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error {
/// `max < mode`.
/// `max < mode` or `max` is `nan`.
MaxTooSmall,
/// `mode < min`.
/// `mode < min` or `mode` is `nan`.
ModeTooSmall,
/// `max == min`.
/// `max == min` or `min` is `nan`.
MaxEqualsMin,
}

Expand All @@ -44,13 +44,13 @@ impl Triangular {
/// `mode`.
#[inline]
pub fn new(min: f64, max: f64, mode: f64) -> Result<Triangular, Error> {
if max < mode {
if !(max >= mode) {
return Err(Error::MaxTooSmall);
}
if mode < min {
if !(mode >= min) {
return Err(Error::ModeTooSmall);
}
if max == min {
if !(max != min) {
dhardy marked this conversation as resolved.
Show resolved Hide resolved
return Err(Error::MaxEqualsMin);
}
Ok(Triangular { min, max, mode })
Expand Down
8 changes: 4 additions & 4 deletions rand_distr/src/weibull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ pub struct Weibull {
/// Error type returned from `Weibull::new`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error {
/// `scale <= 0`.
/// `scale <= 0` or `nan`.
ScaleTooSmall,
/// `shape <= 0`.
/// `shape <= 0` or `nan`.
ShapeTooSmall,
}

impl Weibull {
/// Construct a new `Weibull` distribution with given `scale` and `shape`.
pub fn new(scale: f64, shape: f64) -> Result<Weibull, Error> {
if scale <= 0.0 {
if !(scale > 0.0) {
return Err(Error::ScaleTooSmall);
}
if shape <= 0.0 {
if !(shape > 0.0) {
return Err(Error::ShapeTooSmall);
}
Ok(Weibull { inv_shape: 1./shape, scale })
Expand Down