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

Conversation

@Xaeroxe
Copy link
Contributor

Xaeroxe commented Aug 26, 2017

Implementation of clamp feature:

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

@rust-highfive

This comment has been minimized.

Copy link
Collaborator

rust-highfive commented Aug 26, 2017

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.

@Xaeroxe Xaeroxe force-pushed the Xaeroxe:clamp branch from eabd26e to eeb9a28 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.

@scottmcm

scottmcm Aug 26, 2017

Member

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

This comment has been minimized.

@scottmcm

scottmcm Aug 26, 2017

Member

No need for the feature gate in code for lib changes; the attribute is sufficient.

@Xaeroxe Xaeroxe force-pushed the Xaeroxe:clamp branch from eeb9a28 to c589f86 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.

@lukaramu

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.

@lukaramu

lukaramu Aug 26, 2017

Contributor

Same as above.

Xaeroxe added some commits Aug 26, 2017

@Xaeroxe Xaeroxe changed the title Add clamp functions and feature gate Add clamp functions Aug 27, 2017

@carols10cents

This comment has been minimized.

Copy link
Member

carols10cents commented Aug 28, 2017

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.

This comment has been minimized.

@BurntSushi

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.

} else {
self
}
}

This comment has been minimized.

@BurntSushi

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

@@ -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.

@BurntSushi

BurntSushi Aug 28, 2017

Member

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.

This comment has been minimized.

@BurntSushi

BurntSushi Aug 28, 2017

Member

Needs panic header.

@BurntSushi
Copy link
Member

BurntSushi left a comment

This mostly looks good, but have some nits! I guess this should also technically wait to merge until the RFC has been merged?

@Xaeroxe

This comment has been minimized.

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.

@BurntSushi
Copy link
Member

BurntSushi left a comment

Oh, and could you also add some unit tests for the panic conditions? Thanks!

@Xaeroxe Xaeroxe force-pushed the Xaeroxe:clamp branch from bd556e7 to c3ff2d4 Sep 5, 2017

@Xaeroxe Xaeroxe force-pushed the Xaeroxe:clamp branch from c3ff2d4 to b762283 Sep 5, 2017

@aturon

This comment has been minimized.

Copy link
Member

aturon commented Sep 6, 2017

@bors r=burntsushi

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Sep 6, 2017

📌 Commit b762283 has been approved by burntsushi

@Xaeroxe

This comment has been minimized.

Copy link
Contributor Author

Xaeroxe commented Sep 6, 2017

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

@aturon

This comment has been minimized.

Copy link
Member

aturon commented Sep 6, 2017

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

@Mark-Simulacrum

This comment has been minimized.

Copy link
Member

Mark-Simulacrum commented Sep 6, 2017

@bors rollup

Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Sep 6, 2017

Rollup merge of rust-lang#44097 - Xaeroxe:clamp, r=burntsushi
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 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.

@StefanoD

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.

@Xaeroxe

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.

@StefanoD

StefanoD Sep 6, 2017

Oh, interesting and let x?

This comment has been minimized.

@Xaeroxe

Xaeroxe Sep 6, 2017

Author Contributor

Yeah, and that.

This comment has been minimized.

@StefanoD

StefanoD Sep 6, 2017

Holy caroly (this is actually no real word, but it rhymes)!

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

Rollup merge of rust-lang#44097 - Xaeroxe:clamp, r=burntsushi
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

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@Xaeroxe Xaeroxe deleted the Xaeroxe:clamp branch Sep 7, 2017

@pcwalton

This comment has been minimized.

Copy link
Contributor

pcwalton commented Sep 8, 2017

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

@Xaeroxe Xaeroxe restored the Xaeroxe:clamp branch Sep 8, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.