add Wrapping::round_ties_to_even

This commit is contained in:
Trevor Spiteri 2019-08-29 10:12:17 +02:00
parent 0711850c26
commit de6ecb5782
3 changed files with 31 additions and 0 deletions

View File

@ -68,6 +68,14 @@ The conversions supported cover the following cases.
## Whats new
### Version 0.4.5 news (unreleased)
* The method [`round_ties_to_even`][`Wrapping::round_ties_to_even`]
was added to [`Wrapping`].
[`Wrapping::round_ties_to_even`]: https://docs.rs/fixed/0.4.4/fixed/struct.Wrapping.html#method.round_ties_to_even
[`Wrapping`]: https://docs.rs/fixed/0.4.4/fixed/struct.Wrapping.html
### Version 0.4.4 news (2019-08-24)
* Bug fix: rounding could produce bad output for [`Binary`],

View File

@ -5,6 +5,11 @@ modification, are permitted in any medium without royalty provided the
copyright notice and this notice are preserved. This file is offered
as-is, without any warranty. -->
Version 0.4.5 (unreleased)
==========================
* The method `round_ties_to_even` was added to `Wrapping`.
Version 0.4.4 (2019-08-24)
==========================

View File

@ -211,6 +211,24 @@ impl<F: Fixed> Wrapping<F> {
pub fn round(self) -> Wrapping<F> {
Wrapping(self.0.wrapping_round())
}
/// Wrapping round. Rounds to the next integer to the nearest,
/// with ties rounded to even, and wrapping on overflow.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let two_half = Wrapping(I16F16::from_num(2.5));
/// assert_eq!(two_half.round_ties_to_even(), Wrapping(I16F16::from_num(2)));
/// let three_half = Wrapping(I16F16::from_num(3.5));
/// assert_eq!(three_half.round_ties_to_even(), Wrapping(I16F16::from_num(4)));
/// let max = Wrapping(I16F16::max_value());
/// assert_eq!(max.round_ties_to_even(), Wrapping(I16F16::min_value()));
/// ```
pub fn round_ties_to_even(self) -> Wrapping<F> {
Wrapping(self.0.wrapping_round_ties_to_even())
}
}
impl<F: FixedSigned> Wrapping<F> {