summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2012-06-07 18:12:50 -0700
committerPatrick Walton <pcwalton@mimiga.net>2012-06-07 18:12:50 -0700
commitd8b113f209006e595e13c1baa1557c1fbedad46d (patch)
tree93a718aa0c9b5e163ce752034f0653568a5898cf /src/libstd
parent02b7089e15516fe759edfda1eed59824d7879378 (diff)
downloadrust-d8b113f209006e595e13c1baa1557c1fbedad46d.tar.gz
rust-d8b113f209006e595e13c1baa1557c1fbedad46d.zip
std: Implement a fuzzy-equal interface
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/cmp.rs33
-rw-r--r--src/libstd/std.rc2
2 files changed, 35 insertions, 0 deletions
diff --git a/src/libstd/cmp.rs b/src/libstd/cmp.rs
new file mode 100644
index 00000000000..f94c0a7c6af
--- /dev/null
+++ b/src/libstd/cmp.rs
@@ -0,0 +1,33 @@
+#[doc="Additional general-purpose comparison functionality."]
+
+const fuzzy_epsilon: float = 1.0e-6;
+
+iface fuzzy_eq {
+    fn fuzzy_eq(&&other: self) -> bool;
+}
+
+impl fuzzy_eq of fuzzy_eq for float {
+    fn fuzzy_eq(&&other: float) -> bool {
+        ret float::abs(self - other) < fuzzy_epsilon;
+    }
+}
+
+impl fuzzy_eq of fuzzy_eq for f32 {
+    fn fuzzy_eq(&&other: f32) -> bool {
+        ret f32::abs(self - other) < (fuzzy_epsilon as f32);
+    }
+}
+
+impl fuzzy_eq of fuzzy_eq for f64 {
+    fn fuzzy_eq(&&other: f64) -> bool {
+        ret f64::abs(self - other) < (fuzzy_epsilon as f64);
+    }
+}
+
+#[test]
+fn test_fuzzy_equals() {
+    assert ((1.0).fuzzy_eq(1.0));
+    assert ((1.0f32).fuzzy_eq(1.0f32));
+    assert ((1.0f64).fuzzy_eq(1.0f64));
+}
+
diff --git a/src/libstd/std.rc b/src/libstd/std.rc
index 8d2e0732833..10b008d07c8 100644
--- a/src/libstd/std.rc
+++ b/src/libstd/std.rc
@@ -22,6 +22,7 @@ export bitv, deque, fun_treemap, list, map, smallintmap, sort, treemap;
 export rope, arena, arc, par;
 export ebml, dbg, getopts, json, rand, sha1, term, time, prettyprint;
 export test, tempfile, serialization;
+export cmp;
 
 // General io and system-services modules
 
@@ -70,6 +71,7 @@ mod prettyprint;
 mod arena;
 mod arc;
 mod par;
+mod cmp;
 
 #[cfg(unicode)]
 mod unicode;