diff --git a/src/bin/decimal.rs b/src/bin/decimal.rs index 0e40789..5c0bf79 100644 --- a/src/bin/decimal.rs +++ b/src/bin/decimal.rs @@ -55,24 +55,61 @@ impl std::ops::Add for Decimal { Decimal::::from_raw(self.data + rhs.data) } } +impl std::ops::AddAssign for Decimal { + fn add_assign(&mut self, rhs: Self) { + self.data += rhs.data; + } +} impl std::ops::Sub for Decimal { type Output = Self; fn sub(self, rhs: Self) -> Self::Output { Decimal::::from_raw(self.data - rhs.data) } } +impl std::ops::SubAssign for Decimal { + fn sub_assign(&mut self, rhs: Self) { + self.data -= rhs.data; + } +} impl std::ops::Mul for Decimal { type Output = Self; fn mul(self, rhs: Self) -> Self::Output { Decimal::::from_raw(((self.data as i128 * rhs.data as i128) >> E) as i64) } } +impl std::ops::MulAssign for Decimal { + fn mul_assign(&mut self, rhs: Self) { + self.data = ((self.data as i128 * rhs.data as i128) >> E) as i64; + } +} impl std::ops::Div for Decimal { type Output = Self; fn div(self, rhs: Self) -> Self::Output { Decimal::::from_raw(((self.data as i128 * (1i64 << E) as i128) / rhs.data as i128) as i64) } } +impl std::ops::DivAssign for Decimal { + fn div_assign(&mut self, rhs: Self) { + self.data = ((self.data as i128 * (1i64 << E) as i128) / rhs.data as i128) as i64; + } +} +impl std::ops::Rem for Decimal { + type Output = Self; + fn rem(self, rhs: Self) -> Self::Output { + Decimal::::from_raw(((self.data as i128 * (1i64 << E) as i128) % rhs.data as i128) as i64) + } +} +impl std::ops::RemAssign for Decimal { + fn rem_assign(&mut self, rhs: Self) { + self.data = ((self.data as i128 * (1i64 << E) as i128) % rhs.data as i128) as i64; + } +} +impl std::ops::Neg for Decimal { + type Output = Self; + fn neg(self) -> Self::Output { + Decimal::::from_raw(-self.data) + } +} fn print_bytes(bytes:&Vec) { @@ -83,12 +120,14 @@ fn print_bytes(bytes:&Vec) fn main() { - let a = Decimal::<16>::with(1.0); - let b = Decimal::<16>::with(1.0); + let a = Decimal::<16>::with(5.0); + let b = Decimal::<16>::with(3.0); println!("a = {}", a.f64()); println!("b = {}", b.f64()); println!("add = {}", (a + b).f64()); println!("sub = {}", (a - b).f64()); println!("mul = {}", (a * b).f64()); println!("div = {}", (a / b).f64()); + println!("rem = {}", (a % b).f64()); + println!("neg = {}", (-a).f64()); }