Skip to content

Commit

Permalink
changed data into raw_units, added from_raw_units, changed all functi…
Browse files Browse the repository at this point in the history
…ons to use raw units
  • Loading branch information
mihaicalinluca committed Dec 7, 2023
1 parent 26db4ef commit 37d2535
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
28 changes: 14 additions & 14 deletions framework/base/src/types/managed/wrapped/fixed_point_biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ impl<M: ManagedTypeApi, const DECIMALS: usize> FixedPoint<M, DECIMALS> {
&self.data / &Self::scaling_factor()
}

pub fn data(&self) -> &BigUint<M> {
pub fn into_raw_units(&self) -> &BigUint<M> {
&self.data
}

pub fn from_raw_units(data: BigUint<M>) -> Self {
FixedPoint { data }
}
}

impl<M: ManagedTypeApi, const DECIMALS: usize> From<BigUint<M>> for FixedPoint<M, DECIMALS> {
Expand Down Expand Up @@ -48,8 +52,9 @@ impl<M: ManagedTypeApi, const DECIMALS: usize, const OTHER_DECIMALS: usize>
Convert<M, OTHER_DECIMALS, DECIMALS> for FixedPoint<M, DECIMALS>
{
fn convert_from(other: FixedPoint<M, OTHER_DECIMALS>) -> FixedPoint<M, DECIMALS> {
FixedPoint::<M, DECIMALS>::from(
other.data / FixedPoint::<M, OTHER_DECIMALS>::scaling_factor(),
FixedPoint::<M, DECIMALS>::from_raw_units(
other.data / FixedPoint::<M, OTHER_DECIMALS>::scaling_factor()
* FixedPoint::<M, DECIMALS>::scaling_factor(),
)
}
}
Expand All @@ -60,7 +65,7 @@ impl<M: ManagedTypeApi, const DECIMALS: usize> Add<FixedPoint<M, DECIMALS>>
type Output = Self;

fn add(self, other: FixedPoint<M, DECIMALS>) -> Self::Output {
FixedPoint::<M, DECIMALS>::from((self.data + other.data) / &Self::scaling_factor())
FixedPoint::<M, DECIMALS>::from_raw_units(self.data + other.data)
}
}

Expand All @@ -70,10 +75,11 @@ impl<M: ManagedTypeApi, const DECIMALS: usize> Sub<FixedPoint<M, DECIMALS>>
type Output = Self;

fn sub(self, other: FixedPoint<M, DECIMALS>) -> Self::Output {
FixedPoint::<M, DECIMALS>::from((self.data - other.data) / &Self::scaling_factor())
FixedPoint::<M, DECIMALS>::from_raw_units(self.data - other.data)
}
}

#[allow(clippy::suspicious_arithmetic_impl)]
impl<M: ManagedTypeApi, const DECIMALS: usize, const OTHER_DECIMALS: usize>
Mul<FixedPoint<M, OTHER_DECIMALS>> for FixedPoint<M, DECIMALS>
where
Expand All @@ -82,13 +88,11 @@ where
type Output = FixedPoint<M, { DECIMALS + OTHER_DECIMALS }>;

fn mul(self, other: FixedPoint<M, OTHER_DECIMALS>) -> Self::Output {
FixedPoint::<M, { DECIMALS + OTHER_DECIMALS }>::from(
self.data * other.data
/ FixedPoint::<M, { DECIMALS + OTHER_DECIMALS }>::scaling_factor(),
)
FixedPoint::<M, { DECIMALS + OTHER_DECIMALS }>::from_raw_units(self.data * other.data)
}
}

#[allow(clippy::suspicious_arithmetic_impl)]
impl<M: ManagedTypeApi, const DECIMALS: usize, const OTHER_DECIMALS: usize>
Div<FixedPoint<M, OTHER_DECIMALS>> for FixedPoint<M, DECIMALS>
where
Expand All @@ -97,11 +101,7 @@ where
type Output = FixedPoint<M, { DECIMALS - OTHER_DECIMALS }>;

fn div(self, other: FixedPoint<M, OTHER_DECIMALS>) -> Self::Output {
FixedPoint::<M, { DECIMALS - OTHER_DECIMALS }>::from(
self.data
/ other.data
/ FixedPoint::<M, { DECIMALS - OTHER_DECIMALS }>::scaling_factor(),
)
FixedPoint::<M, { DECIMALS - OTHER_DECIMALS }>::from_raw_units(self.data / other.data)
}
}

Expand Down
2 changes: 1 addition & 1 deletion framework/scenario/tests/fixed_point_biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn test_addition() {
addition,
FixedPoint::<StaticApi, 2usize>::from(BigUint::from(6u64))
);
assert_eq!(addition.data(), &BigUint::from(600u64));
assert_eq!(addition.into_raw_units(), &BigUint::from(600u64));
assert_eq!(addition.trunc(), BigUint::from(6u64));

let subtraction = addition - fixed;
Expand Down

0 comments on commit 37d2535

Please sign in to comment.