Demode Num trait and impls
This commit is contained in:
parent
2d91567892
commit
e85a3d8247
@ -158,11 +158,11 @@ pure fn logarithm(n: f32, b: f32) -> f32 {
|
||||
}
|
||||
|
||||
impl f32: num::Num {
|
||||
pure fn add(&&other: f32) -> f32 { return self + other; }
|
||||
pure fn sub(&&other: f32) -> f32 { return self - other; }
|
||||
pure fn mul(&&other: f32) -> f32 { return self * other; }
|
||||
pure fn div(&&other: f32) -> f32 { return self / other; }
|
||||
pure fn modulo(&&other: f32) -> f32 { return self % other; }
|
||||
pure fn add(other: &f32) -> f32 { return self + *other; }
|
||||
pure fn sub(other: &f32) -> f32 { return self - *other; }
|
||||
pure fn mul(other: &f32) -> f32 { return self * *other; }
|
||||
pure fn div(other: &f32) -> f32 { return self / *other; }
|
||||
pure fn modulo(other: &f32) -> f32 { return self % *other; }
|
||||
pure fn neg() -> f32 { return -self; }
|
||||
|
||||
pure fn to_int() -> int { return self as int; }
|
||||
|
@ -185,11 +185,11 @@ pure fn logarithm(n: f64, b: f64) -> f64 {
|
||||
}
|
||||
|
||||
impl f64: num::Num {
|
||||
pure fn add(&&other: f64) -> f64 { return self + other; }
|
||||
pure fn sub(&&other: f64) -> f64 { return self - other; }
|
||||
pure fn mul(&&other: f64) -> f64 { return self * other; }
|
||||
pure fn div(&&other: f64) -> f64 { return self / other; }
|
||||
pure fn modulo(&&other: f64) -> f64 { return self % other; }
|
||||
pure fn add(other: &f64) -> f64 { return self + *other; }
|
||||
pure fn sub(other: &f64) -> f64 { return self - *other; }
|
||||
pure fn mul(other: &f64) -> f64 { return self * *other; }
|
||||
pure fn div(other: &f64) -> f64 { return self / *other; }
|
||||
pure fn modulo(other: &f64) -> f64 { return self % *other; }
|
||||
pure fn neg() -> f64 { return -self; }
|
||||
|
||||
pure fn to_int() -> int { return self as int; }
|
||||
|
@ -139,7 +139,7 @@ fn to_str_common(num: float, digits: uint, exact: bool) -> ~str {
|
||||
|
||||
// while we still need digits
|
||||
// build stack of digits
|
||||
while ii > 0u && (frac >= epsilon_prime || exact) {
|
||||
while ii > 0 && (frac >= epsilon_prime || exact) {
|
||||
// store the next digit
|
||||
frac *= 10.0;
|
||||
let digit = frac as uint;
|
||||
@ -153,25 +153,25 @@ fn to_str_common(num: float, digits: uint, exact: bool) -> ~str {
|
||||
|
||||
let mut acc;
|
||||
let mut racc = ~"";
|
||||
let mut carry = if frac * 10.0 as uint >= 5u { 1u } else { 0u };
|
||||
let mut carry = if frac * 10.0 as uint >= 5 { 1 } else { 0 };
|
||||
|
||||
// turn digits into string
|
||||
// using stack of digits
|
||||
while vec::len(fractionalParts) > 0u {
|
||||
while fractionalParts.is_not_empty() {
|
||||
let mut adjusted_digit = carry + vec::pop(fractionalParts);
|
||||
|
||||
if adjusted_digit == 10u {
|
||||
carry = 1u;
|
||||
adjusted_digit %= 10u
|
||||
if adjusted_digit == 10 {
|
||||
carry = 1;
|
||||
adjusted_digit %= 10
|
||||
} else {
|
||||
carry = 0u
|
||||
carry = 0;
|
||||
};
|
||||
|
||||
racc = uint::str(adjusted_digit) + racc;
|
||||
}
|
||||
|
||||
// pad decimals with trailing zeroes
|
||||
while str::len(racc) < digits && exact {
|
||||
while racc.len() < digits && exact {
|
||||
racc += ~"0"
|
||||
}
|
||||
|
||||
@ -428,11 +428,11 @@ impl float : Ord {
|
||||
}
|
||||
|
||||
impl float: num::Num {
|
||||
pure fn add(&&other: float) -> float { return self + other; }
|
||||
pure fn sub(&&other: float) -> float { return self - other; }
|
||||
pure fn mul(&&other: float) -> float { return self * other; }
|
||||
pure fn div(&&other: float) -> float { return self / other; }
|
||||
pure fn modulo(&&other: float) -> float { return self % other; }
|
||||
pure fn add(other: &float) -> float { return self + *other; }
|
||||
pure fn sub(other: &float) -> float { return self - *other; }
|
||||
pure fn mul(other: &float) -> float { return self * *other; }
|
||||
pure fn div(other: &float) -> float { return self / *other; }
|
||||
pure fn modulo(other: &float) -> float { return self % *other; }
|
||||
pure fn neg() -> float { return -self; }
|
||||
|
||||
pure fn to_int() -> int { return self as int; }
|
||||
@ -540,11 +540,11 @@ fn test_traits() {
|
||||
let two: U = from_int(2);
|
||||
assert (two.to_int() == 2);
|
||||
|
||||
assert (ten.add(two) == from_int(12));
|
||||
assert (ten.sub(two) == from_int(8));
|
||||
assert (ten.mul(two) == from_int(20));
|
||||
assert (ten.div(two) == from_int(5));
|
||||
assert (ten.modulo(two) == from_int(0));
|
||||
assert (ten.add(&two) == from_int(12));
|
||||
assert (ten.sub(&two) == from_int(8));
|
||||
assert (ten.mul(&two) == from_int(20));
|
||||
assert (ten.div(&two) == from_int(5));
|
||||
assert (ten.modulo(&two) == from_int(0));
|
||||
}
|
||||
|
||||
test(&10.0);
|
||||
|
@ -81,11 +81,11 @@ impl T : Eq {
|
||||
}
|
||||
|
||||
impl T: num::Num {
|
||||
pure fn add(&&other: T) -> T { return self + other; }
|
||||
pure fn sub(&&other: T) -> T { return self - other; }
|
||||
pure fn mul(&&other: T) -> T { return self * other; }
|
||||
pure fn div(&&other: T) -> T { return self / other; }
|
||||
pure fn modulo(&&other: T) -> T { return self % other; }
|
||||
pure fn add(other: &T) -> T { return self + *other; }
|
||||
pure fn sub(other: &T) -> T { return self - *other; }
|
||||
pure fn mul(other: &T) -> T { return self * *other; }
|
||||
pure fn div(other: &T) -> T { return self / *other; }
|
||||
pure fn modulo(other: &T) -> T { return self % *other; }
|
||||
pure fn neg() -> T { return -self; }
|
||||
|
||||
pure fn to_int() -> int { return self as int; }
|
||||
@ -250,11 +250,11 @@ fn test_interfaces() {
|
||||
let two: U = from_int(2);
|
||||
assert (two.to_int() == 2);
|
||||
|
||||
assert (ten.add(two) == from_int(12));
|
||||
assert (ten.sub(two) == from_int(8));
|
||||
assert (ten.mul(two) == from_int(20));
|
||||
assert (ten.div(two) == from_int(5));
|
||||
assert (ten.modulo(two) == from_int(0));
|
||||
assert (ten.add(&two) == from_int(12));
|
||||
assert (ten.sub(&two) == from_int(8));
|
||||
assert (ten.mul(&two) == from_int(20));
|
||||
assert (ten.div(&two) == from_int(5));
|
||||
assert (ten.modulo(&two) == from_int(0));
|
||||
assert (ten.neg() == from_int(-10));
|
||||
}
|
||||
|
||||
|
@ -2,11 +2,11 @@
|
||||
|
||||
trait Num {
|
||||
// FIXME: Trait composition. (#2616)
|
||||
pure fn add(&&other: self) -> self;
|
||||
pure fn sub(&&other: self) -> self;
|
||||
pure fn mul(&&other: self) -> self;
|
||||
pure fn div(&&other: self) -> self;
|
||||
pure fn modulo(&&other: self) -> self;
|
||||
pure fn add(other: &self) -> self;
|
||||
pure fn sub(other: &self) -> self;
|
||||
pure fn mul(other: &self) -> self;
|
||||
pure fn div(other: &self) -> self;
|
||||
pure fn modulo(other: &self) -> self;
|
||||
pure fn neg() -> self;
|
||||
|
||||
pure fn to_int() -> int;
|
||||
|
@ -464,7 +464,7 @@ pure fn byte_slice<T>(s: &str, f: fn(v: &[u8]) -> T) -> T {
|
||||
|
||||
/// Convert a string to a vector of characters
|
||||
pure fn chars(s: &str) -> ~[char] {
|
||||
let mut buf = ~[], i = 0u;
|
||||
let mut buf = ~[], i = 0;
|
||||
let len = len(s);
|
||||
while i < len {
|
||||
let {ch, next} = char_range_at(s, i);
|
||||
|
@ -74,11 +74,11 @@ impl T : Eq {
|
||||
}
|
||||
|
||||
impl T: num::Num {
|
||||
pure fn add(&&other: T) -> T { return self + other; }
|
||||
pure fn sub(&&other: T) -> T { return self - other; }
|
||||
pure fn mul(&&other: T) -> T { return self * other; }
|
||||
pure fn div(&&other: T) -> T { return self / other; }
|
||||
pure fn modulo(&&other: T) -> T { return self % other; }
|
||||
pure fn add(other: &T) -> T { return self + *other; }
|
||||
pure fn sub(other: &T) -> T { return self - *other; }
|
||||
pure fn mul(other: &T) -> T { return self * *other; }
|
||||
pure fn div(other: &T) -> T { return self / *other; }
|
||||
pure fn modulo(other: &T) -> T { return self % *other; }
|
||||
pure fn neg() -> T { return -self; }
|
||||
|
||||
pure fn to_int() -> int { return self as int; }
|
||||
|
@ -5,11 +5,11 @@
|
||||
fn main() {
|
||||
// ints
|
||||
// num
|
||||
assert 15.add(6) == 21;
|
||||
assert 15i8.add(6i8) == 21i8;
|
||||
assert 15i16.add(6i16) == 21i16;
|
||||
assert 15i32.add(6i32) == 21i32;
|
||||
assert 15i64.add(6i64) == 21i64;
|
||||
assert 15.add(&6) == 21;
|
||||
assert 15i8.add(&6i8) == 21i8;
|
||||
assert 15i16.add(&6i16) == 21i16;
|
||||
assert 15i32.add(&6i32) == 21i32;
|
||||
assert 15i64.add(&6i64) == 21i64;
|
||||
// times
|
||||
15.times(|| false);
|
||||
15i8.times(|| false);
|
||||
@ -19,11 +19,11 @@ fn main() {
|
||||
|
||||
// uints
|
||||
// num
|
||||
assert 15u.add(6u) == 21u;
|
||||
assert 15u8.add(6u8) == 21u8;
|
||||
assert 15u16.add(6u16) == 21u16;
|
||||
assert 15u32.add(6u32) == 21u32;
|
||||
assert 15u64.add(6u64) == 21u64;
|
||||
assert 15u.add(&6u) == 21u;
|
||||
assert 15u8.add(&6u8) == 21u8;
|
||||
assert 15u16.add(&6u16) == 21u16;
|
||||
assert 15u32.add(&6u32) == 21u32;
|
||||
assert 15u64.add(&6u64) == 21u64;
|
||||
// times
|
||||
15u.times(|| false);
|
||||
15u8.times(|| false);
|
||||
|
Loading…
Reference in New Issue
Block a user