Skip to content

Commit bca62a8

Browse files
committed
Avoid panic with almost-zero parameters for FisherF
These are too small to construct a ChiSquared distribution from.
1 parent 723bb93 commit bca62a8

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818

1919
### Fixes
2020
- Fix `Geometric::new` for small `p > 0` where `1 - p` rounds to 1 (#36)
21+
- Fix panic in `FisherF::new` on almost zero parameters (#39)
2122

2223
## [0.5.2]
2324

src/fisher_f.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
//! The Fisher F-distribution.
1111
12-
use crate::{ChiSquared, Distribution, Exp1, Open01, StandardNormal};
12+
use crate::{ChiSquared, Distribution, Exp1, Open01, StandardNormal, chi_squared};
1313
use core::fmt;
1414
use num_traits::Float;
1515
use rand::Rng;
@@ -57,9 +57,9 @@ where
5757
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5858
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5959
pub enum Error {
60-
/// `m <= 0` or `nan`.
60+
/// `0.5 * m <= 0.0` or `nan`.
6161
MTooSmall,
62-
/// `n <= 0` or `nan`.
62+
/// `0.5 * n <= 0.0` or `nan`.
6363
NTooSmall,
6464
}
6565

@@ -84,17 +84,13 @@ where
8484
{
8585
/// Create a new `FisherF` distribution, with the given parameter.
8686
pub fn new(m: F, n: F) -> Result<FisherF<F>, Error> {
87-
let zero = F::zero();
88-
if !(m > zero) {
89-
return Err(Error::MTooSmall);
90-
}
91-
if !(n > zero) {
92-
return Err(Error::NTooSmall);
93-
}
94-
9587
Ok(FisherF {
96-
numer: ChiSquared::new(m).unwrap(),
97-
denom: ChiSquared::new(n).unwrap(),
88+
numer: ChiSquared::new(m).map_err(|x| match x {
89+
chi_squared::Error::DoFTooSmall => Error::MTooSmall,
90+
})?,
91+
denom: ChiSquared::new(n).map_err(|x| match x {
92+
chi_squared::Error::DoFTooSmall => Error::NTooSmall,
93+
})?,
9894
dof_ratio: n / m,
9995
})
10096
}

0 commit comments

Comments
 (0)