File tree Expand file tree Collapse file tree 4 files changed +70
-70
lines changed Expand file tree Collapse file tree 4 files changed +70
-70
lines changed Original file line number Diff line number Diff line change @@ -961,4 +961,39 @@ impl f32 {
961961
962962 left. cmp ( & right)
963963 }
964+
965+ /// Restrict a value to a certain interval unless it is NaN.
966+ ///
967+ /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
968+ /// less than `min`. Otherwise this returns `self`.
969+ ///
970+ /// Note that this function returns NaN if the initial value was NaN as
971+ /// well.
972+ ///
973+ /// # Panics
974+ ///
975+ /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
976+ ///
977+ /// # Examples
978+ ///
979+ /// ```
980+ /// assert!((-3.0f32).clamp(-2.0, 1.0) == -2.0);
981+ /// assert!((0.0f32).clamp(-2.0, 1.0) == 0.0);
982+ /// assert!((2.0f32).clamp(-2.0, 1.0) == 1.0);
983+ /// assert!((f32::NAN).clamp(-2.0, 1.0).is_nan());
984+ /// ```
985+ #[ must_use = "method returns a new number and does not mutate the original value" ]
986+ #[ stable( feature = "clamp" , since = "1.50.0" ) ]
987+ #[ inline]
988+ pub fn clamp ( self , min : f32 , max : f32 ) -> f32 {
989+ assert ! ( min <= max) ;
990+ let mut x = self ;
991+ if x < min {
992+ x = min;
993+ }
994+ if x > max {
995+ x = max;
996+ }
997+ x
998+ }
964999}
Original file line number Diff line number Diff line change @@ -975,4 +975,39 @@ impl f64 {
975975
976976 left. cmp ( & right)
977977 }
978+
979+ /// Restrict a value to a certain interval unless it is NaN.
980+ ///
981+ /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
982+ /// less than `min`. Otherwise this returns `self`.
983+ ///
984+ /// Note that this function returns NaN if the initial value was NaN as
985+ /// well.
986+ ///
987+ /// # Panics
988+ ///
989+ /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
990+ ///
991+ /// # Examples
992+ ///
993+ /// ```
994+ /// assert!((-3.0f64).clamp(-2.0, 1.0) == -2.0);
995+ /// assert!((0.0f64).clamp(-2.0, 1.0) == 0.0);
996+ /// assert!((2.0f64).clamp(-2.0, 1.0) == 1.0);
997+ /// assert!((f64::NAN).clamp(-2.0, 1.0).is_nan());
998+ /// ```
999+ #[ must_use = "method returns a new number and does not mutate the original value" ]
1000+ #[ stable( feature = "clamp" , since = "1.50.0" ) ]
1001+ #[ inline]
1002+ pub fn clamp ( self , min : f64 , max : f64 ) -> f64 {
1003+ assert ! ( min <= max) ;
1004+ let mut x = self ;
1005+ if x < min {
1006+ x = min;
1007+ }
1008+ if x > max {
1009+ x = max;
1010+ }
1011+ x
1012+ }
9781013}
Original file line number Diff line number Diff line change @@ -879,39 +879,4 @@ impl f32 {
879879 pub fn atanh ( self ) -> f32 {
880880 0.5 * ( ( 2.0 * self ) / ( 1.0 - self ) ) . ln_1p ( )
881881 }
882-
883- /// Restrict a value to a certain interval unless it is NaN.
884- ///
885- /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
886- /// less than `min`. Otherwise this returns `self`.
887- ///
888- /// Note that this function returns NaN if the initial value was NaN as
889- /// well.
890- ///
891- /// # Panics
892- ///
893- /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
894- ///
895- /// # Examples
896- ///
897- /// ```
898- /// assert!((-3.0f32).clamp(-2.0, 1.0) == -2.0);
899- /// assert!((0.0f32).clamp(-2.0, 1.0) == 0.0);
900- /// assert!((2.0f32).clamp(-2.0, 1.0) == 1.0);
901- /// assert!((f32::NAN).clamp(-2.0, 1.0).is_nan());
902- /// ```
903- #[ must_use = "method returns a new number and does not mutate the original value" ]
904- #[ stable( feature = "clamp" , since = "1.50.0" ) ]
905- #[ inline]
906- pub fn clamp ( self , min : f32 , max : f32 ) -> f32 {
907- assert ! ( min <= max) ;
908- let mut x = self ;
909- if x < min {
910- x = min;
911- }
912- if x > max {
913- x = max;
914- }
915- x
916- }
917882}
Original file line number Diff line number Diff line change @@ -882,41 +882,6 @@ impl f64 {
882882 0.5 * ( ( 2.0 * self ) / ( 1.0 - self ) ) . ln_1p ( )
883883 }
884884
885- /// Restrict a value to a certain interval unless it is NaN.
886- ///
887- /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
888- /// less than `min`. Otherwise this returns `self`.
889- ///
890- /// Note that this function returns NaN if the initial value was NaN as
891- /// well.
892- ///
893- /// # Panics
894- ///
895- /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
896- ///
897- /// # Examples
898- ///
899- /// ```
900- /// assert!((-3.0f64).clamp(-2.0, 1.0) == -2.0);
901- /// assert!((0.0f64).clamp(-2.0, 1.0) == 0.0);
902- /// assert!((2.0f64).clamp(-2.0, 1.0) == 1.0);
903- /// assert!((f64::NAN).clamp(-2.0, 1.0).is_nan());
904- /// ```
905- #[ must_use = "method returns a new number and does not mutate the original value" ]
906- #[ stable( feature = "clamp" , since = "1.50.0" ) ]
907- #[ inline]
908- pub fn clamp ( self , min : f64 , max : f64 ) -> f64 {
909- assert ! ( min <= max) ;
910- let mut x = self ;
911- if x < min {
912- x = min;
913- }
914- if x > max {
915- x = max;
916- }
917- x
918- }
919-
920885 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
921886 // because of their non-standard behavior (e.g., log(-n) returns -Inf instead
922887 // of expected NaN).
You can’t perform that action at this time.
0 commit comments