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> {
258258
259259impl < T : Float + PartialEq > Eq for NotNan < T > { }
260260
261+ /// Adds two NotNans.
262+ ///
263+ /// Panics if the computation results in NaN
261264impl < 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
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments