libcore: Add a num typeclass
This commit is contained in:
parent
3d7400f3ac
commit
02b7089e15
@ -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,17 @@ 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 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,17 @@ 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 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,17 @@ 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 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 +514,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
|
||||
|
15
src/libcore/num.rs
Normal file
15
src/libcore/num.rs
Normal file
@ -0,0 +1,15 @@
|
||||
#[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 to_int() -> int;
|
||||
fn from_int(n: int) -> self; // TODO: Static functions.
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user