Merge branch 'incoming'
This commit is contained in:
commit
7571ee85c4
@ -45,6 +45,7 @@ export tuple;
|
||||
export to_str;
|
||||
export dvec, dvec_iter;
|
||||
export cmp;
|
||||
export num;
|
||||
|
||||
// NDM seems to be necessary for resolve to work
|
||||
export option_iter;
|
||||
@ -154,6 +155,7 @@ mod tuple;
|
||||
// Ubiquitous-utility-type modules
|
||||
|
||||
mod cmp;
|
||||
mod num;
|
||||
mod either;
|
||||
mod iter;
|
||||
mod logging;
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
import cmath::c_float::*;
|
||||
import cmath::c_float_targ_consts::*;
|
||||
import num::num;
|
||||
|
||||
// FIXME find out why these have to be exported explicitly
|
||||
|
||||
@ -19,6 +20,7 @@ export mul_add, fmax, fmin, nextafter, frexp, hypot, ldexp;
|
||||
export lgamma, ln, log_radix, ln1p, log10, log2, ilog_radix;
|
||||
export modf, pow, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc;
|
||||
export signbit;
|
||||
export num;
|
||||
|
||||
// These are not defined inside consts:: for consistency with
|
||||
// the integer types
|
||||
@ -176,6 +178,18 @@ pure fn log2(n: f32) -> f32 {
|
||||
ret ln(n) / consts::ln_2;
|
||||
}
|
||||
|
||||
impl num of 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; }
|
||||
|
||||
fn to_int() -> int { ret self as int; }
|
||||
fn from_int(n: int) -> f32 { ret n as f32; }
|
||||
}
|
||||
|
||||
//
|
||||
// Local Variables:
|
||||
// mode: rust
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
import cmath::c_double::*;
|
||||
import cmath::c_double_targ_consts::*;
|
||||
import num::num;
|
||||
|
||||
// Even though this module exports everything defined in it,
|
||||
// because it contains re-exports, we also have to explicitly
|
||||
@ -21,6 +22,7 @@ export lgamma, ln, log_radix, ln1p, log10, log2, ilog_radix;
|
||||
export modf, pow, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc;
|
||||
export signbit;
|
||||
export epsilon;
|
||||
export num;
|
||||
|
||||
// These are not defined inside consts:: for consistency with
|
||||
// the integer types
|
||||
@ -197,6 +199,18 @@ pure fn log2(n: f64) -> f64 {
|
||||
ret ln(n) / consts::ln_2;
|
||||
}
|
||||
|
||||
impl num of 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; }
|
||||
|
||||
fn to_int() -> int { ret self as int; }
|
||||
fn from_int(n: int) -> f64 { ret n as f64; }
|
||||
}
|
||||
|
||||
//
|
||||
// Local Variables:
|
||||
// mode: rust
|
||||
|
@ -17,6 +17,7 @@ export lgamma, ln, log_radix, ln1p, log10, log2, ilog_radix;
|
||||
export modf, pow, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc;
|
||||
export signbit;
|
||||
export pow_with_uint;
|
||||
export num;
|
||||
|
||||
// export when m_float == c_double
|
||||
|
||||
@ -26,6 +27,7 @@ export j0, j1, jn, y0, y1, yn;
|
||||
|
||||
import m_float = f64;
|
||||
import f64::*;
|
||||
import num::num;
|
||||
|
||||
const NaN: float = 0.0/0.0;
|
||||
|
||||
@ -408,6 +410,18 @@ 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 }
|
||||
|
||||
impl num of 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; }
|
||||
|
||||
fn to_int() -> int { ret self as int; }
|
||||
fn from_int(n: int) -> float { ret n as float; }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_str() {
|
||||
assert from_str("3") == some(3.);
|
||||
@ -501,6 +515,25 @@ fn test_to_str_inf() {
|
||||
assert to_str(-infinity, 10u) == "-inf";
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ifaces() {
|
||||
fn test<U:num>(ten: U) {
|
||||
assert (ten.to_int() == 10);
|
||||
|
||||
let two = ten.from_int(2);
|
||||
assert (two.to_int() == 2);
|
||||
|
||||
assert (ten.add(two) == ten.from_int(12));
|
||||
assert (ten.sub(two) == ten.from_int(8));
|
||||
assert (ten.mul(two) == ten.from_int(20));
|
||||
assert (ten.div(two) == ten.from_int(5));
|
||||
assert (ten.modulo(two) == ten.from_int(0));
|
||||
}
|
||||
|
||||
test(10.0);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Local Variables:
|
||||
// mode: rust
|
||||
|
@ -1,5 +1,6 @@
|
||||
import T = inst::T;
|
||||
import cmp::{eq, ord};
|
||||
import num::num;
|
||||
|
||||
export min_value, max_value;
|
||||
export min, max;
|
||||
@ -11,7 +12,7 @@ export range;
|
||||
export compl;
|
||||
export abs;
|
||||
export parse_buf, from_str, to_str, to_str_bytes, str;
|
||||
export ord, eq;
|
||||
export ord, eq, num;
|
||||
|
||||
const min_value: T = -1 as T << (inst::bits - 1 as T);
|
||||
const max_value: T = min_value - 1 as T;
|
||||
@ -122,6 +123,18 @@ impl eq of eq for T {
|
||||
}
|
||||
}
|
||||
|
||||
impl num of 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; }
|
||||
|
||||
fn to_int() -> int { ret self as int; }
|
||||
fn from_int(n: int) -> T { ret n as T; }
|
||||
}
|
||||
|
||||
|
||||
// FIXME: Has alignment issues on windows and 32-bit linux
|
||||
#[test]
|
||||
@ -179,3 +192,22 @@ fn test_to_str() {
|
||||
assert (eq(to_str(127 as T, 16u), "7f"));
|
||||
assert (eq(to_str(100 as T, 10u), "100"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ifaces() {
|
||||
fn test<U:num>(ten: U) {
|
||||
assert (ten.to_int() == 10);
|
||||
|
||||
let two = ten.from_int(2);
|
||||
assert (two.to_int() == 2);
|
||||
|
||||
assert (ten.add(two) == ten.from_int(12));
|
||||
assert (ten.sub(two) == ten.from_int(8));
|
||||
assert (ten.mul(two) == ten.from_int(20));
|
||||
assert (ten.div(two) == ten.from_int(5));
|
||||
assert (ten.modulo(two) == ten.from_int(0));
|
||||
}
|
||||
|
||||
test(10 as T);
|
||||
}
|
||||
|
||||
|
16
src/libcore/num.rs
Normal file
16
src/libcore/num.rs
Normal file
@ -0,0 +1,16 @@
|
||||
#[doc="An interface for numbers."]
|
||||
|
||||
iface num {
|
||||
// FIXME: Cross-crate overloading doesn't work yet.
|
||||
// FIXME: Interface inheritance.
|
||||
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;
|
||||
|
||||
fn to_int() -> int;
|
||||
fn from_int(n: int) -> self; // TODO: Static functions.
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
import T = inst::T;
|
||||
import cmp::{eq, ord};
|
||||
import num::num;
|
||||
|
||||
export min_value, max_value;
|
||||
export min, max;
|
||||
@ -11,7 +12,7 @@ export range;
|
||||
export compl;
|
||||
export to_str, to_str_bytes;
|
||||
export from_str, from_str_radix, str, parse_buf;
|
||||
export ord, eq;
|
||||
export ord, eq, num;
|
||||
|
||||
const min_value: T = 0 as T;
|
||||
const max_value: T = 0 as T - 1 as T;
|
||||
@ -63,6 +64,18 @@ impl eq of eq for T {
|
||||
}
|
||||
}
|
||||
|
||||
impl num of 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; }
|
||||
|
||||
fn to_int() -> int { ret self as int; }
|
||||
fn from_int(n: int) -> T { ret n as T; }
|
||||
}
|
||||
|
||||
#[doc = "
|
||||
Parse a buffer of bytes
|
||||
|
||||
|
33
src/libstd/cmp.rs
Normal file
33
src/libstd/cmp.rs
Normal file
@ -0,0 +1,33 @@
|
||||
#[doc="Additional general-purpose comparison functionality."]
|
||||
|
||||
const fuzzy_epsilon: float = 1.0e-6;
|
||||
|
||||
iface fuzzy_eq {
|
||||
fn fuzzy_eq(&&other: self) -> bool;
|
||||
}
|
||||
|
||||
impl fuzzy_eq of fuzzy_eq for float {
|
||||
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 {
|
||||
ret f32::abs(self - other) < (fuzzy_epsilon as f32);
|
||||
}
|
||||
}
|
||||
|
||||
impl fuzzy_eq of fuzzy_eq for f64 {
|
||||
fn fuzzy_eq(&&other: f64) -> bool {
|
||||
ret f64::abs(self - other) < (fuzzy_epsilon as f64);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fuzzy_equals() {
|
||||
assert ((1.0).fuzzy_eq(1.0));
|
||||
assert ((1.0f32).fuzzy_eq(1.0f32));
|
||||
assert ((1.0f64).fuzzy_eq(1.0f64));
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ export bitv, deque, fun_treemap, list, map, smallintmap, sort, treemap;
|
||||
export rope, arena, arc, par;
|
||||
export ebml, dbg, getopts, json, rand, sha1, term, time, prettyprint;
|
||||
export test, tempfile, serialization;
|
||||
export cmp;
|
||||
|
||||
// General io and system-services modules
|
||||
|
||||
@ -70,6 +71,7 @@ mod prettyprint;
|
||||
mod arena;
|
||||
mod arc;
|
||||
mod par;
|
||||
mod cmp;
|
||||
|
||||
#[cfg(unicode)]
|
||||
mod unicode;
|
||||
|
Loading…
x
Reference in New Issue
Block a user