From ae6a33f4b84f4d5e8c867d4a081215292e0ee967 Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Wed, 4 Apr 2018 18:12:30 -0400 Subject: [PATCH] Support negation for signed numbers. --- src/cryptonum/signed.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/cryptonum/signed.rs b/src/cryptonum/signed.rs index 1a46c51..1fd0e28 100644 --- a/src/cryptonum/signed.rs +++ b/src/cryptonum/signed.rs @@ -88,6 +88,30 @@ impl Ord for SCN { // //------------------------------------------------------------------------------ +impl Neg for SCN { + type Output = SCN; + + fn neg(self) -> SCN { + if self.is_zero() { + self + } else { + SCN{ negative: !self.negative, value: self.value } + } + } +} + +impl<'a> Neg for &'a SCN { + type Output = SCN; + + fn neg(self) -> SCN { + if self.is_zero() { + self.clone() + } else { + SCN{ negative: !self.negative, value: self.value.clone() } + } + } +} + impl<'a> AddAssign<&'a SCN> for SCN { fn add_assign(&mut self, rhs: &SCN) { if self.negative == rhs.negative {