Skip to content

Commit 7571ee8

Browse files
committed
Merge branch 'incoming'
2 parents 45cc95f + e158ce8 commit 7571ee8

File tree

9 files changed

+161
-2
lines changed

9 files changed

+161
-2
lines changed

src/libcore/core.rc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export tuple;
4545
export to_str;
4646
export dvec, dvec_iter;
4747
export cmp;
48+
export num;
4849

4950
// NDM seems to be necessary for resolve to work
5051
export option_iter;
@@ -154,6 +155,7 @@ mod tuple;
154155
// Ubiquitous-utility-type modules
155156

156157
mod cmp;
158+
mod num;
157159
mod either;
158160
mod iter;
159161
mod logging;

src/libcore/f32.rs

Lines changed: 14 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,18 @@ 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+
fn neg() -> f32 { ret -self; }
188+
189+
fn to_int() -> int { ret self as int; }
190+
fn from_int(n: int) -> f32 { ret n as f32; }
191+
}
192+
179193
//
180194
// Local Variables:
181195
// mode: rust

src/libcore/f64.rs

Lines changed: 14 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,18 @@ 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+
fn neg() -> f64 { ret -self; }
209+
210+
fn to_int() -> int { ret self as int; }
211+
fn from_int(n: int) -> f64 { ret n as f64; }
212+
}
213+
200214
//
201215
// Local Variables:
202216
// mode: rust

src/libcore/float.rs

Lines changed: 33 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,18 @@ 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+
fn neg() -> float { ret -self; }
420+
421+
fn to_int() -> int { ret self as int; }
422+
fn from_int(n: int) -> float { ret n as float; }
423+
}
424+
411425
#[test]
412426
fn test_from_str() {
413427
assert from_str("3") == some(3.);
@@ -501,6 +515,25 @@ fn test_to_str_inf() {
501515
assert to_str(-infinity, 10u) == "-inf";
502516
}
503517

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

src/libcore/int-template.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import T = inst::T;
22
import cmp::{eq, ord};
3+
import num::num;
34

45
export min_value, max_value;
56
export min, max;
@@ -11,7 +12,7 @@ export range;
1112
export compl;
1213
export abs;
1314
export parse_buf, from_str, to_str, to_str_bytes, str;
14-
export ord, eq;
15+
export ord, eq, num;
1516

1617
const min_value: T = -1 as T << (inst::bits - 1 as T);
1718
const max_value: T = min_value - 1 as T;
@@ -122,6 +123,18 @@ impl eq of eq for T {
122123
}
123124
}
124125

126+
impl num of num for T {
127+
fn add(&&other: T) -> T { ret self + other; }
128+
fn sub(&&other: T) -> T { ret self - other; }
129+
fn mul(&&other: T) -> T { ret self * other; }
130+
fn div(&&other: T) -> T { ret self / other; }
131+
fn modulo(&&other: T) -> T { ret self % other; }
132+
fn neg() -> T { ret -self; }
133+
134+
fn to_int() -> int { ret self as int; }
135+
fn from_int(n: int) -> T { ret n as T; }
136+
}
137+
125138

126139
// FIXME: Has alignment issues on windows and 32-bit linux
127140
#[test]
@@ -179,3 +192,22 @@ fn test_to_str() {
179192
assert (eq(to_str(127 as T, 16u), "7f"));
180193
assert (eq(to_str(100 as T, 10u), "100"));
181194
}
195+
196+
#[test]
197+
fn test_ifaces() {
198+
fn test<U:num>(ten: U) {
199+
assert (ten.to_int() == 10);
200+
201+
let two = ten.from_int(2);
202+
assert (two.to_int() == 2);
203+
204+
assert (ten.add(two) == ten.from_int(12));
205+
assert (ten.sub(two) == ten.from_int(8));
206+
assert (ten.mul(two) == ten.from_int(20));
207+
assert (ten.div(two) == ten.from_int(5));
208+
assert (ten.modulo(two) == ten.from_int(0));
209+
}
210+
211+
test(10 as T);
212+
}
213+

src/libcore/num.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
fn neg() -> self;
12+
13+
fn to_int() -> int;
14+
fn from_int(n: int) -> self; // TODO: Static functions.
15+
}
16+

src/libcore/uint-template.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import T = inst::T;
22
import cmp::{eq, ord};
3+
import num::num;
34

45
export min_value, max_value;
56
export min, max;
@@ -11,7 +12,7 @@ export range;
1112
export compl;
1213
export to_str, to_str_bytes;
1314
export from_str, from_str_radix, str, parse_buf;
14-
export ord, eq;
15+
export ord, eq, num;
1516

1617
const min_value: T = 0 as T;
1718
const max_value: T = 0 as T - 1 as T;
@@ -63,6 +64,18 @@ impl eq of eq for T {
6364
}
6465
}
6566

67+
impl num of num for T {
68+
fn add(&&other: T) -> T { ret self + other; }
69+
fn sub(&&other: T) -> T { ret self - other; }
70+
fn mul(&&other: T) -> T { ret self * other; }
71+
fn div(&&other: T) -> T { ret self / other; }
72+
fn modulo(&&other: T) -> T { ret self % other; }
73+
fn neg() -> T { ret -self; }
74+
75+
fn to_int() -> int { ret self as int; }
76+
fn from_int(n: int) -> T { ret n as T; }
77+
}
78+
6679
#[doc = "
6780
Parse a buffer of bytes
6881

src/libstd/cmp.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#[doc="Additional general-purpose comparison functionality."]
2+
3+
const fuzzy_epsilon: float = 1.0e-6;
4+
5+
iface fuzzy_eq {
6+
fn fuzzy_eq(&&other: self) -> bool;
7+
}
8+
9+
impl fuzzy_eq of fuzzy_eq for float {
10+
fn fuzzy_eq(&&other: float) -> bool {
11+
ret float::abs(self - other) < fuzzy_epsilon;
12+
}
13+
}
14+
15+
impl fuzzy_eq of fuzzy_eq for f32 {
16+
fn fuzzy_eq(&&other: f32) -> bool {
17+
ret f32::abs(self - other) < (fuzzy_epsilon as f32);
18+
}
19+
}
20+
21+
impl fuzzy_eq of fuzzy_eq for f64 {
22+
fn fuzzy_eq(&&other: f64) -> bool {
23+
ret f64::abs(self - other) < (fuzzy_epsilon as f64);
24+
}
25+
}
26+
27+
#[test]
28+
fn test_fuzzy_equals() {
29+
assert ((1.0).fuzzy_eq(1.0));
30+
assert ((1.0f32).fuzzy_eq(1.0f32));
31+
assert ((1.0f64).fuzzy_eq(1.0f64));
32+
}
33+

src/libstd/std.rc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export bitv, deque, fun_treemap, list, map, smallintmap, sort, treemap;
2222
export rope, arena, arc, par;
2323
export ebml, dbg, getopts, json, rand, sha1, term, time, prettyprint;
2424
export test, tempfile, serialization;
25+
export cmp;
2526

2627
// General io and system-services modules
2728

@@ -70,6 +71,7 @@ mod prettyprint;
7071
mod arena;
7172
mod arc;
7273
mod par;
74+
mod cmp;
7375

7476
#[cfg(unicode)]
7577
mod unicode;

0 commit comments

Comments
 (0)