From a135b117ee8ed169502ba518bf2484e20a01d891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Thu, 12 Oct 2023 15:23:23 +0200 Subject: [PATCH 1/4] feat: add an extra method to (Checked)Euclid trait to get both quotien and rem --- src/ops/euclid.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/ops/euclid.rs b/src/ops/euclid.rs index 4547fee..3043bbf 100644 --- a/src/ops/euclid.rs +++ b/src/ops/euclid.rs @@ -46,6 +46,14 @@ pub trait Euclid: Sized + Div + Rem { /// assert_eq!(Euclid::rem_euclid(&-a, &-b), 1); /// ``` fn rem_euclid(&self, v: &Self) -> Self; + + /// Returns both the euclidian division quotien and remainer + /// + /// By default, it internaly calls both `Euclid::div_euclid` and `Euclid::rem_euclid`, + /// but it can be overwritten in order to implement some optimization. + fn div_with_rem_euclid(&self, v: &Self) -> (Self, Self) { + (self.div_euclid(v), self.rem_euclid(v)) + } } macro_rules! euclid_forward_impl { @@ -174,6 +182,14 @@ pub trait CheckedEuclid: Euclid { /// Finds the euclid remainder of dividing two numbers, checking for underflow, overflow and /// division by zero. If any of that happens, `None` is returned. fn checked_rem_euclid(&self, v: &Self) -> Option; + + /// Returns both the checked euclidian division quotien and remainer + /// + /// By default, it internaly calls both `Euclid::div_euclid` and `Euclid::rem_euclid`, + /// but it can be overwritten in order to implement some optimization. + fn checked_div_with_rem_euclid(&self, v: &Self) -> Option<(Self, Self)> { + Some((self.checked_div_euclid(v)?, self.checked_rem_euclid(v)?)) + } } macro_rules! checked_euclid_forward_impl { From 3def73d32a89e5a973914e3b59104506993869df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Fri, 13 Oct 2023 12:36:35 +0200 Subject: [PATCH 2/4] typos, doc and naming --- src/ops/euclid.rs | 52 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/src/ops/euclid.rs b/src/ops/euclid.rs index 3043bbf..452305b 100644 --- a/src/ops/euclid.rs +++ b/src/ops/euclid.rs @@ -47,11 +47,24 @@ pub trait Euclid: Sized + Div + Rem { /// ``` fn rem_euclid(&self, v: &Self) -> Self; - /// Returns both the euclidian division quotien and remainer + /// Returns both the quotient and remainder from Euclidean division. /// - /// By default, it internaly calls both `Euclid::div_euclid` and `Euclid::rem_euclid`, - /// but it can be overwritten in order to implement some optimization. - fn div_with_rem_euclid(&self, v: &Self) -> (Self, Self) { + /// By default, it internally calls both `Euclid::div_euclid` and `Euclid::rem_euclid`, + /// but it can be overidden in order to implement some optimization. + /// + /// # Examples + /// + /// ``` + /// # use num_traits::Euclid; + /// let x = 5u8; + /// let y = 3u8; + /// + /// let div = Euclid::div_euclid(&x, &y); + /// let rem = Euclid::rem_euclid(&x, &y); + /// + /// assert_eq!((div, rem), Euclid::div_rem_euclid(&x, &y)); + /// ``` + fn div_rem_euclid(&self, v: &Self) -> (Self, Self) { (self.div_euclid(v), self.rem_euclid(v)) } } @@ -183,11 +196,23 @@ pub trait CheckedEuclid: Euclid { /// division by zero. If any of that happens, `None` is returned. fn checked_rem_euclid(&self, v: &Self) -> Option; - /// Returns both the checked euclidian division quotien and remainer + /// Returns both the quotient and remainder from checked Euclidean division. /// - /// By default, it internaly calls both `Euclid::div_euclid` and `Euclid::rem_euclid`, - /// but it can be overwritten in order to implement some optimization. - fn checked_div_with_rem_euclid(&self, v: &Self) -> Option<(Self, Self)> { + /// By default, it internally calls both `CheckedEuclid::checked_div_euclid` and `CheckedEuclid::checked_rem_euclid`, + /// but it can be overridden in order to implement some optimization. + /// # Examples + /// + /// ``` + /// # use num_traits::CheckedEuclid; + /// let x = 5u8; + /// let y = 3u8; + /// + /// let div = CheckedEuclid::checked_div_euclid(&x, &y); + /// let rem = CheckedEuclid::checked_rem_euclid(&x, &y); + /// + /// assert_eq!(Some((div.unwrap(), rem.unwrap())), CheckedEuclid::checked_div_rem_euclid(&x, &y)); + /// ``` + fn checked_div_rem_euclid(&self, v: &Self) -> Option<(Self, Self)> { Some((self.checked_div_euclid(v)?, self.checked_rem_euclid(v)?)) } } @@ -278,8 +303,11 @@ mod tests { { let x: $t = 10; let y: $t = 3; - assert_eq!(Euclid::div_euclid(&x, &y), 3); - assert_eq!(Euclid::rem_euclid(&x, &y), 1); + let div = Euclid::div_euclid(&x, &y); + let rem = Euclid::rem_euclid(&x, &y); + assert_eq!(div, 3); + assert_eq!(rem, 1); + assert_eq!((div, rem), Euclid::div_rem_euclid(&x, &y)); } )+ }; @@ -300,6 +328,7 @@ mod tests { assert_eq!(Euclid::div_euclid(&-x, &y), 4); assert_eq!(Euclid::rem_euclid(&x, &y), 1); assert_eq!(Euclid::rem_euclid(&-x, &y), 2); + assert_eq!((Euclid::div_euclid(&x, &y), Euclid::rem_euclid(&x, &y)), Euclid::div_rem_euclid(&x, &y)); let x: $t = $t::min_value() + 1; let y: $t = -1; assert_eq!(Euclid::div_euclid(&x, &y), $t::max_value()); @@ -327,6 +356,7 @@ mod tests { <= 46.4 * <$t as crate::float::FloatCore>::epsilon()); assert!(Euclid::div_euclid(&-x, &-y) * -y + Euclid::rem_euclid(&-x, &-y) + x <= 46.4 * <$t as crate::float::FloatCore>::epsilon()); + assert_eq!((Euclid::div_euclid(&x, &y), Euclid::rem_euclid(&x, &y)), Euclid::div_rem_euclid(&x, &y)); } )+ }; @@ -343,8 +373,6 @@ mod tests { { assert_eq!(CheckedEuclid::checked_div_euclid(&$t::min_value(), &-1), None); assert_eq!(CheckedEuclid::checked_rem_euclid(&$t::min_value(), &-1), None); - assert_eq!(CheckedEuclid::checked_div_euclid(&1, &0), None); - assert_eq!(CheckedEuclid::checked_rem_euclid(&1, &0), None); } )+ }; From df7b60d972f161a4e27e5dcb8d0ed259bef7f559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Thu, 19 Oct 2023 09:28:14 +0200 Subject: [PATCH 3/4] put back code deleted by mistake --- src/ops/euclid.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ops/euclid.rs b/src/ops/euclid.rs index 452305b..4bf5cdb 100644 --- a/src/ops/euclid.rs +++ b/src/ops/euclid.rs @@ -373,6 +373,8 @@ mod tests { { assert_eq!(CheckedEuclid::checked_div_euclid(&$t::min_value(), &-1), None); assert_eq!(CheckedEuclid::checked_rem_euclid(&$t::min_value(), &-1), None); + assert_eq!(CheckedEuclid::checked_div_euclid(&1, &0), None); + assert_eq!(CheckedEuclid::checked_rem_euclid(&1, &0), None); } )+ }; From 8175e127c871386555b33b6414907a25f04e5093 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 25 Oct 2023 12:44:41 -0700 Subject: [PATCH 4/4] Fix spelling of "overridden" --- src/ops/euclid.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ops/euclid.rs b/src/ops/euclid.rs index 4bf5cdb..c12137d 100644 --- a/src/ops/euclid.rs +++ b/src/ops/euclid.rs @@ -50,7 +50,7 @@ pub trait Euclid: Sized + Div + Rem { /// Returns both the quotient and remainder from Euclidean division. /// /// By default, it internally calls both `Euclid::div_euclid` and `Euclid::rem_euclid`, - /// but it can be overidden in order to implement some optimization. + /// but it can be overridden in order to implement some optimization. /// /// # Examples ///