Skip to content
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

Merged
merged 6 commits into from Sep 7, 2017
Merged

Add clamp functions #44097

merged 6 commits into from Sep 7, 2017

Conversation

Xaeroxe
Copy link
Contributor

@Xaeroxe Xaeroxe commented Aug 26, 2017

Implementation of clamp feature:

Tracking issue: #44095
RFC: rust-lang/rfcs#1961

@rust-highfive
Copy link
Collaborator

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.

/// 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);
/// ```
Copy link
Member

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?

@@ -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)),
Copy link
Member

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.

@@ -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.
Copy link
Contributor

@chordowl chordowl Aug 26, 2017

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.

@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

@Xaeroxe Xaeroxe changed the title Add clamp functions and feature gate Add clamp functions Aug 27, 2017
@carols10cents carols10cents added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 28, 2017
@carols10cents
Copy link
Member

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.

@@ -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.
Copy link
Member

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.

} else {
self
}
}
Copy link
Member

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?)

@@ -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.
Copy link
Member

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.

@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs panic header.

Copy link
Member

@BurntSushi BurntSushi left a 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?

@BurntSushi BurntSushi added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 28, 2017
@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Sep 1, 2017

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.

Copy link
Member

@BurntSushi BurntSushi left a 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!

@aturon
Copy link
Member

aturon commented Sep 6, 2017

@bors r=burntsushi

@bors
Copy link
Contributor

bors commented Sep 6, 2017

📌 Commit b762283 has been approved by burntsushi

@Xaeroxe
Copy link
Contributor Author

Xaeroxe commented Sep 6, 2017

@aturon @BurntSushi That didn't seem to work :/

@aturon
Copy link
Member

aturon commented Sep 6, 2017

@Xaeroxe it's on the queue, which is pretty backlogged at the moment.

@Mark-Simulacrum
Copy link
Member

@bors rollup

Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Sep 6, 2017
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 {
Copy link

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 }

Copy link
Contributor Author

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.

Copy link

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, and that.

Copy link

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)!

Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Sep 7, 2017
Add clamp functions

Implementation of clamp feature:

Tracking issue: rust-lang#44095
RFC: rust-lang/rfcs#1961
bors added a commit that referenced this pull request Sep 7, 2017
@bors bors merged commit b762283 into rust-lang:master Sep 7, 2017
@Xaeroxe Xaeroxe deleted the clamp branch September 7, 2017 21:52
@pcwalton
Copy link
Contributor

pcwalton commented Sep 8, 2017

This broke Servo and Pathfinder. See servo/app_units#37

@Xaeroxe Xaeroxe restored the clamp branch September 8, 2017 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet