Skip to content

Commit

Permalink
Implement Error and Display for all errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vks committed May 13, 2021
1 parent 17dbc4c commit f952177
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions rand_distr/src/hypergeometric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ impl fmt::Display for Error {
}
}

#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
impl std::error::Error for Error {}

// evaluate fact(numerator.0)*fact(numerator.1) / fact(denominator.0)*fact(denominator.1)
fn fraction_of_products_of_factorials(numerator: (u64, u64), denominator: (u64, u64)) -> f64 {
let min_top = u64::min(numerator.0, numerator.1);
Expand Down
14 changes: 14 additions & 0 deletions rand_distr/src/inverse_gaussian.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{Distribution, Standard, StandardNormal};
use num_traits::Float;
use rand::Rng;
use core::fmt;

/// Error type returned from `InverseGaussian::new`
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand All @@ -11,6 +12,19 @@ pub enum Error {
ShapeNegativeOrNull,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Error::MeanNegativeOrNull => "mean <= 0 or is NaN in inverse Gaussian distribution",
Error::ShapeNegativeOrNull => "shape <= 0 or is NaN in inverse Gaussian distribution",
})
}
}

#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
impl std::error::Error for Error {}

/// The [inverse Gaussian distribution](https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution)
#[derive(Debug, Clone, Copy)]
pub struct InverseGaussian<F>
Expand Down
14 changes: 14 additions & 0 deletions rand_distr/src/normal_inverse_gaussian.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{Distribution, InverseGaussian, Standard, StandardNormal};
use num_traits::Float;
use rand::Rng;
use core::fmt;

/// Error type returned from `NormalInverseGaussian::new`
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand All @@ -11,6 +12,19 @@ pub enum Error {
AbsoluteBetaNotLessThanAlpha,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Error::AlphaNegativeOrNull => "alpha <= 0 or is NaN in normal inverse Gaussian distribution",
Error::AbsoluteBetaNotLessThanAlpha => "|beta| >= alpha or is NaN in normal inverse Gaussian distribution",
})
}
}

#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
impl std::error::Error for Error {}

/// The [normal-inverse Gaussian distribution](https://en.wikipedia.org/wiki/Normal-inverse_Gaussian_distribution)
#[derive(Debug, Clone, Copy)]
pub struct NormalInverseGaussian<F>
Expand Down

0 comments on commit f952177

Please sign in to comment.