From 3a85f3f45e4a0baa101c902f2caa35bce4a7c1ca Mon Sep 17 00:00:00 2001 From: qiuhao <qiuhao951@gmail.com> Date: Sun, 8 Dec 2019 10:45:28 +0800 Subject: [PATCH] add recip for FixedU128 (#61) --- utilities/src/fixed128.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/utilities/src/fixed128.rs b/utilities/src/fixed128.rs index 1de4ab4..d611c11 100644 --- a/utilities/src/fixed128.rs +++ b/utilities/src/fixed128.rs @@ -53,6 +53,11 @@ impl FixedU128 { self.0 } + /// Takes the reciprocal(inverse) of FixedU128, 1/x + pub fn recip(&self) -> Option<Self> { + Self::from_natural(1u128).checked_div(self) + } + /// Checked add. Same semantic to `num_traits::CheckedAdd`. pub fn checked_add(&self, rhs: &Self) -> Option<Self> { self.0.checked_add(rhs.0).map(Self) @@ -290,4 +295,22 @@ mod tests { let ten_percent_perquintill: FixedU128 = Perquintill::from_percent(10).into(); assert_eq!(ten_percent_perquintill.deconstruct(), DIV / 10); } + + #[test] + fn recip_should_work() { + let a = FixedU128::from_natural(2); + assert_eq!(a.recip(), Some(FixedU128::from_rational(1, 2))); + + let a = FixedU128::from_natural(2); + assert_eq!(a.recip().unwrap().checked_mul_int(&4i32), Some(2i32)); + + let a = FixedU128::from_rational(100, 121); + assert_eq!(a.recip(), Some(FixedU128::from_rational(121, 100))); + + let a = FixedU128::from_rational(1, 2); + assert_eq!(a.recip().unwrap().checked_mul(&a), Some(FixedU128::from_natural(1))); + + let a = FixedU128::from_natural(0); + assert_eq!(a.recip(), None); + } } -- GitLab