Skip to content

Commit c28c27b

Browse files
Add round_ties_even to f32 and f64
1 parent 2956236 commit c28c27b

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

core/src/intrinsics.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,22 @@ extern "rust-intrinsic" {
16131613
/// [`f64::round`](../../std/primitive.f64.html#method.round)
16141614
pub fn roundf64(x: f64) -> f64;
16151615

1616+
/// Returns the nearest integer to an `f32`. Rounds half-way cases to the number
1617+
/// with an even least significant digit.
1618+
///
1619+
/// The stabilized version of this intrinsic is
1620+
/// [`f32::round_ties_even`](../../std/primitive.f32.html#method.round_ties_even)
1621+
#[cfg(not(bootstrap))]
1622+
pub fn roundevenf32(x: f32) -> f32;
1623+
1624+
/// Returns the nearest integer to an `f64`. Rounds half-way cases to the number
1625+
/// with an even least significant digit.
1626+
///
1627+
/// The stabilized version of this intrinsic is
1628+
/// [`f64::round_ties_even`](../../std/primitive.f64.html#method.round_ties_even)
1629+
#[cfg(not(bootstrap))]
1630+
pub fn roundevenf64(x: f64) -> f64;
1631+
16161632
/// Float addition that allows optimizations based on algebraic rules.
16171633
/// May assume inputs are finite.
16181634
///

std/src/f32.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,14 @@ impl f32 {
7878
/// let f = 3.3_f32;
7979
/// let g = -3.3_f32;
8080
/// let h = -3.7_f32;
81+
/// let i = 3.5_f32;
82+
/// let j = 4.5_f32;
8183
///
8284
/// assert_eq!(f.round(), 3.0);
8385
/// assert_eq!(g.round(), -3.0);
8486
/// assert_eq!(h.round(), -4.0);
87+
/// assert_eq!(i.round(), 4.0);
88+
/// assert_eq!(j.round(), 5.0);
8589
/// ```
8690
#[rustc_allow_incoherent_impl]
8791
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -91,6 +95,33 @@ impl f32 {
9195
unsafe { intrinsics::roundf32(self) }
9296
}
9397

98+
/// Returns the nearest integer to a number. Rounds half-way cases to the number
99+
/// with an even least significant digit.
100+
///
101+
/// # Examples
102+
///
103+
/// ```
104+
/// #![feature(round_ties_even)]
105+
///
106+
/// let f = 3.3_f32;
107+
/// let g = -3.3_f32;
108+
/// let h = 3.5_f32;
109+
/// let i = 4.5_f32;
110+
///
111+
/// assert_eq!(f.round_ties_even(), 3.0);
112+
/// assert_eq!(g.round_ties_even(), -3.0);
113+
/// assert_eq!(h.round_ties_even(), 4.0);
114+
/// assert_eq!(i.round_ties_even(), 4.0);
115+
/// ```
116+
#[cfg(not(bootstrap))]
117+
#[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
118+
#[must_use = "method returns a new number and does not mutate the original value"]
119+
#[unstable(feature = "round_ties_even", issue = "none")]
120+
#[inline]
121+
pub fn round_ties_even(self) -> f32 {
122+
unsafe { intrinsics::roundevenf32(self) }
123+
}
124+
94125
/// Returns the integer part of `self`.
95126
/// This means that non-integer numbers are always truncated towards zero.
96127
///

std/src/f32/tests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ fn test_ceil() {
209209

210210
#[test]
211211
fn test_round() {
212+
assert_approx_eq!(2.5f32.round(), 3.0f32);
212213
assert_approx_eq!(1.0f32.round(), 1.0f32);
213214
assert_approx_eq!(1.3f32.round(), 1.0f32);
214215
assert_approx_eq!(1.5f32.round(), 2.0f32);
@@ -221,6 +222,22 @@ fn test_round() {
221222
assert_approx_eq!((-1.7f32).round(), -2.0f32);
222223
}
223224

225+
#[cfg(not(bootstrap))]
226+
#[test]
227+
fn test_round_ties_even() {
228+
assert_approx_eq!(2.5f32.round_ties_even(), 2.0f32);
229+
assert_approx_eq!(1.0f32.round_ties_even(), 1.0f32);
230+
assert_approx_eq!(1.3f32.round_ties_even(), 1.0f32);
231+
assert_approx_eq!(1.5f32.round_ties_even(), 2.0f32);
232+
assert_approx_eq!(1.7f32.round_ties_even(), 2.0f32);
233+
assert_approx_eq!(0.0f32.round_ties_even(), 0.0f32);
234+
assert_approx_eq!((-0.0f32).round_ties_even(), -0.0f32);
235+
assert_approx_eq!((-1.0f32).round_ties_even(), -1.0f32);
236+
assert_approx_eq!((-1.3f32).round_ties_even(), -1.0f32);
237+
assert_approx_eq!((-1.5f32).round_ties_even(), -2.0f32);
238+
assert_approx_eq!((-1.7f32).round_ties_even(), -2.0f32);
239+
}
240+
224241
#[test]
225242
fn test_trunc() {
226243
assert_approx_eq!(1.0f32.trunc(), 1.0f32);

std/src/f64.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,14 @@ impl f64 {
7878
/// let f = 3.3_f64;
7979
/// let g = -3.3_f64;
8080
/// let h = -3.7_f64;
81+
/// let i = 3.5_f64;
82+
/// let j = 4.5_f64;
8183
///
8284
/// assert_eq!(f.round(), 3.0);
8385
/// assert_eq!(g.round(), -3.0);
8486
/// assert_eq!(h.round(), -4.0);
87+
/// assert_eq!(i.round(), 4.0);
88+
/// assert_eq!(j.round(), 5.0);
8589
/// ```
8690
#[rustc_allow_incoherent_impl]
8791
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -91,6 +95,33 @@ impl f64 {
9195
unsafe { intrinsics::roundf64(self) }
9296
}
9397

98+
/// Returns the nearest integer to a number. Rounds half-way cases to the number
99+
/// with an even least significant digit.
100+
///
101+
/// # Examples
102+
///
103+
/// ```
104+
/// #![feature(round_ties_even)]
105+
///
106+
/// let f = 3.3_f64;
107+
/// let g = -3.3_f64;
108+
/// let h = 3.5_f64;
109+
/// let i = 4.5_f64;
110+
///
111+
/// assert_eq!(f.round_ties_even(), 3.0);
112+
/// assert_eq!(g.round_ties_even(), -3.0);
113+
/// assert_eq!(h.round_ties_even(), 4.0);
114+
/// assert_eq!(i.round_ties_even(), 4.0);
115+
/// ```
116+
#[cfg(not(bootstrap))]
117+
#[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
118+
#[must_use = "method returns a new number and does not mutate the original value"]
119+
#[unstable(feature = "round_ties_even", issue = "none")]
120+
#[inline]
121+
pub fn round_ties_even(self) -> f64 {
122+
unsafe { intrinsics::roundevenf64(self) }
123+
}
124+
94125
/// Returns the integer part of `self`.
95126
/// This means that non-integer numbers are always truncated towards zero.
96127
///

std/src/f64/tests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ fn test_ceil() {
199199

200200
#[test]
201201
fn test_round() {
202+
assert_approx_eq!(2.5f64.round(), 3.0f64);
202203
assert_approx_eq!(1.0f64.round(), 1.0f64);
203204
assert_approx_eq!(1.3f64.round(), 1.0f64);
204205
assert_approx_eq!(1.5f64.round(), 2.0f64);
@@ -211,6 +212,22 @@ fn test_round() {
211212
assert_approx_eq!((-1.7f64).round(), -2.0f64);
212213
}
213214

215+
#[cfg(not(bootstrap))]
216+
#[test]
217+
fn test_round_ties_even() {
218+
assert_approx_eq!(2.5f64.round_ties_even(), 2.0f64);
219+
assert_approx_eq!(1.0f64.round_ties_even(), 1.0f64);
220+
assert_approx_eq!(1.3f64.round_ties_even(), 1.0f64);
221+
assert_approx_eq!(1.5f64.round_ties_even(), 2.0f64);
222+
assert_approx_eq!(1.7f64.round_ties_even(), 2.0f64);
223+
assert_approx_eq!(0.0f64.round_ties_even(), 0.0f64);
224+
assert_approx_eq!((-0.0f64).round_ties_even(), -0.0f64);
225+
assert_approx_eq!((-1.0f64).round_ties_even(), -1.0f64);
226+
assert_approx_eq!((-1.3f64).round_ties_even(), -1.0f64);
227+
assert_approx_eq!((-1.5f64).round_ties_even(), -2.0f64);
228+
assert_approx_eq!((-1.7f64).round_ties_even(), -2.0f64);
229+
}
230+
214231
#[test]
215232
fn test_trunc() {
216233
assert_approx_eq!(1.0f64.trunc(), 1.0f64);

std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@
307307
#![feature(provide_any)]
308308
#![feature(ptr_as_uninit)]
309309
#![feature(raw_os_nonzero)]
310+
#![cfg_attr(not(bootstrap), feature(round_ties_even))]
310311
#![feature(slice_internals)]
311312
#![feature(slice_ptr_get)]
312313
#![feature(std_internals)]

0 commit comments

Comments
 (0)