-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b87b90
commit 4f69193
Showing
7 changed files
with
291 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_op_add_signed.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use crate::{ | ||
api::ManagedTypeApi, | ||
types::{ConstDecimals, Decimals, ManagedDecimalSigned, NumDecimals}, | ||
}; | ||
|
||
use core::ops::{Add, AddAssign}; | ||
|
||
impl<M: ManagedTypeApi, D1: Decimals, D2: Decimals> AddAssign<&ManagedDecimalSigned<M, D2>> | ||
for ManagedDecimalSigned<M, D1> | ||
{ | ||
fn add_assign(&mut self, rhs: &ManagedDecimalSigned<M, D2>) { | ||
let scaled_data = rhs.rescale_data(self.scale().num_decimals()); | ||
self.data += scaled_data; | ||
} | ||
} | ||
|
||
impl<M: ManagedTypeApi, D1: Decimals, D2: Decimals> AddAssign<ManagedDecimalSigned<M, D2>> | ||
for ManagedDecimalSigned<M, D1> | ||
{ | ||
#[inline] | ||
fn add_assign(&mut self, rhs: ManagedDecimalSigned<M, D2>) { | ||
self.add_assign(&rhs); | ||
} | ||
} | ||
|
||
// const + const | ||
impl<M: ManagedTypeApi, const DECIMALS: NumDecimals> | ||
Add<ManagedDecimalSigned<M, ConstDecimals<DECIMALS>>> | ||
for ManagedDecimalSigned<M, ConstDecimals<DECIMALS>> | ||
{ | ||
type Output = Self; | ||
|
||
fn add(mut self, rhs: ManagedDecimalSigned<M, ConstDecimals<DECIMALS>>) -> Self::Output { | ||
self.data += rhs.data; | ||
self | ||
} | ||
} | ||
|
||
// var + var | ||
impl<M: ManagedTypeApi> Add<ManagedDecimalSigned<M, NumDecimals>> | ||
for ManagedDecimalSigned<M, NumDecimals> | ||
{ | ||
type Output = Self; | ||
|
||
fn add(mut self, rhs: ManagedDecimalSigned<M, NumDecimals>) -> Self::Output { | ||
match self.decimals.cmp(&rhs.decimals) { | ||
core::cmp::Ordering::Less => { | ||
self = self.rescale(rhs.decimals); | ||
self.data += rhs.data; | ||
}, | ||
core::cmp::Ordering::Equal => self.data += rhs.data, | ||
core::cmp::Ordering::Greater => { | ||
let rhs_data = rhs.rescale_data(self.decimals); | ||
self.data += rhs_data; | ||
}, | ||
} | ||
self | ||
} | ||
} | ||
|
||
// var + const | ||
impl<const DECIMALS: usize, M: ManagedTypeApi> Add<ManagedDecimalSigned<M, ConstDecimals<DECIMALS>>> | ||
for ManagedDecimalSigned<M, NumDecimals> | ||
{ | ||
type Output = ManagedDecimalSigned<M, NumDecimals>; | ||
|
||
fn add(self, rhs: ManagedDecimalSigned<M, ConstDecimals<DECIMALS>>) -> Self::Output { | ||
self + rhs.into_var_decimals() | ||
} | ||
} | ||
|
||
// const + var | ||
impl<const DECIMALS: usize, M: ManagedTypeApi> Add<ManagedDecimalSigned<M, NumDecimals>> | ||
for ManagedDecimalSigned<M, ConstDecimals<DECIMALS>> | ||
{ | ||
type Output = ManagedDecimalSigned<M, NumDecimals>; | ||
|
||
fn add(self, rhs: ManagedDecimalSigned<M, NumDecimals>) -> Self::Output { | ||
self.into_var_decimals() + rhs | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_op_div_signed.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::{ | ||
api::ManagedTypeApi, | ||
types::{BigUint, Decimals, ManagedDecimalSigned, NumDecimals}, | ||
}; | ||
|
||
use core::ops::{Div, DivAssign, Sub}; | ||
|
||
impl<M: ManagedTypeApi, D1: Decimals, D2: Decimals> DivAssign<&ManagedDecimalSigned<M, D2>> | ||
for ManagedDecimalSigned<M, D1> | ||
{ | ||
fn div_assign(&mut self, rhs: &ManagedDecimalSigned<M, D2>) { | ||
self.data *= rhs.scaling_factor().as_big_int(); | ||
self.data /= &rhs.data; | ||
} | ||
} | ||
|
||
impl<M: ManagedTypeApi, D1: Decimals, D2: Decimals> DivAssign<ManagedDecimalSigned<M, D2>> | ||
for ManagedDecimalSigned<M, D1> | ||
{ | ||
#[inline] | ||
fn div_assign(&mut self, rhs: ManagedDecimalSigned<M, D2>) { | ||
self.div_assign(&rhs); | ||
} | ||
} | ||
|
||
impl<M: ManagedTypeApi, D: Decimals> Div<NumDecimals> for ManagedDecimalSigned<M, D> { | ||
type Output = Self; | ||
|
||
fn div(self, other: NumDecimals) -> Self::Output { | ||
ManagedDecimalSigned { | ||
data: self.data / BigUint::from(other), | ||
decimals: self.decimals, | ||
} | ||
} | ||
} | ||
|
||
impl<M: ManagedTypeApi, D1: Decimals, D2: Decimals> Div<ManagedDecimalSigned<M, D2>> | ||
for ManagedDecimalSigned<M, D1> | ||
where | ||
D1: Sub<D2>, | ||
<D1 as Sub<D2>>::Output: Decimals, | ||
{ | ||
type Output = ManagedDecimalSigned<M, <D1 as Sub<D2>>::Output>; | ||
|
||
fn div(self, other: ManagedDecimalSigned<M, D2>) -> Self::Output { | ||
ManagedDecimalSigned { | ||
data: self.data / other.data, | ||
decimals: self.decimals - other.decimals, | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_op_mul_signed.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use crate::{ | ||
api::ManagedTypeApi, | ||
types::{Decimals, ManagedDecimalSigned}, | ||
}; | ||
|
||
use core::ops::{Add, Mul, MulAssign}; | ||
|
||
impl<M: ManagedTypeApi, D1: Decimals, D2: Decimals> MulAssign<&ManagedDecimalSigned<M, D2>> | ||
for ManagedDecimalSigned<M, D1> | ||
{ | ||
fn mul_assign(&mut self, rhs: &ManagedDecimalSigned<M, D2>) { | ||
self.data *= &rhs.data; | ||
self.data /= rhs.scaling_factor().as_big_int(); | ||
} | ||
} | ||
|
||
impl<M: ManagedTypeApi, D1: Decimals, D2: Decimals> MulAssign<ManagedDecimalSigned<M, D2>> | ||
for ManagedDecimalSigned<M, D1> | ||
{ | ||
#[inline] | ||
fn mul_assign(&mut self, rhs: ManagedDecimalSigned<M, D2>) { | ||
self.mul_assign(&rhs); | ||
} | ||
} | ||
|
||
impl<M: ManagedTypeApi, D1: Decimals, D2: Decimals> Mul<ManagedDecimalSigned<M, D2>> | ||
for ManagedDecimalSigned<M, D1> | ||
where | ||
D1: Add<D2>, | ||
<D1 as Add<D2>>::Output: Decimals, | ||
{ | ||
type Output = ManagedDecimalSigned<M, <D1 as Add<D2>>::Output>; | ||
|
||
fn mul(self, other: ManagedDecimalSigned<M, D2>) -> Self::Output { | ||
ManagedDecimalSigned { | ||
data: self.data * other.data, | ||
decimals: self.decimals + other.decimals, | ||
} | ||
} | ||
} | ||
|
||
impl<M: ManagedTypeApi, D1: Decimals> ManagedDecimalSigned<M, D1> { | ||
pub fn mul_with_precision<D2: Decimals, T: Decimals>( | ||
self, | ||
other: ManagedDecimalSigned<M, D2>, | ||
precision: T, | ||
) -> ManagedDecimalSigned<M, T> { | ||
let result = ManagedDecimalSigned { | ||
data: self.data * other.data, | ||
decimals: self.decimals.num_decimals() + other.decimals.num_decimals(), | ||
}; | ||
result.rescale(precision) | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
framework/base/src/types/managed/wrapped/managed_decimal/managed_decimal_op_sub_signed.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use crate::{ | ||
api::ManagedTypeApi, | ||
types::{ConstDecimals, Decimals, ManagedDecimalSigned, NumDecimals}, | ||
}; | ||
|
||
use core::ops::{Sub, SubAssign}; | ||
|
||
impl<M: ManagedTypeApi, D1: Decimals, D2: Decimals> SubAssign<&ManagedDecimalSigned<M, D2>> | ||
for ManagedDecimalSigned<M, D1> | ||
{ | ||
fn sub_assign(&mut self, rhs: &ManagedDecimalSigned<M, D2>) { | ||
let scaled_data = rhs.rescale_data(self.scale().num_decimals()); | ||
self.data -= scaled_data; | ||
} | ||
} | ||
|
||
impl<M: ManagedTypeApi, D1: Decimals, D2: Decimals> SubAssign<ManagedDecimalSigned<M, D2>> | ||
for ManagedDecimalSigned<M, D1> | ||
{ | ||
#[inline] | ||
fn sub_assign(&mut self, rhs: ManagedDecimalSigned<M, D2>) { | ||
self.sub_assign(&rhs); | ||
} | ||
} | ||
|
||
// const + const | ||
impl<M: ManagedTypeApi, const DECIMALS: NumDecimals> | ||
Sub<ManagedDecimalSigned<M, ConstDecimals<DECIMALS>>> | ||
for ManagedDecimalSigned<M, ConstDecimals<DECIMALS>> | ||
{ | ||
type Output = Self; | ||
|
||
fn sub(mut self, rhs: ManagedDecimalSigned<M, ConstDecimals<DECIMALS>>) -> Self::Output { | ||
self.data -= rhs.data; | ||
self | ||
} | ||
} | ||
|
||
// var + var | ||
impl<M: ManagedTypeApi> Sub<ManagedDecimalSigned<M, NumDecimals>> | ||
for ManagedDecimalSigned<M, NumDecimals> | ||
{ | ||
type Output = Self; | ||
|
||
fn sub(mut self, rhs: ManagedDecimalSigned<M, NumDecimals>) -> Self::Output { | ||
match self.decimals.cmp(&rhs.decimals) { | ||
core::cmp::Ordering::Less => { | ||
self = self.rescale(rhs.decimals); | ||
self.data -= rhs.data; | ||
}, | ||
core::cmp::Ordering::Equal => self.data -= rhs.data, | ||
core::cmp::Ordering::Greater => { | ||
let rhs_data = rhs.rescale_data(self.decimals); | ||
self.data -= rhs_data; | ||
}, | ||
} | ||
self | ||
} | ||
} | ||
|
||
// var + const | ||
impl<const DECIMALS: usize, M: ManagedTypeApi> Sub<ManagedDecimalSigned<M, ConstDecimals<DECIMALS>>> | ||
for ManagedDecimalSigned<M, NumDecimals> | ||
{ | ||
type Output = ManagedDecimalSigned<M, NumDecimals>; | ||
|
||
fn sub(self, rhs: ManagedDecimalSigned<M, ConstDecimals<DECIMALS>>) -> Self::Output { | ||
self - rhs.into_var_decimals() | ||
} | ||
} | ||
|
||
// const + var | ||
impl<const DECIMALS: usize, M: ManagedTypeApi> Sub<ManagedDecimalSigned<M, NumDecimals>> | ||
for ManagedDecimalSigned<M, ConstDecimals<DECIMALS>> | ||
{ | ||
type Output = ManagedDecimalSigned<M, NumDecimals>; | ||
|
||
fn sub(self, rhs: ManagedDecimalSigned<M, NumDecimals>) -> Self::Output { | ||
self.into_var_decimals() - rhs | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters