From de6ecb578259a89921316015197d3c7950c04107 Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Thu, 29 Aug 2019 10:12:17 +0200 Subject: [PATCH] add Wrapping::round_ties_to_even --- README.md | 8 ++++++++ RELEASES.md | 5 +++++ src/wrapping.rs | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/README.md b/README.md index 77a8df2..8e692e7 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,14 @@ The conversions supported cover the following cases. ## What’s 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`], diff --git a/RELEASES.md b/RELEASES.md index d18d0e4..0d6b52e 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -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) ========================== diff --git a/src/wrapping.rs b/src/wrapping.rs index 6dfbc43..b81766d 100644 --- a/src/wrapping.rs +++ b/src/wrapping.rs @@ -211,6 +211,24 @@ impl Wrapping { pub fn round(self) -> Wrapping { 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 { + Wrapping(self.0.wrapping_round_ties_to_even()) + } } impl Wrapping {