Skip to content

Commit a6a9f7b

Browse files
authored
Forbid negative zero lambda in Exp distribution (#44)
1 parent cf6508b commit a6a9f7b

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Use `direct-minimal-versions` (#38)
2222
- Fix panic in `FisherF::new` on almost zero parameters (#39)
2323
- Fix panic in `NormalInverseGaussian::new` with very large `alpha`; this is a Value-breaking change (#40)
24+
- Error instead of producing `-inf` output for `Exp` when `lambda` is `-0.0` (#44)
2425

2526
## [0.5.2]
2627

src/exponential.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,16 @@ where
133133
/// Error type returned from [`Exp::new`].
134134
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
135135
pub enum Error {
136-
/// `lambda < 0` or `nan`.
136+
/// `lambda < 0` or is `-0.0` is `nan`.
137137
LambdaTooSmall,
138138
}
139139

140140
impl fmt::Display for Error {
141141
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142142
f.write_str(match self {
143-
Error::LambdaTooSmall => "lambda is negative or NaN in exponential distribution",
143+
Error::LambdaTooSmall => {
144+
"lambda is negative (including -0.0) or NaN in exponential distribution"
145+
}
144146
})
145147
}
146148
}
@@ -162,9 +164,13 @@ where
162164
/// the case `lambda = 0` is handled as follows: each sample corresponds
163165
/// to a sample from an `Exp1` multiplied by `1 / 0`. Primitive types
164166
/// yield infinity, since `1 / 0 = infinity`.
167+
///
168+
/// The case `lambda = N::neg_zero()` returns an error, because `-0.0` is typically
169+
/// produced through underflow of computations with a negative ideal result, and
170+
/// because `1 / -0.0 = -infinity`.
165171
#[inline]
166172
pub fn new(lambda: F) -> Result<Exp<F>, Error> {
167-
if !(lambda >= F::zero()) {
173+
if lambda.is_sign_negative() || lambda.is_nan() {
168174
return Err(Error::LambdaTooSmall);
169175
}
170176
Ok(Exp {

0 commit comments

Comments
 (0)