mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-27 17:24:06 +00:00
core: Add a Zero and One trait to num
I believe these are the last traits we need in order to start grouping our numerical types into mathematical groups and rings.
This commit is contained in:
parent
8554d5e710
commit
e8102e73a9
@ -174,6 +174,14 @@ impl f32: num::Num {
|
||||
static pure fn from_int(n: int) -> f32 { return n as f32; }
|
||||
}
|
||||
|
||||
impl f32: num::Zero {
|
||||
static pure fn zero() -> f32 { 0.0 }
|
||||
}
|
||||
|
||||
impl f32: num::One {
|
||||
static pure fn one() -> f32 { 1.0 }
|
||||
}
|
||||
|
||||
//
|
||||
// Local Variables:
|
||||
// mode: rust
|
||||
|
@ -193,6 +193,14 @@ impl f64: num::Num {
|
||||
static pure fn from_int(n: int) -> f64 { return n as f64; }
|
||||
}
|
||||
|
||||
impl f64: num::Zero {
|
||||
static pure fn zero() -> f64 { 0.0 }
|
||||
}
|
||||
|
||||
impl f64: num::One {
|
||||
static pure fn one() -> f64 { 1.0 }
|
||||
}
|
||||
|
||||
//
|
||||
// Local Variables:
|
||||
// mode: rust
|
||||
|
@ -443,6 +443,14 @@ impl float: num::Num {
|
||||
static pure fn from_int(&self, n: int) -> float { return n as float; }
|
||||
}
|
||||
|
||||
impl float: num::Zero {
|
||||
static pure fn zero() -> float { 0.0 }
|
||||
}
|
||||
|
||||
impl float: num::One {
|
||||
static pure fn one() -> float { 1.0 }
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_from_str() {
|
||||
assert from_str(~"3") == Some(3.);
|
||||
|
@ -91,6 +91,14 @@ impl T: num::Num {
|
||||
static pure fn from_int(n: int) -> T { return n as T; }
|
||||
}
|
||||
|
||||
impl T: num::Zero {
|
||||
static pure fn zero() -> T { 0 }
|
||||
}
|
||||
|
||||
impl T: num::One {
|
||||
static pure fn one() -> T { 1 }
|
||||
}
|
||||
|
||||
impl T: iter::Times {
|
||||
#[inline(always)]
|
||||
#[doc = "A convenience form for basic iteration. Given a variable `x` \
|
||||
|
@ -22,3 +22,11 @@ pub trait Num {
|
||||
pure fn to_int(&self) -> int;
|
||||
static pure fn from_int(n: int) -> self;
|
||||
}
|
||||
|
||||
pub trait Zero {
|
||||
static pure fn zero() -> self;
|
||||
}
|
||||
|
||||
pub trait One {
|
||||
static pure fn one() -> self;
|
||||
}
|
||||
|
@ -85,6 +85,14 @@ impl T: num::Num {
|
||||
static pure fn from_int(n: int) -> T { return n as T; }
|
||||
}
|
||||
|
||||
impl T: num::Zero {
|
||||
static pure fn zero() -> T { 0 }
|
||||
}
|
||||
|
||||
impl T: num::One {
|
||||
static pure fn one() -> T { 1 }
|
||||
}
|
||||
|
||||
impl T: iter::Times {
|
||||
#[inline(always)]
|
||||
#[doc = "A convenience form for basic iteration. Given a variable `x` \
|
||||
|
Loading…
Reference in New Issue
Block a user