Implement arithmetic operations for decimal demo.
This commit is contained in:
parent
6daedfe5d4
commit
cf5834a511
@ -55,24 +55,61 @@ impl<const E:u8> std::ops::Add for Decimal<E> {
|
|||||||
Decimal::<E>::from_raw(self.data + rhs.data)
|
Decimal::<E>::from_raw(self.data + rhs.data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl<const E:u8> std::ops::AddAssign for Decimal<E> {
|
||||||
|
fn add_assign(&mut self, rhs: Self) {
|
||||||
|
self.data += rhs.data;
|
||||||
|
}
|
||||||
|
}
|
||||||
impl<const E:u8> std::ops::Sub for Decimal<E> {
|
impl<const E:u8> std::ops::Sub for Decimal<E> {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn sub(self, rhs: Self) -> Self::Output {
|
fn sub(self, rhs: Self) -> Self::Output {
|
||||||
Decimal::<E>::from_raw(self.data - rhs.data)
|
Decimal::<E>::from_raw(self.data - rhs.data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl<const E:u8> std::ops::SubAssign for Decimal<E> {
|
||||||
|
fn sub_assign(&mut self, rhs: Self) {
|
||||||
|
self.data -= rhs.data;
|
||||||
|
}
|
||||||
|
}
|
||||||
impl<const E:u8> std::ops::Mul for Decimal<E> {
|
impl<const E:u8> std::ops::Mul for Decimal<E> {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn mul(self, rhs: Self) -> Self::Output {
|
fn mul(self, rhs: Self) -> Self::Output {
|
||||||
Decimal::<E>::from_raw(((self.data as i128 * rhs.data as i128) >> E) as i64)
|
Decimal::<E>::from_raw(((self.data as i128 * rhs.data as i128) >> E) as i64)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl<const E:u8> std::ops::MulAssign for Decimal<E> {
|
||||||
|
fn mul_assign(&mut self, rhs: Self) {
|
||||||
|
self.data = ((self.data as i128 * rhs.data as i128) >> E) as i64;
|
||||||
|
}
|
||||||
|
}
|
||||||
impl<const E:u8> std::ops::Div for Decimal<E> {
|
impl<const E:u8> std::ops::Div for Decimal<E> {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn div(self, rhs: Self) -> Self::Output {
|
fn div(self, rhs: Self) -> Self::Output {
|
||||||
Decimal::<E>::from_raw(((self.data as i128 * (1i64 << E) as i128) / rhs.data as i128) as i64)
|
Decimal::<E>::from_raw(((self.data as i128 * (1i64 << E) as i128) / rhs.data as i128) as i64)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl<const E:u8> std::ops::DivAssign for Decimal<E> {
|
||||||
|
fn div_assign(&mut self, rhs: Self) {
|
||||||
|
self.data = ((self.data as i128 * (1i64 << E) as i128) / rhs.data as i128) as i64;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<const E:u8> std::ops::Rem for Decimal<E> {
|
||||||
|
type Output = Self;
|
||||||
|
fn rem(self, rhs: Self) -> Self::Output {
|
||||||
|
Decimal::<E>::from_raw(((self.data as i128 * (1i64 << E) as i128) % rhs.data as i128) as i64)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<const E:u8> std::ops::RemAssign for Decimal<E> {
|
||||||
|
fn rem_assign(&mut self, rhs: Self) {
|
||||||
|
self.data = ((self.data as i128 * (1i64 << E) as i128) % rhs.data as i128) as i64;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<const E:u8> std::ops::Neg for Decimal<E> {
|
||||||
|
type Output = Self;
|
||||||
|
fn neg(self) -> Self::Output {
|
||||||
|
Decimal::<E>::from_raw(-self.data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn print_bytes(bytes:&Vec<u8>)
|
fn print_bytes(bytes:&Vec<u8>)
|
||||||
{
|
{
|
||||||
@ -83,12 +120,14 @@ fn print_bytes(bytes:&Vec<u8>)
|
|||||||
|
|
||||||
fn main()
|
fn main()
|
||||||
{
|
{
|
||||||
let a = Decimal::<16>::with(1.0);
|
let a = Decimal::<16>::with(5.0);
|
||||||
let b = Decimal::<16>::with(1.0);
|
let b = Decimal::<16>::with(3.0);
|
||||||
println!("a = {}", a.f64());
|
println!("a = {}", a.f64());
|
||||||
println!("b = {}", b.f64());
|
println!("b = {}", b.f64());
|
||||||
println!("add = {}", (a + b).f64());
|
println!("add = {}", (a + b).f64());
|
||||||
println!("sub = {}", (a - b).f64());
|
println!("sub = {}", (a - b).f64());
|
||||||
println!("mul = {}", (a * b).f64());
|
println!("mul = {}", (a * b).f64());
|
||||||
println!("div = {}", (a / b).f64());
|
println!("div = {}", (a / b).f64());
|
||||||
|
println!("rem = {}", (a % b).f64());
|
||||||
|
println!("neg = {}", (-a).f64());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user