From c589f867f89d4e6e48c6602aed8e878208d4822f Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Sat, 26 Aug 2017 00:06:13 -0600 Subject: [PATCH 1/6] 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 ec6525485f7a1..174ceb6b8a7e6 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -481,6 +481,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 0135cd0a588cf..18bc27faa8227 100644 --- a/src/libstd/f32.rs +++ b/src/libstd/f32.rs @@ -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, diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs index d73d7cd2c7bd1..b04ba9eabdbb3 100644 --- a/src/libstd/f64.rs +++ b/src/libstd/f64.rs @@ -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). From f74c5d2e18e50c24de2cc1192bf2088cdaa61916 Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Sat, 26 Aug 2017 10:36:14 -0600 Subject: [PATCH 2/6] Add NAN examples --- src/libstd/f32.rs | 1 + src/libstd/f64.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs index 18bc27faa8227..cef53671e806c 100644 --- a/src/libstd/f32.rs +++ b/src/libstd/f32.rs @@ -1089,6 +1089,7 @@ impl f32 { /// 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) == NAN); /// ``` #[unstable(feature = "clamp", issue = "44095")] #[inline] diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs index b04ba9eabdbb3..ab8f2cd2fdfbd 100644 --- a/src/libstd/f64.rs +++ b/src/libstd/f64.rs @@ -979,6 +979,7 @@ impl f64 { /// 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) == NAN); /// ``` #[unstable(feature = "clamp", issue = "44095")] #[inline] From 61f20f8df02e53ee60dc1719ce0e502eecebf8b4 Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Sat, 26 Aug 2017 17:53:01 -0600 Subject: [PATCH 3/6] Fix f32 examples. --- src/libstd/f32.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs index cef53671e806c..6b6bd39e7e84c 100644 --- a/src/libstd/f32.rs +++ b/src/libstd/f32.rs @@ -1081,15 +1081,17 @@ impl f32 { } /// 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. + /// Otherwise this returns self. Panics if min > max, min is NaN, or max is NaN. /// /// # 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) == NAN); + /// assert!((NAN).clamp(-2.0f32, 1.0f32).is_nan()); /// ``` #[unstable(feature = "clamp", issue = "44095")] #[inline] From 576426a05a1a6cb33eece7082d7341b7c6bb5277 Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Sat, 26 Aug 2017 17:54:51 -0600 Subject: [PATCH 4/6] Fix f64 examples --- src/libstd/f64.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs index ab8f2cd2fdfbd..0a7176f9cc75e 100644 --- a/src/libstd/f64.rs +++ b/src/libstd/f64.rs @@ -971,15 +971,17 @@ impl f64 { } /// 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. + /// Otherwise this returns self. Panics if min > max, min is NaN, or max is NaN. /// /// # 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) == NAN); + /// assert!((NAN).clamp(-2.0f64, 1.0f64).is_nan()); /// ``` #[unstable(feature = "clamp", issue = "44095")] #[inline] From 2e34ff767113c6a15c5862b0646ca9ad7ffd81b1 Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Fri, 1 Sep 2017 00:07:26 -0600 Subject: [PATCH 5/6] Fix documentation and formatting. --- src/libcore/cmp.rs | 16 +++++++--------- src/libstd/f32.rs | 5 ++++- src/libstd/f64.rs | 5 ++++- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 174ceb6b8a7e6..dc1f2981a50ad 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -483,7 +483,7 @@ pub trait Ord: Eq + PartialOrd { } /// 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. + /// Otherwise this will return self. /// /// # Examples /// @@ -494,18 +494,16 @@ pub trait Ord: Eq + PartialOrd { /// 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 - } + if self < min { min } + else if self > max { max } + else { self } } } diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs index 6b6bd39e7e84c..aaeac06535ee6 100644 --- a/src/libstd/f32.rs +++ b/src/libstd/f32.rs @@ -1081,7 +1081,7 @@ impl f32 { } /// 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 is NaN, or max is NaN. + /// Otherwise this returns self. /// /// # Examples /// @@ -1093,6 +1093,9 @@ impl f32 { /// 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 { diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs index 0a7176f9cc75e..4ab319c3cf100 100644 --- a/src/libstd/f64.rs +++ b/src/libstd/f64.rs @@ -971,7 +971,7 @@ impl f64 { } /// 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 is NaN, or max is NaN. + /// Otherwise this returns self. /// /// # Examples /// @@ -983,6 +983,9 @@ impl f64 { /// 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 { From b762283e57ff71f6763effb9cfc7fc0c7967b6b0 Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Mon, 4 Sep 2017 20:49:46 -0600 Subject: [PATCH 6/6] Add panic unit tests --- src/libstd/f32.rs | 18 ++++++++++++++++++ src/libstd/f64.rs | 18 ++++++++++++++++++ src/libstd/lib.rs | 1 + 3 files changed, 37 insertions(+) diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs index aaeac06535ee6..69ca77f54b44a 100644 --- a/src/libstd/f32.rs +++ b/src/libstd/f32.rs @@ -1777,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); + } } diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs index 4ab319c3cf100..6ec633bfaaac1 100644 --- a/src/libstd/f64.rs +++ b/src/libstd/f64.rs @@ -1668,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); + } } diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index cf1eb5cd52ea4..191f7fe648ed2 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -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)]