Skip to content

Commit d9b67a5

Browse files
authored
Merge pull request #48 from mbrubeck/add
Check result of NotNan addition for NaN
2 parents 3ee8c96 + 8d56a3c commit d9b67a5

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,14 @@ impl<T: Float> Deref for NotNan<T> {
258258

259259
impl<T: Float + PartialEq> Eq for NotNan<T> {}
260260

261+
/// Adds two NotNans.
262+
///
263+
/// Panics if the computation results in NaN
261264
impl<T: Float> Add for NotNan<T> {
262265
type Output = Self;
263266

264267
fn add(self, other: Self) -> Self {
265-
NotNan(self.0 + other.0)
268+
NotNan::new(self.0 + other.0).expect("Addition resulted in NaN")
266269
}
267270
}
268271

tests/test.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,11 @@ fn hash_is_good_for_fractional_numbers() {
522522
let pct_unique = set.len() as f64 / limit as f64;
523523
assert!(0.99f64 < pct_unique, "percent-unique={}", pct_unique);
524524
}
525+
526+
#[test]
527+
#[should_panic]
528+
fn test_add_fails_on_nan() {
529+
let a = NotNan::new(std::f32::INFINITY).unwrap();
530+
let b = NotNan::new(std::f32::NEG_INFINITY).unwrap();
531+
let _c = a + b;
532+
}

0 commit comments

Comments
 (0)