Skip to content

Commit 69572c2

Browse files
committed
Add round_to_even() to float types
1 parent 11e396b commit 69572c2

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

library/core/src/intrinsics.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,21 @@ extern "rust-intrinsic" {
13881388
/// [`f64::round`](../../std/primitive.f64.html#method.round)
13891389
pub fn roundf64(x: f64) -> f64;
13901390

1391+
/// Returns the nearest integer to an `f32`. Rounds half-way to the nearest even
1392+
/// integer.
1393+
///
1394+
/// The version of this intrinsic in the standard libary is
1395+
/// [`f32::round`](../../std/primitive.f32.html#method.round_to_even)
1396+
#[cfg(not(bootstrap))]
1397+
pub fn roundevenf32(x: f32) -> f32;
1398+
/// Returns the nearest integer to an `f64`. Rounds half-way to the nearest even
1399+
/// integer.
1400+
///
1401+
/// The version of this intrinsic in the standard libary is
1402+
/// [`f64::round`](../../std/primitive.f64.html#method.round_to_even)
1403+
#[cfg(not(bootstrap))]
1404+
pub fn roundevenf64(x: f64) -> f64;
1405+
13911406
/// Float addition that allows optimizations based on algebraic rules.
13921407
/// May assume inputs are finite.
13931408
///

library/std/src/f32.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,27 @@ impl f32 {
8787
unsafe { intrinsics::roundf32(self) }
8888
}
8989

90+
/// Returns the nearest integer to a number. Round half-way cases to the
91+
/// nearest even integer.
92+
///
93+
/// # Examples
94+
///
95+
/// ```
96+
/// # #![cfg_attr(not(bootstrap), feature(round_to_even))]
97+
/// let f = 3.3_f32;
98+
/// let g = -3.3_f32;
99+
///
100+
/// assert_eq!(f.round_to_even(), 3.0);
101+
/// assert_eq!(g.round_to_even(), -3.0);
102+
/// ```
103+
#[cfg(not(bootstrap))]
104+
#[must_use = "method returns a new number and does not mutate the original value"]
105+
#[unstable(feature = "round_to_even", issue = "none")]
106+
#[inline]
107+
pub fn round_to_even(self) -> f32 {
108+
unsafe { intrinsics::roundevenf32(self) }
109+
}
110+
90111
/// Returns the integer part of a number.
91112
///
92113
/// # Examples

library/std/src/f64.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,27 @@ impl f64 {
8787
unsafe { intrinsics::roundf64(self) }
8888
}
8989

90+
/// Returns the nearest integer to a number. Round half-way cases to the
91+
/// nearest even integer.
92+
///
93+
/// # Examples
94+
///
95+
/// ```
96+
/// # #![cfg_attr(not(bootstrap), feature(round_to_even))]
97+
/// let f = 3.3_f64;
98+
/// let g = -3.3_f64;
99+
///
100+
/// assert_eq!(f.round_to_even(), 3.0);
101+
/// assert_eq!(g.round_to_even(), -3.0);
102+
/// ```
103+
#[cfg(not(bootstrap))]
104+
#[must_use = "method returns a new number and does not mutate the original value"]
105+
#[unstable(feature = "round_to_even", issue = "none")]
106+
#[inline]
107+
pub fn round_to_even(self) -> f64 {
108+
unsafe { intrinsics::roundevenf64(self) }
109+
}
110+
90111
/// Returns the integer part of a number.
91112
///
92113
/// # Examples

0 commit comments

Comments
 (0)