File tree Expand file tree Collapse file tree 3 files changed +66
-0
lines changed Expand file tree Collapse file tree 3 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -567,6 +567,32 @@ pub trait Ord: Eq + PartialOrd<Self> {
567567 where Self : Sized {
568568 if self <= other { self } else { other }
569569 }
570+
571+ /// Returns max if self is greater than max, and min if self is less than min.
572+ /// Otherwise this will return self. Panics if min > max.
573+ ///
574+ /// # Examples
575+ ///
576+ /// ```
577+ /// #![feature(clamp)]
578+ ///
579+ /// assert!((-3).clamp(-2, 1) == -2);
580+ /// assert!(0.clamp(-2, 1) == 0);
581+ /// assert!(2.clamp(-2, 1) == 1);
582+ /// ```
583+ #[ unstable( feature = "clamp" , issue = "44095" ) ]
584+ fn clamp ( self , min : Self , max : Self ) -> Self
585+ where Self : Sized {
586+ assert ! ( min <= max) ;
587+ if self < min {
588+ min
589+ }
590+ else if self > max {
591+ max
592+ } else {
593+ self
594+ }
595+ }
570596}
571597
572598#[ stable( feature = "rust1" , since = "1.0.0" ) ]
Original file line number Diff line number Diff line change @@ -956,6 +956,26 @@ impl f32 {
956956 pub fn atanh ( self ) -> f32 {
957957 0.5 * ( ( 2.0 * self ) / ( 1.0 - self ) ) . ln_1p ( )
958958 }
959+ /// Returns max if self is greater than max, and min if self is less than min.
960+ /// Otherwise this returns self. Panics if min > max, min equals NaN, or max equals NaN.
961+ ///
962+ /// # Examples
963+ ///
964+ /// ```
965+ /// assert!((-3.0f32).clamp(-2.0f32, 1.0f32) == -2.0f32);
966+ /// assert!((0.0f32).clamp(-2.0f32, 1.0f32) == 0.0f32);
967+ /// assert!((2.0f32).clamp(-2.0f32, 1.0f32) == 1.0f32);
968+ /// ```
969+ #[ unstable( feature = "clamp" , issue = "44095" ) ]
970+ #[ inline]
971+ pub fn clamp ( self , min : f32 , max : f32 ) -> f32 {
972+ assert ! ( min <= max) ;
973+ let mut x = self ;
974+ if x < min { x = min; }
975+ if x > max { x = max; }
976+ x
977+ }
978+
959979}
960980
961981#[ cfg( test) ]
Original file line number Diff line number Diff line change @@ -878,6 +878,26 @@ impl f64 {
878878 0.5 * ( ( 2.0 * self ) / ( 1.0 - self ) ) . ln_1p ( )
879879 }
880880
881+ /// Returns max if self is greater than max, and min if self is less than min.
882+ /// Otherwise this returns self. Panics if min > max, min equals NaN, or max equals NaN.
883+ ///
884+ /// # Examples
885+ ///
886+ /// ```
887+ /// assert!((-3.0f64).clamp(-2.0f64, 1.0f64) == -2.0f64);
888+ /// assert!((0.0f64).clamp(-2.0f64, 1.0f64) == 0.0f64);
889+ /// assert!((2.0f64).clamp(-2.0f64, 1.0f64) == 1.0f64);
890+ /// ```
891+ #[ unstable( feature = "clamp" , issue = "44095" ) ]
892+ #[ inline]
893+ pub fn clamp ( self , min : f64 , max : f64 ) -> f64 {
894+ assert ! ( min <= max) ;
895+ let mut x = self ;
896+ if x < min { x = min; }
897+ if x > max { x = max; }
898+ x
899+ }
900+
881901 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
882902 // because of their non-standard behavior (e.g., log(-n) returns -Inf instead
883903 // of expected NaN).
You can’t perform that action at this time.
0 commit comments