eryon_core/ops/
difference.rs

1/*
2    Appellation: difference <module>
3    Contrib: @FL03
4*/
5
6/// a trait for computing the percent difference between two values where the caller is
7/// considered the original value
8pub trait PercentDifference<Rhs = Self> {
9    type Output;
10
11    /// Computes the percent difference between two values.
12    fn percent_diff(self, rhs: Rhs) -> Self::Output;
13}
14
15impl<A, B, C> PercentDifference<B> for A
16where
17    A: Clone,
18    B: core::ops::Sub<A, Output = C>,
19    C: core::ops::Div<A, Output = C>,
20{
21    type Output = C;
22
23    fn percent_diff(self, rhs: B) -> Self::Output {
24        (rhs - self.clone()) / self
25    }
26}