1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
use crate::num::arithmetic::traits::NegAssign;
use crate::rounding_modes::RoundingMode;
use core::ops::Neg;
/// Returns the negative of a [`RoundingMode`].
///
/// The negative is defined so that if a [`RoundingMode`] $m$ is used to round the result of an odd
/// function $f$, then $f(x, -m) = -f(-x, m)$. `Floor` and `Ceiling` are swapped, and the other
/// modes are unchanged.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// ```
/// use malachite_base::rounding_modes::RoundingMode;
///
/// assert_eq!(-RoundingMode::Down, RoundingMode::Down);
/// assert_eq!(-RoundingMode::Up, RoundingMode::Up);
/// assert_eq!(-RoundingMode::Floor, RoundingMode::Ceiling);
/// assert_eq!(-RoundingMode::Ceiling, RoundingMode::Floor);
/// assert_eq!(-RoundingMode::Nearest, RoundingMode::Nearest);
/// assert_eq!(-RoundingMode::Exact, RoundingMode::Exact);
/// ```
impl Neg for RoundingMode {
type Output = RoundingMode;
#[inline]
fn neg(self) -> RoundingMode {
match self {
RoundingMode::Floor => RoundingMode::Ceiling,
RoundingMode::Ceiling => RoundingMode::Floor,
rm => rm,
}
}
}
impl NegAssign for RoundingMode {
/// Replaces a [`RoundingMode`] with its negative.
///
/// The negative is defined so that if a [`RoundingMode`] $m$ is used to round the result of an
/// odd function $f$, then $f(x, -m) = -f(-x, m)$. `Floor` and `Ceiling` are swapped, and the
/// other modes are unchanged.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::NegAssign;
/// use malachite_base::rounding_modes::RoundingMode;
///
/// let mut rm = RoundingMode::Down;
/// rm.neg_assign();
/// assert_eq!(rm, RoundingMode::Down);
///
/// let mut rm = RoundingMode::Floor;
/// rm.neg_assign();
/// assert_eq!(rm, RoundingMode::Ceiling);
/// ```
#[inline]
fn neg_assign(&mut self) {
if *self == RoundingMode::Floor {
*self = RoundingMode::Ceiling;
} else if *self == RoundingMode::Ceiling {
*self = RoundingMode::Floor
}
}
}