Skip to content

Commit d8b113f

Browse files
committed
std: Implement a fuzzy-equal interface
1 parent 02b7089 commit d8b113f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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)