Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clamp functions #44097

Merged
merged 6 commits into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,30 @@ 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.
///
/// # Examples
///
/// ```
/// #![feature(clamp)]
///
/// assert!((-3).clamp(-2, 1) == -2);
/// assert!(0.clamp(-2, 1) == 0);
/// assert!(2.clamp(-2, 1) == 1);
/// ```
///
/// # Panics
/// Panics if min > max.
#[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
44 changes: 44 additions & 0 deletions src/libstd/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,32 @@ 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.
///
/// # Examples
///
/// ```
/// #![feature(clamp)]
/// use std::f32::NAN;
/// 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);
/// assert!((NAN).clamp(-2.0f32, 1.0f32).is_nan());
/// ```
///
/// # Panics
/// Panics if min > max, min is NaN, or max is NaN.
#[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 Expand Up @@ -1751,4 +1777,22 @@ mod tests {
assert_ne!(nan_masked & QNAN_MASK, 0);
assert!(nan_masked_fl.is_nan());
}

#[test]
#[should_panic]
fn test_clamp_min_greater_than_max() {
1.0f32.clamp(3.0, 1.0);
}

#[test]
#[should_panic]
fn test_clamp_min_is_nan() {
1.0f32.clamp(NAN, 1.0);
}

#[test]
#[should_panic]
fn test_clamp_max_is_nan() {
1.0f32.clamp(3.0, NAN);
}
}
44 changes: 44 additions & 0 deletions src/libstd/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,32 @@ 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.
///
/// # Examples
///
/// ```
/// #![feature(clamp)]
/// use std::f64::NAN;
/// 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);
/// assert!((NAN).clamp(-2.0f64, 1.0f64).is_nan());
/// ```
///
/// # Panics
/// Panics if min > max, min is NaN, or max is NaN.
#[unstable(feature = "clamp", issue = "44095")]
#[inline]
pub fn clamp(self, min: f64, max: f64) -> f64 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a Rust beginner, but I think, this should also work. I don't like the mut.

if x < min { min }
if x > max { max }
else { self }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the original implementation. This specific code pattern was chosen because it yields a highly efficient assembly implementation. This was actually the source of quite a bit of conversation before we settled on this.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, interesting and let x?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, and that.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Holy caroly (this is actually no real word, but it rhymes)!

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 Expand Up @@ -1642,4 +1668,22 @@ mod tests {
assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);
}

#[test]
#[should_panic]
fn test_clamp_min_greater_than_max() {
1.0f64.clamp(3.0, 1.0);
}

#[test]
#[should_panic]
fn test_clamp_min_is_nan() {
1.0f64.clamp(NAN, 1.0);
}

#[test]
#[should_panic]
fn test_clamp_max_is_nan() {
1.0f64.clamp(3.0, NAN);
}
}
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
#![feature(cfg_target_vendor)]
#![feature(char_error_internals)]
#![feature(char_internals)]
#![feature(clamp)]
#![feature(collections_range)]
#![feature(compiler_builtins_lib)]
#![feature(const_fn)]
Expand Down