Skip to content

Commit

Permalink
Add clamp functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaeroxe committed Aug 26, 2017
1 parent 398aaff commit c589f86
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,32 @@ pub trait Ord: Eq + PartialOrd<Self> {
where Self: Sized {
if self <= other { self } else { other }
}

/// Returns max if self is greater than max, and min if self is less than min.
/// Otherwise this will return self. Panics if min > max.
///
/// # Examples
///
/// ```
/// #![feature(clamp)]
///
/// assert!((-3).clamp(-2, 1) == -2);
/// assert!(0.clamp(-2, 1) == 0);
/// assert!(2.clamp(-2, 1) == 1);
/// ```
#[unstable(feature = "clamp", issue = "44095")]
fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized {
assert!(min <= max);
if self < min {
min
}
else if self > max {
max
} else {
self
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
20 changes: 20 additions & 0 deletions src/libstd/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,26 @@ impl f32 {
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
}

/// Returns max if self is greater than max, and min if self is less than min.
/// Otherwise this returns self. Panics if min > max, min equals NaN, or max equals NaN.
///
/// # Examples
///
/// ```
/// assert!((-3.0f32).clamp(-2.0f32, 1.0f32) == -2.0f32);
/// assert!((0.0f32).clamp(-2.0f32, 1.0f32) == 0.0f32);
/// assert!((2.0f32).clamp(-2.0f32, 1.0f32) == 1.0f32);
/// ```
#[unstable(feature = "clamp", issue = "44095")]
#[inline]
pub fn clamp(self, min: f32, max: f32) -> f32 {
assert!(min <= max);
let mut x = self;
if x < min { x = min; }
if x > max { x = max; }
x
}

/// Raw transmutation to `u32`.
///
/// Converts the `f32` into its raw memory representation,
Expand Down
20 changes: 20 additions & 0 deletions src/libstd/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,26 @@ impl f64 {
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
}

/// Returns max if self is greater than max, and min if self is less than min.
/// Otherwise this returns self. Panics if min > max, min equals NaN, or max equals NaN.
///
/// # Examples
///
/// ```
/// assert!((-3.0f64).clamp(-2.0f64, 1.0f64) == -2.0f64);
/// assert!((0.0f64).clamp(-2.0f64, 1.0f64) == 0.0f64);
/// assert!((2.0f64).clamp(-2.0f64, 1.0f64) == 1.0f64);
/// ```
#[unstable(feature = "clamp", issue = "44095")]
#[inline]
pub fn clamp(self, min: f64, max: f64) -> f64 {
assert!(min <= max);
let mut x = self;
if x < min { x = min; }
if x > max { x = max; }
x
}

// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
// because of their non-standard behavior (e.g. log(-n) returns -Inf instead
// of expected NaN).
Expand Down

0 comments on commit c589f86

Please sign in to comment.