Implement round

This commit is contained in:
Michael Howell 2018-07-13 00:40:05 +00:00
parent 19ee60e7da
commit 50f9ad97a5
4 changed files with 44 additions and 1 deletions

View file

@ -12,6 +12,12 @@
#![deny(warnings)]
#![no_std]
macro_rules! force_eval {
($e:expr) => {
unsafe { ::core::ptr::read_volatile(&$e); }
}
}
mod math;
#[cfg(todo)]

View file

@ -2,6 +2,7 @@ mod fabs;
mod fabsf;
mod fmodf;
mod powf;
mod round;
mod scalbnf;
mod sqrtf;
@ -9,6 +10,7 @@ pub use self::fabs::fabs;
pub use self::fabsf::fabsf;
pub use self::fmodf::fmodf;
pub use self::powf::powf;
pub use self::round::round;
pub use self::scalbnf::scalbnf;
pub use self::sqrtf::sqrtf;

View file

@ -0,0 +1,35 @@
use core::f64;
const TOINT: f64 = 1.0 / f64::EPSILON;
pub fn round(mut x: f64) -> f64 {
let (f, i) = (x, x.to_bits());
let e: u64 = i >> 52 & 0x7ff;
let mut y: f64;
if e >= 0x3ff + 52 {
return x;
}
if i >> 63 != 0 {
x = -x;
}
if e < 0x3ff - 1 {
// raise inexact if x!=0
force_eval!(x + TOINT);
return 0.0 * f;
}
y = x + TOINT - TOINT - x;
if y > 0.5 {
y = y + x - 1.0;
} else if y <= -0.5 {
y = y + x + 1.0;
} else {
y = y + x;
}
if i >> 63 != 0 {
-y
} else {
y
}
}

View file

@ -618,7 +618,7 @@ f64_f64! {
// log10,
// log1p,
// log2,
// round,
round,
// sin,
// sinh,
// sqrt,