From bd2e12609f190e7bf206dac2de2d68dbb4b1b5e6 Mon Sep 17 00:00:00 2001 From: Dirk Leifeld Date: Sat, 9 Mar 2019 19:16:54 +0100 Subject: [PATCH 1/2] Revert "Revert "Add clamp functions"" --- src/libcore/cmp.rs | 26 ++++++++++++++++++++++++++ src/libstd/f32.rs | 20 ++++++++++++++++++++ src/libstd/f64.rs | 20 ++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 81fcdeee12d29..259135b9ca0ab 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -567,6 +567,32 @@ pub trait Ord: Eq + PartialOrd { 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")] diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs index cb1f93581775c..df4dc2c779125 100644 --- a/src/libstd/f32.rs +++ b/src/libstd/f32.rs @@ -956,6 +956,26 @@ impl f32 { pub fn atanh(self) -> 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 + } + } #[cfg(test)] diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs index 7fa7b80751938..00e7f27912817 100644 --- a/src/libstd/f64.rs +++ b/src/libstd/f64.rs @@ -878,6 +878,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). From 6041ec3b788a519ab740925c6b1c2f0ac19ad17d Mon Sep 17 00:00:00 2001 From: Dirk Leifeld Date: Sat, 9 Mar 2019 20:10:48 +0100 Subject: [PATCH 2/2] add feature clamp --- src/libstd/f32.rs | 1 + src/libstd/f64.rs | 1 + src/libstd/lib.rs | 1 + 3 files changed, 3 insertions(+) diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs index df4dc2c779125..d158a6a5bccc8 100644 --- a/src/libstd/f32.rs +++ b/src/libstd/f32.rs @@ -962,6 +962,7 @@ impl f32 { /// # Examples /// /// ``` + /// #![feature(clamp)] /// 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); diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs index 00e7f27912817..5be0cfa9b8f0a 100644 --- a/src/libstd/f64.rs +++ b/src/libstd/f64.rs @@ -884,6 +884,7 @@ impl f64 { /// # Examples /// /// ``` + /// #![feature(clamp)] /// 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); diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 6dd3a6cc0fdbd..1c4918de42882 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -240,6 +240,7 @@ #![feature(cfg_target_thread_local)] #![feature(char_error_internals)] #![feature(checked_duration_since)] +#![feature(clamp)] #![feature(compiler_builtins_lib)] #![feature(concat_idents)] #![feature(const_cstr_unchecked)]