Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to
### Added

- cosmwasm-std: Add `Uint{64,128,256,512}::one`.
- cosmwasm-std: Add `Uint{64,128,256,512}::abs_diff` and
`Decimal{,256}::abs_diff` ([#1334]).

[#1334]: https://github.com/CosmWasm/cosmwasm/pull/1334

## [1.0.0] - 2022-05-14

Expand Down
13 changes: 13 additions & 0 deletions packages/std/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ impl Decimal {
Decimal(inner.isqrt().checked_mul(Uint128::from(outer_mul)).unwrap())
})
}

pub const fn abs_diff(self, other: Self) -> Self {
Self(self.0.abs_diff(other.0))
}
}

impl Fraction<Uint128> for Decimal {
Expand Down Expand Up @@ -1664,4 +1668,13 @@ mod tests {
Decimal::percent(8765)
);
}

#[test]
fn decimal_abs_diff_works() {
let a = Decimal::percent(285);
let b = Decimal::percent(200);
let expected = Decimal::percent(85);
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}
}
17 changes: 17 additions & 0 deletions packages/std/src/math/decimal256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@ impl Decimal256 {
Self(inner.isqrt().checked_mul(outer_mul).unwrap())
})
}

pub fn abs_diff(self, other: Self) -> Self {
if self < other {
other - self
} else {
self - other
}
}
}

impl Fraction<Uint256> for Decimal256 {
Expand Down Expand Up @@ -1783,4 +1791,13 @@ mod tests {
Decimal256::percent(8765)
);
}

#[test]
fn decimal256_abs_diff_works() {
let a = Decimal256::percent(285);
let b = Decimal256::percent(200);
let expected = Decimal256::percent(85);
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}
}
17 changes: 17 additions & 0 deletions packages/std/src/math/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ impl Uint128 {
pub fn saturating_pow(self, other: u32) -> Self {
Self(self.0.saturating_pow(other))
}

pub const fn abs_diff(self, other: Self) -> Self {
Self(if self.0 < other.0 {
other.0 - self.0
} else {
self.0 - other.0
})
}
}

// `From<u{128,64,32,16,8}>` is implemented manually instead of
Expand Down Expand Up @@ -965,4 +973,13 @@ mod tests {
a %= &b;
assert_eq!(a, Uint128::from(1u32));
}

#[test]
fn uint128_abs_diff_works() {
let a = Uint128::from(42u32);
let b = Uint128::from(5u32);
let expected = Uint128::from(37u32);
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}
}
17 changes: 17 additions & 0 deletions packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,14 @@ impl Uint256 {
pub fn saturating_mul(self, other: Self) -> Self {
Self(self.0.saturating_mul(other.0))
}

pub fn abs_diff(self, other: Self) -> Self {
if self < other {
other - self
} else {
self - other
}
}
}

impl From<Uint128> for Uint256 {
Expand Down Expand Up @@ -1516,4 +1524,13 @@ mod tests {
a %= &b;
assert_eq!(a, Uint256::from(1u32));
}

#[test]
fn uint256_abs_diff_works() {
let a = Uint256::from(42u32);
let b = Uint256::from(5u32);
let expected = Uint256::from(37u32);
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}
}
17 changes: 17 additions & 0 deletions packages/std/src/math/uint512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ impl Uint512 {
pub fn saturating_mul(self, other: Self) -> Self {
Self(self.0.saturating_mul(other.0))
}

pub fn abs_diff(self, other: Self) -> Self {
if self < other {
other - self
} else {
self - other
}
}
}

impl From<Uint256> for Uint512 {
Expand Down Expand Up @@ -1151,4 +1159,13 @@ mod tests {
a %= &b;
assert_eq!(a, Uint512::from(1u32));
}

#[test]
fn uint512_abs_diff_works() {
let a = Uint512::from(42u32);
let b = Uint512::from(5u32);
let expected = Uint512::from(37u32);
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}
}
17 changes: 17 additions & 0 deletions packages/std/src/math/uint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ impl Uint64 {
pub fn saturating_pow(self, other: u32) -> Self {
Self(self.0.saturating_pow(other))
}

pub const fn abs_diff(self, other: Self) -> Self {
Self(if self.0 < other.0 {
other.0 - self.0
} else {
self.0 - other.0
})
}
}

// `From<u{128,64,32,16,8}>` is implemented manually instead of
Expand Down Expand Up @@ -878,4 +886,13 @@ mod tests {
a %= &b;
assert_eq!(a, Uint64::from(1u32));
}

#[test]
fn uint64_abs_diff_works() {
let a = Uint64::from(42u32);
let b = Uint64::from(5u32);
let expected = Uint64::from(37u32);
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}
}