Added Round trait to core
This commit is contained in:
parent
40f0b45f8e
commit
96f0512a45
6 changed files with 126 additions and 0 deletions
|
|
@ -306,6 +306,33 @@ pub extern {
|
|||
fn floorf32(val: f32) -> f32;
|
||||
}
|
||||
|
||||
impl f32: num::Round {
|
||||
#[inline(always)]
|
||||
pure fn round(&self, mode: num::RoundMode) -> f32 {
|
||||
match mode {
|
||||
num::RoundDown => floor(*self),
|
||||
num::RoundUp => ceil(*self),
|
||||
num::RoundToZero if is_negative(*self) => ceil(*self),
|
||||
num::RoundToZero => floor(*self),
|
||||
num::RoundFromZero if is_negative(*self) => floor(*self),
|
||||
num::RoundFromZero => ceil(*self)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn floor(&self) -> f32 { floor(*self) }
|
||||
#[inline(always)]
|
||||
pure fn ceil(&self) -> f32 { ceil(*self) }
|
||||
#[inline(always)]
|
||||
pure fn fract(&self) -> f32 {
|
||||
if is_negative(*self) {
|
||||
(*self) - ceil(*self)
|
||||
} else {
|
||||
(*self) - floor(*self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Local Variables:
|
||||
// mode: rust
|
||||
|
|
|
|||
|
|
@ -330,6 +330,33 @@ pub extern {
|
|||
fn floorf64(val: f64) -> f64;
|
||||
}
|
||||
|
||||
impl f64: num::Round {
|
||||
#[inline(always)]
|
||||
pure fn round(&self, mode: num::RoundMode) -> f64 {
|
||||
match mode {
|
||||
num::RoundDown => floor(*self),
|
||||
num::RoundUp => ceil(*self),
|
||||
num::RoundToZero if is_negative(*self) => ceil(*self),
|
||||
num::RoundToZero => floor(*self),
|
||||
num::RoundFromZero if is_negative(*self) => floor(*self),
|
||||
num::RoundFromZero => ceil(*self)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn floor(&self) -> f64 { floor(*self) }
|
||||
#[inline(always)]
|
||||
pure fn ceil(&self) -> f64 { ceil(*self) }
|
||||
#[inline(always)]
|
||||
pure fn fract(&self) -> f64 {
|
||||
if is_negative(*self) {
|
||||
(*self) - ceil(*self)
|
||||
} else {
|
||||
(*self) - floor(*self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Local Variables:
|
||||
// mode: rust
|
||||
|
|
|
|||
|
|
@ -488,6 +488,39 @@ impl float: num::One {
|
|||
static pure fn one() -> float { 1.0 }
|
||||
}
|
||||
|
||||
impl float: num::Round {
|
||||
#[inline(always)]
|
||||
pure fn round(&self, mode: num::RoundMode) -> float {
|
||||
match mode {
|
||||
num::RoundDown
|
||||
=> f64::floor(*self as f64) as float,
|
||||
num::RoundUp
|
||||
=> f64::ceil(*self as f64) as float,
|
||||
num::RoundToZero if is_negative(*self)
|
||||
=> f64::ceil(*self as f64) as float,
|
||||
num::RoundToZero
|
||||
=> f64::floor(*self as f64) as float,
|
||||
num::RoundFromZero if is_negative(*self)
|
||||
=> f64::floor(*self as f64) as float,
|
||||
num::RoundFromZero
|
||||
=> f64::ceil(*self as f64) as float
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn floor(&self) -> float { f64::floor(*self as f64) as float}
|
||||
#[inline(always)]
|
||||
pure fn ceil(&self) -> float { f64::ceil(*self as f64) as float}
|
||||
#[inline(always)]
|
||||
pure fn fract(&self) -> float {
|
||||
if is_negative(*self) {
|
||||
(*self) - (f64::ceil(*self as f64) as float)
|
||||
} else {
|
||||
(*self) - (f64::floor(*self as f64) as float)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_from_str() {
|
||||
assert from_str(~"3") == Some(3.);
|
||||
|
|
|
|||
|
|
@ -200,6 +200,18 @@ impl T: num::One {
|
|||
static pure fn one() -> T { 1 }
|
||||
}
|
||||
|
||||
impl T: num::Round {
|
||||
#[inline(always)]
|
||||
pure fn round(&self, _: num::RoundMode) -> T { *self }
|
||||
|
||||
#[inline(always)]
|
||||
pure fn floor(&self) -> T { *self }
|
||||
#[inline(always)]
|
||||
pure fn ceil(&self) -> T { *self }
|
||||
#[inline(always)]
|
||||
pure fn fract(&self) -> T { 0 }
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a buffer of bytes
|
||||
*
|
||||
|
|
|
|||
|
|
@ -35,3 +35,18 @@ pub trait Zero {
|
|||
pub trait One {
|
||||
static pure fn one() -> Self;
|
||||
}
|
||||
|
||||
pub trait Round {
|
||||
pure fn round(&self, mode: RoundMode) -> self;
|
||||
|
||||
pure fn floor(&self) -> self;
|
||||
pure fn ceil(&self) -> self;
|
||||
pure fn fract(&self) -> self;
|
||||
}
|
||||
|
||||
pub enum RoundMode {
|
||||
RoundDown,
|
||||
RoundUp,
|
||||
RoundToZero,
|
||||
RoundFromZero
|
||||
}
|
||||
|
|
|
|||
|
|
@ -160,6 +160,18 @@ impl T: num::One {
|
|||
static pure fn one() -> T { 1 }
|
||||
}
|
||||
|
||||
impl T: num::Round {
|
||||
#[inline(always)]
|
||||
pure fn round(&self, _: num::RoundMode) -> T { *self }
|
||||
|
||||
#[inline(always)]
|
||||
pure fn floor(&self) -> T { *self }
|
||||
#[inline(always)]
|
||||
pure fn ceil(&self) -> T { *self }
|
||||
#[inline(always)]
|
||||
pure fn fract(&self) -> T { 0 }
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a buffer of bytes
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue