-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add clamp functions #44097
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you format this in a way that is consistent with the surrounding code? (Do we do rustmt on this stuff yet?) |
||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1080,6 +1080,27 @@ 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be "is |
||
/// | ||
/// # 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); | ||
/// assert!((NAN).clamp(-2.0f32, 1.0f32) == NAN); | ||
/// ``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps an example of NAN input giving NAN output? |
||
#[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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -970,6 +970,27 @@ 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
/// | ||
/// # 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); | ||
/// assert!((NAN).clamp(-2.0f64, 1.0f64) == NAN); | ||
/// ``` | ||
#[unstable(feature = "clamp", issue = "44095")] | ||
#[inline] | ||
pub fn clamp(self, min: f64, max: f64) -> f64 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 if x < min { min }
if x > max { max }
else { self } There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, interesting and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, and that. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should follow our doc formatting conventions, no? I think we usually have a panics header for documenting panic'ing conditions.