@@ -246,16 +246,26 @@ impl Ord for f64 {
246246}
247247
248248impl Orderable for f64 {
249+ /// Returns `NaN` if either of the numbers are `NaN`.
249250 #[ inline( always) ]
250- fn min ( & self , other : & f64 ) -> f64 { fmin ( * self , * other) }
251+ fn min ( & self , other : & f64 ) -> f64 {
252+ if self . is_NaN ( ) || other. is_NaN ( ) { Float :: NaN ( ) } else { fmin ( * self , * other) }
253+ }
251254
255+ /// Returns `NaN` if either of the numbers are `NaN`.
252256 #[ inline( always) ]
253- fn max ( & self , other : & f64 ) -> f64 { fmax ( * self , * other) }
257+ fn max ( & self , other : & f64 ) -> f64 {
258+ if self . is_NaN ( ) || other. is_NaN ( ) { Float :: NaN ( ) } else { fmax ( * self , * other) }
259+ }
254260
261+ /// Returns the number constrained within the range `mn <= self <= mx`.
262+ /// If any of the numbers are `NaN` then `NaN` is returned.
255263 #[ inline( always) ]
256264 fn clamp ( & self , mn : & f64 , mx : & f64 ) -> f64 {
257- if * self > * mx { * mx } else
258- if * self < * mn { * mn } else { * self }
265+ if self . is_NaN ( ) { * self }
266+ else if !( * self <= * mx) { * mx }
267+ else if !( * self >= * mn) { * mn }
268+ else { * self }
259269 }
260270}
261271
@@ -869,14 +879,29 @@ mod tests {
869879 }
870880
871881 #[ test]
872- fn test_orderable ( ) {
882+ fn test_min ( ) {
873883 assert_eq ! ( 1f64 . min( & 2f64 ) , 1f64 ) ;
874884 assert_eq ! ( 2f64 . min( & 1f64 ) , 1f64 ) ;
885+ assert ! ( 1f64 . min( & Float :: NaN :: <f64 >( ) ) . is_NaN( ) ) ;
886+ assert ! ( Float :: NaN :: <f64 >( ) . min( & 1f64 ) . is_NaN( ) ) ;
887+ }
888+
889+ #[ test]
890+ fn test_max ( ) {
875891 assert_eq ! ( 1f64 . max( & 2f64 ) , 2f64 ) ;
876892 assert_eq ! ( 2f64 . max( & 1f64 ) , 2f64 ) ;
893+ assert ! ( 1f64 . max( & Float :: NaN :: <f64 >( ) ) . is_NaN( ) ) ;
894+ assert ! ( Float :: NaN :: <f64 >( ) . max( & 1f64 ) . is_NaN( ) ) ;
895+ }
896+
897+ #[ test]
898+ fn test_clamp ( ) {
877899 assert_eq ! ( 1f64 . clamp( & 2f64 , & 4f64 ) , 2f64 ) ;
878900 assert_eq ! ( 8f64 . clamp( & 2f64 , & 4f64 ) , 4f64 ) ;
879901 assert_eq ! ( 3f64 . clamp( & 2f64 , & 4f64 ) , 3f64 ) ;
902+ assert ! ( 3f64 . clamp( & Float :: NaN :: <f64 >( ) , & 4f64 ) . is_NaN( ) ) ;
903+ assert ! ( 3f64 . clamp( & 2f64 , & Float :: NaN :: <f64 >( ) ) . is_NaN( ) ) ;
904+ assert ! ( Float :: NaN :: <f64 >( ) . clamp( & 2f64 , & 4f64 ) . is_NaN( ) ) ;
880905 }
881906
882907 #[ test]
0 commit comments