summary refs log tree commit diff
path: root/src/libstd/cmp.rs
blob: a89148ecec9defebe20b1f950ee7b87d34eeb3be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/// 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));
}