Skip to content

Commit 396a286

Browse files
committed
Add round_to_even() to float types
1 parent 11e396b commit 396a286

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

library/core/src/intrinsics.rs

+15
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 stabilized version of this intrinsic is
1395+
/// [`f32::round`](../../std/primitive.f32.html#method.round)
1396+
#[cfg(not(bootstrap))]
1397+
pub fn roundevenf32(x: f32) -> f32;
1398+
/// Returns the nearest integer to an `f32`. Rounds half-way to the nearest even
1399+
/// integer.
1400+
///
1401+
/// The stabilized version of this intrinsic is
1402+
/// [`f64::round`](../../std/primitive.f64.html#method.round)
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

+20
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ 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+
/// let f = 3.3_f32;
97+
/// let g = -3.3_f32;
98+
///
99+
/// assert_eq!(f.round(), 3.0);
100+
/// assert_eq!(g.round(), -3.0);
101+
/// ```
102+
#[cfg(not(bootstrap))]
103+
#[must_use = "method returns a new number and does not mutate the original value"]
104+
#[unstable(feature = "round_to_even", issue = "none")]
105+
#[inline]
106+
pub fn round_to_even(self) -> f32 {
107+
unsafe { intrinsics::roundevenf32(self) }
108+
}
109+
90110
/// Returns the integer part of a number.
91111
///
92112
/// # Examples

library/std/src/f64.rs

+20
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ impl f64 {
8787
unsafe { intrinsics::roundf64(self) }
8888
}
8989

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

0 commit comments

Comments
 (0)