File tree 4 files changed +69
-0
lines changed
4 files changed +69
-0
lines changed Original file line number Diff line number Diff line change @@ -567,6 +567,32 @@ pub trait Ord: Eq + PartialOrd<Self> {
567
567
where Self : Sized {
568
568
if self <= other { self } else { other }
569
569
}
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
+ }
570
596
}
571
597
572
598
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
Original file line number Diff line number Diff line change @@ -956,6 +956,27 @@ impl f32 {
956
956
pub fn atanh ( self ) -> f32 {
957
957
0.5 * ( ( 2.0 * self ) / ( 1.0 - self ) ) . ln_1p ( )
958
958
}
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
+ /// #![feature(clamp)]
966
+ /// assert!((-3.0f32).clamp(-2.0f32, 1.0f32) == -2.0f32);
967
+ /// assert!((0.0f32).clamp(-2.0f32, 1.0f32) == 0.0f32);
968
+ /// assert!((2.0f32).clamp(-2.0f32, 1.0f32) == 1.0f32);
969
+ /// ```
970
+ #[ unstable( feature = "clamp" , issue = "44095" ) ]
971
+ #[ inline]
972
+ pub fn clamp ( self , min : f32 , max : f32 ) -> f32 {
973
+ assert ! ( min <= max) ;
974
+ let mut x = self ;
975
+ if x < min { x = min; }
976
+ if x > max { x = max; }
977
+ x
978
+ }
979
+
959
980
}
960
981
961
982
#[ cfg( test) ]
Original file line number Diff line number Diff line change @@ -878,6 +878,27 @@ impl f64 {
878
878
0.5 * ( ( 2.0 * self ) / ( 1.0 - self ) ) . ln_1p ( )
879
879
}
880
880
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
+ /// #![feature(clamp)]
888
+ /// assert!((-3.0f64).clamp(-2.0f64, 1.0f64) == -2.0f64);
889
+ /// assert!((0.0f64).clamp(-2.0f64, 1.0f64) == 0.0f64);
890
+ /// assert!((2.0f64).clamp(-2.0f64, 1.0f64) == 1.0f64);
891
+ /// ```
892
+ #[ unstable( feature = "clamp" , issue = "44095" ) ]
893
+ #[ inline]
894
+ pub fn clamp ( self , min : f64 , max : f64 ) -> f64 {
895
+ assert ! ( min <= max) ;
896
+ let mut x = self ;
897
+ if x < min { x = min; }
898
+ if x > max { x = max; }
899
+ x
900
+ }
901
+
881
902
// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
882
903
// because of their non-standard behavior (e.g., log(-n) returns -Inf instead
883
904
// of expected NaN).
Original file line number Diff line number Diff line change 245
245
#![ feature( cfg_target_thread_local) ]
246
246
#![ feature( char_error_internals) ]
247
247
#![ feature( checked_duration_since) ]
248
+ #![ feature( clamp) ]
248
249
#![ feature( compiler_builtins_lib) ]
249
250
#![ feature( concat_idents) ]
250
251
#![ feature( const_cstr_unchecked) ]
You can’t perform that action at this time.
0 commit comments