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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
use malachite_base::num::arithmetic::traits::{
RoundToMultipleOfPowerOf2, RoundToMultipleOfPowerOf2Assign,
};
use malachite_base::num::conversion::traits::RoundingFrom;
use malachite_base::rounding_modes::RoundingMode;
use malachite_nz::integer::Integer;
use Rational;
impl RoundToMultipleOfPowerOf2<i64> for Rational {
type Output = Rational;
/// Rounds a [`Rational`] to an integer multiple of $2^k$ according to a specified rounding
/// mode. The [`Rational`] is taken by value.
///
/// Let $q = \frac{x}{2^k}$:
///
/// $f(x, k, \mathrm{Down}) = 2^k \operatorname{sgn}(q) \lfloor |q| \rfloor.$
///
/// $f(x, k, \mathrm{Up}) = 2^k \operatorname{sgn}(q) \lceil |q| \rceil.$
///
/// $f(x, k, \mathrm{Floor}) = 2^k \lfloor q \rfloor.$
///
/// $f(x, k, \mathrm{Ceiling}) = 2^k \lceil q \rceil.$
///
/// $$
/// f(x, k, \mathrm{Nearest}) = \begin{cases}
/// 2^k \lfloor q \rfloor & \text{if} \\quad
/// q - \lfloor q \rfloor < \frac{1}{2} \\\\
/// 2^k \lceil q \rceil & \text{if} \\quad q - \lfloor q \rfloor > \frac{1}{2} \\\\
/// 2^k \lfloor q \rfloor &
/// \text{if} \\quad q - \lfloor q \rfloor =
/// \frac{1}{2} \\ \text{and} \\ \lfloor q \rfloor
/// \\ \text{is even} \\\\
/// 2^k \lceil q \rceil &
/// \text{if} \\quad q - \lfloor q \rfloor =
/// \frac{1}{2} \\ \text{and} \\ \lfloor q \rfloor \\ \text{is odd.}
/// \end{cases}
/// $$
///
/// $f(x, k, \mathrm{Exact}) = 2^k q$, but panics if $q \notin \Z$.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is
/// `max(self.significant_bits(), pow / Limb::WIDTH)`.
///
/// # Panics
/// Panics if `rm` is `Exact`, but `self` is not a multiple of the power of 2.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::RoundToMultipleOfPowerOf2;
/// use malachite_base::rounding_modes::RoundingMode;
/// use malachite_q::Rational;
///
/// let q = Rational::from(std::f64::consts::PI);
/// assert_eq!(
/// q.clone().round_to_multiple_of_power_of_2(-3, RoundingMode::Floor).to_string(),
/// "25/8"
/// );
/// assert_eq!(
/// q.clone().round_to_multiple_of_power_of_2(-3, RoundingMode::Down).to_string(),
/// "25/8"
/// );
/// assert_eq!(
/// q.clone().round_to_multiple_of_power_of_2(-3, RoundingMode::Ceiling).to_string(),
/// "13/4"
/// );
/// assert_eq!(
/// q.clone().round_to_multiple_of_power_of_2(-3, RoundingMode::Up).to_string(),
/// "13/4"
/// );
/// assert_eq!(
/// q.clone().round_to_multiple_of_power_of_2(-3, RoundingMode::Nearest).to_string(),
/// "25/8"
/// );
/// ```
#[inline]
fn round_to_multiple_of_power_of_2(mut self, pow: i64, rm: RoundingMode) -> Rational {
self.round_to_multiple_of_power_of_2_assign(pow, rm);
self
}
}
impl<'a> RoundToMultipleOfPowerOf2<i64> for &'a Rational {
type Output = Rational;
/// Rounds a [`Rational`] to an integer multiple of $2^k$ according to a specified rounding
/// mode. The [`Rational`] is taken by reference.
///
/// Let $q = \frac{x}{2^k}$:
///
/// $f(x, k, \mathrm{Down}) = 2^k \operatorname{sgn}(q) \lfloor |q| \rfloor.$
///
/// $f(x, k, \mathrm{Up}) = 2^k \operatorname{sgn}(q) \lceil |q| \rceil.$
///
/// $f(x, k, \mathrm{Floor}) = 2^k \lfloor q \rfloor.$
///
/// $f(x, k, \mathrm{Ceiling}) = 2^k \lceil q \rceil.$
///
/// $$
/// f(x, k, \mathrm{Nearest}) = \begin{cases}
/// 2^k \lfloor q \rfloor & \text{if} \\quad
/// q - \lfloor q \rfloor < \frac{1}{2} \\\\
/// 2^k \lceil q \rceil & \text{if} \\quad q - \lfloor q \rfloor > \frac{1}{2} \\\\
/// 2^k \lfloor q \rfloor &
/// \text{if} \\quad q - \lfloor q \rfloor =
/// \frac{1}{2} \\ \text{and} \\ \lfloor q \rfloor
/// \\ \text{is even} \\\\
/// 2^k \lceil q \rceil &
/// \text{if} \\quad q - \lfloor q \rfloor =
/// \frac{1}{2} \\ \text{and} \\ \lfloor q \rfloor \\ \text{is odd.}
/// \end{cases}
/// $$
///
/// $f(x, k, \mathrm{Exact}) = 2^k q$, but panics if $q \notin \Z$.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is
/// `max(self.significant_bits(), pow / Limb::WIDTH)`.
///
/// # Panics
/// Panics if `rm` is `Exact`, but `self` is not a multiple of the power of 2.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::RoundToMultipleOfPowerOf2;
/// use malachite_base::rounding_modes::RoundingMode;
/// use malachite_q::Rational;
///
/// let q = Rational::from(std::f64::consts::PI);
/// assert_eq!(
/// (&q).round_to_multiple_of_power_of_2(-3, RoundingMode::Floor).to_string(),
/// "25/8"
/// );
/// assert_eq!(
/// (&q).round_to_multiple_of_power_of_2(-3, RoundingMode::Down).to_string(),
/// "25/8"
/// );
/// assert_eq!(
/// (&q).round_to_multiple_of_power_of_2(-3, RoundingMode::Ceiling).to_string(),
/// "13/4"
/// );
/// assert_eq!(
/// (&q).round_to_multiple_of_power_of_2(-3, RoundingMode::Up).to_string(),
/// "13/4"
/// );
/// assert_eq!(
/// (&q).round_to_multiple_of_power_of_2(-3, RoundingMode::Nearest).to_string(),
/// "25/8"
/// );
/// ```
fn round_to_multiple_of_power_of_2(self, pow: i64, rm: RoundingMode) -> Rational {
Rational::from(Integer::rounding_from(self >> pow, rm)) << pow
}
}
impl RoundToMultipleOfPowerOf2Assign<i64> for Rational {
/// Rounds a [`Rational`] to a multiple of $2^k$ in place, according to a specified rounding
/// mode.
///
/// See the [`RoundToMultipleOfPowerOf2`](RoundToMultipleOfPowerOf2) documentation for details.
///
/// but the latter should be used as it is clearer and more efficient.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is
/// `max(self.significant_bits(), pow / Limb::WIDTH)`.
///
/// # Panics
/// Panics if `rm` is `Exact`, but `self` is not a multiple of the power of 2.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::RoundToMultipleOfPowerOf2Assign;
/// use malachite_base::rounding_modes::RoundingMode;
/// use malachite_q::Rational;
///
/// let q = Rational::from(std::f64::consts::PI);
///
/// let mut x = q.clone();
/// x.round_to_multiple_of_power_of_2_assign(-3, RoundingMode::Floor);
/// assert_eq!(x.to_string(), "25/8");
///
/// let mut x = q.clone();
/// x.round_to_multiple_of_power_of_2_assign(-3, RoundingMode::Down);
/// assert_eq!(x.to_string(), "25/8");
///
/// let mut x = q.clone();
/// x.round_to_multiple_of_power_of_2_assign(-3, RoundingMode::Ceiling);
/// assert_eq!(x.to_string(), "13/4");
///
/// let mut x = q.clone();
/// x.round_to_multiple_of_power_of_2_assign(-3, RoundingMode::Up);
/// assert_eq!(x.to_string(), "13/4");
///
/// let mut x = q.clone();
/// x.round_to_multiple_of_power_of_2_assign(-3, RoundingMode::Nearest);
/// assert_eq!(x.to_string(), "25/8");
/// ```
fn round_to_multiple_of_power_of_2_assign(&mut self, pow: i64, rm: RoundingMode) {
*self >>= pow;
*self = Rational::from(Integer::rounding_from(&*self, rm)) << pow
}
}