jagua_rs/util/
fpa.rs

1use std::cmp::Ordering;
2use std::fmt::{Debug, Display};
3
4///Wrapper around the [`float_cmp::approx_eq!()`] macro for easy comparison of floats with a certain tolerance.
5///Two FPAs are considered equal if they are within a certain tolerance of each other.
6#[derive(Debug, Clone, Copy)]
7pub struct FPA(pub f32);
8
9impl<T> From<T> for FPA
10where
11    T: Into<f32>,
12{
13    fn from(n: T) -> Self {
14        FPA(n.into())
15    }
16}
17
18impl PartialEq<Self> for FPA {
19    fn eq(&self, other: &Self) -> bool {
20        float_cmp::approx_eq!(f32, self.0, other.0)
21    }
22}
23
24impl PartialOrd<Self> for FPA {
25    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
26        if self.eq(other) {
27            Some(Ordering::Equal)
28        } else {
29            self.0.partial_cmp(&other.0)
30        }
31    }
32}
33
34impl Display for FPA {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        std::fmt::Display::fmt(&self.0, f)
37    }
38}