@@ -246,16 +246,26 @@ impl Ord for f64 {
246
246
}
247
247
248
248
impl Orderable for f64 {
249
+ /// Returns `NaN` if either of the numbers are `NaN`.
249
250
#[ 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
+ }
251
254
255
+ /// Returns `NaN` if either of the numbers are `NaN`.
252
256
#[ 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
+ }
254
260
261
+ /// Returns the number constrained within the range `mn <= self <= mx`.
262
+ /// If any of the numbers are `NaN` then `NaN` is returned.
255
263
#[ inline( always) ]
256
264
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 }
259
269
}
260
270
}
261
271
@@ -869,14 +879,29 @@ mod tests {
869
879
}
870
880
871
881
#[ test]
872
- fn test_orderable ( ) {
882
+ fn test_min ( ) {
873
883
assert_eq ! ( 1f64 . min( & 2f64 ) , 1f64 ) ;
874
884
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 ( ) {
875
891
assert_eq ! ( 1f64 . max( & 2f64 ) , 2f64 ) ;
876
892
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 ( ) {
877
899
assert_eq ! ( 1f64 . clamp( & 2f64 , & 4f64 ) , 2f64 ) ;
878
900
assert_eq ! ( 8f64 . clamp( & 2f64 , & 4f64 ) , 4f64 ) ;
879
901
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( ) ) ;
880
905
}
881
906
882
907
#[ test]
0 commit comments