From 3ee5811556bbbbe4718d4d480d2b76f0d1bf1223 Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Wed, 12 Feb 2020 18:40:04 +0100 Subject: [PATCH] fix remaining checked {div,rem}_euclid_int methods Closes https://gitlab.com/tspiteri/fixed/issues/13 --- src/macros_frac.rs | 310 ++++++++++++++++++++++++++++++++++++++++-- src/macros_no_frac.rs | 291 --------------------------------------- src/traits.rs | 65 +++++---- src/wrapping.rs | 2 +- 4 files changed, 337 insertions(+), 331 deletions(-) diff --git a/src/macros_frac.rs b/src/macros_frac.rs index 5fae7ac..30a312a 100644 --- a/src/macros_frac.rs +++ b/src/macros_frac.rs @@ -142,7 +142,13 @@ assert_eq!(Fix::from_num(-5).signum(), -1); # Panics -Panics if the divisor is zero or if the division results in overflow. +Panics if the divisor is zero. + +When debug assertions are enabled, this method also panics if the +division overflows. When debug assertions are not enabled, the wrapped +value can be returned, but it is not considered a breaking change if +in the future it panics; if wrapping is required use +[`wrapping_div_euclid`] instead. # Examples @@ -157,6 +163,8 @@ assert_eq!(Fix::from_num(7.5).div_euclid(Fix::from_num(2)), Fix::from_num(3)); ", }, "``` + +[`wrapping_div_euclid`]: #method.wrapping_div_euclid "; #[inline] pub fn div_euclid(self, rhs: $Fixed) -> $Fixed { @@ -175,6 +183,86 @@ assert_eq!(Fix::from_num(7.5).div_euclid(Fix::from_num(2)), Fix::from_num(3)); } } + comment! { + "Euclidean division by an integer. + +# Panics + +Panics if the divisor is zero. + +When debug assertions are enabled, this method also panics if the +division overflows. When debug assertions are not enabled, the wrapped +value can be returned, but it is not considered a breaking change if +in the future it panics; if wrapping is required use +[`wrapping_div_euclid_int`] instead. + + +# Examples + +```rust +use fixed::{types::extra::U4, ", $s_fixed, "}; +type Fix = ", $s_fixed, "; +assert_eq!(Fix::from_num(7.5).div_euclid_int(2), Fix::from_num(3)); +", + if_signed_else_empty_str! { + $Signedness, + "assert_eq!(Fix::from_num(-7.5).div_euclid_int(2), Fix::from_num(-4)); +", + }, + "``` + +[`wrapping_div_euclid_int`]: #method.wrapping_div_euclid_int +"; + #[inline] + pub fn div_euclid_int(self, rhs: $Inner) -> $Fixed { + let q = (self / rhs).round_to_zero(); + if_signed! { + $Signedness; + if (self % rhs).is_negative() { + return if rhs.is_positive() { + q - Self::from_num(1) + } else { + q + Self::from_num(1) + }; + } + } + q + } + } + + comment! { + "Remainder for Euclidean division by an integer. + +# Panics + +Panics if the divisor is zero. + +# Examples + +```rust +use fixed::{types::extra::U4, ", $s_fixed, "}; +type Fix = ", $s_fixed, "; +assert_eq!(Fix::from_num(7.5).rem_euclid_int(2), Fix::from_num(1.5)); +", + if_signed_else_empty_str! { + $Signedness, + "assert_eq!(Fix::from_num(-7.5).rem_euclid_int(2), Fix::from_num(0.5)); +", + }, + "``` +"; + #[inline] + pub fn rem_euclid_int(self, rhs: $Inner) -> $Fixed { + // Any overflow in coverting rhs to $Fixed means that |rhs| > |self|, + // and consequently the remainder is self. + let fixed_rhs = match Self::checked_from_num(rhs) { + Some(s) => s, + None => return self, + }; + self.rem_euclid(fixed_rhs) + } + } + comment! { "Checked multiplication. Returns the product, or [`None`] on overflow. @@ -253,11 +341,10 @@ assert_eq!(Fix::max_value().checked_div_euclid(Fix::from_num(0.25)), None); if_signed! { $Signedness; if (self % rhs).is_negative() { - let one = Self::checked_from_num(1)?; return if rhs.is_positive() { - q.checked_sub(one) + q.checked_add(Self::checked_from_num(-1)?) } else { - q.checked_add(one) + q.checked_add(Self::checked_from_num(1)?) }; } } @@ -298,6 +385,83 @@ assert_eq!(Fix::from_num(3.75).checked_rem_int(0), None); } } + + comment! { + "Checked Euclidean division by an integer. Returns the +quotient, or [`None`] if the divisor is zero", + if_signed_else_empty_str! { + $Signedness, + " or if the division results in overflow", + }, + ". + +# Examples + +```rust +use fixed::{types::extra::U4, ", $s_fixed, "}; +type Fix = ", $s_fixed, "; +assert_eq!(Fix::from_num(7.5).checked_div_euclid_int(2), Some(Fix::from_num(3))); +assert_eq!(Fix::from_num(7.5).checked_div_euclid_int(0), None); +", + if_signed_else_empty_str! { + $Signedness, + "assert_eq!(Fix::min_value().checked_div_euclid_int(-1), None); +", + }, + "``` + +[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None +"; + #[inline] + pub fn checked_div_euclid_int(self, rhs: $Inner) -> Option<$Fixed> { + let q = self.checked_div_int(rhs)?.round_to_zero(); + if_signed! { + $Signedness; + if (self % rhs).is_negative() { + return if rhs.is_positive() { + q.checked_add(Self::checked_from_num(-1)?) + } else { + q.checked_add(Self::checked_from_num(1)?) + }; + } + } + Some(q) + } + } + + comment! { + "Checked remainder for Euclidean division by an integer. +Returns the remainder, or [`None`] if the divisor is zero. + +# Examples + +```rust +use fixed::{types::extra::U4, ", $s_fixed, "}; +type Fix = ", $s_fixed, "; +assert_eq!(Fix::from_num(7.5).checked_rem_euclid_int(2), Some(Fix::from_num(1.5))); +assert_eq!(Fix::from_num(7.5).checked_rem_euclid_int(0), None); +", + if_signed_else_empty_str! { + $Signedness, + "assert_eq!(Fix::from_num(-7.5).checked_rem_euclid_int(2), Some(Fix::from_num(0.5))); +", + }, + "``` + +[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None +"; + #[inline] + pub fn checked_rem_euclid_int(self, rhs: $Inner) -> Option<$Fixed> { + // Any overflow in coverting rhs to $Fixed means that |rhs| > |self|, + // and consequently the remainder is self. + let fixed_rhs = match Self::checked_from_num(rhs) { + Some(s) => s, + None => return Some(self), + }; + self.checked_rem_euclid(fixed_rhs) + } + } + comment! { "Saturating multiplication. Returns the product, saturating on overflow. @@ -467,6 +631,45 @@ assert_eq!(Fix::max_value().wrapping_div_euclid(Fix::from_num(0.25)), wrapped); } } + comment! { + "Wrapping Euclidean division by an integer. Returns the quotient", + if_signed_unsigned! { + $Signedness, + ", wrapping on overflow. + +Overflow can only occur when dividing the minimum value by −1.", + ". + +Can never overflow for unsigned values.", + }, + " + +# Panics + +Panics if the divisor is zero. + +# Examples + +```rust +use fixed::{types::extra::U4, ", $s_fixed, "}; +type Fix = ", $s_fixed, "; +assert_eq!(Fix::from_num(7.5).wrapping_div_euclid_int(2), Fix::from_num(3)); +", + if_signed_else_empty_str! { + $Signedness, + "assert_eq!(Fix::from_num(-7.5).wrapping_div_euclid_int(2), Fix::from_num(-4)); +let wrapped = Fix::min_value().round_to_zero(); +assert_eq!(Fix::min_value().wrapping_div_euclid_int(-1), wrapped); +", + }, + "``` +"; + #[inline] + pub fn wrapping_div_euclid_int(self, rhs: $Inner) -> $Fixed { + self.overflowing_div_euclid_int(rhs).0 + } + } + comment! { "Overflowing multiplication. @@ -555,13 +758,80 @@ assert_eq!(Fix::max_value().overflowing_div_euclid(Fix::from_num(0.25)), (wrappe if_signed! { $Signedness; if (self % rhs).is_negative() { - let one = match Self::checked_from_num(1) { - None => return (q, true), - Some(one) => one, - }; let (q, overflow2) = if rhs.is_positive() { - q.overflowing_sub(one) + let minus_one = match Self::checked_from_num(-1) { + None => return (q, true), + Some(s) => s, + }; + q.overflowing_add(minus_one) } else { + let one = match Self::checked_from_num(1) { + None => return (q, true), + Some(s) => s, + }; + q.overflowing_add(one) + }; + return (q, overflow | overflow2); + } + } + (q, overflow) + } + } + + comment! { + "Overflowing Euclidean division by an integer. + +Returns a [tuple] of the quotient and ", + if_signed_unsigned! { + $Signedness, + "a [`bool`] indicating whether an overflow has +occurred. On overflow, the wrapped value is returned. Overflow can +only occur when dividing the minimum value by −1.", + "[`false`][`bool`], as the division can never overflow for unsigned values.", + }, + " + +# Panics + +Panics if the divisor is zero. + +# Examples + +```rust +use fixed::{types::extra::U4, ", $s_fixed, "}; +type Fix = ", $s_fixed, "; +assert_eq!(Fix::from_num(7.5).overflowing_div_euclid_int(2), (Fix::from_num(3), false)); +", + if_signed_else_empty_str! { + $Signedness, + "assert_eq!(Fix::from_num(-7.5).overflowing_div_euclid_int(2), (Fix::from_num(-4), false)); +let wrapped = Fix::min_value().round_to_zero(); +assert_eq!(Fix::min_value().overflowing_div_euclid_int(-1), (wrapped, true)); +", + }, + "``` + +[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html +[tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html +"; + #[inline] + pub fn overflowing_div_euclid_int(self, rhs: $Inner) -> ($Fixed, bool) { + let (mut q, overflow) = self.overflowing_div_int(rhs); + q = q.round_to_zero(); + if_signed! { + $Signedness; + if (self % rhs).is_negative() { + let (q, overflow2) = if rhs.is_positive() { + let minus_one = match Self::checked_from_num(-1) { + None => return (q, true), + Some(s) => s, + }; + q.overflowing_add(minus_one) + } else { + let one = match Self::checked_from_num(1) { + None => return (q, true), + Some(s) => s, + }; q.overflowing_add(one) }; return (q, overflow | overflow2); @@ -592,6 +862,28 @@ assert_eq!(Fix::max_value().overflowing_div_euclid(Fix::from_num(0.25)), (wrappe pub fn overflowing_rem_int(self, rhs: $Inner) -> ($Fixed, bool) { (self % rhs, false) } + + /// Remainder for Euclidean division by an integer. + /// + /// # Panics + /// + /// Panics if the divisor is zero. + #[deprecated(since = "0.5.3", note = "cannot overflow, use `rem_euclid_int` instead")] + #[inline] + pub fn wrapping_rem_euclid_int(self, rhs: $Inner) -> $Fixed { + self.rem_euclid_int(rhs) + } + + /// Remainder for Euclidean division by an integer. + /// + /// # Panics + /// + /// Panics if the divisor is zero. + #[deprecated(since = "0.5.3", note = "cannot overflow, use `rem_euclid_int` instead")] + #[inline] + pub fn overflowing_rem_euclid_int(self, rhs: $Inner) -> ($Fixed, bool) { + (self.rem_euclid_int(rhs), false) + } } }; } diff --git a/src/macros_no_frac.rs b/src/macros_no_frac.rs index a11682b..d60f727 100644 --- a/src/macros_no_frac.rs +++ b/src/macros_no_frac.rs @@ -453,70 +453,6 @@ assert_eq!(Fix::from_num(7.5).rem_euclid(Fix::from_num(2)), Fix::from_num(1.5)); } } - comment! { - "Euclidean division by an integer. - -# Panics - -Panics if the divisor is zero", - if_signed_else_empty_str! { - $Signedness, - " or if the division results in overflow", - }, - ". - -# Examples - -```rust -use fixed::{types::extra::U4, ", $s_fixed, "}; -type Fix = ", $s_fixed, "; -assert_eq!(Fix::from_bits(0x7F).div_euclid_int(4), Fix::from_bits(0x1F)); -", - if_signed_else_empty_str! { - $Signedness, - "assert_eq!(Fix::from_bits(-0x7F).div_euclid_int(4), Fix::from_bits(-0x20)); -", - }, - "``` -"; - #[inline] - pub fn div_euclid_int(self, rhs: $Inner) -> $Fixed { - Self::from_bits(self.to_bits().div_euclid(rhs)) - } - } - - comment! { - "Remainder for Euclidean division by an integer. - -# Panics - -Panics if the divisor is zero", - if_signed_else_empty_str! { - $Signedness, - " or if the division results in overflow", - }, - ". - -# Examples - -```rust -use fixed::{types::extra::U4, ", $s_fixed, "}; -type Fix = ", $s_fixed, "; -assert_eq!(Fix::from_bits(0x7F).rem_euclid_int(4), Fix::from_bits(0x03)); -", - if_signed_else_empty_str! { - $Signedness, - "assert_eq!(Fix::from_bits(-0x7F).rem_euclid_int(4), Fix::from_bits(0x01)); -", - }, - "``` -"; - #[inline] - pub fn rem_euclid_int(self, rhs: $Inner) -> $Fixed { - Self::from_bits(self.to_bits().rem_euclid(rhs)) - } - } - if_signed! { $Signedness; comment! { @@ -769,72 +705,6 @@ assert_eq!(num.checked_rem_euclid(Fix::from_num(0)), None); } } - comment! { - "Checked Euclidean division by an integer. Returns the -quotient, or [`None`] if the divisor is zero", - if_signed_else_empty_str! { - $Signedness, - " or if the division results in overflow", - }, - ". - -# Examples - -```rust -use fixed::{types::extra::U4, ", $s_fixed, "}; -type Fix = ", $s_fixed, "; -assert_eq!(Fix::from_num(1).checked_div_euclid_int(0), None); -assert_eq!(Fix::from_bits(0x7F).checked_div_euclid_int(4), Some(Fix::from_bits(0x1F))); -", - if_signed_else_empty_str! { - $Signedness, - "assert_eq!(Fix::from_bits(-0x7F).checked_div_euclid_int(4), Some(Fix::from_bits(-0x20))); -assert_eq!(Fix::min_value().checked_div_int(-1), None); -", - }, - "``` - -[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None -"; - #[inline] - pub fn checked_div_euclid_int(self, rhs: $Inner) -> Option<$Fixed> { - self.to_bits().checked_div_euclid(rhs).map(Self::from_bits) - } - } - - comment! { - "Checked fixed-point remainder for Euclidean division by an integer. -Returns the remainder, or [`None`] if the divisor is zero", - if_signed_else_empty_str! { - $Signedness, - " or if the division results in overflow", - }, - ". - -# Examples - -```rust -use fixed::{types::extra::U4, ", $s_fixed, "}; -type Fix = ", $s_fixed, "; -assert_eq!(Fix::from_num(1).checked_rem_euclid_int(0), None); -assert_eq!(Fix::from_bits(0x7F).checked_rem_euclid_int(4), Some(Fix::from_bits(0x03))); -", - if_signed_else_empty_str! { - $Signedness, - "assert_eq!(Fix::from_bits(-0x7F).checked_rem_euclid_int(4), Some(Fix::from_bits(0x01))); -assert_eq!(Fix::min_value().checked_rem_euclid_int(-1), None); -", - }, - "``` - -[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None -"; - #[inline] - pub fn checked_rem_euclid_int(self, rhs: $Inner) -> Option<$Fixed> { - self.to_bits().checked_rem_euclid(rhs).map(Self::from_bits) - } - } - comment! { "Checked shift left. Returns the shifted number, or [`None`] if `rhs` ≥ ", $s_nbits, ". @@ -1227,83 +1097,6 @@ assert_eq!(Fix::from_num(3).wrapping_div_int(2), one_point_5); } } - comment! { - "Wrapping Euclidean division by an integer. Returns the quotient", - if_signed_unsigned! { - $Signedness, - ", wrapping on overflow. - -Overflow can only occur when dividing the minimum value by −1.", - ". - -Can never overflow for unsigned values.", - }, - " - -# Panics - -Panics if the divisor is zero. - -# Examples - -```rust -use fixed::{types::extra::U4, ", $s_fixed, "}; -type Fix = ", $s_fixed, "; -assert_eq!(Fix::from_bits(0x7F).wrapping_div_euclid_int(4), Fix::from_bits(0x1F)); -", - if_signed_else_empty_str! { - $Signedness, - "assert_eq!(Fix::from_bits(-0x7F).wrapping_div_euclid_int(4), Fix::from_bits(-0x20)); -assert_eq!(Fix::min_value().wrapping_div_euclid_int(-1), Fix::min_value()); -", - }, - "``` -"; - #[inline] - pub fn wrapping_div_euclid_int(self, rhs: $Inner) -> $Fixed { - Self::from_bits(self.to_bits().wrapping_div_euclid(rhs)) - } - } - - comment! { - "Wrapping fixed-point remainder for Euclidean division -by an integer. Returns the remainder", - if_signed_unsigned! { - $Signedness, - ", wrapping on overflow. - -Overflow can only occur when dividing the minimum value by −1.", - ". - -Can never overflow for unsigned values.", - }, - " - -# Panics - -Panics if the divisor is zero. - -# Examples - -```rust -use fixed::{types::extra::U4, ", $s_fixed, "}; -type Fix = ", $s_fixed, "; -assert_eq!(Fix::from_bits(0x7F).wrapping_rem_euclid_int(4), Fix::from_bits(0x03)); -", - if_signed_else_empty_str! { - $Signedness, - "assert_eq!(Fix::from_bits(-0x7F).wrapping_rem_euclid_int(4), Fix::from_bits(0x01)); -assert_eq!(Fix::min_value().wrapping_rem_euclid_int(-1), 0); -", - }, - "``` -"; - #[inline] - pub fn wrapping_rem_euclid_int(self, rhs: $Inner) -> $Fixed { - Self::from_bits(self.to_bits().wrapping_rem_euclid(rhs)) - } - } - comment! { "Wrapping shift left. Wraps `rhs` if `rhs` ≥ ", $s_nbits, ", then shifts and returns the number. @@ -1539,90 +1332,6 @@ assert_eq!(Fix::from_num(3).overflowing_div_int(2), (one_point_5, false)); } } - comment! { - "Overflowing Euclidean division by an integer. - -Returns a [tuple] of the quotient and ", - if_signed_unsigned! { - $Signedness, - "a [`bool`] indicating whether an overflow has -occurred. On overflow, the wrapped value is returned. Overflow can -only occur when dividing the minimum value by −1.", - "[`false`][`bool`], as the division can never overflow for unsigned values.", - }, - " - -# Panics - -Panics if the divisor is zero. - -# Examples - -```rust -use fixed::{types::extra::U4, ", $s_fixed, "}; -type Fix = ", $s_fixed, "; -assert_eq!(Fix::from_bits(0x7F).overflowing_div_euclid_int(4), (Fix::from_bits(0x1F), false)); -", - if_signed_else_empty_str! { - $Signedness, - "assert_eq!(Fix::from_bits(-0x7F).overflowing_div_euclid_int(4), (Fix::from_bits(-0x20), false)); -assert_eq!(Fix::min_value().overflowing_div_euclid_int(-1), (Fix::min_value(), true)); -", - }, - "``` - -[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html -[tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html -"; - #[inline] - pub fn overflowing_div_euclid_int(self, rhs: $Inner) -> ($Fixed, bool) { - let (ans, o) = self.to_bits().overflowing_div_euclid(rhs); - (Self::from_bits(ans), o) - } - } - - comment! { - "Overflowing fixed-point remainder for Euclidean division by an integer. - -Returns a [tuple] of the remainder and ", - if_signed_unsigned! { - $Signedness, - "a [`bool`] indicating whether an overflow has -occurred. On overflow, the wrapped value is returned. Overflow can -only occur when dividing the minimum value by −1.", - "[`false`][`bool`], as the division can never overflow for unsigned values.", - }, - " - -# Panics - -Panics if the divisor is zero. - -# Examples - -```rust -use fixed::{types::extra::U4, ", $s_fixed, "}; -type Fix = ", $s_fixed, "; -assert_eq!(Fix::from_bits(0x7F).overflowing_rem_euclid_int(4), (Fix::from_bits(0x03), false)); -", - if_signed_else_empty_str! { - $Signedness, - "assert_eq!(Fix::from_bits(-0x7F).overflowing_rem_euclid_int(4), (Fix::from_bits(0x01), false)); -assert_eq!(Fix::min_value().overflowing_rem_euclid_int(-1), (Fix::from_num(0), true)); -", - }, - "``` - -[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html -[tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html -"; - #[inline] - pub fn overflowing_rem_euclid_int(self, rhs: $Inner) -> ($Fixed, bool) { - let (ans, o) = self.to_bits().overflowing_rem_euclid(rhs); - (Self::from_bits(ans), o) - } - } - comment! { "Overflowing shift left. diff --git a/src/traits.rs b/src/traits.rs index 22adde2..cdcb8a0 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -692,7 +692,7 @@ where /// Checked fixed-point remainder for Euclidean division by an /// integer. Returns the remainder, or [`None`] if the divisor is - /// zero or if the division results in overflow. + /// zero. /// /// [`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None fn checked_rem_euclid_int(self, rhs: Self::Bits) -> Option; @@ -786,16 +786,6 @@ where /// Panics if the divisor is zero. fn wrapping_div_euclid_int(self, rhs: Self::Bits) -> Self; - /// Wrapping fixed-point remainder for Euclidean division by an - /// integer. Returns the remainder, wrapping on overflow. - /// - /// Overflow can only occur when dividing the minimum value by −1. - /// - /// # Panics - /// - /// Panics if the divisor is zero. - fn wrapping_rem_euclid_int(self, rhs: Self::Bits) -> Self; - /// Wrapping shift left. Wraps `rhs` if `rhs` ≥ the number of /// bits, then shifts and returns the number. fn wrapping_shl(self, rhs: u32) -> Self; @@ -910,21 +900,6 @@ where /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_div_euclid_int(self, rhs: Self::Bits) -> (Self, bool); - /// Overflowing fixed-point remainder for Euclidean division by an integer. - /// - /// Returns a [tuple] of the remainder and a [`bool`], indicating - /// whether an overflow has occurred. On overflow, the wrapped - /// value is returned. Overflow can only occur when dividing the - /// minimum value by −1. - /// - /// # Panics - /// - /// Panics if the divisor is zero. - /// - /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html - /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html - fn overflowing_rem_euclid_int(self, rhs: Self::Bits) -> (Self, bool); - /// Overflowing shift left. /// /// Returns a [tuple] of the shifted value and a [`bool`], @@ -950,7 +925,10 @@ where /// # Panics /// /// Panics if the divisor is zero. - #[deprecated(since = "0.5.3", note = "cannot overflow, use `%` or `Rem::rem` instead")] + #[deprecated( + since = "0.5.3", + note = "cannot overflow, use `%` or `Rem::rem` instead" + )] fn wrapping_rem_int(self, rhs: Self::Bits) -> Self { self % rhs } @@ -960,10 +938,39 @@ where /// # Panics /// /// Panics if the divisor is zero. - #[deprecated(since = "0.5.3", note = "cannot overflow, use `%` or `Rem::rem` instead")] + #[deprecated( + since = "0.5.3", + note = "cannot overflow, use `%` or `Rem::rem` instead" + )] fn overflowing_rem_int(self, rhs: Self::Bits) -> (Self, bool) { (self % rhs, false) } + + /// Remainder for Euclidean division by an integer. + /// + /// # Panics + /// + /// Panics if the divisor is zero. + #[deprecated( + since = "0.5.3", + note = "cannot overflow, use `rem_euclid_int` instead" + )] + fn wrapping_rem_euclid_int(self, rhs: Self::Bits) -> Self { + self.rem_euclid_int(rhs) + } + + /// Remainder for Euclidean division by an integer. + /// + /// # Panics + /// + /// Panics if the divisor is zero. + #[deprecated( + since = "0.5.3", + note = "cannot overflow, use `rem_euclid_int` instead" + )] + fn overflowing_rem_euclid_int(self, rhs: Self::Bits) -> (Self, bool) { + (self.rem_euclid_int(rhs), false) + } } /// This trait provides methods common to all signed fixed-point numbers. @@ -1610,7 +1617,6 @@ macro_rules! impl_fixed { trait_delegate! { fn wrapping_mul_int(self, rhs: Self::Bits) -> Self } trait_delegate! { fn wrapping_div_int(self, rhs: Self::Bits) -> Self } trait_delegate! { fn wrapping_div_euclid_int(self, rhs: Self::Bits) -> Self } - trait_delegate! { fn wrapping_rem_euclid_int(self, rhs: Self::Bits) -> Self } trait_delegate! { fn wrapping_shl(self, rhs: u32) -> Self } trait_delegate! { fn wrapping_shr(self, rhs: u32) -> Self } trait_delegate! { fn overflowing_neg(self) -> (Self, bool) } @@ -1622,7 +1628,6 @@ macro_rules! impl_fixed { trait_delegate! { fn overflowing_mul_int(self, rhs: Self::Bits) -> (Self, bool) } trait_delegate! { fn overflowing_div_int(self, rhs: Self::Bits) -> (Self, bool) } trait_delegate! { fn overflowing_div_euclid_int(self, rhs: Self::Bits) -> (Self, bool) } - trait_delegate! { fn overflowing_rem_euclid_int(self, rhs: Self::Bits) -> (Self, bool) } trait_delegate! { fn overflowing_shl(self, rhs: u32) -> (Self, bool) } trait_delegate! { fn overflowing_shr(self, rhs: u32) -> (Self, bool) } } diff --git a/src/wrapping.rs b/src/wrapping.rs index 46c841a..7be0a77 100644 --- a/src/wrapping.rs +++ b/src/wrapping.rs @@ -18,9 +18,9 @@ use crate::{ from_str::ParseFixedError, traits::{Fixed, FixedSigned, FixedUnsigned, FromFixed, ToFixed}, + types::extra::{LeEqU128, LeEqU16, LeEqU32, LeEqU64, LeEqU8}, FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64, FixedU8, - types::extra::{LeEqU128, LeEqU16, LeEqU32, LeEqU64, LeEqU8}, }; use core::{ fmt::{Display, Formatter, Result as FmtResult},