提交 02b7089e 编写于 作者: P Patrick Walton

libcore: Add a num typeclass

上级 3d7400f3
......@@ -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 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 @@ mod consts {
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 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 @@ mod consts {
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 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 @@
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
......
#[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.
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册