proximipy/numerics/
comp.rs

1use super::f32vector::F32Vector;
2
3// rust Ord trait has some issues
4pub trait ApproxComparable {
5    fn roughly_matches(&self, instore: &Self, tolerance: f32) -> bool;
6}
7
8impl ApproxComparable for f32 {
9    fn roughly_matches(&self, target: &f32, tolerance: f32) -> bool {
10        (self - target).abs() < tolerance
11    }
12}
13
14impl<'a> ApproxComparable for F32Vector<'a> {
15    fn roughly_matches(&self, target: &F32Vector<'a>, square_tolerance: f32) -> bool {
16        self.l2_dist_squared(target) < square_tolerance
17    }
18}
19
20impl ApproxComparable for i16 {
21    fn roughly_matches(&self, instore: &Self, tolerance: f32) -> bool {
22        let fself = f32::from(*self);
23        let foth = f32::from(*instore);
24        fself.roughly_matches(&foth, tolerance)
25    }
26}