From 3175271a67ade708a576026ac041518433a9ee1b Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Wed, 13 Jul 2022 18:38:30 +0200 Subject: [PATCH 1/5] Decimal/256: Implement checked_div_euclid --- packages/std/src/math/decimal.rs | 25 +++++++++++++++++++++++-- packages/std/src/math/decimal256.rs | 21 +++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/std/src/math/decimal.rs b/packages/std/src/math/decimal.rs index a97bf29ead..d3e445acaa 100644 --- a/packages/std/src/math/decimal.rs +++ b/packages/std/src/math/decimal.rs @@ -289,6 +289,10 @@ impl Decimal { Decimal::checked_from_ratio(self.numerator(), other.numerator()) } + pub fn checked_div_euclid(self, other: Self) -> Result { + Ok(self.checked_div(other)?.floor()) + } + pub fn checked_rem(self, other: Self) -> Result { self.0 .checked_rem(other.0) @@ -1880,12 +1884,29 @@ mod tests { ); assert!(matches!( Decimal::MAX.checked_div(Decimal::zero()), - Err(CheckedFromRatioError::DivideByZero { .. }) + Err(CheckedFromRatioError::DivideByZero { }) )); assert!(matches!( Decimal::MAX.checked_div(Decimal::percent(1)), - Err(CheckedFromRatioError::Overflow { .. }) + Err(CheckedFromRatioError::Overflow { }) + )); + + assert!(matches!( + Decimal::MAX.checked_div_euclid(Decimal::zero()), + Err(CheckedFromRatioError::DivideByZero {}) + )); + assert!(matches!( + Decimal::MAX.checked_div_euclid(Decimal::percent(1)), + Err(CheckedFromRatioError::Overflow {}) )); + assert_eq!( + Decimal::percent(600).checked_div_euclid(Decimal::percent(200)).unwrap(), + Decimal::percent(300), + ); + assert_eq!( + Decimal::percent(1500).checked_div_euclid(Decimal::percent(700)).unwrap(), + Decimal::percent(200), + ); // checked rem assert_eq!( diff --git a/packages/std/src/math/decimal256.rs b/packages/std/src/math/decimal256.rs index eed988c18d..1417910f96 100644 --- a/packages/std/src/math/decimal256.rs +++ b/packages/std/src/math/decimal256.rs @@ -302,6 +302,10 @@ impl Decimal256 { Decimal256::checked_from_ratio(self.numerator(), other.numerator()) } + pub fn checked_div_euclid(self, other: Self) -> Result { + Ok(self.checked_div(other)?.floor()) + } + pub fn checked_rem(self, other: Self) -> Result { self.0 .checked_rem(other.0) @@ -2034,6 +2038,23 @@ mod tests { Err(CheckedFromRatioError::Overflow { .. }) )); + assert!(matches!( + Decimal256::MAX.checked_div_euclid(Decimal256::zero()), + Err(CheckedFromRatioError::DivideByZero {}) + )); + assert!(matches!( + Decimal256::MAX.checked_div_euclid(Decimal256::percent(1)), + Err(CheckedFromRatioError::Overflow {}) + )); + assert_eq!( + Decimal256::percent(600).checked_div_euclid(Decimal256::percent(200)).unwrap(), + Decimal256::percent(300), + ); + assert_eq!( + Decimal256::percent(1500).checked_div_euclid(Decimal256::percent(700)).unwrap(), + Decimal256::percent(200), + ); + // checked rem assert_eq!( Decimal256::percent(402) From a4c4aaae09b8268445b40b6006d201036a035f34 Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Wed, 13 Jul 2022 18:43:28 +0200 Subject: [PATCH 2/5] Decimal/256: Implement checked_div_euclid --- packages/std/src/math/decimal.rs | 16 ++++++++++------ packages/std/src/math/decimal256.rs | 10 +++++++--- packages/std/src/math/uint256.rs | 17 +++++++++++++++++ packages/std/src/math/uint512.rs | 26 ++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/packages/std/src/math/decimal.rs b/packages/std/src/math/decimal.rs index d3e445acaa..74a3cc35e8 100644 --- a/packages/std/src/math/decimal.rs +++ b/packages/std/src/math/decimal.rs @@ -1884,27 +1884,31 @@ mod tests { ); assert!(matches!( Decimal::MAX.checked_div(Decimal::zero()), - Err(CheckedFromRatioError::DivideByZero { }) + Err(CheckedFromRatioError::DivideByZero {}) )); assert!(matches!( Decimal::MAX.checked_div(Decimal::percent(1)), - Err(CheckedFromRatioError::Overflow { }) + Err(CheckedFromRatioError::Overflow {}) )); - assert!(matches!( + assert!(matches!( Decimal::MAX.checked_div_euclid(Decimal::zero()), Err(CheckedFromRatioError::DivideByZero {}) )); - assert!(matches!( + assert!(matches!( Decimal::MAX.checked_div_euclid(Decimal::percent(1)), Err(CheckedFromRatioError::Overflow {}) )); assert_eq!( - Decimal::percent(600).checked_div_euclid(Decimal::percent(200)).unwrap(), + Decimal::percent(600) + .checked_div_euclid(Decimal::percent(200)) + .unwrap(), Decimal::percent(300), ); assert_eq!( - Decimal::percent(1500).checked_div_euclid(Decimal::percent(700)).unwrap(), + Decimal::percent(1500) + .checked_div_euclid(Decimal::percent(700)) + .unwrap(), Decimal::percent(200), ); diff --git a/packages/std/src/math/decimal256.rs b/packages/std/src/math/decimal256.rs index 1417910f96..e49b964959 100644 --- a/packages/std/src/math/decimal256.rs +++ b/packages/std/src/math/decimal256.rs @@ -2042,16 +2042,20 @@ mod tests { Decimal256::MAX.checked_div_euclid(Decimal256::zero()), Err(CheckedFromRatioError::DivideByZero {}) )); - assert!(matches!( + assert!(matches!( Decimal256::MAX.checked_div_euclid(Decimal256::percent(1)), Err(CheckedFromRatioError::Overflow {}) )); assert_eq!( - Decimal256::percent(600).checked_div_euclid(Decimal256::percent(200)).unwrap(), + Decimal256::percent(600) + .checked_div_euclid(Decimal256::percent(200)) + .unwrap(), Decimal256::percent(300), ); assert_eq!( - Decimal256::percent(1500).checked_div_euclid(Decimal256::percent(700)).unwrap(), + Decimal256::percent(1500) + .checked_div_euclid(Decimal256::percent(700)) + .unwrap(), Decimal256::percent(200), ); diff --git a/packages/std/src/math/uint256.rs b/packages/std/src/math/uint256.rs index e1c14ac94a..6847039ad0 100644 --- a/packages/std/src/math/uint256.rs +++ b/packages/std/src/math/uint256.rs @@ -251,6 +251,10 @@ impl Uint256 { .ok_or_else(|| DivideByZeroError::new(self)) } + pub fn checked_div_euclid(self, other: Self) -> Result { + self.checked_div(other) + } + pub fn checked_rem(self, other: Self) -> Result { self.0 .checked_rem(other.0) @@ -1461,6 +1465,19 @@ mod tests { Err(DivideByZeroError { .. }) )); + assert!(matches!( + Uint256::MAX.checked_div_euclid(Uint256::from(0u32)), + Err(DivideByZeroError { .. }) + )); + assert_eq!( + Uint256::from(6u32).checked_div_euclid(Uint256::from(2u32)), + Ok(Uint256::from(3u32)), + ); + assert_eq!( + Uint256::from(7u32).checked_div_euclid(Uint256::from(2u32)), + Ok(Uint256::from(3u32)), + ); + // saturating_* assert_eq!( Uint256::MAX.saturating_add(Uint256::from(1u32)), diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index 2a66189b44..ec8a160b5c 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -234,6 +234,10 @@ impl Uint512 { .ok_or_else(|| DivideByZeroError::new(self)) } + pub fn checked_div_euclid(self, other: Self) -> Result { + self.checked_div(other) + } + pub fn checked_rem(self, other: Self) -> Result { self.0 .checked_rem(other.0) @@ -1091,6 +1095,28 @@ mod tests { Uint512::from(6u32).checked_div(Uint512::from(2u32)), Ok(Uint512::from(3u32)), ); + assert!(matches!( + Uint512::MAX.checked_div(Uint512::from(0u32)), + Err(DivideByZeroError { .. }) + )); + assert_eq!( + Uint512::from(6u32).checked_div(Uint512::from(2u32)), + Ok(Uint512::from(3u32)), + ); + + assert!(matches!( + Uint512::MAX.checked_div_euclid(Uint512::from(0u32)), + Err(DivideByZeroError { .. }) + )); + assert_eq!( + Uint512::from(6u32).checked_div_euclid(Uint512::from(2u32)), + Ok(Uint512::from(3u32)), + ); + assert_eq!( + Uint512::from(7u32).checked_div_euclid(Uint512::from(2u32)), + Ok(Uint512::from(3u32)), + ); + assert!(matches!( Uint512::MAX.checked_rem(Uint512::from(0u32)), Err(DivideByZeroError { .. }) From 1b8352bda14c032af1d824c51171cf36853367e9 Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Thu, 14 Jul 2022 08:32:11 +0200 Subject: [PATCH 3/5] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27ec8119e8..9a05d8a7a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ and this project adheres to - cosmwasm-std: Implement `saturating_add`/`sub`/`mul` for `Decimal`/`Decimal256`. - cosmwasm-std: Implement `MIN` const value for all `Uint` and `Decimal` types +- cosmwasm-std: Implement `checked_div_euclid` for + `Uint256`/`Uint512`/`Decimal`/`Decimal256` [#1334]: https://github.com/CosmWasm/cosmwasm/pull/1334 From 7ebbe29c7bde7f88fbdb17d05c2656a1239e5a7e Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Thu, 14 Jul 2022 08:41:25 +0200 Subject: [PATCH 4/5] Decimal: Remove unnecessarily added testcases --- packages/std/src/math/decimal.rs | 1 + packages/std/src/math/decimal256.rs | 1 + packages/std/src/math/uint256.rs | 9 ++++----- packages/std/src/math/uint512.rs | 10 ---------- 4 files changed, 6 insertions(+), 15 deletions(-) diff --git a/packages/std/src/math/decimal.rs b/packages/std/src/math/decimal.rs index 74a3cc35e8..ea06395066 100644 --- a/packages/std/src/math/decimal.rs +++ b/packages/std/src/math/decimal.rs @@ -1891,6 +1891,7 @@ mod tests { Err(CheckedFromRatioError::Overflow {}) )); + // checked div euclid assert!(matches!( Decimal::MAX.checked_div_euclid(Decimal::zero()), Err(CheckedFromRatioError::DivideByZero {}) diff --git a/packages/std/src/math/decimal256.rs b/packages/std/src/math/decimal256.rs index e49b964959..40f8095dde 100644 --- a/packages/std/src/math/decimal256.rs +++ b/packages/std/src/math/decimal256.rs @@ -2038,6 +2038,7 @@ mod tests { Err(CheckedFromRatioError::Overflow { .. }) )); + // checked div euclid assert!(matches!( Decimal256::MAX.checked_div_euclid(Decimal256::zero()), Err(CheckedFromRatioError::DivideByZero {}) diff --git a/packages/std/src/math/uint256.rs b/packages/std/src/math/uint256.rs index 6847039ad0..3d7883b608 100644 --- a/packages/std/src/math/uint256.rs +++ b/packages/std/src/math/uint256.rs @@ -1460,11 +1460,6 @@ mod tests { Uint256::from(6u32).checked_div(Uint256::from(2u32)), Ok(Uint256::from(3u32)), ); - assert!(matches!( - Uint256::MAX.checked_rem(Uint256::from(0u32)), - Err(DivideByZeroError { .. }) - )); - assert!(matches!( Uint256::MAX.checked_div_euclid(Uint256::from(0u32)), Err(DivideByZeroError { .. }) @@ -1477,6 +1472,10 @@ mod tests { Uint256::from(7u32).checked_div_euclid(Uint256::from(2u32)), Ok(Uint256::from(3u32)), ); + assert!(matches!( + Uint256::MAX.checked_rem(Uint256::from(0u32)), + Err(DivideByZeroError { .. }) + )); // saturating_* assert_eq!( diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index ec8a160b5c..e53bad95d3 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -1095,15 +1095,6 @@ mod tests { Uint512::from(6u32).checked_div(Uint512::from(2u32)), Ok(Uint512::from(3u32)), ); - assert!(matches!( - Uint512::MAX.checked_div(Uint512::from(0u32)), - Err(DivideByZeroError { .. }) - )); - assert_eq!( - Uint512::from(6u32).checked_div(Uint512::from(2u32)), - Ok(Uint512::from(3u32)), - ); - assert!(matches!( Uint512::MAX.checked_div_euclid(Uint512::from(0u32)), Err(DivideByZeroError { .. }) @@ -1116,7 +1107,6 @@ mod tests { Uint512::from(7u32).checked_div_euclid(Uint512::from(2u32)), Ok(Uint512::from(3u32)), ); - assert!(matches!( Uint512::MAX.checked_rem(Uint512::from(0u32)), Err(DivideByZeroError { .. }) From f64697ab1ab238bd415035a572a009ce2cbabf15 Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Sun, 31 Jul 2022 17:59:53 +0200 Subject: [PATCH 5/5] Uint256/512: Implement checked_div_euclid --- CHANGELOG.md | 3 +-- packages/std/src/math/decimal.rs | 26 -------------------------- packages/std/src/math/decimal256.rs | 26 -------------------------- 3 files changed, 1 insertion(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a05d8a7a6..1cd7c8e89a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,8 +20,7 @@ and this project adheres to - cosmwasm-std: Implement `saturating_add`/`sub`/`mul` for `Decimal`/`Decimal256`. - cosmwasm-std: Implement `MIN` const value for all `Uint` and `Decimal` types -- cosmwasm-std: Implement `checked_div_euclid` for - `Uint256`/`Uint512`/`Decimal`/`Decimal256` +- cosmwasm-std: Implement `checked_div_euclid` for `Uint256`/`Uint512` [#1334]: https://github.com/CosmWasm/cosmwasm/pull/1334 diff --git a/packages/std/src/math/decimal.rs b/packages/std/src/math/decimal.rs index ea06395066..1bb985d626 100644 --- a/packages/std/src/math/decimal.rs +++ b/packages/std/src/math/decimal.rs @@ -289,10 +289,6 @@ impl Decimal { Decimal::checked_from_ratio(self.numerator(), other.numerator()) } - pub fn checked_div_euclid(self, other: Self) -> Result { - Ok(self.checked_div(other)?.floor()) - } - pub fn checked_rem(self, other: Self) -> Result { self.0 .checked_rem(other.0) @@ -1891,28 +1887,6 @@ mod tests { Err(CheckedFromRatioError::Overflow {}) )); - // checked div euclid - assert!(matches!( - Decimal::MAX.checked_div_euclid(Decimal::zero()), - Err(CheckedFromRatioError::DivideByZero {}) - )); - assert!(matches!( - Decimal::MAX.checked_div_euclid(Decimal::percent(1)), - Err(CheckedFromRatioError::Overflow {}) - )); - assert_eq!( - Decimal::percent(600) - .checked_div_euclid(Decimal::percent(200)) - .unwrap(), - Decimal::percent(300), - ); - assert_eq!( - Decimal::percent(1500) - .checked_div_euclid(Decimal::percent(700)) - .unwrap(), - Decimal::percent(200), - ); - // checked rem assert_eq!( Decimal::percent(402) diff --git a/packages/std/src/math/decimal256.rs b/packages/std/src/math/decimal256.rs index 40f8095dde..eed988c18d 100644 --- a/packages/std/src/math/decimal256.rs +++ b/packages/std/src/math/decimal256.rs @@ -302,10 +302,6 @@ impl Decimal256 { Decimal256::checked_from_ratio(self.numerator(), other.numerator()) } - pub fn checked_div_euclid(self, other: Self) -> Result { - Ok(self.checked_div(other)?.floor()) - } - pub fn checked_rem(self, other: Self) -> Result { self.0 .checked_rem(other.0) @@ -2038,28 +2034,6 @@ mod tests { Err(CheckedFromRatioError::Overflow { .. }) )); - // checked div euclid - assert!(matches!( - Decimal256::MAX.checked_div_euclid(Decimal256::zero()), - Err(CheckedFromRatioError::DivideByZero {}) - )); - assert!(matches!( - Decimal256::MAX.checked_div_euclid(Decimal256::percent(1)), - Err(CheckedFromRatioError::Overflow {}) - )); - assert_eq!( - Decimal256::percent(600) - .checked_div_euclid(Decimal256::percent(200)) - .unwrap(), - Decimal256::percent(300), - ); - assert_eq!( - Decimal256::percent(1500) - .checked_div_euclid(Decimal256::percent(700)) - .unwrap(), - Decimal256::percent(200), - ); - // checked rem assert_eq!( Decimal256::percent(402)