core: Mark a bunch of numeric functions as pure

This commit is contained in:
Patrick Walton 2012-07-26 14:42:44 -07:00
parent d19b915bc4
commit 1dd8acd56a
8 changed files with 76 additions and 76 deletions

View File

@ -1,10 +1,10 @@
/// Interfaces used for comparison.
iface ord {
fn lt(&&other: self) -> bool;
trait ord {
pure fn lt(&&other: self) -> bool;
}
iface eq {
fn eq(&&other: self) -> bool;
trait eq {
pure fn eq(&&other: self) -> bool;
}

View File

@ -168,15 +168,15 @@ pure fn log2(n: f32) -> f32 {
}
impl num of num::num for f32 {
fn add(&&other: f32) -> f32 { ret self + other; }
fn sub(&&other: f32) -> f32 { ret self - other; }
fn mul(&&other: f32) -> f32 { ret self * other; }
fn div(&&other: f32) -> f32 { ret self / other; }
fn modulo(&&other: f32) -> f32 { ret self % other; }
fn neg() -> f32 { ret -self; }
pure fn add(&&other: f32) -> f32 { ret self + other; }
pure fn sub(&&other: f32) -> f32 { ret self - other; }
pure fn mul(&&other: f32) -> f32 { ret self * other; }
pure fn div(&&other: f32) -> f32 { ret self / other; }
pure fn modulo(&&other: f32) -> f32 { ret self % other; }
pure fn neg() -> f32 { ret -self; }
fn to_int() -> int { ret self as int; }
fn from_int(n: int) -> f32 { ret n as f32; }
pure fn to_int() -> int { ret self as int; }
pure fn from_int(n: int) -> f32 { ret n as f32; }
}
//

View File

@ -195,15 +195,15 @@ pure fn log2(n: f64) -> f64 {
}
impl num of num::num for f64 {
fn add(&&other: f64) -> f64 { ret self + other; }
fn sub(&&other: f64) -> f64 { ret self - other; }
fn mul(&&other: f64) -> f64 { ret self * other; }
fn div(&&other: f64) -> f64 { ret self / other; }
fn modulo(&&other: f64) -> f64 { ret self % other; }
fn neg() -> f64 { ret -self; }
pure fn add(&&other: f64) -> f64 { ret self + other; }
pure fn sub(&&other: f64) -> f64 { ret self - other; }
pure fn mul(&&other: f64) -> f64 { ret self * other; }
pure fn div(&&other: f64) -> f64 { ret self / other; }
pure fn modulo(&&other: f64) -> f64 { ret self % other; }
pure fn neg() -> f64 { ret -self; }
fn to_int() -> int { ret self as int; }
fn from_int(n: int) -> f64 { ret n as f64; }
pure fn to_int() -> int { ret self as int; }
pure fn from_int(n: int) -> f64 { ret n as f64; }
}
//

View File

@ -403,32 +403,32 @@ fn pow_with_uint(base: uint, pow: uint) -> float {
ret total;
}
fn is_positive(x: float) -> bool { f64::is_positive(x as f64) }
fn is_negative(x: float) -> bool { f64::is_negative(x as f64) }
fn is_nonpositive(x: float) -> bool { f64::is_nonpositive(x as f64) }
fn is_nonnegative(x: float) -> bool { f64::is_nonnegative(x as f64) }
fn is_zero(x: float) -> bool { f64::is_zero(x as f64) }
fn is_infinite(x: float) -> bool { f64::is_infinite(x as f64) }
fn is_finite(x: float) -> bool { f64::is_finite(x as f64) }
fn is_NaN(x: float) -> bool { f64::is_NaN(x as f64) }
pure fn is_positive(x: float) -> bool { f64::is_positive(x as f64) }
pure fn is_negative(x: float) -> bool { f64::is_negative(x as f64) }
pure fn is_nonpositive(x: float) -> bool { f64::is_nonpositive(x as f64) }
pure fn is_nonnegative(x: float) -> bool { f64::is_nonnegative(x as f64) }
pure fn is_zero(x: float) -> bool { f64::is_zero(x as f64) }
pure fn is_infinite(x: float) -> bool { f64::is_infinite(x as f64) }
pure fn is_finite(x: float) -> bool { f64::is_finite(x as f64) }
pure fn is_NaN(x: float) -> bool { f64::is_NaN(x as f64) }
fn abs(x: float) -> float { f64::abs(x as f64) as float }
fn sqrt(x: float) -> float { f64::sqrt(x as f64) as float }
fn atan(x: float) -> float { f64::atan(x as f64) as float }
fn sin(x: float) -> float { f64::sin(x as f64) as float }
fn cos(x: float) -> float { f64::cos(x as f64) as float }
fn tan(x: float) -> float { f64::tan(x as f64) as float }
pure fn abs(x: float) -> float { f64::abs(x as f64) as float }
pure fn sqrt(x: float) -> float { f64::sqrt(x as f64) as float }
pure fn atan(x: float) -> float { f64::atan(x as f64) as float }
pure fn sin(x: float) -> float { f64::sin(x as f64) as float }
pure fn cos(x: float) -> float { f64::cos(x as f64) as float }
pure fn tan(x: float) -> float { f64::tan(x as f64) as float }
impl num of num::num for float {
fn add(&&other: float) -> float { ret self + other; }
fn sub(&&other: float) -> float { ret self - other; }
fn mul(&&other: float) -> float { ret self * other; }
fn div(&&other: float) -> float { ret self / other; }
fn modulo(&&other: float) -> float { ret self % other; }
fn neg() -> float { ret -self; }
pure fn add(&&other: float) -> float { ret self + other; }
pure fn sub(&&other: float) -> float { ret self - other; }
pure fn mul(&&other: float) -> float { ret self * other; }
pure fn div(&&other: float) -> float { ret self / other; }
pure fn modulo(&&other: float) -> float { ret self % other; }
pure fn neg() -> float { ret -self; }
fn to_int() -> int { ret self as int; }
fn from_int(n: int) -> float { ret n as float; }
pure fn to_int() -> int { ret self as int; }
pure fn from_int(n: int) -> float { ret n as float; }
}
#[test]

View File

@ -112,27 +112,27 @@ fn to_str_bytes<U>(n: T, radix: uint, f: fn(v: &[u8]) -> U) -> U {
fn str(i: T) -> ~str { ret to_str(i, 10u); }
impl ord of ord for T {
fn lt(&&other: T) -> bool {
pure fn lt(&&other: T) -> bool {
ret self < other;
}
}
impl eq of eq for T {
fn eq(&&other: T) -> bool {
pure fn eq(&&other: T) -> bool {
ret self == other;
}
}
impl num of num::num for T {
fn add(&&other: T) -> T { ret self + other; }
fn sub(&&other: T) -> T { ret self - other; }
fn mul(&&other: T) -> T { ret self * other; }
fn div(&&other: T) -> T { ret self / other; }
fn modulo(&&other: T) -> T { ret self % other; }
fn neg() -> T { ret -self; }
pure fn add(&&other: T) -> T { ret self + other; }
pure fn sub(&&other: T) -> T { ret self - other; }
pure fn mul(&&other: T) -> T { ret self * other; }
pure fn div(&&other: T) -> T { ret self / other; }
pure fn modulo(&&other: T) -> T { ret self % other; }
pure fn neg() -> T { ret -self; }
fn to_int() -> int { ret self as int; }
fn from_int(n: int) -> T { ret n as T; }
pure fn to_int() -> int { ret self as int; }
pure fn from_int(n: int) -> T { ret n as T; }
}
impl times of iter::times for T {

View File

@ -1,17 +1,17 @@
/// An interface for numbers.
iface num {
trait num {
// FIXME: Cross-crate overloading doesn't work yet. (#2615)
// FIXME: Interface inheritance. (#2616)
fn add(&&other: self) -> self;
fn sub(&&other: self) -> self;
fn mul(&&other: self) -> self;
fn div(&&other: self) -> self;
fn modulo(&&other: self) -> self;
fn neg() -> 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;
fn to_int() -> int;
fn from_int(n: int) -> self; // FIXME (#2376) Static functions.
pure fn to_int() -> int;
pure fn from_int(n: int) -> self; // FIXME (#2376) Static functions.
// n.b. #2376 is for classes, not ifaces, but it could be generalized...
}

View File

@ -53,27 +53,27 @@ pure fn compl(i: T) -> T {
}
impl ord of ord for T {
fn lt(&&other: T) -> bool {
pure fn lt(&&other: T) -> bool {
ret self < other;
}
}
impl eq of eq for T {
fn eq(&&other: T) -> bool {
pure fn eq(&&other: T) -> bool {
ret self == other;
}
}
impl num of num::num for T {
fn add(&&other: T) -> T { ret self + other; }
fn sub(&&other: T) -> T { ret self - other; }
fn mul(&&other: T) -> T { ret self * other; }
fn div(&&other: T) -> T { ret self / other; }
fn modulo(&&other: T) -> T { ret self % other; }
fn neg() -> T { ret -self; }
pure fn add(&&other: T) -> T { ret self + other; }
pure fn sub(&&other: T) -> T { ret self - other; }
pure fn mul(&&other: T) -> T { ret self * other; }
pure fn div(&&other: T) -> T { ret self / other; }
pure fn modulo(&&other: T) -> T { ret self % other; }
pure fn neg() -> T { ret -self; }
fn to_int() -> int { ret self as int; }
fn from_int(n: int) -> T { ret n as T; }
pure fn to_int() -> int { ret self as int; }
pure fn from_int(n: int) -> T { ret n as T; }
}
/**

View File

@ -2,24 +2,24 @@
const fuzzy_epsilon: float = 1.0e-6;
iface fuzzy_eq {
fn fuzzy_eq(&&other: self) -> bool;
trait fuzzy_eq {
pure fn fuzzy_eq(&&other: self) -> bool;
}
impl fuzzy_eq of fuzzy_eq for float {
fn fuzzy_eq(&&other: float) -> bool {
pure fn fuzzy_eq(&&other: float) -> bool {
ret float::abs(self - other) < fuzzy_epsilon;
}
}
impl fuzzy_eq of fuzzy_eq for f32 {
fn fuzzy_eq(&&other: f32) -> bool {
pure fn fuzzy_eq(&&other: f32) -> bool {
ret f32::abs(self - other) < (fuzzy_epsilon as f32);
}
}
impl fuzzy_eq of fuzzy_eq for f64 {
fn fuzzy_eq(&&other: f64) -> bool {
pure fn fuzzy_eq(&&other: f64) -> bool {
ret f64::abs(self - other) < (fuzzy_epsilon as f64);
}
}