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

Stabilize unsigned and float variants of num_midpoint feature #131784

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Urgau
Copy link
Member

@Urgau Urgau commented Oct 16, 2024

This PR proposes that we stabilize the unsigned variants of the num_midpoint feature as well as the floats variants, since they are not subject to any unresolved questions, which is equivalent to doing (a + b) >> 1 or (a + b) / 2 in a sufficiently large number.

The stabilized API surface would be:

/// Calculates the middle point of `self` and `rhs`.
///
/// `midpoint(a, b)` is `(a + b) >> 1` as if it were performed in a sufficiently-large unsigned integral type.
/// This implies that the result is always rounded towards negative infinity and that no overflow will ever occur.

impl u{8,16,32,64,128,size} {
    pub const fn midpoint(self, rhs: Self) -> Self;
}

impl NonZeroU{8,16,32,64,size} {
    pub const fn midpoint(self, rhs: Self) -> Self;
}

impl f{32,64} {
    pub fn midpoint(self, rhs: Self) -> Self;
}

The signed variants u{8,16,32,64,128,size} would remain gated, until a decision is made about the rounding mode, in other words that the unresolved questions are resolved.

cc @rust-lang/libs-api
cc @scottmcm
r? libs-api

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 16, 2024
@Urgau Urgau mentioned this pull request Oct 16, 2024
3 tasks
@dtolnay dtolnay assigned dtolnay and unassigned m-ou-se Oct 16, 2024
@dtolnay dtolnay added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 16, 2024
@dtolnay
Copy link
Member

dtolnay commented Oct 16, 2024

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Oct 16, 2024

Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Oct 16, 2024
@dtolnay dtolnay added S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 16, 2024
@scottmcm
Copy link
Member

This absolutely is 👍 from me. (Thanks for the ping!)

I'm a fan of it as described here where it's consistent with (a + b) / 2, like the discussion of this in https://devblogs.microsoft.com/oldnewthing/20220207-00/?p=106223 . I like midpoint(5, 10) == 7 == midpoint(10, 5). To me, being consistent with what people wrote for small values that are in-range is good, and that it being symmetric is also good.

That said, I feel obliged to mention that in the tracking issue that some people want midpoint(5, 10) == 7 but midpoint(10, 5) == 8, where it's consistent with a + (b - a) / 2 instead, so wanted to make sure that libs-api discusses the rounding explicitly.

@scottmcm
Copy link
Member

Actually, @Urgau , consider putting "and float" in the title & OP here. To me the "stabilize the unsigned variants" text wouldn't include floats, since they're obviously signed.

@Urgau Urgau changed the title Stabilize unsigned variants of num_midpoint feature Stabilize unsigned variants and floats of num_midpoint feature Oct 16, 2024
@Urgau Urgau changed the title Stabilize unsigned variants and floats of num_midpoint feature Stabilize unsigned and float variants of num_midpoint feature Oct 16, 2024
@tgross35
Copy link
Contributor

Float midpoint should probably get a const gate, either here or separately. Bit unfortunate that it doesn't already have one to get stabilized at the same time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants