Skip to content

Commit c589f86

Browse files
committed
Add clamp functions
1 parent 398aaff commit c589f86

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/libcore/cmp.rs

+26
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,32 @@ pub trait Ord: Eq + PartialOrd<Self> {
481481
where Self: Sized {
482482
if self <= other { self } else { other }
483483
}
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+
}
484510
}
485511

486512
#[stable(feature = "rust1", since = "1.0.0")]

src/libstd/f32.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,26 @@ impl f32 {
10801080
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
10811081
}
10821082

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+
10831103
/// Raw transmutation to `u32`.
10841104
///
10851105
/// Converts the `f32` into its raw memory representation,

src/libstd/f64.rs

+20
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,26 @@ impl f64 {
970970
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
971971
}
972972

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+
973993
// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
974994
// because of their non-standard behavior (e.g. log(-n) returns -Inf instead
975995
// of expected NaN).

0 commit comments

Comments
 (0)