Skip to content

Commit cbe4b8d

Browse files
authored
Unrolled build for rust-lang#134340
Rollup merge of rust-lang#134340 - Urgau:stabilize-num_midpoint_signed, r=scottmcm Stabilize `num_midpoint_signed` feature This PR proposes that we stabilize the signed variants of [`iN::midpoint`](rust-lang#110840 (comment)), the operation is equivalent to doing `(a + b) / 2` in a sufficiently large number. The stabilized API surface would be: ```rust /// Calculates the middle point of `self` and `rhs`. /// /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a /// sufficiently-large signed integer type. This implies that the result is /// always rounded towards zero and that no overflow will ever occur. impl i{8,16,32,64,128,size} { pub const fn midpoint(self, rhs: Self) -> Self; } ``` T-libs-api previously stabilized the unsigned (and float) variants in rust-lang#131784, the signed variants were left out because of the rounding that should be used in case of negative midpoint. This stabilization proposal proposes that we round towards zero because: - it makes the obvious `(a + b) / 2` in a sufficiently-large number always true - using another rounding for the positive result would be inconsistent with the unsigned variants - it makes `midpoint(-a, -b)` == `-midpoint(a, b)` always true - it is consistent with `midpoint(a as f64, b as f64) as i64` - it makes it possible to always suggest `midpoint` as a replacement for `(a + b) / 2` expressions *(which we may want to do as a future work given the 21.2k hits on [GitHub Search](https://github.com/search?q=lang%3Arust+%2F%5C%28%5Ba-zA-Z_%5D*+%5C%2B+%5Ba-zA-Z_%5D*%5C%29+%5C%2F+2%2F&type=code&p=1))* `@scottmcm` mentioned a drawback in rust-lang#132191 (comment): > I'm torn, because rounding towards zero makes it "wider" than other values, which `>> 1` avoids -- `(a + b) >> 1` has the nice behaviour that `midpoint(a, b) + 2 == midpoint(a + 2, b + 2)`. > > But I guess overall sticking with `(a + b) / 2` makes sense as well, and I do like the negation property 🤷 Which I think is outweigh by the advantages cited above. Closes rust-lang#110840 cc `@rust-lang/libs-api` cc `@scottmcm` r? `@dtolnay`
2 parents f04bbc6 + 5914fb7 commit cbe4b8d

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

library/core/src/num/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,14 @@ macro_rules! midpoint_impl {
163163
/// # Examples
164164
///
165165
/// ```
166-
/// #![feature(num_midpoint_signed)]
167166
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
168167
#[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").midpoint(2), 0);")]
169168
#[doc = concat!("assert_eq!((-7", stringify!($SelfT), ").midpoint(0), -3);")]
170169
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(-7), -3);")]
171170
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(7), 3);")]
172171
/// ```
173-
#[unstable(feature = "num_midpoint_signed", issue = "110840")]
172+
#[stable(feature = "num_midpoint_signed", since = "CURRENT_RUSTC_VERSION")]
173+
#[rustc_const_stable(feature = "num_midpoint_signed", since = "CURRENT_RUSTC_VERSION")]
174174
#[must_use = "this returns the result of the operation, \
175175
without modifying the original"]
176176
#[inline]
@@ -215,14 +215,14 @@ macro_rules! midpoint_impl {
215215
/// # Examples
216216
///
217217
/// ```
218-
/// #![feature(num_midpoint_signed)]
219218
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
220219
#[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").midpoint(2), 0);")]
221220
#[doc = concat!("assert_eq!((-7", stringify!($SelfT), ").midpoint(0), -3);")]
222221
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(-7), -3);")]
223222
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(7), 3);")]
224223
/// ```
225-
#[unstable(feature = "num_midpoint_signed", issue = "110840")]
224+
#[stable(feature = "num_midpoint_signed", since = "CURRENT_RUSTC_VERSION")]
225+
#[rustc_const_stable(feature = "num_midpoint_signed", since = "CURRENT_RUSTC_VERSION")]
226226
#[must_use = "this returns the result of the operation, \
227227
without modifying the original"]
228228
#[inline]

library/coretests/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
#![feature(maybe_uninit_write_slice)]
6464
#![feature(min_specialization)]
6565
#![feature(never_type)]
66-
#![feature(num_midpoint_signed)]
6766
#![feature(numfmt)]
6867
#![feature(pattern)]
6968
#![feature(pointer_is_aligned_to)]

0 commit comments

Comments
 (0)