File tree Expand file tree Collapse file tree 2 files changed +12
-1
lines changed Expand file tree Collapse file tree 2 files changed +12
-1
lines changed Original file line number Diff line number Diff line change @@ -258,11 +258,14 @@ impl<T: Float> Deref for NotNan<T> {
258
258
259
259
impl < T : Float + PartialEq > Eq for NotNan < T > { }
260
260
261
+ /// Adds two NotNans.
262
+ ///
263
+ /// Panics if the computation results in NaN
261
264
impl < T : Float > Add for NotNan < T > {
262
265
type Output = Self ;
263
266
264
267
fn add ( self , other : Self ) -> Self {
265
- NotNan ( self . 0 + other. 0 )
268
+ NotNan :: new ( self . 0 + other. 0 ) . expect ( "Addition resulted in NaN" )
266
269
}
267
270
}
268
271
Original file line number Diff line number Diff line change @@ -522,3 +522,11 @@ fn hash_is_good_for_fractional_numbers() {
522
522
let pct_unique = set. len ( ) as f64 / limit as f64 ;
523
523
assert ! ( 0.99f64 < pct_unique, "percent-unique={}" , pct_unique) ;
524
524
}
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
+ }
You can’t perform that action at this time.
0 commit comments