-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add clamp functions #44097
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @BurntSushi (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
src/libstd/f32.rs
Outdated
/// 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); | ||
/// ``` |
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.
Perhaps an example of NAN input giving NAN output?
src/libsyntax/feature_gate.rs
Outdated
@@ -372,6 +372,9 @@ declare_features! ( | |||
|
|||
// #[doc(cfg(...))] | |||
(active, doc_cfg, "1.21.0", Some(43781)), | |||
|
|||
// Provide clamp functions | |||
(active, clamp, "1.21.0", Some(44095)), |
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.
No need for the feature gate in code for lib changes; the attribute is sufficient.
src/libstd/f32.rs
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be "is NaN
" and not "equals NaN
"? Nothing is equal (as in ==
) to NaN
, not even NaN
.
src/libstd/f64.rs
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
Thanks for the PR! We’ll periodically check in on it to make sure that @BurntSushi or someone else from the team reviews it soon. |
src/libcore/cmp.rs
Outdated
@@ -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. |
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.
src/libcore/cmp.rs
Outdated
} else { | ||
self | ||
} | ||
} |
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.
Could you format this in a way that is consistent with the surrounding code? (Do we do rustmt on this stuff yet?)
src/libstd/f32.rs
Outdated
@@ -1080,6 +1080,29 @@ 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 is NaN, or max is 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.
Needs panic header for here too.
src/libstd/f64.rs
Outdated
@@ -970,6 +970,29 @@ 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 is NaN, or max is 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.
Needs panic header.
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.
This mostly looks good, but have some nits! I guess this should also technically wait to merge until the RFC has been merged?
r? @BurntSushi Yeah this will have to wait for the RFC to be merged. I mostly opened this early since the implementation was so straightforward. |
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.
Oh, and could you also add some unit tests for the panic conditions? Thanks!
@bors r=burntsushi |
📌 Commit b762283 has been approved by |
@aturon @BurntSushi That didn't seem to work :/ |
@bors rollup |
Add clamp functions Implementation of clamp feature: Tracking issue: rust-lang#44095 RFC: rust-lang/rfcs#1961
/// 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 { |
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'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 }
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, interesting and let x
?
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.
Yeah, and that.
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.
Holy caroly (this is actually no real word, but it rhymes)!
Add clamp functions Implementation of clamp feature: Tracking issue: rust-lang#44095 RFC: rust-lang/rfcs#1961
This broke Servo and Pathfinder. See servo/app_units#37 |
Implementation of clamp feature:
Tracking issue: #44095
RFC: rust-lang/rfcs#1961