From e17328e307b50e622ee3a0bfb4544ad6c929739b Mon Sep 17 00:00:00 2001 From: Pakorn Nathong Date: Sun, 12 Jun 2022 11:38:18 +0700 Subject: [PATCH 1/4] Add `abs_diff` function for math package --- packages/std/src/math/decimal.rs | 17 +++++++++++++++++ packages/std/src/math/decimal256.rs | 17 +++++++++++++++++ packages/std/src/math/uint128.rs | 13 +++++++++++++ packages/std/src/math/uint256.rs | 18 ++++++++++++++++++ packages/std/src/math/uint512.rs | 18 ++++++++++++++++++ packages/std/src/math/uint64.rs | 13 +++++++++++++ 6 files changed, 96 insertions(+) diff --git a/packages/std/src/math/decimal.rs b/packages/std/src/math/decimal.rs index f426030711..c4744e6b14 100644 --- a/packages/std/src/math/decimal.rs +++ b/packages/std/src/math/decimal.rs @@ -258,6 +258,14 @@ impl Decimal { Decimal(inner.isqrt().checked_mul(Uint128::from(outer_mul)).unwrap()) }) } + + pub fn abs_diff(self, other: Self) -> Self { + if self < other { + other - self + } else { + self - other + } + } } impl Fraction for Decimal { @@ -1664,4 +1672,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); + } } diff --git a/packages/std/src/math/decimal256.rs b/packages/std/src/math/decimal256.rs index 67d216b3e7..5b4804f5f1 100644 --- a/packages/std/src/math/decimal256.rs +++ b/packages/std/src/math/decimal256.rs @@ -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 for Decimal256 { @@ -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); + } } diff --git a/packages/std/src/math/uint128.rs b/packages/std/src/math/uint128.rs index 11d214bddd..f051a97c76 100644 --- a/packages/std/src/math/uint128.rs +++ b/packages/std/src/math/uint128.rs @@ -208,6 +208,10 @@ impl Uint128 { pub fn saturating_pow(self, other: u32) -> Self { Self(self.0.saturating_pow(other)) } + + pub fn abs_diff(self, other: impl Into) -> Self { + Self(self.0.abs_diff(other.into())) + } } // `From` is implemented manually instead of @@ -965,4 +969,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); + } } diff --git a/packages/std/src/math/uint256.rs b/packages/std/src/math/uint256.rs index a4b7c4b002..020f97a845 100644 --- a/packages/std/src/math/uint256.rs +++ b/packages/std/src/math/uint256.rs @@ -283,6 +283,15 @@ impl Uint256 { pub fn saturating_mul(self, other: Self) -> Self { Self(self.0.saturating_mul(other.0)) } + + pub fn abs_diff(self, other: impl Into) -> Self { + let other = other.into(); + if self < other { + other - self + } else { + self - other + } + } } impl From for Uint256 { @@ -1516,4 +1525,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); + } } diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index a8779b7c91..a23fe34d7e 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -258,6 +258,15 @@ impl Uint512 { pub fn saturating_mul(self, other: Self) -> Self { Self(self.0.saturating_mul(other.0)) } + + pub fn abs_diff(self, other: impl Into) -> Self { + let other = other.into(); + if self < other { + other - self + } else { + self - other + } + } } impl From for Uint512 { @@ -1151,4 +1160,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); + } } diff --git a/packages/std/src/math/uint64.rs b/packages/std/src/math/uint64.rs index cfe437f995..e75a5f9ebe 100644 --- a/packages/std/src/math/uint64.rs +++ b/packages/std/src/math/uint64.rs @@ -204,6 +204,10 @@ impl Uint64 { pub fn saturating_pow(self, other: u32) -> Self { Self(self.0.saturating_pow(other)) } + + pub fn abs_diff(self, other: impl Into) -> Self { + Self(self.0.abs_diff(other.into())) + } } // `From` is implemented manually instead of @@ -878,4 +882,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); + } } From 705ae4970c8b242d984af54809ec54e312b470bb Mon Sep 17 00:00:00 2001 From: Pakorn Nathong Date: Mon, 13 Jun 2022 08:17:27 +0700 Subject: [PATCH 2/4] Add const ident for possible struct and remove Into --- packages/std/src/math/decimal.rs | 8 ++------ packages/std/src/math/uint128.rs | 4 ++-- packages/std/src/math/uint256.rs | 3 +-- packages/std/src/math/uint512.rs | 3 +-- packages/std/src/math/uint64.rs | 4 ++-- 5 files changed, 8 insertions(+), 14 deletions(-) diff --git a/packages/std/src/math/decimal.rs b/packages/std/src/math/decimal.rs index c4744e6b14..497b29c926 100644 --- a/packages/std/src/math/decimal.rs +++ b/packages/std/src/math/decimal.rs @@ -259,12 +259,8 @@ impl Decimal { }) } - pub fn abs_diff(self, other: Self) -> Self { - if self < other { - other - self - } else { - self - other - } + pub const fn abs_diff(self, other: Self) -> Self { + Self(self.0.abs_diff(other.0)) } } diff --git a/packages/std/src/math/uint128.rs b/packages/std/src/math/uint128.rs index f051a97c76..5def248f6d 100644 --- a/packages/std/src/math/uint128.rs +++ b/packages/std/src/math/uint128.rs @@ -209,8 +209,8 @@ impl Uint128 { Self(self.0.saturating_pow(other)) } - pub fn abs_diff(self, other: impl Into) -> Self { - Self(self.0.abs_diff(other.into())) + pub const fn abs_diff(self, other: Self) -> Self { + Self(self.0.abs_diff(other.0)) } } diff --git a/packages/std/src/math/uint256.rs b/packages/std/src/math/uint256.rs index 020f97a845..9150bed567 100644 --- a/packages/std/src/math/uint256.rs +++ b/packages/std/src/math/uint256.rs @@ -284,8 +284,7 @@ impl Uint256 { Self(self.0.saturating_mul(other.0)) } - pub fn abs_diff(self, other: impl Into) -> Self { - let other = other.into(); + pub fn abs_diff(self, other: Self) -> Self { if self < other { other - self } else { diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index a23fe34d7e..ceeb7b8694 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -259,8 +259,7 @@ impl Uint512 { Self(self.0.saturating_mul(other.0)) } - pub fn abs_diff(self, other: impl Into) -> Self { - let other = other.into(); + pub fn abs_diff(self, other: Self) -> Self { if self < other { other - self } else { diff --git a/packages/std/src/math/uint64.rs b/packages/std/src/math/uint64.rs index e75a5f9ebe..bb56d9bc9f 100644 --- a/packages/std/src/math/uint64.rs +++ b/packages/std/src/math/uint64.rs @@ -205,8 +205,8 @@ impl Uint64 { Self(self.0.saturating_pow(other)) } - pub fn abs_diff(self, other: impl Into) -> Self { - Self(self.0.abs_diff(other.into())) + pub const fn abs_diff(self, other: Self) -> Self { + Self(self.0.abs_diff(other.0)) } } From e5837a38c9c82586f00d3e0c6ed3d4b7f0e14da0 Mon Sep 17 00:00:00 2001 From: Pakorn Nathong Date: Mon, 13 Jun 2022 14:51:01 +0700 Subject: [PATCH 3/4] Use manual implementation of abs_diff instead of native --- packages/std/src/math/uint128.rs | 6 +++++- packages/std/src/math/uint64.rs | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/std/src/math/uint128.rs b/packages/std/src/math/uint128.rs index 5def248f6d..5aee2fc8f6 100644 --- a/packages/std/src/math/uint128.rs +++ b/packages/std/src/math/uint128.rs @@ -210,7 +210,11 @@ impl Uint128 { } pub const fn abs_diff(self, other: Self) -> Self { - Self(self.0.abs_diff(other.0)) + Self(if self.0 < other.0 { + other.0 - self.0 + } else { + self.0 - other.0 + }) } } diff --git a/packages/std/src/math/uint64.rs b/packages/std/src/math/uint64.rs index bb56d9bc9f..6bf19b2c3e 100644 --- a/packages/std/src/math/uint64.rs +++ b/packages/std/src/math/uint64.rs @@ -206,7 +206,11 @@ impl Uint64 { } pub const fn abs_diff(self, other: Self) -> Self { - Self(self.0.abs_diff(other.0)) + Self(if self.0 < other.0 { + other.0 - self.0 + } else { + self.0 - other.0 + }) } } From 158224f2c19907e038a6d82f0380186395f12df5 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 13 Jun 2022 10:18:25 +0200 Subject: [PATCH 4/4] Add CHANGELOG entry for abs_diff --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 045d57213b..9103aa0aed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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