Skip to content

Commit 02b7089

Browse files
committed
libcore: Add a num typeclass
1 parent 3d7400f commit 02b7089

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

src/libcore/f32.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import cmath::c_float::*;
66
import cmath::c_float_targ_consts::*;
7+
import num::num;
78

89
// FIXME find out why these have to be exported explicitly
910

@@ -19,6 +20,7 @@ export mul_add, fmax, fmin, nextafter, frexp, hypot, ldexp;
1920
export lgamma, ln, log_radix, ln1p, log10, log2, ilog_radix;
2021
export modf, pow, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc;
2122
export signbit;
23+
export num;
2224

2325
// These are not defined inside consts:: for consistency with
2426
// the integer types
@@ -176,6 +178,17 @@ pure fn log2(n: f32) -> f32 {
176178
ret ln(n) / consts::ln_2;
177179
}
178180

181+
impl num of num for f32 {
182+
fn add(&&other: f32) -> f32 { ret self + other; }
183+
fn sub(&&other: f32) -> f32 { ret self - other; }
184+
fn mul(&&other: f32) -> f32 { ret self * other; }
185+
fn div(&&other: f32) -> f32 { ret self / other; }
186+
fn modulo(&&other: f32) -> f32 { ret self % other; }
187+
188+
fn to_int() -> int { ret self as int; }
189+
fn from_int(n: int) -> f32 { ret n as f32; }
190+
}
191+
179192
//
180193
// Local Variables:
181194
// mode: rust

src/libcore/f64.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import cmath::c_double::*;
66
import cmath::c_double_targ_consts::*;
7+
import num::num;
78

89
// Even though this module exports everything defined in it,
910
// because it contains re-exports, we also have to explicitly
@@ -21,6 +22,7 @@ export lgamma, ln, log_radix, ln1p, log10, log2, ilog_radix;
2122
export modf, pow, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc;
2223
export signbit;
2324
export epsilon;
25+
export num;
2426

2527
// These are not defined inside consts:: for consistency with
2628
// the integer types
@@ -197,6 +199,17 @@ pure fn log2(n: f64) -> f64 {
197199
ret ln(n) / consts::ln_2;
198200
}
199201

202+
impl num of num for f64 {
203+
fn add(&&other: f64) -> f64 { ret self + other; }
204+
fn sub(&&other: f64) -> f64 { ret self - other; }
205+
fn mul(&&other: f64) -> f64 { ret self * other; }
206+
fn div(&&other: f64) -> f64 { ret self / other; }
207+
fn modulo(&&other: f64) -> f64 { ret self % other; }
208+
209+
fn to_int() -> int { ret self as int; }
210+
fn from_int(n: int) -> f64 { ret n as f64; }
211+
}
212+
200213
//
201214
// Local Variables:
202215
// mode: rust

src/libcore/float.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export lgamma, ln, log_radix, ln1p, log10, log2, ilog_radix;
1717
export modf, pow, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc;
1818
export signbit;
1919
export pow_with_uint;
20+
export num;
2021

2122
// export when m_float == c_double
2223

@@ -26,6 +27,7 @@ export j0, j1, jn, y0, y1, yn;
2627

2728
import m_float = f64;
2829
import f64::*;
30+
import num::num;
2931

3032
const NaN: float = 0.0/0.0;
3133

@@ -408,6 +410,17 @@ fn sin(x: float) -> float { f64::sin(x as f64) as float }
408410
fn cos(x: float) -> float { f64::cos(x as f64) as float }
409411
fn tan(x: float) -> float { f64::tan(x as f64) as float }
410412

413+
impl num of num for float {
414+
fn add(&&other: float) -> float { ret self + other; }
415+
fn sub(&&other: float) -> float { ret self - other; }
416+
fn mul(&&other: float) -> float { ret self * other; }
417+
fn div(&&other: float) -> float { ret self / other; }
418+
fn modulo(&&other: float) -> float { ret self % other; }
419+
420+
fn to_int() -> int { ret self as int; }
421+
fn from_int(n: int) -> float { ret n as float; }
422+
}
423+
411424
#[test]
412425
fn test_from_str() {
413426
assert from_str("3") == some(3.);
@@ -501,6 +514,25 @@ fn test_to_str_inf() {
501514
assert to_str(-infinity, 10u) == "-inf";
502515
}
503516

517+
#[test]
518+
fn test_ifaces() {
519+
fn test<U:num>(ten: U) {
520+
assert (ten.to_int() == 10);
521+
522+
let two = ten.from_int(2);
523+
assert (two.to_int() == 2);
524+
525+
assert (ten.add(two) == ten.from_int(12));
526+
assert (ten.sub(two) == ten.from_int(8));
527+
assert (ten.mul(two) == ten.from_int(20));
528+
assert (ten.div(two) == ten.from_int(5));
529+
assert (ten.modulo(two) == ten.from_int(0));
530+
}
531+
532+
test(10.0);
533+
}
534+
535+
504536
//
505537
// Local Variables:
506538
// mode: rust

src/libcore/num.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#[doc="An interface for numbers."]
2+
3+
iface num {
4+
// FIXME: Cross-crate overloading doesn't work yet.
5+
// FIXME: Interface inheritance.
6+
fn add(&&other: self) -> self;
7+
fn sub(&&other: self) -> self;
8+
fn mul(&&other: self) -> self;
9+
fn div(&&other: self) -> self;
10+
fn modulo(&&other: self) -> self;
11+
12+
fn to_int() -> int;
13+
fn from_int(n: int) -> self; // TODO: Static functions.
14+
}
15+

0 commit comments

Comments
 (0)