Skip to content

Commit bd2e126

Browse files
committed
Revert "Revert "Add clamp functions""
1 parent 097c04c commit bd2e126

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
@@ -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")]

src/libstd/f32.rs

+20
Original file line numberDiff line numberDiff 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)]

src/libstd/f64.rs

+20
Original file line numberDiff line numberDiff 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).

0 commit comments

Comments
 (0)