proximitylib/numerics/
comp.rs

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
use super::f32vector::F32Vector;

// rust Ord trait has some issues
pub trait ApproxComparable {
    fn roughly_matches(&self, instore: &Self, tolerance: f32) -> bool;
}

impl ApproxComparable for f32 {
    fn roughly_matches(&self, target: &f32, tolerance: f32) -> bool {
        (self - target).abs() < tolerance
    }
}

impl<'a> ApproxComparable for F32Vector<'a> {
    fn roughly_matches(&self, target: &F32Vector<'a>, square_tolerance: f32) -> bool {
        self.l2_dist_squared(target) < square_tolerance
    }
}

impl ApproxComparable for i16 {
    fn roughly_matches(&self, instore: &Self, tolerance: f32) -> bool {
        let fself = f32::from(*self);
        let foth = f32::from(*instore);
        fself.roughly_matches(&foth, tolerance)
    }
}