File tree 3 files changed +66
-0
lines changed
3 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -481,6 +481,32 @@ pub trait Ord: Eq + PartialOrd<Self> {
481
481
where Self : Sized {
482
482
if self <= other { self } else { other }
483
483
}
484
+
485
+ /// Returns max if self is greater than max, and min if self is less than min.
486
+ /// Otherwise this will return self. Panics if min > max.
487
+ ///
488
+ /// # Examples
489
+ ///
490
+ /// ```
491
+ /// #![feature(clamp)]
492
+ ///
493
+ /// assert!((-3).clamp(-2, 1) == -2);
494
+ /// assert!(0.clamp(-2, 1) == 0);
495
+ /// assert!(2.clamp(-2, 1) == 1);
496
+ /// ```
497
+ #[ unstable( feature = "clamp" , issue = "44095" ) ]
498
+ fn clamp ( self , min : Self , max : Self ) -> Self
499
+ where Self : Sized {
500
+ assert ! ( min <= max) ;
501
+ if self < min {
502
+ min
503
+ }
504
+ else if self > max {
505
+ max
506
+ } else {
507
+ self
508
+ }
509
+ }
484
510
}
485
511
486
512
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
Original file line number Diff line number Diff line change @@ -1080,6 +1080,26 @@ impl f32 {
1080
1080
0.5 * ( ( 2.0 * self ) / ( 1.0 - self ) ) . ln_1p ( )
1081
1081
}
1082
1082
1083
+ /// Returns max if self is greater than max, and min if self is less than min.
1084
+ /// Otherwise this returns self. Panics if min > max, min equals NaN, or max equals NaN.
1085
+ ///
1086
+ /// # Examples
1087
+ ///
1088
+ /// ```
1089
+ /// assert!((-3.0f32).clamp(-2.0f32, 1.0f32) == -2.0f32);
1090
+ /// assert!((0.0f32).clamp(-2.0f32, 1.0f32) == 0.0f32);
1091
+ /// assert!((2.0f32).clamp(-2.0f32, 1.0f32) == 1.0f32);
1092
+ /// ```
1093
+ #[ unstable( feature = "clamp" , issue = "44095" ) ]
1094
+ #[ inline]
1095
+ pub fn clamp ( self , min : f32 , max : f32 ) -> f32 {
1096
+ assert ! ( min <= max) ;
1097
+ let mut x = self ;
1098
+ if x < min { x = min; }
1099
+ if x > max { x = max; }
1100
+ x
1101
+ }
1102
+
1083
1103
/// Raw transmutation to `u32`.
1084
1104
///
1085
1105
/// Converts the `f32` into its raw memory representation,
Original file line number Diff line number Diff line change @@ -970,6 +970,26 @@ impl f64 {
970
970
0.5 * ( ( 2.0 * self ) / ( 1.0 - self ) ) . ln_1p ( )
971
971
}
972
972
973
+ /// Returns max if self is greater than max, and min if self is less than min.
974
+ /// Otherwise this returns self. Panics if min > max, min equals NaN, or max equals NaN.
975
+ ///
976
+ /// # Examples
977
+ ///
978
+ /// ```
979
+ /// assert!((-3.0f64).clamp(-2.0f64, 1.0f64) == -2.0f64);
980
+ /// assert!((0.0f64).clamp(-2.0f64, 1.0f64) == 0.0f64);
981
+ /// assert!((2.0f64).clamp(-2.0f64, 1.0f64) == 1.0f64);
982
+ /// ```
983
+ #[ unstable( feature = "clamp" , issue = "44095" ) ]
984
+ #[ inline]
985
+ pub fn clamp ( self , min : f64 , max : f64 ) -> f64 {
986
+ assert ! ( min <= max) ;
987
+ let mut x = self ;
988
+ if x < min { x = min; }
989
+ if x > max { x = max; }
990
+ x
991
+ }
992
+
973
993
// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
974
994
// because of their non-standard behavior (e.g. log(-n) returns -Inf instead
975
995
// of expected NaN).
You can’t perform that action at this time.
0 commit comments