Skip to content

Commit 6b0860a

Browse files
bors[bot]notriddle
andcommitted
61: Implement `round` r=japaric a=notriddle Fixes rust-lang#31 Co-authored-by: Michael Howell <[email protected]>
2 parents 667035f + dd2bbcb commit 6b0860a

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ pub trait F64Ext {
377377
#[cfg(todo)]
378378
fn ceil(self) -> Self;
379379

380-
#[cfg(todo)]
381380
fn round(self) -> Self;
382381

383382
#[cfg(todo)]
@@ -498,7 +497,6 @@ impl F64Ext for f64 {
498497
ceil(self)
499498
}
500499

501-
#[cfg(todo)]
502500
#[inline]
503501
fn round(self) -> Self {
504502
round(self)

src/math/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
macro_rules! force_eval {
2+
($e:expr) => {
3+
unsafe { ::core::ptr::read_volatile(&$e); }
4+
}
5+
}
6+
17
mod fabs;
28
mod fabsf;
39
mod fmodf;
410
mod powf;
11+
mod round;
512
mod scalbnf;
613
mod sqrtf;
714

815
pub use self::fabs::fabs;
916
pub use self::fabsf::fabsf;
1017
pub use self::fmodf::fmodf;
1118
pub use self::powf::powf;
19+
pub use self::round::round;
1220
pub use self::scalbnf::scalbnf;
1321
pub use self::sqrtf::sqrtf;
1422

src/math/round.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use core::f64;
2+
3+
const TOINT: f64 = 1.0 / f64::EPSILON;
4+
5+
pub fn round(mut x: f64) -> f64 {
6+
let (f, i) = (x, x.to_bits());
7+
let e: u64 = i >> 52 & 0x7ff;
8+
let mut y: f64;
9+
10+
if e >= 0x3ff + 52 {
11+
return x;
12+
}
13+
if i >> 63 != 0 {
14+
x = -x;
15+
}
16+
if e < 0x3ff - 1 {
17+
// raise inexact if x!=0
18+
force_eval!(x + TOINT);
19+
return 0.0 * f;
20+
}
21+
y = x + TOINT - TOINT - x;
22+
if y > 0.5 {
23+
y = y + x - 1.0;
24+
} else if y <= -0.5 {
25+
y = y + x + 1.0;
26+
} else {
27+
y = y + x;
28+
}
29+
30+
if i >> 63 != 0 {
31+
-y
32+
} else {
33+
y
34+
}
35+
}

test-generator/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ f64_f64! {
618618
// log10,
619619
// log1p,
620620
// log2,
621-
// round,
621+
round,
622622
// sin,
623623
// sinh,
624624
// sqrt,

0 commit comments

Comments
 (0)