Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd clamp functions #44097
Conversation
rust-highfive
assigned
BurntSushi
Aug 26, 2017
This comment has been minimized.
This comment has been minimized.
|
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. |
This was referenced Aug 26, 2017
Xaeroxe
force-pushed the
Xaeroxe:clamp
branch
from
eabd26e
to
eeb9a28
Aug 26, 2017
scottmcm
reviewed
Aug 26, 2017
| /// 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); | ||
| /// ``` |
This comment has been minimized.
This comment has been minimized.
| @@ -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)), | |||
This comment has been minimized.
This comment has been minimized.
scottmcm
Aug 26, 2017
Member
No need for the feature gate in code for lib changes; the attribute is sufficient.
Xaeroxe
force-pushed the
Xaeroxe:clamp
branch
from
eeb9a28
to
c589f86
Aug 26, 2017
lukaramu
reviewed
Aug 26, 2017
| @@ -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. | |||
This comment has been minimized.
This comment has been minimized.
lukaramu
Aug 26, 2017
•
Contributor
Should this be "is NaN" and not "equals NaN"? Nothing is equal (as in ==) to NaN, not even NaN.
| @@ -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. | |||
This comment has been minimized.
This comment has been minimized.
Xaeroxe
added some commits
Aug 26, 2017
Xaeroxe
changed the title
Add clamp functions and feature gate
Add clamp functions
Aug 27, 2017
carols10cents
added
the
S-waiting-on-review
label
Aug 28, 2017
This comment has been minimized.
This comment has been minimized.
|
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. |
BurntSushi
reviewed
Aug 28, 2017
| @@ -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. | |||
This comment has been minimized.
This comment has been minimized.
BurntSushi
Aug 28, 2017
Member
I think this should follow our doc formatting conventions, no? I think we usually have a panics header for documenting panic'ing conditions.
BurntSushi
reviewed
Aug 28, 2017
| } else { | ||
| self | ||
| } | ||
| } |
This comment has been minimized.
This comment has been minimized.
BurntSushi
Aug 28, 2017
Member
Could you format this in a way that is consistent with the surrounding code? (Do we do rustmt on this stuff yet?)
BurntSushi
reviewed
Aug 28, 2017
| @@ -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. | |||
This comment has been minimized.
This comment has been minimized.
BurntSushi
reviewed
Aug 28, 2017
| @@ -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. | |||
This comment has been minimized.
This comment has been minimized.
BurntSushi
requested changes
Aug 28, 2017
|
This mostly looks good, but have some nits! I guess this should also technically wait to merge until the RFC has been merged? |
BurntSushi
added
S-waiting-on-author
and removed
S-waiting-on-review
labels
Aug 28, 2017
This comment has been minimized.
This comment has been minimized.
|
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. |
BurntSushi
requested changes
Sep 1, 2017
|
Oh, and could you also add some unit tests for the panic conditions? Thanks! |
Xaeroxe
force-pushed the
Xaeroxe:clamp
branch
from
bd556e7
to
c3ff2d4
Sep 5, 2017
Xaeroxe
force-pushed the
Xaeroxe:clamp
branch
from
c3ff2d4
to
b762283
Sep 5, 2017
This comment has been minimized.
This comment has been minimized.
|
@bors r=burntsushi |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
@aturon @BurntSushi That didn't seem to work :/ |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@bors rollup |
Mark-Simulacrum
added a commit
to Mark-Simulacrum/rust
that referenced
this pull request
Sep 6, 2017
bors
added a commit
that referenced
this pull request
Sep 6, 2017
StefanoD
reviewed
Sep 6, 2017
| /// 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 { |
This comment has been minimized.
This comment has been minimized.
StefanoD
Sep 6, 2017
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 }
This comment has been minimized.
This comment has been minimized.
Xaeroxe
Sep 6, 2017
Author
Contributor
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.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Sep 6, 2017
Mark-Simulacrum
added a commit
to Mark-Simulacrum/rust
that referenced
this pull request
Sep 7, 2017
bors
added a commit
that referenced
this pull request
Sep 7, 2017
bors
merged commit b762283
into
rust-lang:master
Sep 7, 2017
1 check passed
Xaeroxe
deleted the
Xaeroxe:clamp
branch
Sep 7, 2017
This comment has been minimized.
This comment has been minimized.
|
This broke Servo and Pathfinder. See servo/app_units#37 |
Xaeroxe commentedAug 26, 2017
Implementation of clamp feature:
Tracking issue: #44095
RFC: rust-lang/rfcs#1961