ep_core/
math.rs

1use sp_arithmetic::traits::BaseArithmetic;
2pub trait AbsDiff<T> {
3	fn abs_diff(&self, other: T) -> T;
4}
5
6impl<T: BaseArithmetic + Copy> AbsDiff<T> for T {
7	fn abs_diff(&self, other: T) -> T {
8		if *self > other {
9			*self - other
10		} else {
11			other - *self
12		}
13	}
14}
15
16#[cfg(test)]
17mod tests {
18	#[test]
19	fn abs_diff_works() {
20		assert_eq!(12u64.abs_diff(10u64), 2u64);
21		assert_eq!(10u64.abs_diff(13u64), 3u64);
22		assert_eq!(10u64.abs_diff(10u64), 0u64);
23	}
24}