Skip to content

Commit

Permalink
Merge pull request #1087 from CosmWasm/Uint128-fraction
Browse files Browse the repository at this point in the history
Update Fraction implementation in Decimal to Uint128
  • Loading branch information
webmaster128 authored Sep 9, 2021
2 parents 6b57f92 + 14da462 commit 6867ac6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ and this project adheres to
`do_instantiate`, `do_execute`, `do_migrate`, `do_sudo`, `do_reply`,
`do_query`, `ibc_channel_open`, `ibc_channel_connect`, `ibc_channel_close`,
`ibc_packet_receive`, `ibc_packet_ack` and `ibc_packet_timeout`.
- cosmwasm-std: In `Decimal` change `Fraction<u128>` to `Fraction<Uint128>`,
such that `Decimal::numerator` and `::denominator` now return `Uint128`.

### Removed

Expand Down
20 changes: 13 additions & 7 deletions packages/std/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ impl Decimal {
}
}

impl Fraction<u128> for Decimal {
impl Fraction<Uint128> for Decimal {
#[inline]
fn numerator(&self) -> u128 {
self.0.u128()
fn numerator(&self) -> Uint128 {
self.0
}

#[inline]
fn denominator(&self) -> u128 {
Self::DECIMAL_FRACTIONAL.u128()
fn denominator(&self) -> Uint128 {
Self::DECIMAL_FRACTIONAL
}

/// Returns the multiplicative inverse `1/d` for decimal `d`.
Expand Down Expand Up @@ -360,8 +360,14 @@ mod tests {
#[test]
fn decimal_implements_fraction() {
let fraction = Decimal::from_str("1234.567").unwrap();
assert_eq!(fraction.numerator(), 1_234_567_000_000_000_000_000u128);
assert_eq!(fraction.denominator(), 1_000_000_000_000_000_000u128);
assert_eq!(
fraction.numerator(),
Uint128::from(1_234_567_000_000_000_000_000u128)
);
assert_eq!(
fraction.denominator(),
Uint128::from(1_000_000_000_000_000_000u128)
);
}

#[test]
Expand Down

0 comments on commit 6867ac6

Please sign in to comment.