add {div,rem}_euclid_int to Wrapping

This commit is contained in:
Trevor Spiteri 2020-02-12 23:51:31 +01:00
parent 873bdfa128
commit e3e17341f2
3 changed files with 51 additions and 3 deletions

View File

@ -86,6 +86,9 @@ The conversions supported cover the following cases.
* [`saturating_div_euclid`] * [`saturating_div_euclid`]
* [`wrapping_div_euclid`] * [`wrapping_div_euclid`]
* [`overflowing_div_euclid`] * [`overflowing_div_euclid`]
* The following methods were added to the [`Wrapping`] wrapper:
* [`div_euclid`][wde], [`rem_euclid`][wre]
* [`div_euclid_int`][wdei], [`rem_euclid_int`][wrei]
[`RemAssign`]: https://doc.rust-lang.org/nightly/core/ops/trait.RemAssign.html [`RemAssign`]: https://doc.rust-lang.org/nightly/core/ops/trait.RemAssign.html
[`Rem`]: https://doc.rust-lang.org/nightly/core/ops/trait.Rem.html [`Rem`]: https://doc.rust-lang.org/nightly/core/ops/trait.Rem.html
@ -99,6 +102,10 @@ The conversions supported cover the following cases.
[`saturating_div_euclid`]: https://docs.rs/fixed/0.5.3/fixed/struct.FixedI32.html#method.saturating_div_euclid [`saturating_div_euclid`]: https://docs.rs/fixed/0.5.3/fixed/struct.FixedI32.html#method.saturating_div_euclid
[`wrapping_div_euclid`]: https://docs.rs/fixed/0.5.3/fixed/struct.FixedI32.html#method.wrapping_div_euclid [`wrapping_div_euclid`]: https://docs.rs/fixed/0.5.3/fixed/struct.FixedI32.html#method.wrapping_div_euclid
[issue 13]: https://gitlab.com/tspiteri/fixed/issues/13 [issue 13]: https://gitlab.com/tspiteri/fixed/issues/13
[wde]: https://docs.rs/fixed/0.5.3/fixed/struct.Wrapping.html#method.div_euclid
[wdei]: https://docs.rs/fixed/0.5.3/fixed/struct.Wrapping.html#method.div_euclid_int
[wre]: https://docs.rs/fixed/0.5.3/fixed/struct.Wrapping.html#method.rem_euclid
[wrei]: https://docs.rs/fixed/0.5.3/fixed/struct.Wrapping.html#method.rem_euclid_int
### Version 0.5.2 news (2020-02-02) ### Version 0.5.2 news (2020-02-02)

View File

@ -24,6 +24,9 @@ Version 0.5.3 (unreleased)
* `saturating_div_euclid` * `saturating_div_euclid`
* `wrapping_div_euclid` * `wrapping_div_euclid`
* `overflowing_div_euclid` * `overflowing_div_euclid`
* The following methods were added to the `Wrapping` wrapper:
* `div_euclid`, `rem_euclid`
* `div_euclid_int`, `rem_euclid_int`
Version 0.5.2 (2020-02-02) Version 0.5.2 (2020-02-02)
========================== ==========================
@ -113,13 +116,12 @@ Version 0.4.3 (2019-08-20)
Version 0.4.2 (2019-08-16) Version 0.4.2 (2019-08-16)
========================== ==========================
* The new methods [`from_num`] and [`to_num`] together with their * The new methods `from_num` and `to_num` together with their
checked versions were added to all fixed-point numbers. checked versions were added to all fixed-point numbers.
* The methods `from_fixed`, `to_fixed`, `from_int`, `to_int`, * The methods `from_fixed`, `to_fixed`, `from_int`, `to_int`,
`from_float`, and `to_float`, and their checked versions, were `from_float`, and `to_float`, and their checked versions, were
deprecated. deprecated.
* The new method [`from_num`][`Wrapping::from_num`] was added to the * The new method `from_num` was added to the `Wrapping` wrapper.
[`Wrapping`] wrapper.
* Bug fix: parsing of decimal fractions was fixed to give correctly * Bug fix: parsing of decimal fractions was fixed to give correctly
rounded results for long decimal fraction strings, for example rounded results for long decimal fraction strings, for example
with four fractional bits, 0.96874999… (just below 3132) and with four fractional bits, 0.96874999… (just below 3132) and

View File

@ -566,6 +566,45 @@ impl<F: Fixed> Wrapping<F> {
pub fn rem_euclid(self, divisor: Wrapping<F>) -> Wrapping<F> { pub fn rem_euclid(self, divisor: Wrapping<F>) -> Wrapping<F> {
Wrapping(self.0.rem_euclid(divisor.0)) Wrapping(self.0.rem_euclid(divisor.0))
} }
/// Euclidean division by an integer.
///
/// # Panics
///
/// Panics if the divisor is zero.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let num = Wrapping(I16F16::from_num(7.5));
/// assert_eq!(num.div_euclid_int(2), Wrapping(I16F16::from_num(3)));
/// let min = Wrapping(I16F16::min_value());
/// assert_eq!(min.div_euclid_int(-1), min);
/// ```
#[inline]
pub fn div_euclid_int(self, divisor: F::Bits) -> Wrapping<F> {
Wrapping(self.0.wrapping_div_euclid_int(divisor))
}
/// Remainder for Euclidean division.
///
/// # Panics
///
/// Panics if the divisor is zero.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let num = Wrapping(I16F16::from_num(7.5));
/// assert_eq!(num.rem_euclid_int(2), Wrapping(I16F16::from_num(1.5)));
/// assert_eq!((-num).rem_euclid_int(2), Wrapping(I16F16::from_num(0.5)));
/// ```
#[inline]
pub fn rem_euclid_int(self, divisor: F::Bits) -> Wrapping<F> {
Wrapping(self.0.rem_euclid_int(divisor))
}
} }
impl<F: FixedSigned> Wrapping<F> { impl<F: FixedSigned> Wrapping<F> {