Skip to content

Commit

Permalink
feat: reject constructing Uniform of infinite support
Browse files Browse the repository at this point in the history
additionally removes logic handling infinite support
  • Loading branch information
YeungOnion committed Apr 22, 2024
1 parent 1e67391 commit 18d0a12
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/distribution/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Uniform {
///
/// # Errors
///
/// Returns an error if `min` or `max` are `NaN`
/// Returns an error if `min` or `max` are `NaN` or unbounded
///
/// # Examples
///
Expand All @@ -44,12 +44,21 @@ impl Uniform {
///
/// result = Uniform::new(f64::NAN, f64::NAN);
/// assert!(result.is_err());
///
/// result = Uniform::new(f64::NEG_INFINITY, 1.0);
/// assert!(result.is_err());
/// ```
pub fn new(min: f64, max: f64) -> Result<Uniform> {
if min > max || min.is_nan() || max.is_nan() {
Err(StatsError::BadParams)
} else {
Ok(Uniform { min, max })
if min.is_nan() || max.is_nan() {
return Err(StatsError::BadParams);
}

match (min.is_finite(), max.is_finite(), min < max) {
(false, false, _) => Err(StatsError::ArgFinite("min and max")),
(false, true, _) => Err(StatsError::ArgFinite("min")),
(true, false, _) => Err(StatsError::ArgFinite("max")),
(true, true, false) => Err(StatsError::ArgLteArg("min", "max")),
(true, true, true) => Ok(Uniform { min, max }),
}
}
}
Expand Down Expand Up @@ -94,10 +103,6 @@ impl ContinuousCDF<f64, f64> for Uniform {
1.0
} else if x >= self.max {
0.0
} else if x.is_infinite() && self.max.is_infinite() {
0.0
} else if self.max.is_infinite() {
1.0
} else {
(self.max - x) / (self.max - self.min)
}
Expand Down

0 comments on commit 18d0a12

Please sign in to comment.