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 @@ -877,39 +877,4 @@ impl f32 {
877877 pub fn atanh ( self ) -> f32 {
878878 0.5 * ( ( 2.0 * self ) / ( 1.0 - self ) ) . ln_1p ( )
879879 }
880-
881- /// Restrict a value to a certain interval unless it is NaN.
882- ///
883- /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
884- /// less than `min`. Otherwise this returns `self`.
885- ///
886- /// Note that this function returns NaN if the initial value was NaN as
887- /// well.
888- ///
889- /// # Panics
890- ///
891- /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
892- ///
893- /// # Examples
894- ///
895- /// ```
896- /// assert!((-3.0f32).clamp(-2.0, 1.0) == -2.0);
897- /// assert!((0.0f32).clamp(-2.0, 1.0) == 0.0);
898- /// assert!((2.0f32).clamp(-2.0, 1.0) == 1.0);
899- /// assert!((f32::NAN).clamp(-2.0, 1.0).is_nan());
900- /// ```
901- #[ must_use = "method returns a new number and does not mutate the original value" ]
902- #[ stable( feature = "clamp" , since = "1.50.0" ) ]
903- #[ inline]
904- pub fn clamp ( self , min : f32 , max : f32 ) -> f32 {
905- assert ! ( min <= max) ;
906- let mut x = self ;
907- if x < min {
908- x = min;
909- }
910- if x > max {
911- x = max;
912- }
913- x
914- }
915880}
Original file line number Diff line number Diff line change @@ -880,41 +880,6 @@ impl f64 {
880880 0.5 * ( ( 2.0 * self ) / ( 1.0 - self ) ) . ln_1p ( )
881881 }
882882
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.0f64).clamp(-2.0, 1.0) == -2.0);
899- /// assert!((0.0f64).clamp(-2.0, 1.0) == 0.0);
900- /// assert!((2.0f64).clamp(-2.0, 1.0) == 1.0);
901- /// assert!((f64::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 : f64 , max : f64 ) -> f64 {
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- }
917-
918883 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
919884 // because of their non-standard behavior (e.g., log(-n) returns -Inf instead
920885 // of expected NaN).
You can’t perform that action at this time.
0 commit comments