malachite_float/float/arithmetic/rem.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// Uses code adopted from the GNU MPFR Library.
4//
5// Copyright © 2007-2025 Free Software Foundation, Inc.
6//
7// This file is part of Malachite.
8//
9// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
10// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
11// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
12
13use crate::InnerFloat::{Finite, Infinity, NaN, Zero};
14use crate::{
15 Float, emulate_float_float_to_float_and_i64_fn, emulate_float_float_to_float_fn,
16 emulate_float_to_float_and_i64_fn, emulate_float_to_float_fn, float_either_infinity,
17 float_either_zero, float_nan, significand_bits,
18};
19use core::cmp::Ordering::{self, *};
20use core::cmp::{max, min};
21use core::ops::{Rem, RemAssign};
22use malachite_base::num::arithmetic::traits::{
23 DivMod, ModPow, ModPowerOf2, NegAssign, Parity, PowerOf2,
24};
25use malachite_base::num::basic::floats::PrimitiveFloat;
26use malachite_base::num::basic::traits::{NegativeZero, One, Two, Zero as ZeroTrait};
27use malachite_base::num::conversion::traits::ExactFrom;
28use malachite_base::num::logic::traits::SignificantBits;
29use malachite_base::rounding_modes::RoundingMode::{self, *};
30use malachite_nz::integer::Integer;
31use malachite_nz::natural::Natural;
32use malachite_q::Rational;
33
34// This is mpfr_rem1 from rem1.c, MPFR 4.2.2, with the result's precision passed explicitly, the
35// first rounding mode `rnd_q` (which is always `MPFR_RNDZ` for the fmod family and `MPFR_RNDN` for
36// the remainder family) represented by the `nearest_quotient` flag, and the optional `quo` output
37// always returned (the callers that don't want it discard it).
38//
39// rem1 works as follows: let q = x/y rounded to an integer toward zero if `nearest_quotient` is
40// false, and to the nearest integer (ties to even) if it is true. Put x - q*y in the returned
41// `Float`, rounded to `prec` bits according to `rm`. The returned `i64` has the sign of q, and
42// agrees with q in its 63 low order bits; in other words, quo = q (mod 2^63) and quo * q >= 0. If
43// the remainder is zero, it has the sign of x. The returned `Ordering` gives the place of the
44// rounded remainder relative to x - q*y.
45//
46// If x or y is NaN, or x is infinite, or y is zero: quo is 0 (unspecified in MPFR), and the
47// remainder is NaN. If y is infinite and x is finite, or x is zero and y is nonzero: quo is 0 and
48// the remainder is x rounded to `prec`.
49//
50// Since |x - q*y| <= y/2, no overflow is possible. Only an underflow is possible when y is very
51// small.
52
53fn rem1_helper(
54 x: &Float,
55 y: &Float,
56 nearest_quotient: bool,
57 want_quo: bool,
58 prec: u64,
59 rm: RoundingMode,
60) -> (Float, Ordering, i64) {
61 assert_ne!(prec, 0);
62 match (x, y) {
63 (Float(NaN | Infinity { .. }), _) | (_, Float(NaN | Zero { .. })) => {
64 (float_nan!(), Equal, 0)
65 }
66 (_, float_either_infinity!()) | (float_either_zero!(), _) => {
67 // either y is infinite and x is zero or finite, or x is zero and y is not special; in
68 // both cases the quotient is zero and the remainder is x.
69 let (rem, o) = Float::from_float_prec_round_ref(x, prec, rm);
70 (rem, o, 0)
71 }
72 (
73 Float(Finite {
74 sign: x_sign,
75 exponent: x_exponent,
76 significand: x_significand,
77 ..
78 }),
79 Float(Finite {
80 sign: y_sign,
81 exponent: y_exponent,
82 significand: y_significand,
83 ..
84 }),
85 ) => rem1_core(
86 *x_sign,
87 x_significand,
88 i64::from(*x_exponent) - i64::exact_from(significand_bits(x_significand)),
89 *y_sign,
90 y_significand,
91 i64::from(*y_exponent) - i64::exact_from(significand_bits(y_significand)),
92 &Natural::ONE,
93 nearest_quotient,
94 want_quo,
95 prec,
96 rm,
97 ),
98 }
99}
100
101// The integer-level core shared by the Float-Float and mixed Float-Rational remainder functions:
102// computes the remainder of A by B rounded to `prec` bits with `rm`, where A = a*2^ea with sign
103// `x_sign` and B = b*2^eb with sign `y_sign`, a and b positive integers, dividing the result by the
104// positive integer `den`. The identity rem(x, n/d) = rem(xd, n)/d, which preserves the quotient
105// (and so its parity and low bits), reduces a Rational operand on either side to this form; `den`
106// is 1 in the Float-Float case.
107#[allow(clippy::too_many_arguments)]
108fn rem1_core(
109 x_sign: bool,
110 mx: &Natural,
111 ex: i64,
112 y_sign: bool,
113 b: &Natural,
114 eb: i64,
115 den: &Natural,
116 nearest_quotient: bool,
117 want_quo: bool,
118 prec: u64,
119 rm: RoundingMode,
120) -> (Float, Ordering, i64) {
121 let signx = x_sign;
122 // To get rid of sign problems, we compute the result separately: quo(-x,-y) = quo(x,y),
123 // rem(-x,-y) = -rem(x,y) quo(-x,y) = -quo(x,y), rem(-x,y) = -rem(x,y) thus quo =
124 // sign(x/y)*quo(|x|,|y|), rem = sign(x)*rem(|x|,|y|)
125 let sign = x_sign == y_sign;
126 // A = mx*2^ex, B = my*2^ey
127 let mut ey = eb;
128 let mut q_is_odd = false;
129 let mut quo = 0i64;
130 let mut tiny = false;
131 // Divide my by 2^k if possible to make operations mod my easier. Since the exponents come from
132 // regular floats, due to the constraints on the exponent and the precision, there can be no
133 // integer overflow below.
134 let k = b.trailing_zeros().unwrap();
135 ey += i64::exact_from(k);
136 let mut my = b >> k;
137 let mut r;
138 if ex <= ey {
139 // q = x/y = mx/(my*2^(ey-ex))
140 //
141 // First detect cases where q = 0, to avoid creating a huge number my*2^(ey-ex): if sx =
142 // mx.significant_bits() and sy = my.significant_bits(), we have x < 2^(ex + sx) and y >=
143 // 2^(ey + sy - 1), thus if ex + sx <= ey + sy - 1 the quotient is 0.
144 let q;
145 if ex + i64::exact_from(mx.significant_bits()) < ey + i64::exact_from(my.significant_bits())
146 {
147 tiny = true;
148 q = Natural::ZERO;
149 r = mx.clone();
150 } else {
151 // divide mx by my*2^(ey-ex)
152 my <<= u64::exact_from(ey - ex);
153 // since mx > 0 and my > 0, truncating division is the same as floor division
154 (q, r) = mx.div_mod(&my);
155 // 0 <= r < my
156 }
157 if nearest_quotient {
158 q_is_odd = q.odd();
159 }
160 if want_quo {
161 quo = i64::exact_from(&(&q).mod_power_of_2(63));
162 }
163 } else {
164 // ex > ey
165 if want_quo {
166 // for the quotient-bits variants, to get the low 63 more bits of the quotient, we first
167 // compute R = X mod Y*2^63, where X and Y are defined below. Then the low 63 bits of
168 // the quotient are floor(R/Y).
169 my <<= 63u32;
170 } else if nearest_quotient {
171 // remainder case: let X = mx*2^(ex-ey) and Y = my. Then both X and Y are integers.
172 // Assume X = R mod Y; then x = X*2^ey = R*2^ey mod (Y*2^ey=y). To be able to perform
173 // the rounding, we need the least significant bit of the quotient, i.e., one more bit
174 // in the remainder, which is obtained by dividing by 2Y.
175 my <<= 1u32;
176 }
177 let d = u64::exact_from(ex - ey);
178 r = if d > 3 * my.significant_bits() {
179 // 2^(ex-ey) mod my. When 2^(ex-ey) is at least my^3, modular exponentiation is faster
180 // than the exact power and a single reduction.
181 (&(Natural::TWO % &my)).mod_pow(Natural::from(d), &my)
182 } else {
183 Natural::power_of_2(d)
184 };
185 r = r * mx % &my;
186 if want_quo {
187 // now 0 <= r < 2^63*Y
188 my >>= 63u32;
189 let q;
190 (q, r) = r.div_mod(&my);
191 // oldr = q*my + newr
192 quo = i64::exact_from(&q);
193 q_is_odd = quo.odd();
194 } else if nearest_quotient {
195 // now 0 <= r < 2Y in the remainder case
196 my >>= 1u32;
197 // least significant bit of q
198 q_is_odd = r >= my;
199 if q_is_odd {
200 r -= &my;
201 }
202 }
203 // now 0 <= r < my, and if needed, q_is_odd is the least significant bit of q
204 }
205 if r == 0u32 {
206 // a zero remainder takes the sign of x, and is always exact
207 (
208 if signx {
209 Float::ZERO
210 } else {
211 Float::NEGATIVE_ZERO
212 },
213 Equal,
214 if sign { quo } else { quo.wrapping_neg() },
215 )
216 } else {
217 let mut my = Integer::from(my);
218 let mut r = Integer::from(r);
219 if nearest_quotient {
220 // determine whether 2r is greater than my; both are nonnegative, so plain comparison
221 // mirrors mpz_cmpabs
222 let r2 = &r << 1u32;
223 let c = if tiny {
224 // if tiny, we should compare r with my*2^(ey-ex)
225 if ex + i64::exact_from(r2.significant_bits())
226 < ey + i64::exact_from(my.significant_bits())
227 {
228 // r*2^ex < my*2^ey
229 Less
230 } else {
231 my <<= u64::exact_from(ey - ex);
232 r2.cmp(&my)
233 }
234 } else {
235 r2.cmp(&my)
236 };
237 // if the quotient rounds away, we need to subtract my from r, and add 1 to quo
238 if c == Greater || c == Equal && q_is_odd {
239 r -= &my;
240 if want_quo {
241 // The C code increments a long here, which can overflow; we keep the documented
242 // low-63-bits contract instead.
243 quo = quo.wrapping_add(1) & i64::MAX;
244 }
245 }
246 }
247 // take into account sign of x
248 if !signx {
249 r.neg_assign();
250 }
251 // The result is r*2^sh/den. In the den = 1 case, rounding r to prec bits gives an exponent
252 // of e or e + 1 (on a rounding carry), so when e is strictly inside the representable range
253 // no underflow or overflow is possible: round r once and shift exactly, avoiding the
254 // Rational construction, whose denominator has |sh| bits when sh is negative. The unshifted
255 // rounding of r must be representable too: r can have as many bits as the divisor's
256 // mantissa, which for a divisor of more than 2^30 bits would overflow the intermediate
257 // `Float` before the shift brings it back into range. At the range edges, and whenever den
258 // is not 1, fall back to the Rational conversion, whose single rounding handles underflow.
259 // (Both paths are a single rounding of the same value, so they agree wherever both apply.)
260 let sh = min(ex, ey);
261 let (rem, o) = if *den == 1u32 {
262 let r_bits = i64::exact_from(r.significant_bits());
263 let e = r_bits + sh;
264 if e > Float::MIN_EXPONENT_I64
265 && e < Float::MAX_EXPONENT_I64
266 && r_bits < Float::MAX_EXPONENT_I64
267 {
268 let (rem, o) = Float::from_integer_prec_round(r, prec, rm);
269 (rem << sh, o)
270 } else {
271 Float::from_rational_prec_round(Rational::from(r) << sh, prec, rm)
272 }
273 } else {
274 Float::from_rational_prec_round(
275 Rational::from_integers(r, Integer::from(den)) << sh,
276 prec,
277 rm,
278 )
279 };
280 (rem, o, if sign { quo } else { quo.wrapping_neg() })
281 }
282}
283
284// This is mpfr_fmod_ui from fmod_ui.c, MPFR 4.2.2, generalized over `nearest_quotient` like the
285// helper it wraps. The conversion of `other` to a `Float` is exact, and `rem1_helper` depends only
286// on its arguments' values, so this is a pure thin wrapper. A zero modulus yields NaN, matching
287// mpfr_fmod_ui.
288fn rem_unsigned_helper(
289 x: &Float,
290 other: u64,
291 nearest_quotient: bool,
292 prec: u64,
293 rm: RoundingMode,
294) -> (Float, Ordering) {
295 if other == 0 {
296 (float_nan!(), Equal)
297 } else {
298 let (r, o, _) = rem1_helper(x, &Float::from(other), nearest_quotient, false, prec, rm);
299 (r, o)
300 }
301}
302
303// Shared special-case handling and scaling for the Float-mod-Rational functions. A Rational modulus
304// keeps the reduction exact: converting it to a Float first would perturb the remainder by the
305// quotient times the conversion error. A zero modulus yields NaN, as with a zero Float modulus.
306fn rem_rational_helper(
307 x: &Float,
308 y: &Rational,
309 nearest_quotient: bool,
310 want_quo: bool,
311 prec: u64,
312 rm: RoundingMode,
313) -> (Float, Ordering, i64) {
314 assert_ne!(prec, 0);
315 match x {
316 _ if *y == 0u32 => (float_nan!(), Equal, 0),
317 Float(NaN | Infinity { .. }) => (float_nan!(), Equal, 0),
318 float_either_zero!() => {
319 // the quotient is zero and the remainder is x
320 let (rem, o) = Float::from_float_prec_round_ref(x, prec, rm);
321 (rem, o, 0)
322 }
323 Float(Finite {
324 sign,
325 exponent,
326 significand,
327 ..
328 }) => {
329 let d = y.denominator_ref();
330 rem1_core(
331 *sign,
332 &(significand * d),
333 i64::from(*exponent) - i64::exact_from(significand_bits(significand)),
334 *y > 0u32,
335 y.numerator_ref(),
336 0,
337 d,
338 nearest_quotient,
339 want_quo,
340 prec,
341 rm,
342 )
343 }
344 }
345}
346
347// The reversed direction: the remainder of a Rational by a Float. A zero Rational gives a positive
348// zero (a Rational zero has no sign), an infinite Float modulus returns the Rational rounded, and a
349// NaN or zero Float modulus gives NaN.
350fn rational_rem_float_helper(
351 x: &Rational,
352 y: &Float,
353 nearest_quotient: bool,
354 want_quo: bool,
355 prec: u64,
356 rm: RoundingMode,
357) -> (Float, Ordering, i64) {
358 assert_ne!(prec, 0);
359 match y {
360 Float(NaN | Zero { .. }) => (float_nan!(), Equal, 0),
361 float_either_infinity!() => {
362 // the quotient is zero and the remainder is x
363 let (rem, o) = Float::from_rational_prec_round_ref(x, prec, rm);
364 (rem, o, 0)
365 }
366 Float(Finite {
367 sign,
368 exponent,
369 significand,
370 ..
371 }) => {
372 if *x == 0u32 {
373 (Float::ZERO, Equal, 0)
374 } else {
375 let d = x.denominator_ref();
376 rem1_core(
377 *x > 0u32,
378 x.numerator_ref(),
379 0,
380 *sign,
381 &(significand * d),
382 i64::from(*exponent) - i64::exact_from(significand_bits(significand)),
383 d,
384 nearest_quotient,
385 want_quo,
386 prec,
387 rm,
388 )
389 }
390 }
391 }
392}
393
394impl Float {
395 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
396 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the specified
397 /// precision and with the specified rounding mode. Both [`Float`]s are taken by value. An
398 /// [`Ordering`] is also returned, indicating whether the rounded remainder is less than, equal
399 /// to, or greater than the exact remainder. Although `NaN`s are not comparable to any
400 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
401 ///
402 /// $$
403 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
404 /// $$
405 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
406 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
407 ///
408 /// If the output has a precision, it is `prec`.
409 ///
410 /// Special cases:
411 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
412 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
413 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
414 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
415 ///
416 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
417 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
418 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
419 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
420 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
421 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
422 ///
423 /// If you know you'll be using `Nearest`, consider using [`Float::rem_prec`] instead. If you
424 /// know that your target precision is the maximum of the precisions of the two inputs, consider
425 /// using [`Float::rem_round`] instead. If both of these things are true, consider using `%`
426 /// instead.
427 ///
428 /// # Worst-case complexity
429 /// $T(n) = O(n \log n \log\log n)$
430 ///
431 /// $M(n) = O(n)$
432 ///
433 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
434 /// other.complexity(), prec)`.
435 ///
436 /// # Panics
437 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
438 /// with `prec` bits.
439 ///
440 /// # Examples
441 /// ```
442 /// use core::cmp::Ordering::*;
443 /// use malachite_base::rounding_modes::RoundingMode::*;
444 /// use malachite_float::Float;
445 ///
446 /// let (r, o) = Float::from(10u32).rem_prec_round(Float::from(7u32), 1, Floor);
447 /// assert_eq!(r.to_string(), "2.0");
448 /// assert_eq!(o, Less);
449 ///
450 /// let (r, o) = Float::from(10u32).rem_prec_round(Float::from(7u32), 1, Ceiling);
451 /// assert_eq!(r.to_string(), "4.0");
452 /// assert_eq!(o, Greater);
453 /// ```
454 #[allow(clippy::needless_pass_by_value)]
455 pub fn rem_prec_round(self, other: Self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
456 let (r, o, _) = rem1_helper(&self, &other, false, false, prec, rm);
457 (r, o)
458 }
459
460 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
461 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the specified
462 /// precision and with the specified rounding mode. The first [`Float`] is taken by value and
463 /// the second by reference. An [`Ordering`] is also returned, indicating whether the rounded
464 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
465 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
466 /// `Equal`.
467 ///
468 /// $$
469 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
470 /// $$
471 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
472 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
473 ///
474 /// If the output has a precision, it is `prec`.
475 ///
476 /// Special cases:
477 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
478 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
479 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
480 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
481 ///
482 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
483 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
484 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
485 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
486 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
487 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
488 ///
489 /// If you know you'll be using `Nearest`, consider using [`Float::rem_prec`] instead. If you
490 /// know that your target precision is the maximum of the precisions of the two inputs, consider
491 /// using [`Float::rem_round`] instead. If both of these things are true, consider using `%`
492 /// instead.
493 ///
494 /// # Worst-case complexity
495 /// $T(n) = O(n \log n \log\log n)$
496 ///
497 /// $M(n) = O(n)$
498 ///
499 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
500 /// other.complexity(), prec)`.
501 ///
502 /// # Panics
503 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
504 /// with `prec` bits.
505 ///
506 /// # Examples
507 /// ```
508 /// use core::cmp::Ordering::*;
509 /// use malachite_base::rounding_modes::RoundingMode::*;
510 /// use malachite_float::Float;
511 ///
512 /// let (r, o) = Float::from(10u32).rem_prec_round_val_ref(&Float::from(7u32), 1, Floor);
513 /// assert_eq!(r.to_string(), "2.0");
514 /// assert_eq!(o, Less);
515 ///
516 /// let (r, o) = Float::from(10u32).rem_prec_round_val_ref(&Float::from(7u32), 1, Ceiling);
517 /// assert_eq!(r.to_string(), "4.0");
518 /// assert_eq!(o, Greater);
519 /// ```
520 pub fn rem_prec_round_val_ref(
521 self,
522 other: &Self,
523 prec: u64,
524 rm: RoundingMode,
525 ) -> (Self, Ordering) {
526 let (r, o, _) = rem1_helper(&self, other, false, false, prec, rm);
527 (r, o)
528 }
529
530 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
531 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the specified
532 /// precision and with the specified rounding mode. The first [`Float`] is taken by reference
533 /// and the second by value. An [`Ordering`] is also returned, indicating whether the rounded
534 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
535 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
536 /// `Equal`.
537 ///
538 /// $$
539 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
540 /// $$
541 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
542 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
543 ///
544 /// If the output has a precision, it is `prec`.
545 ///
546 /// Special cases:
547 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
548 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
549 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
550 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
551 ///
552 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
553 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
554 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
555 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
556 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
557 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
558 ///
559 /// If you know you'll be using `Nearest`, consider using [`Float::rem_prec`] instead. If you
560 /// know that your target precision is the maximum of the precisions of the two inputs, consider
561 /// using [`Float::rem_round`] instead. If both of these things are true, consider using `%`
562 /// instead.
563 ///
564 /// # Worst-case complexity
565 /// $T(n) = O(n \log n \log\log n)$
566 ///
567 /// $M(n) = O(n)$
568 ///
569 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
570 /// other.complexity(), prec)`.
571 ///
572 /// # Panics
573 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
574 /// with `prec` bits.
575 ///
576 /// # Examples
577 /// ```
578 /// use core::cmp::Ordering::*;
579 /// use malachite_base::rounding_modes::RoundingMode::*;
580 /// use malachite_float::Float;
581 ///
582 /// let (r, o) = Float::from(10u32).rem_prec_round_ref_val(Float::from(7u32), 1, Floor);
583 /// assert_eq!(r.to_string(), "2.0");
584 /// assert_eq!(o, Less);
585 ///
586 /// let (r, o) = Float::from(10u32).rem_prec_round_ref_val(Float::from(7u32), 1, Ceiling);
587 /// assert_eq!(r.to_string(), "4.0");
588 /// assert_eq!(o, Greater);
589 /// ```
590 #[allow(clippy::needless_pass_by_value)]
591 pub fn rem_prec_round_ref_val(
592 &self,
593 other: Self,
594 prec: u64,
595 rm: RoundingMode,
596 ) -> (Self, Ordering) {
597 let (r, o, _) = rem1_helper(self, &other, false, false, prec, rm);
598 (r, o)
599 }
600
601 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
602 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the specified
603 /// precision and with the specified rounding mode. Both [`Float`]s are taken by reference. An
604 /// [`Ordering`] is also returned, indicating whether the rounded remainder is less than, equal
605 /// to, or greater than the exact remainder. Although `NaN`s are not comparable to any
606 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
607 ///
608 /// $$
609 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
610 /// $$
611 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
612 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
613 ///
614 /// If the output has a precision, it is `prec`.
615 ///
616 /// Special cases:
617 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
618 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
619 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
620 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
621 ///
622 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
623 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
624 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
625 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
626 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
627 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
628 ///
629 /// If you know you'll be using `Nearest`, consider using [`Float::rem_prec`] instead. If you
630 /// know that your target precision is the maximum of the precisions of the two inputs, consider
631 /// using [`Float::rem_round`] instead. If both of these things are true, consider using `%`
632 /// instead.
633 ///
634 /// # Worst-case complexity
635 /// $T(n) = O(n \log n \log\log n)$
636 ///
637 /// $M(n) = O(n)$
638 ///
639 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
640 /// other.complexity(), prec)`.
641 ///
642 /// # Panics
643 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
644 /// with `prec` bits.
645 ///
646 /// # Examples
647 /// ```
648 /// use core::cmp::Ordering::*;
649 /// use malachite_base::rounding_modes::RoundingMode::*;
650 /// use malachite_float::Float;
651 ///
652 /// let (r, o) = Float::from(10u32).rem_prec_round_ref_ref(&Float::from(7u32), 1, Floor);
653 /// assert_eq!(r.to_string(), "2.0");
654 /// assert_eq!(o, Less);
655 ///
656 /// let (r, o) = Float::from(10u32).rem_prec_round_ref_ref(&Float::from(7u32), 1, Ceiling);
657 /// assert_eq!(r.to_string(), "4.0");
658 /// assert_eq!(o, Greater);
659 /// ```
660 pub fn rem_prec_round_ref_ref(
661 &self,
662 other: &Self,
663 prec: u64,
664 rm: RoundingMode,
665 ) -> (Self, Ordering) {
666 let (r, o, _) = rem1_helper(self, other, false, false, prec, rm);
667 (r, o)
668 }
669
670 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
671 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the nearest value
672 /// of the specified precision. Both [`Float`]s are taken by value. An [`Ordering`] is also
673 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
674 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
675 /// function returns a `NaN` it also returns `Equal`.
676 ///
677 /// $$
678 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
679 /// $$
680 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
681 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
682 ///
683 /// If the output has a precision, it is `prec`.
684 ///
685 /// Special cases:
686 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
687 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
688 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
689 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
690 ///
691 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
692 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
693 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
694 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
695 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
696 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
697 ///
698 /// If you want to use a rounding mode other than `Nearest`, consider using
699 /// [`Float::rem_prec_round`] instead. If you know that your target precision is the maximum of
700 /// the precisions of the two inputs, consider using `%` instead.
701 ///
702 /// # Worst-case complexity
703 /// $T(n) = O(n \log n \log\log n)$
704 ///
705 /// $M(n) = O(n)$
706 ///
707 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
708 /// other.complexity(), prec)`.
709 ///
710 /// # Panics
711 /// Panics if `prec` is zero.
712 ///
713 /// # Examples
714 /// ```
715 /// use core::cmp::Ordering::*;
716 /// use malachite_float::Float;
717 ///
718 /// let (r, o) = Float::from(10u32).rem_prec(Float::from(7u32), 1);
719 /// assert_eq!(r.to_string(), "4.0");
720 /// assert_eq!(o, Greater);
721 ///
722 /// let (r, o) = Float::from(10u32).rem_prec(Float::from(7u32), 2);
723 /// assert_eq!(r.to_string(), "3.0");
724 /// assert_eq!(o, Equal);
725 /// ```
726 #[inline]
727 pub fn rem_prec(self, other: Self, prec: u64) -> (Self, Ordering) {
728 self.rem_prec_round(other, prec, Nearest)
729 }
730
731 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
732 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the nearest value
733 /// of the specified precision. The first [`Float`] is taken by value and the second by
734 /// reference. An [`Ordering`] is also returned, indicating whether the rounded remainder is
735 /// less than, equal to, or greater than the exact remainder. Although `NaN`s are not comparable
736 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
737 ///
738 /// $$
739 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
740 /// $$
741 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
742 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
743 ///
744 /// If the output has a precision, it is `prec`.
745 ///
746 /// Special cases:
747 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
748 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
749 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
750 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
751 ///
752 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
753 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
754 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
755 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
756 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
757 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
758 ///
759 /// If you want to use a rounding mode other than `Nearest`, consider using
760 /// [`Float::rem_prec_round`] instead. If you know that your target precision is the maximum of
761 /// the precisions of the two inputs, consider using `%` instead.
762 ///
763 /// # Worst-case complexity
764 /// $T(n) = O(n \log n \log\log n)$
765 ///
766 /// $M(n) = O(n)$
767 ///
768 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
769 /// other.complexity(), prec)`.
770 ///
771 /// # Panics
772 /// Panics if `prec` is zero.
773 ///
774 /// # Examples
775 /// ```
776 /// use core::cmp::Ordering::*;
777 /// use malachite_float::Float;
778 ///
779 /// let (r, o) = Float::from(10u32).rem_prec_val_ref(&Float::from(7u32), 1);
780 /// assert_eq!(r.to_string(), "4.0");
781 /// assert_eq!(o, Greater);
782 ///
783 /// let (r, o) = Float::from(10u32).rem_prec_val_ref(&Float::from(7u32), 2);
784 /// assert_eq!(r.to_string(), "3.0");
785 /// assert_eq!(o, Equal);
786 /// ```
787 #[inline]
788 pub fn rem_prec_val_ref(self, other: &Self, prec: u64) -> (Self, Ordering) {
789 self.rem_prec_round_val_ref(other, prec, Nearest)
790 }
791
792 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
793 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the nearest value
794 /// of the specified precision. The first [`Float`] is taken by reference and the second by
795 /// value. An [`Ordering`] is also returned, indicating whether the rounded remainder is less
796 /// than, equal to, or greater than the exact remainder. Although `NaN`s are not comparable to
797 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
798 ///
799 /// $$
800 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
801 /// $$
802 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
803 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
804 ///
805 /// If the output has a precision, it is `prec`.
806 ///
807 /// Special cases:
808 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
809 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
810 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
811 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
812 ///
813 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
814 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
815 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
816 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
817 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
818 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
819 ///
820 /// If you want to use a rounding mode other than `Nearest`, consider using
821 /// [`Float::rem_prec_round`] instead. If you know that your target precision is the maximum of
822 /// the precisions of the two inputs, consider using `%` instead.
823 ///
824 /// # Worst-case complexity
825 /// $T(n) = O(n \log n \log\log n)$
826 ///
827 /// $M(n) = O(n)$
828 ///
829 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
830 /// other.complexity(), prec)`.
831 ///
832 /// # Panics
833 /// Panics if `prec` is zero.
834 ///
835 /// # Examples
836 /// ```
837 /// use core::cmp::Ordering::*;
838 /// use malachite_float::Float;
839 ///
840 /// let (r, o) = Float::from(10u32).rem_prec_ref_val(Float::from(7u32), 1);
841 /// assert_eq!(r.to_string(), "4.0");
842 /// assert_eq!(o, Greater);
843 ///
844 /// let (r, o) = Float::from(10u32).rem_prec_ref_val(Float::from(7u32), 2);
845 /// assert_eq!(r.to_string(), "3.0");
846 /// assert_eq!(o, Equal);
847 /// ```
848 #[inline]
849 pub fn rem_prec_ref_val(&self, other: Self, prec: u64) -> (Self, Ordering) {
850 self.rem_prec_round_ref_val(other, prec, Nearest)
851 }
852
853 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
854 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the nearest value
855 /// of the specified precision. Both [`Float`]s are taken by reference. An [`Ordering`] is also
856 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
857 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
858 /// function returns a `NaN` it also returns `Equal`.
859 ///
860 /// $$
861 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
862 /// $$
863 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
864 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
865 ///
866 /// If the output has a precision, it is `prec`.
867 ///
868 /// Special cases:
869 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
870 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
871 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
872 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
873 ///
874 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
875 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
876 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
877 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
878 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
879 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
880 ///
881 /// If you want to use a rounding mode other than `Nearest`, consider using
882 /// [`Float::rem_prec_round`] instead. If you know that your target precision is the maximum of
883 /// the precisions of the two inputs, consider using `%` instead.
884 ///
885 /// # Worst-case complexity
886 /// $T(n) = O(n \log n \log\log n)$
887 ///
888 /// $M(n) = O(n)$
889 ///
890 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
891 /// other.complexity(), prec)`.
892 ///
893 /// # Panics
894 /// Panics if `prec` is zero.
895 ///
896 /// # Examples
897 /// ```
898 /// use core::cmp::Ordering::*;
899 /// use malachite_float::Float;
900 ///
901 /// let (r, o) = Float::from(10u32).rem_prec_ref_ref(&Float::from(7u32), 1);
902 /// assert_eq!(r.to_string(), "4.0");
903 /// assert_eq!(o, Greater);
904 ///
905 /// let (r, o) = Float::from(10u32).rem_prec_ref_ref(&Float::from(7u32), 2);
906 /// assert_eq!(r.to_string(), "3.0");
907 /// assert_eq!(o, Equal);
908 /// ```
909 #[inline]
910 pub fn rem_prec_ref_ref(&self, other: &Self, prec: u64) -> (Self, Ordering) {
911 self.rem_prec_round_ref_ref(other, prec, Nearest)
912 }
913
914 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
915 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the maximum of
916 /// the precisions of the inputs, with the specified rounding mode. Both [`Float`]s are taken by
917 /// value. An [`Ordering`] is also returned, indicating whether the rounded remainder is less
918 /// than, equal to, or greater than the exact remainder. Although `NaN`s are not comparable to
919 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
920 ///
921 /// $$
922 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
923 /// $$
924 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
925 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
926 ///
927 /// If the output has a precision, it is the maximum of the precisions of the inputs.
928 ///
929 /// Special cases:
930 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
931 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
932 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
933 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
934 ///
935 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
936 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
937 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
938 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
939 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
940 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
941 ///
942 /// If you want to specify an output precision, consider using [`Float::rem_prec_round`]
943 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `%`
944 /// instead.
945 ///
946 /// # Worst-case complexity
947 /// $T(n) = O(n \log n \log\log n)$
948 ///
949 /// $M(n) = O(n)$
950 ///
951 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
952 /// other.complexity())`.
953 ///
954 /// # Panics
955 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
956 /// precision.
957 ///
958 /// # Examples
959 /// ```
960 /// use core::cmp::Ordering::*;
961 /// use malachite_base::rounding_modes::RoundingMode::*;
962 /// use malachite_float::Float;
963 ///
964 /// let (r, o) = Float::from(10u32).rem_round(Float::from(7u32), Floor);
965 /// assert_eq!(r.to_string(), "3.0");
966 /// assert_eq!(o, Equal);
967 ///
968 /// let (r, o) = (-Float::from(10u32)).rem_round(Float::from(7u32), Floor);
969 /// assert_eq!(r.to_string(), "-3.0");
970 /// assert_eq!(o, Equal);
971 /// ```
972 pub fn rem_round(self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
973 let prec = max(self.significant_bits(), other.significant_bits());
974 self.rem_prec_round(other, prec, rm)
975 }
976
977 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
978 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the maximum of
979 /// the precisions of the inputs, with the specified rounding mode. The first [`Float`] is taken
980 /// by value and the second by reference. An [`Ordering`] is also returned, indicating whether
981 /// the rounded remainder is less than, equal to, or greater than the exact remainder. Although
982 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
983 /// returns `Equal`.
984 ///
985 /// $$
986 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
987 /// $$
988 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
989 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
990 ///
991 /// If the output has a precision, it is the maximum of the precisions of the inputs.
992 ///
993 /// Special cases:
994 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
995 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
996 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
997 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
998 ///
999 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1000 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1001 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1002 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1003 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1004 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1005 ///
1006 /// If you want to specify an output precision, consider using [`Float::rem_prec_round`]
1007 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `%`
1008 /// instead.
1009 ///
1010 /// # Worst-case complexity
1011 /// $T(n) = O(n \log n \log\log n)$
1012 ///
1013 /// $M(n) = O(n)$
1014 ///
1015 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1016 /// other.complexity())`.
1017 ///
1018 /// # Panics
1019 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
1020 /// precision.
1021 ///
1022 /// # Examples
1023 /// ```
1024 /// use core::cmp::Ordering::*;
1025 /// use malachite_base::rounding_modes::RoundingMode::*;
1026 /// use malachite_float::Float;
1027 ///
1028 /// let (r, o) = Float::from(10u32).rem_round_val_ref(&Float::from(7u32), Floor);
1029 /// assert_eq!(r.to_string(), "3.0");
1030 /// assert_eq!(o, Equal);
1031 ///
1032 /// let (r, o) = (-Float::from(10u32)).rem_round_val_ref(&Float::from(7u32), Floor);
1033 /// assert_eq!(r.to_string(), "-3.0");
1034 /// assert_eq!(o, Equal);
1035 /// ```
1036 pub fn rem_round_val_ref(self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
1037 let prec = max(self.significant_bits(), other.significant_bits());
1038 self.rem_prec_round_val_ref(other, prec, rm)
1039 }
1040
1041 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
1042 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the maximum of
1043 /// the precisions of the inputs, with the specified rounding mode. The first [`Float`] is taken
1044 /// by reference and the second by value. An [`Ordering`] is also returned, indicating whether
1045 /// the rounded remainder is less than, equal to, or greater than the exact remainder. Although
1046 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
1047 /// returns `Equal`.
1048 ///
1049 /// $$
1050 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
1051 /// $$
1052 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
1053 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
1054 ///
1055 /// If the output has a precision, it is the maximum of the precisions of the inputs.
1056 ///
1057 /// Special cases:
1058 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
1059 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
1060 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
1061 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
1062 ///
1063 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1064 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1065 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1066 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1067 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1068 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1069 ///
1070 /// If you want to specify an output precision, consider using [`Float::rem_prec_round`]
1071 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `%`
1072 /// instead.
1073 ///
1074 /// # Worst-case complexity
1075 /// $T(n) = O(n \log n \log\log n)$
1076 ///
1077 /// $M(n) = O(n)$
1078 ///
1079 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1080 /// other.complexity())`.
1081 ///
1082 /// # Panics
1083 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
1084 /// precision.
1085 ///
1086 /// # Examples
1087 /// ```
1088 /// use core::cmp::Ordering::*;
1089 /// use malachite_base::rounding_modes::RoundingMode::*;
1090 /// use malachite_float::Float;
1091 ///
1092 /// let (r, o) = Float::from(10u32).rem_round_ref_val(Float::from(7u32), Floor);
1093 /// assert_eq!(r.to_string(), "3.0");
1094 /// assert_eq!(o, Equal);
1095 ///
1096 /// let (r, o) = (-Float::from(10u32)).rem_round_ref_val(Float::from(7u32), Floor);
1097 /// assert_eq!(r.to_string(), "-3.0");
1098 /// assert_eq!(o, Equal);
1099 /// ```
1100 pub fn rem_round_ref_val(&self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
1101 let prec = max(self.significant_bits(), other.significant_bits());
1102 self.rem_prec_round_ref_val(other, prec, rm)
1103 }
1104
1105 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
1106 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the maximum of
1107 /// the precisions of the inputs, with the specified rounding mode. Both [`Float`]s are taken by
1108 /// reference. An [`Ordering`] is also returned, indicating whether the rounded remainder is
1109 /// less than, equal to, or greater than the exact remainder. Although `NaN`s are not comparable
1110 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1111 ///
1112 /// $$
1113 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
1114 /// $$
1115 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
1116 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
1117 ///
1118 /// If the output has a precision, it is the maximum of the precisions of the inputs.
1119 ///
1120 /// Special cases:
1121 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
1122 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
1123 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
1124 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
1125 ///
1126 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1127 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1128 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1129 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1130 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1131 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1132 ///
1133 /// If you want to specify an output precision, consider using [`Float::rem_prec_round`]
1134 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `%`
1135 /// instead.
1136 ///
1137 /// # Worst-case complexity
1138 /// $T(n) = O(n \log n \log\log n)$
1139 ///
1140 /// $M(n) = O(n)$
1141 ///
1142 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1143 /// other.complexity())`.
1144 ///
1145 /// # Panics
1146 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
1147 /// precision.
1148 ///
1149 /// # Examples
1150 /// ```
1151 /// use core::cmp::Ordering::*;
1152 /// use malachite_base::rounding_modes::RoundingMode::*;
1153 /// use malachite_float::Float;
1154 ///
1155 /// let (r, o) = Float::from(10u32).rem_round_ref_ref(&Float::from(7u32), Floor);
1156 /// assert_eq!(r.to_string(), "3.0");
1157 /// assert_eq!(o, Equal);
1158 ///
1159 /// let (r, o) = (-Float::from(10u32)).rem_round_ref_ref(&Float::from(7u32), Floor);
1160 /// assert_eq!(r.to_string(), "-3.0");
1161 /// assert_eq!(o, Equal);
1162 /// ```
1163 pub fn rem_round_ref_ref(&self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
1164 let prec = max(self.significant_bits(), other.significant_bits());
1165 self.rem_prec_round_ref_ref(other, prec, rm)
1166 }
1167
1168 /// Computes the remainder of two [`Float`]s in place, with the quotient rounded toward zero, as
1169 /// for the `%` operator on primitive floats and C's `fmod`, rounding the remainder to the
1170 /// specified precision and with the specified rounding mode. The [`Float`] on the right-hand
1171 /// side is taken by value. An [`Ordering`] is returned, indicating whether the rounded
1172 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
1173 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
1174 /// `Equal`.
1175 ///
1176 /// $$
1177 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
1178 /// $$
1179 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
1180 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
1181 ///
1182 /// If the output has a precision, it is `prec`.
1183 ///
1184 /// Special cases:
1185 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
1186 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
1187 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
1188 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
1189 ///
1190 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1191 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1192 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1193 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1194 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1195 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1196 ///
1197 /// # Worst-case complexity
1198 /// $T(n) = O(n \log n \log\log n)$
1199 ///
1200 /// $M(n) = O(n)$
1201 ///
1202 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1203 /// other.complexity(), prec)`.
1204 ///
1205 /// # Panics
1206 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
1207 /// with `prec` bits.
1208 ///
1209 /// # Examples
1210 /// ```
1211 /// use core::cmp::Ordering::*;
1212 /// use malachite_base::rounding_modes::RoundingMode::*;
1213 /// use malachite_float::Float;
1214 ///
1215 /// let mut x = Float::from(10u32);
1216 /// assert_eq!(x.rem_prec_round_assign(Float::from(7u32), 1, Floor), Less);
1217 /// assert_eq!(x.to_string(), "2.0");
1218 /// ```
1219 #[allow(clippy::needless_pass_by_value)]
1220 pub fn rem_prec_round_assign(&mut self, other: Self, prec: u64, rm: RoundingMode) -> Ordering {
1221 let (r, o, _) = rem1_helper(self, &other, false, false, prec, rm);
1222 *self = r;
1223 o
1224 }
1225
1226 /// Computes the remainder of two [`Float`]s in place, with the quotient rounded toward zero, as
1227 /// for the `%` operator on primitive floats and C's `fmod`, rounding the remainder to the
1228 /// specified precision and with the specified rounding mode. The [`Float`] on the right-hand
1229 /// side is taken by reference. An [`Ordering`] is returned, indicating whether the rounded
1230 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
1231 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
1232 /// `Equal`.
1233 ///
1234 /// $$
1235 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
1236 /// $$
1237 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
1238 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
1239 ///
1240 /// If the output has a precision, it is `prec`.
1241 ///
1242 /// Special cases:
1243 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
1244 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
1245 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
1246 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
1247 ///
1248 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1249 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1250 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1251 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1252 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1253 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1254 ///
1255 /// # Worst-case complexity
1256 /// $T(n) = O(n \log n \log\log n)$
1257 ///
1258 /// $M(n) = O(n)$
1259 ///
1260 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1261 /// other.complexity(), prec)`.
1262 ///
1263 /// # Panics
1264 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
1265 /// with `prec` bits.
1266 ///
1267 /// # Examples
1268 /// ```
1269 /// use core::cmp::Ordering::*;
1270 /// use malachite_base::rounding_modes::RoundingMode::*;
1271 /// use malachite_float::Float;
1272 ///
1273 /// let mut x = Float::from(10u32);
1274 /// assert_eq!(
1275 /// x.rem_prec_round_assign_ref(&Float::from(7u32), 1, Floor),
1276 /// Less
1277 /// );
1278 /// assert_eq!(x.to_string(), "2.0");
1279 /// ```
1280 pub fn rem_prec_round_assign_ref(
1281 &mut self,
1282 other: &Self,
1283 prec: u64,
1284 rm: RoundingMode,
1285 ) -> Ordering {
1286 let (r, o, _) = rem1_helper(self, other, false, false, prec, rm);
1287 *self = r;
1288 o
1289 }
1290
1291 /// Computes the remainder of two [`Float`]s in place, with the quotient rounded toward zero, as
1292 /// for the `%` operator on primitive floats and C's `fmod`, rounding the remainder to the
1293 /// nearest value of the specified precision. The [`Float`] on the right-hand side is taken by
1294 /// value. An [`Ordering`] is returned, indicating whether the rounded remainder is less than,
1295 /// equal to, or greater than the exact remainder. Although `NaN`s are not comparable to any
1296 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1297 ///
1298 /// $$
1299 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
1300 /// $$
1301 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
1302 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
1303 ///
1304 /// If the output has a precision, it is `prec`.
1305 ///
1306 /// Special cases:
1307 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
1308 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
1309 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
1310 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
1311 ///
1312 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1313 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1314 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1315 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1316 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1317 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1318 ///
1319 /// # Worst-case complexity
1320 /// $T(n) = O(n \log n \log\log n)$
1321 ///
1322 /// $M(n) = O(n)$
1323 ///
1324 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1325 /// other.complexity(), prec)`.
1326 ///
1327 /// # Panics
1328 /// Panics if `prec` is zero.
1329 ///
1330 /// # Examples
1331 /// ```
1332 /// use core::cmp::Ordering::*;
1333 /// use malachite_float::Float;
1334 ///
1335 /// let mut x = Float::from(10u32);
1336 /// assert_eq!(x.rem_prec_assign(Float::from(7u32), 2), Equal);
1337 /// assert_eq!(x.to_string(), "3.0");
1338 /// ```
1339 #[inline]
1340 pub fn rem_prec_assign(&mut self, other: Self, prec: u64) -> Ordering {
1341 self.rem_prec_round_assign(other, prec, Nearest)
1342 }
1343
1344 /// Computes the remainder of two [`Float`]s in place, with the quotient rounded toward zero, as
1345 /// for the `%` operator on primitive floats and C's `fmod`, rounding the remainder to the
1346 /// nearest value of the specified precision. The [`Float`] on the right-hand side is taken by
1347 /// reference. An [`Ordering`] is returned, indicating whether the rounded remainder is less
1348 /// than, equal to, or greater than the exact remainder. Although `NaN`s are not comparable to
1349 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1350 ///
1351 /// $$
1352 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
1353 /// $$
1354 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
1355 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
1356 ///
1357 /// If the output has a precision, it is `prec`.
1358 ///
1359 /// Special cases:
1360 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
1361 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
1362 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
1363 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
1364 ///
1365 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1366 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1367 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1368 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1369 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1370 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1371 ///
1372 /// # Worst-case complexity
1373 /// $T(n) = O(n \log n \log\log n)$
1374 ///
1375 /// $M(n) = O(n)$
1376 ///
1377 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1378 /// other.complexity(), prec)`.
1379 ///
1380 /// # Panics
1381 /// Panics if `prec` is zero.
1382 ///
1383 /// # Examples
1384 /// ```
1385 /// use core::cmp::Ordering::*;
1386 /// use malachite_float::Float;
1387 ///
1388 /// let mut x = Float::from(10u32);
1389 /// assert_eq!(x.rem_prec_assign_ref(&Float::from(7u32), 2), Equal);
1390 /// assert_eq!(x.to_string(), "3.0");
1391 /// ```
1392 #[inline]
1393 pub fn rem_prec_assign_ref(&mut self, other: &Self, prec: u64) -> Ordering {
1394 self.rem_prec_round_assign_ref(other, prec, Nearest)
1395 }
1396
1397 /// Computes the remainder of two [`Float`]s in place, with the quotient rounded toward zero, as
1398 /// for the `%` operator on primitive floats and C's `fmod`, rounding the remainder to the
1399 /// maximum of the precisions of the inputs, with the specified rounding mode. The [`Float`] on
1400 /// the right-hand side is taken by value. An [`Ordering`] is returned, indicating whether the
1401 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
1402 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
1403 /// returns `Equal`.
1404 ///
1405 /// $$
1406 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
1407 /// $$
1408 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
1409 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
1410 ///
1411 /// If the output has a precision, it is the maximum of the precisions of the inputs.
1412 ///
1413 /// Special cases:
1414 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
1415 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
1416 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
1417 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
1418 ///
1419 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1420 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1421 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1422 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1423 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1424 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1425 ///
1426 /// # Worst-case complexity
1427 /// $T(n) = O(n \log n \log\log n)$
1428 ///
1429 /// $M(n) = O(n)$
1430 ///
1431 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1432 /// other.complexity())`.
1433 ///
1434 /// # Panics
1435 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
1436 /// precision.
1437 ///
1438 /// # Examples
1439 /// ```
1440 /// use core::cmp::Ordering::*;
1441 /// use malachite_base::rounding_modes::RoundingMode::*;
1442 /// use malachite_float::Float;
1443 ///
1444 /// let mut x = Float::from(10u32);
1445 /// assert_eq!(x.rem_round_assign(Float::from(7u32), Floor), Equal);
1446 /// assert_eq!(x.to_string(), "3.0");
1447 /// ```
1448 pub fn rem_round_assign(&mut self, other: Self, rm: RoundingMode) -> Ordering {
1449 let prec = max(self.significant_bits(), other.significant_bits());
1450 self.rem_prec_round_assign(other, prec, rm)
1451 }
1452
1453 /// Computes the remainder of two [`Float`]s in place, with the quotient rounded toward zero, as
1454 /// for the `%` operator on primitive floats and C's `fmod`, rounding the remainder to the
1455 /// maximum of the precisions of the inputs, with the specified rounding mode. The [`Float`] on
1456 /// the right-hand side is taken by reference. An [`Ordering`] is returned, indicating whether
1457 /// the rounded remainder is less than, equal to, or greater than the exact remainder. Although
1458 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
1459 /// returns `Equal`.
1460 ///
1461 /// $$
1462 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
1463 /// $$
1464 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
1465 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
1466 ///
1467 /// If the output has a precision, it is the maximum of the precisions of the inputs.
1468 ///
1469 /// Special cases:
1470 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
1471 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
1472 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
1473 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
1474 ///
1475 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1476 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1477 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1478 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1479 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1480 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1481 ///
1482 /// # Worst-case complexity
1483 /// $T(n) = O(n \log n \log\log n)$
1484 ///
1485 /// $M(n) = O(n)$
1486 ///
1487 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1488 /// other.complexity())`.
1489 ///
1490 /// # Panics
1491 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
1492 /// precision.
1493 ///
1494 /// # Examples
1495 /// ```
1496 /// use core::cmp::Ordering::*;
1497 /// use malachite_base::rounding_modes::RoundingMode::*;
1498 /// use malachite_float::Float;
1499 ///
1500 /// let mut x = Float::from(10u32);
1501 /// assert_eq!(x.rem_round_assign_ref(&Float::from(7u32), Floor), Equal);
1502 /// assert_eq!(x.to_string(), "3.0");
1503 /// ```
1504 pub fn rem_round_assign_ref(&mut self, other: &Self, rm: RoundingMode) -> Ordering {
1505 let prec = max(self.significant_bits(), other.significant_bits());
1506 self.rem_prec_round_assign_ref(other, prec, rm)
1507 }
1508
1509 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
1510 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the specified
1511 /// precision and with the specified rounding mode. Both [`Float`]s are taken by value. An
1512 /// [`Ordering`] is also returned, indicating whether the rounded remainder is less than, equal
1513 /// to, or greater than the exact remainder, along with the low bits of the quotient as an
1514 /// `i64`. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
1515 /// `NaN` it also returns `Equal`.
1516 ///
1517 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
1518 /// it equals $\pm(|q|\bmod 2^{63})$. (MPFR documents the same contract for its `quo` output,
1519 /// but its C implementation can overflow a `long` when the low 63 bits are all ones and the
1520 /// quotient rounds away from zero; this implementation always keeps the modular contract.)
1521 ///
1522 /// $$
1523 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
1524 /// $$
1525 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
1526 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
1527 ///
1528 /// If the output has a precision, it is `prec`.
1529 ///
1530 /// Special cases:
1531 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
1532 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
1533 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
1534 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
1535 /// - The quotient bits are 0 in all of the above special cases.
1536 ///
1537 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1538 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1539 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1540 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1541 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1542 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1543 ///
1544 /// If you know you'll be using `Nearest`, consider using [`Float::rem_and_quotient_bits_prec`]
1545 /// instead. If you know that your target precision is the maximum of the precisions of the two
1546 /// inputs, consider using [`Float::rem_and_quotient_bits_round`] instead. If both of these
1547 /// things are true, consider using [`Float::rem_and_quotient_bits`] instead.
1548 ///
1549 /// # Worst-case complexity
1550 /// $T(n) = O(n \log n \log\log n)$
1551 ///
1552 /// $M(n) = O(n)$
1553 ///
1554 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1555 /// other.complexity(), prec)`.
1556 ///
1557 /// # Panics
1558 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
1559 /// with `prec` bits.
1560 ///
1561 /// # Examples
1562 /// ```
1563 /// use core::cmp::Ordering::*;
1564 /// use malachite_base::rounding_modes::RoundingMode::*;
1565 /// use malachite_float::Float;
1566 ///
1567 /// let (r, o, q) =
1568 /// Float::from(100u32).rem_and_quotient_bits_prec_round(Float::from(7u32), 5, Floor);
1569 /// assert_eq!(r.to_string(), "2.00");
1570 /// assert_eq!(o, Equal);
1571 /// assert_eq!(q, 14);
1572 /// ```
1573 #[allow(clippy::needless_pass_by_value)]
1574 #[inline]
1575 pub fn rem_and_quotient_bits_prec_round(
1576 self,
1577 other: Self,
1578 prec: u64,
1579 rm: RoundingMode,
1580 ) -> (Self, Ordering, i64) {
1581 rem1_helper(&self, &other, false, true, prec, rm)
1582 }
1583
1584 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
1585 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the specified
1586 /// precision and with the specified rounding mode. The first [`Float`] is taken by value and
1587 /// the second by reference. An [`Ordering`] is also returned, indicating whether the rounded
1588 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
1589 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
1590 /// whenever this function returns a `NaN` it also returns `Equal`.
1591 ///
1592 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
1593 /// it equals $\pm(|q|\bmod 2^{63})$. (MPFR documents the same contract for its `quo` output,
1594 /// but its C implementation can overflow a `long` when the low 63 bits are all ones and the
1595 /// quotient rounds away from zero; this implementation always keeps the modular contract.)
1596 ///
1597 /// $$
1598 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
1599 /// $$
1600 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
1601 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
1602 ///
1603 /// If the output has a precision, it is `prec`.
1604 ///
1605 /// Special cases:
1606 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
1607 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
1608 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
1609 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
1610 /// - The quotient bits are 0 in all of the above special cases.
1611 ///
1612 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1613 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1614 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1615 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1616 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1617 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1618 ///
1619 /// If you know you'll be using `Nearest`, consider using [`Float::rem_and_quotient_bits_prec`]
1620 /// instead. If you know that your target precision is the maximum of the precisions of the two
1621 /// inputs, consider using [`Float::rem_and_quotient_bits_round`] instead. If both of these
1622 /// things are true, consider using [`Float::rem_and_quotient_bits`] instead.
1623 ///
1624 /// # Worst-case complexity
1625 /// $T(n) = O(n \log n \log\log n)$
1626 ///
1627 /// $M(n) = O(n)$
1628 ///
1629 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1630 /// other.complexity(), prec)`.
1631 ///
1632 /// # Panics
1633 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
1634 /// with `prec` bits.
1635 ///
1636 /// # Examples
1637 /// ```
1638 /// use core::cmp::Ordering::*;
1639 /// use malachite_base::rounding_modes::RoundingMode::*;
1640 /// use malachite_float::Float;
1641 ///
1642 /// let x = Float::from(100u32);
1643 /// let y = Float::from(7u32);
1644 /// let (r, o, q) = x.rem_and_quotient_bits_prec_round_val_ref(&y, 5, Floor);
1645 /// assert_eq!(r.to_string(), "2.00");
1646 /// assert_eq!(o, Equal);
1647 /// assert_eq!(q, 14);
1648 /// ```
1649 #[inline]
1650 pub fn rem_and_quotient_bits_prec_round_val_ref(
1651 self,
1652 other: &Self,
1653 prec: u64,
1654 rm: RoundingMode,
1655 ) -> (Self, Ordering, i64) {
1656 rem1_helper(&self, other, false, true, prec, rm)
1657 }
1658
1659 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
1660 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the specified
1661 /// precision and with the specified rounding mode. The first [`Float`] is taken by reference
1662 /// and the second by value. An [`Ordering`] is also returned, indicating whether the rounded
1663 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
1664 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
1665 /// whenever this function returns a `NaN` it also returns `Equal`.
1666 ///
1667 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
1668 /// it equals $\pm(|q|\bmod 2^{63})$. (MPFR documents the same contract for its `quo` output,
1669 /// but its C implementation can overflow a `long` when the low 63 bits are all ones and the
1670 /// quotient rounds away from zero; this implementation always keeps the modular contract.)
1671 ///
1672 /// $$
1673 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
1674 /// $$
1675 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
1676 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
1677 ///
1678 /// If the output has a precision, it is `prec`.
1679 ///
1680 /// Special cases:
1681 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
1682 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
1683 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
1684 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
1685 /// - The quotient bits are 0 in all of the above special cases.
1686 ///
1687 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1688 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1689 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1690 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1691 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1692 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1693 ///
1694 /// If you know you'll be using `Nearest`, consider using [`Float::rem_and_quotient_bits_prec`]
1695 /// instead. If you know that your target precision is the maximum of the precisions of the two
1696 /// inputs, consider using [`Float::rem_and_quotient_bits_round`] instead. If both of these
1697 /// things are true, consider using [`Float::rem_and_quotient_bits`] instead.
1698 ///
1699 /// # Worst-case complexity
1700 /// $T(n) = O(n \log n \log\log n)$
1701 ///
1702 /// $M(n) = O(n)$
1703 ///
1704 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1705 /// other.complexity(), prec)`.
1706 ///
1707 /// # Panics
1708 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
1709 /// with `prec` bits.
1710 ///
1711 /// # Examples
1712 /// ```
1713 /// use core::cmp::Ordering::*;
1714 /// use malachite_base::rounding_modes::RoundingMode::*;
1715 /// use malachite_float::Float;
1716 ///
1717 /// let x = Float::from(100u32);
1718 /// let y = Float::from(7u32);
1719 /// let (r, o, q) = x.rem_and_quotient_bits_prec_round_ref_val(y, 5, Floor);
1720 /// assert_eq!(r.to_string(), "2.00");
1721 /// assert_eq!(o, Equal);
1722 /// assert_eq!(q, 14);
1723 /// ```
1724 #[allow(clippy::needless_pass_by_value)]
1725 #[inline]
1726 pub fn rem_and_quotient_bits_prec_round_ref_val(
1727 &self,
1728 other: Self,
1729 prec: u64,
1730 rm: RoundingMode,
1731 ) -> (Self, Ordering, i64) {
1732 rem1_helper(self, &other, false, true, prec, rm)
1733 }
1734
1735 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
1736 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the specified
1737 /// precision and with the specified rounding mode. Both [`Float`]s are taken by reference. An
1738 /// [`Ordering`] is also returned, indicating whether the rounded remainder is less than, equal
1739 /// to, or greater than the exact remainder, along with the low bits of the quotient as an
1740 /// `i64`. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
1741 /// `NaN` it also returns `Equal`.
1742 ///
1743 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
1744 /// it equals $\pm(|q|\bmod 2^{63})$. (MPFR documents the same contract for its `quo` output,
1745 /// but its C implementation can overflow a `long` when the low 63 bits are all ones and the
1746 /// quotient rounds away from zero; this implementation always keeps the modular contract.)
1747 ///
1748 /// $$
1749 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
1750 /// $$
1751 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
1752 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
1753 ///
1754 /// If the output has a precision, it is `prec`.
1755 ///
1756 /// Special cases:
1757 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
1758 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
1759 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
1760 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
1761 /// - The quotient bits are 0 in all of the above special cases.
1762 ///
1763 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1764 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1765 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1766 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1767 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1768 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1769 ///
1770 /// If you know you'll be using `Nearest`, consider using [`Float::rem_and_quotient_bits_prec`]
1771 /// instead. If you know that your target precision is the maximum of the precisions of the two
1772 /// inputs, consider using [`Float::rem_and_quotient_bits_round`] instead. If both of these
1773 /// things are true, consider using [`Float::rem_and_quotient_bits`] instead.
1774 ///
1775 /// # Worst-case complexity
1776 /// $T(n) = O(n \log n \log\log n)$
1777 ///
1778 /// $M(n) = O(n)$
1779 ///
1780 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1781 /// other.complexity(), prec)`.
1782 ///
1783 /// # Panics
1784 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
1785 /// with `prec` bits.
1786 ///
1787 /// # Examples
1788 /// ```
1789 /// use core::cmp::Ordering::*;
1790 /// use malachite_base::rounding_modes::RoundingMode::*;
1791 /// use malachite_float::Float;
1792 ///
1793 /// let x = Float::from(100u32);
1794 /// let y = Float::from(7u32);
1795 /// let (r, o, q) = x.rem_and_quotient_bits_prec_round_ref_ref(&y, 5, Floor);
1796 /// assert_eq!(r.to_string(), "2.00");
1797 /// assert_eq!(o, Equal);
1798 /// assert_eq!(q, 14);
1799 /// ```
1800 #[inline]
1801 pub fn rem_and_quotient_bits_prec_round_ref_ref(
1802 &self,
1803 other: &Self,
1804 prec: u64,
1805 rm: RoundingMode,
1806 ) -> (Self, Ordering, i64) {
1807 rem1_helper(self, other, false, true, prec, rm)
1808 }
1809
1810 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
1811 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the nearest value
1812 /// of the specified precision. Both [`Float`]s are taken by value. An [`Ordering`] is also
1813 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
1814 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
1815 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
1816 /// `Equal`.
1817 ///
1818 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
1819 /// it equals $\pm(|q|\bmod 2^{63})$.
1820 ///
1821 /// $$
1822 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
1823 /// $$
1824 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
1825 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
1826 ///
1827 /// If the output has a precision, it is `prec`.
1828 ///
1829 /// Special cases:
1830 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
1831 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
1832 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
1833 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
1834 /// - The quotient bits are 0 in all of the above special cases.
1835 ///
1836 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1837 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1838 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1839 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1840 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1841 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1842 ///
1843 /// If you want to use a rounding mode other than `Nearest`, consider using
1844 /// [`Float::rem_and_quotient_bits_prec_round`] instead. If you know that your target precision
1845 /// is the maximum of the precisions of the two inputs, consider using
1846 /// [`Float::rem_and_quotient_bits`] instead.
1847 ///
1848 /// # Worst-case complexity
1849 /// $T(n) = O(n \log n \log\log n)$
1850 ///
1851 /// $M(n) = O(n)$
1852 ///
1853 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1854 /// other.complexity(), prec)`.
1855 ///
1856 /// # Panics
1857 /// Panics if `prec` is zero.
1858 ///
1859 /// # Examples
1860 /// ```
1861 /// use core::cmp::Ordering::*;
1862 /// use malachite_float::Float;
1863 ///
1864 /// let (r, o, q) = Float::from(100u32).rem_and_quotient_bits_prec(Float::from(7u32), 5);
1865 /// assert_eq!(r.to_string(), "2.00");
1866 /// assert_eq!(o, Equal);
1867 /// assert_eq!(q, 14);
1868 /// ```
1869 #[inline]
1870 pub fn rem_and_quotient_bits_prec(self, other: Self, prec: u64) -> (Self, Ordering, i64) {
1871 self.rem_and_quotient_bits_prec_round(other, prec, Nearest)
1872 }
1873
1874 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
1875 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the nearest value
1876 /// of the specified precision. The first [`Float`] is taken by value and the second by
1877 /// reference. An [`Ordering`] is also returned, indicating whether the rounded remainder is
1878 /// less than, equal to, or greater than the exact remainder, along with the low bits of the
1879 /// quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`], whenever this
1880 /// function returns a `NaN` it also returns `Equal`.
1881 ///
1882 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
1883 /// it equals $\pm(|q|\bmod 2^{63})$.
1884 ///
1885 /// $$
1886 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
1887 /// $$
1888 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
1889 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
1890 ///
1891 /// If the output has a precision, it is `prec`.
1892 ///
1893 /// Special cases:
1894 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
1895 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
1896 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
1897 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
1898 /// - The quotient bits are 0 in all of the above special cases.
1899 ///
1900 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1901 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1902 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1903 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1904 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1905 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1906 ///
1907 /// If you want to use a rounding mode other than `Nearest`, consider using
1908 /// [`Float::rem_and_quotient_bits_prec_round`] instead. If you know that your target precision
1909 /// is the maximum of the precisions of the two inputs, consider using
1910 /// [`Float::rem_and_quotient_bits`] instead.
1911 ///
1912 /// # Worst-case complexity
1913 /// $T(n) = O(n \log n \log\log n)$
1914 ///
1915 /// $M(n) = O(n)$
1916 ///
1917 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1918 /// other.complexity(), prec)`.
1919 ///
1920 /// # Panics
1921 /// Panics if `prec` is zero.
1922 ///
1923 /// # Examples
1924 /// ```
1925 /// use core::cmp::Ordering::*;
1926 /// use malachite_float::Float;
1927 ///
1928 /// let (r, o, q) =
1929 /// Float::from(100u32).rem_and_quotient_bits_prec_val_ref(&Float::from(7u32), 5);
1930 /// assert_eq!(r.to_string(), "2.00");
1931 /// assert_eq!(o, Equal);
1932 /// assert_eq!(q, 14);
1933 /// ```
1934 #[inline]
1935 pub fn rem_and_quotient_bits_prec_val_ref(
1936 self,
1937 other: &Self,
1938 prec: u64,
1939 ) -> (Self, Ordering, i64) {
1940 self.rem_and_quotient_bits_prec_round_val_ref(other, prec, Nearest)
1941 }
1942
1943 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
1944 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the nearest value
1945 /// of the specified precision. The first [`Float`] is taken by reference and the second by
1946 /// value. An [`Ordering`] is also returned, indicating whether the rounded remainder is less
1947 /// than, equal to, or greater than the exact remainder, along with the low bits of the quotient
1948 /// as an `i64`. Although `NaN`s are not comparable to any [`Float`], whenever this function
1949 /// returns a `NaN` it also returns `Equal`.
1950 ///
1951 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
1952 /// it equals $\pm(|q|\bmod 2^{63})$.
1953 ///
1954 /// $$
1955 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
1956 /// $$
1957 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
1958 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
1959 ///
1960 /// If the output has a precision, it is `prec`.
1961 ///
1962 /// Special cases:
1963 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
1964 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
1965 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
1966 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
1967 /// - The quotient bits are 0 in all of the above special cases.
1968 ///
1969 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
1970 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
1971 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1972 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1973 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1974 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1975 ///
1976 /// If you want to use a rounding mode other than `Nearest`, consider using
1977 /// [`Float::rem_and_quotient_bits_prec_round`] instead. If you know that your target precision
1978 /// is the maximum of the precisions of the two inputs, consider using
1979 /// [`Float::rem_and_quotient_bits`] instead.
1980 ///
1981 /// # Worst-case complexity
1982 /// $T(n) = O(n \log n \log\log n)$
1983 ///
1984 /// $M(n) = O(n)$
1985 ///
1986 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1987 /// other.complexity(), prec)`.
1988 ///
1989 /// # Panics
1990 /// Panics if `prec` is zero.
1991 ///
1992 /// # Examples
1993 /// ```
1994 /// use core::cmp::Ordering::*;
1995 /// use malachite_float::Float;
1996 ///
1997 /// let (r, o, q) =
1998 /// Float::from(100u32).rem_and_quotient_bits_prec_ref_val(Float::from(7u32), 5);
1999 /// assert_eq!(r.to_string(), "2.00");
2000 /// assert_eq!(o, Equal);
2001 /// assert_eq!(q, 14);
2002 /// ```
2003 #[inline]
2004 pub fn rem_and_quotient_bits_prec_ref_val(
2005 &self,
2006 other: Self,
2007 prec: u64,
2008 ) -> (Self, Ordering, i64) {
2009 self.rem_and_quotient_bits_prec_round_ref_val(other, prec, Nearest)
2010 }
2011
2012 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
2013 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the nearest value
2014 /// of the specified precision. Both [`Float`]s are taken by reference. An [`Ordering`] is also
2015 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
2016 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
2017 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
2018 /// `Equal`.
2019 ///
2020 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
2021 /// it equals $\pm(|q|\bmod 2^{63})$.
2022 ///
2023 /// $$
2024 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
2025 /// $$
2026 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
2027 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
2028 ///
2029 /// If the output has a precision, it is `prec`.
2030 ///
2031 /// Special cases:
2032 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
2033 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
2034 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
2035 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
2036 /// - The quotient bits are 0 in all of the above special cases.
2037 ///
2038 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
2039 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
2040 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2041 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2042 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2043 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2044 ///
2045 /// If you want to use a rounding mode other than `Nearest`, consider using
2046 /// [`Float::rem_and_quotient_bits_prec_round`] instead. If you know that your target precision
2047 /// is the maximum of the precisions of the two inputs, consider using
2048 /// [`Float::rem_and_quotient_bits`] instead.
2049 ///
2050 /// # Worst-case complexity
2051 /// $T(n) = O(n \log n \log\log n)$
2052 ///
2053 /// $M(n) = O(n)$
2054 ///
2055 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2056 /// other.complexity(), prec)`.
2057 ///
2058 /// # Panics
2059 /// Panics if `prec` is zero.
2060 ///
2061 /// # Examples
2062 /// ```
2063 /// use core::cmp::Ordering::*;
2064 /// use malachite_float::Float;
2065 ///
2066 /// let (r, o, q) =
2067 /// Float::from(100u32).rem_and_quotient_bits_prec_ref_ref(&Float::from(7u32), 5);
2068 /// assert_eq!(r.to_string(), "2.00");
2069 /// assert_eq!(o, Equal);
2070 /// assert_eq!(q, 14);
2071 /// ```
2072 #[inline]
2073 pub fn rem_and_quotient_bits_prec_ref_ref(
2074 &self,
2075 other: &Self,
2076 prec: u64,
2077 ) -> (Self, Ordering, i64) {
2078 self.rem_and_quotient_bits_prec_round_ref_ref(other, prec, Nearest)
2079 }
2080
2081 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
2082 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the maximum of
2083 /// the precisions of the inputs, with the specified rounding mode. Both [`Float`]s are taken by
2084 /// value. An [`Ordering`] is also returned, indicating whether the rounded remainder is less
2085 /// than, equal to, or greater than the exact remainder, along with the low bits of the quotient
2086 /// as an `i64`. Although `NaN`s are not comparable to any [`Float`], whenever this function
2087 /// returns a `NaN` it also returns `Equal`.
2088 ///
2089 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
2090 /// it equals $\pm(|q|\bmod 2^{63})$.
2091 ///
2092 /// $$
2093 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
2094 /// $$
2095 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
2096 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
2097 ///
2098 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2099 ///
2100 /// Special cases:
2101 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
2102 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
2103 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
2104 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
2105 /// - The quotient bits are 0 in all of the above special cases.
2106 ///
2107 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
2108 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
2109 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2110 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2111 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2112 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2113 ///
2114 /// If you want to specify an output precision, consider using
2115 /// [`Float::rem_and_quotient_bits_prec_round`] instead. If you know you'll be using the
2116 /// `Nearest` rounding mode, consider using [`Float::rem_and_quotient_bits`] instead.
2117 ///
2118 /// # Worst-case complexity
2119 /// $T(n) = O(n \log n \log\log n)$
2120 ///
2121 /// $M(n) = O(n)$
2122 ///
2123 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2124 /// other.complexity())`.
2125 ///
2126 /// # Panics
2127 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
2128 /// precision.
2129 ///
2130 /// # Examples
2131 /// ```
2132 /// use core::cmp::Ordering::*;
2133 /// use malachite_base::rounding_modes::RoundingMode::*;
2134 /// use malachite_float::Float;
2135 ///
2136 /// let (r, o, q) = Float::from(100u32).rem_and_quotient_bits_round(Float::from(7u32), Floor);
2137 /// assert_eq!(r.to_string(), "2.00");
2138 /// assert_eq!(o, Equal);
2139 /// assert_eq!(q, 14);
2140 /// ```
2141 pub fn rem_and_quotient_bits_round(
2142 self,
2143 other: Self,
2144 rm: RoundingMode,
2145 ) -> (Self, Ordering, i64) {
2146 let prec = max(self.significant_bits(), other.significant_bits());
2147 self.rem_and_quotient_bits_prec_round(other, prec, rm)
2148 }
2149
2150 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
2151 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the maximum of
2152 /// the precisions of the inputs, with the specified rounding mode. The first [`Float`] is taken
2153 /// by value and the second by reference. An [`Ordering`] is also returned, indicating whether
2154 /// the rounded remainder is less than, equal to, or greater than the exact remainder, along
2155 /// with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
2156 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2157 ///
2158 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
2159 /// it equals $\pm(|q|\bmod 2^{63})$.
2160 ///
2161 /// $$
2162 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
2163 /// $$
2164 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
2165 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
2166 ///
2167 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2168 ///
2169 /// Special cases:
2170 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
2171 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
2172 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
2173 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
2174 /// - The quotient bits are 0 in all of the above special cases.
2175 ///
2176 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
2177 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
2178 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2179 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2180 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2181 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2182 ///
2183 /// If you want to specify an output precision, consider using
2184 /// [`Float::rem_and_quotient_bits_prec_round`] instead. If you know you'll be using the
2185 /// `Nearest` rounding mode, consider using [`Float::rem_and_quotient_bits`] instead.
2186 ///
2187 /// # Worst-case complexity
2188 /// $T(n) = O(n \log n \log\log n)$
2189 ///
2190 /// $M(n) = O(n)$
2191 ///
2192 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2193 /// other.complexity())`.
2194 ///
2195 /// # Panics
2196 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
2197 /// precision.
2198 ///
2199 /// # Examples
2200 /// ```
2201 /// use core::cmp::Ordering::*;
2202 /// use malachite_base::rounding_modes::RoundingMode::*;
2203 /// use malachite_float::Float;
2204 ///
2205 /// let (r, o, q) =
2206 /// Float::from(100u32).rem_and_quotient_bits_round_val_ref(&Float::from(7u32), Floor);
2207 /// assert_eq!(r.to_string(), "2.00");
2208 /// assert_eq!(o, Equal);
2209 /// assert_eq!(q, 14);
2210 /// ```
2211 pub fn rem_and_quotient_bits_round_val_ref(
2212 self,
2213 other: &Self,
2214 rm: RoundingMode,
2215 ) -> (Self, Ordering, i64) {
2216 let prec = max(self.significant_bits(), other.significant_bits());
2217 self.rem_and_quotient_bits_prec_round_val_ref(other, prec, rm)
2218 }
2219
2220 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
2221 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the maximum of
2222 /// the precisions of the inputs, with the specified rounding mode. The first [`Float`] is taken
2223 /// by reference and the second by value. An [`Ordering`] is also returned, indicating whether
2224 /// the rounded remainder is less than, equal to, or greater than the exact remainder, along
2225 /// with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
2226 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2227 ///
2228 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
2229 /// it equals $\pm(|q|\bmod 2^{63})$.
2230 ///
2231 /// $$
2232 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
2233 /// $$
2234 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
2235 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
2236 ///
2237 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2238 ///
2239 /// Special cases:
2240 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
2241 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
2242 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
2243 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
2244 /// - The quotient bits are 0 in all of the above special cases.
2245 ///
2246 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
2247 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
2248 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2249 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2250 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2251 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2252 ///
2253 /// If you want to specify an output precision, consider using
2254 /// [`Float::rem_and_quotient_bits_prec_round`] instead. If you know you'll be using the
2255 /// `Nearest` rounding mode, consider using [`Float::rem_and_quotient_bits`] instead.
2256 ///
2257 /// # Worst-case complexity
2258 /// $T(n) = O(n \log n \log\log n)$
2259 ///
2260 /// $M(n) = O(n)$
2261 ///
2262 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2263 /// other.complexity())`.
2264 ///
2265 /// # Panics
2266 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
2267 /// precision.
2268 ///
2269 /// # Examples
2270 /// ```
2271 /// use core::cmp::Ordering::*;
2272 /// use malachite_base::rounding_modes::RoundingMode::*;
2273 /// use malachite_float::Float;
2274 ///
2275 /// let (r, o, q) =
2276 /// Float::from(100u32).rem_and_quotient_bits_round_ref_val(Float::from(7u32), Floor);
2277 /// assert_eq!(r.to_string(), "2.00");
2278 /// assert_eq!(o, Equal);
2279 /// assert_eq!(q, 14);
2280 /// ```
2281 pub fn rem_and_quotient_bits_round_ref_val(
2282 &self,
2283 other: Self,
2284 rm: RoundingMode,
2285 ) -> (Self, Ordering, i64) {
2286 let prec = max(self.significant_bits(), other.significant_bits());
2287 self.rem_and_quotient_bits_prec_round_ref_val(other, prec, rm)
2288 }
2289
2290 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
2291 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the maximum of
2292 /// the precisions of the inputs, with the specified rounding mode. Both [`Float`]s are taken by
2293 /// reference. An [`Ordering`] is also returned, indicating whether the rounded remainder is
2294 /// less than, equal to, or greater than the exact remainder, along with the low bits of the
2295 /// quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`], whenever this
2296 /// function returns a `NaN` it also returns `Equal`.
2297 ///
2298 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
2299 /// it equals $\pm(|q|\bmod 2^{63})$.
2300 ///
2301 /// $$
2302 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
2303 /// $$
2304 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
2305 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
2306 ///
2307 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2308 ///
2309 /// Special cases:
2310 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
2311 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
2312 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
2313 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
2314 /// - The quotient bits are 0 in all of the above special cases.
2315 ///
2316 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
2317 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
2318 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2319 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2320 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2321 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2322 ///
2323 /// If you want to specify an output precision, consider using
2324 /// [`Float::rem_and_quotient_bits_prec_round`] instead. If you know you'll be using the
2325 /// `Nearest` rounding mode, consider using [`Float::rem_and_quotient_bits`] instead.
2326 ///
2327 /// # Worst-case complexity
2328 /// $T(n) = O(n \log n \log\log n)$
2329 ///
2330 /// $M(n) = O(n)$
2331 ///
2332 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2333 /// other.complexity())`.
2334 ///
2335 /// # Panics
2336 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
2337 /// precision.
2338 ///
2339 /// # Examples
2340 /// ```
2341 /// use core::cmp::Ordering::*;
2342 /// use malachite_base::rounding_modes::RoundingMode::*;
2343 /// use malachite_float::Float;
2344 ///
2345 /// let (r, o, q) =
2346 /// Float::from(100u32).rem_and_quotient_bits_round_ref_ref(&Float::from(7u32), Floor);
2347 /// assert_eq!(r.to_string(), "2.00");
2348 /// assert_eq!(o, Equal);
2349 /// assert_eq!(q, 14);
2350 /// ```
2351 pub fn rem_and_quotient_bits_round_ref_ref(
2352 &self,
2353 other: &Self,
2354 rm: RoundingMode,
2355 ) -> (Self, Ordering, i64) {
2356 let prec = max(self.significant_bits(), other.significant_bits());
2357 self.rem_and_quotient_bits_prec_round_ref_ref(other, prec, rm)
2358 }
2359
2360 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
2361 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the nearest value
2362 /// of the maximum of the precisions of the inputs. Both [`Float`]s are taken by value. An
2363 /// [`Ordering`] is also returned, indicating whether the rounded remainder is less than, equal
2364 /// to, or greater than the exact remainder, along with the low bits of the quotient as an
2365 /// `i64`. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
2366 /// `NaN` it also returns `Equal`.
2367 ///
2368 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
2369 /// it equals $\pm(|q|\bmod 2^{63})$.
2370 ///
2371 /// $$
2372 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
2373 /// $$
2374 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
2375 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
2376 ///
2377 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2378 ///
2379 /// Special cases:
2380 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
2381 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
2382 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
2383 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
2384 /// - The quotient bits are 0 in all of the above special cases.
2385 ///
2386 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
2387 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
2388 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2389 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2390 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2391 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2392 ///
2393 /// If you want to specify an output precision, consider using
2394 /// [`Float::rem_and_quotient_bits_prec`] instead. If you want to use a rounding mode other than
2395 /// `Nearest`, consider using [`Float::rem_and_quotient_bits_round`] instead.
2396 ///
2397 /// # Worst-case complexity
2398 /// $T(n) = O(n \log n \log\log n)$
2399 ///
2400 /// $M(n) = O(n)$
2401 ///
2402 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2403 /// other.complexity())`.
2404 ///
2405 /// # Examples
2406 /// ```
2407 /// use core::cmp::Ordering::*;
2408 /// use malachite_float::Float;
2409 ///
2410 /// let (r, o, q) = Float::from(100u32).rem_and_quotient_bits(Float::from(7u32));
2411 /// assert_eq!(r.to_string(), "2.00");
2412 /// assert_eq!(o, Equal);
2413 /// assert_eq!(q, 14);
2414 /// ```
2415 #[inline]
2416 pub fn rem_and_quotient_bits(self, other: Self) -> (Self, Ordering, i64) {
2417 self.rem_and_quotient_bits_round(other, Nearest)
2418 }
2419
2420 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
2421 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the nearest value
2422 /// of the maximum of the precisions of the inputs. The first [`Float`] is taken by value and
2423 /// the second by reference. An [`Ordering`] is also returned, indicating whether the rounded
2424 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
2425 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
2426 /// whenever this function returns a `NaN` it also returns `Equal`.
2427 ///
2428 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
2429 /// it equals $\pm(|q|\bmod 2^{63})$.
2430 ///
2431 /// $$
2432 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
2433 /// $$
2434 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
2435 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
2436 ///
2437 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2438 ///
2439 /// Special cases:
2440 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
2441 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
2442 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
2443 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
2444 /// - The quotient bits are 0 in all of the above special cases.
2445 ///
2446 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
2447 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
2448 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2449 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2450 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2451 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2452 ///
2453 /// If you want to specify an output precision, consider using
2454 /// [`Float::rem_and_quotient_bits_prec`] instead. If you want to use a rounding mode other than
2455 /// `Nearest`, consider using [`Float::rem_and_quotient_bits_round`] instead.
2456 ///
2457 /// # Worst-case complexity
2458 /// $T(n) = O(n \log n \log\log n)$
2459 ///
2460 /// $M(n) = O(n)$
2461 ///
2462 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2463 /// other.complexity())`.
2464 ///
2465 /// # Examples
2466 /// ```
2467 /// use core::cmp::Ordering::*;
2468 /// use malachite_float::Float;
2469 ///
2470 /// let (r, o, q) = Float::from(100u32).rem_and_quotient_bits_val_ref(&Float::from(7u32));
2471 /// assert_eq!(r.to_string(), "2.00");
2472 /// assert_eq!(o, Equal);
2473 /// assert_eq!(q, 14);
2474 /// ```
2475 #[inline]
2476 pub fn rem_and_quotient_bits_val_ref(self, other: &Self) -> (Self, Ordering, i64) {
2477 self.rem_and_quotient_bits_round_val_ref(other, Nearest)
2478 }
2479
2480 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
2481 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the nearest value
2482 /// of the maximum of the precisions of the inputs. The first [`Float`] is taken by reference
2483 /// and the second by value. An [`Ordering`] is also returned, indicating whether the rounded
2484 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
2485 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
2486 /// whenever this function returns a `NaN` it also returns `Equal`.
2487 ///
2488 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
2489 /// it equals $\pm(|q|\bmod 2^{63})$.
2490 ///
2491 /// $$
2492 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
2493 /// $$
2494 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
2495 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
2496 ///
2497 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2498 ///
2499 /// Special cases:
2500 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
2501 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
2502 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
2503 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
2504 /// - The quotient bits are 0 in all of the above special cases.
2505 ///
2506 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
2507 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
2508 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2509 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2510 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2511 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2512 ///
2513 /// If you want to specify an output precision, consider using
2514 /// [`Float::rem_and_quotient_bits_prec`] instead. If you want to use a rounding mode other than
2515 /// `Nearest`, consider using [`Float::rem_and_quotient_bits_round`] instead.
2516 ///
2517 /// # Worst-case complexity
2518 /// $T(n) = O(n \log n \log\log n)$
2519 ///
2520 /// $M(n) = O(n)$
2521 ///
2522 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2523 /// other.complexity())`.
2524 ///
2525 /// # Examples
2526 /// ```
2527 /// use core::cmp::Ordering::*;
2528 /// use malachite_float::Float;
2529 ///
2530 /// let (r, o, q) = Float::from(100u32).rem_and_quotient_bits_ref_val(Float::from(7u32));
2531 /// assert_eq!(r.to_string(), "2.00");
2532 /// assert_eq!(o, Equal);
2533 /// assert_eq!(q, 14);
2534 /// ```
2535 #[inline]
2536 pub fn rem_and_quotient_bits_ref_val(&self, other: Self) -> (Self, Ordering, i64) {
2537 self.rem_and_quotient_bits_round_ref_val(other, Nearest)
2538 }
2539
2540 /// Computes the remainder of two [`Float`]s, with the quotient rounded toward zero, as for the
2541 /// `%` operator on primitive floats and C's `fmod`, rounding the remainder to the nearest value
2542 /// of the maximum of the precisions of the inputs. Both [`Float`]s are taken by reference. An
2543 /// [`Ordering`] is also returned, indicating whether the rounded remainder is less than, equal
2544 /// to, or greater than the exact remainder, along with the low bits of the quotient as an
2545 /// `i64`. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
2546 /// `NaN` it also returns `Equal`.
2547 ///
2548 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
2549 /// it equals $\pm(|q|\bmod 2^{63})$.
2550 ///
2551 /// $$
2552 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
2553 /// $$
2554 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
2555 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
2556 ///
2557 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2558 ///
2559 /// Special cases:
2560 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
2561 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
2562 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
2563 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
2564 /// - The quotient bits are 0 in all of the above special cases.
2565 ///
2566 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
2567 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
2568 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2569 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2570 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2571 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2572 ///
2573 /// If you want to specify an output precision, consider using
2574 /// [`Float::rem_and_quotient_bits_prec`] instead. If you want to use a rounding mode other than
2575 /// `Nearest`, consider using [`Float::rem_and_quotient_bits_round`] instead.
2576 ///
2577 /// # Worst-case complexity
2578 /// $T(n) = O(n \log n \log\log n)$
2579 ///
2580 /// $M(n) = O(n)$
2581 ///
2582 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2583 /// other.complexity())`.
2584 ///
2585 /// # Examples
2586 /// ```
2587 /// use core::cmp::Ordering::*;
2588 /// use malachite_float::Float;
2589 ///
2590 /// let (r, o, q) = Float::from(100u32).rem_and_quotient_bits_ref_ref(&Float::from(7u32));
2591 /// assert_eq!(r.to_string(), "2.00");
2592 /// assert_eq!(o, Equal);
2593 /// assert_eq!(q, 14);
2594 /// ```
2595 #[inline]
2596 pub fn rem_and_quotient_bits_ref_ref(&self, other: &Self) -> (Self, Ordering, i64) {
2597 self.rem_and_quotient_bits_round_ref_ref(other, Nearest)
2598 }
2599
2600 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
2601 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
2602 /// remainder to the specified precision and with the specified rounding mode. Both [`Float`]s
2603 /// are taken by value. An [`Ordering`] is also returned, indicating whether the rounded
2604 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
2605 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
2606 /// `Equal`.
2607 ///
2608 /// $$
2609 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
2610 /// $$
2611 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
2612 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
2613 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
2614 ///
2615 /// If the output has a precision, it is `prec`.
2616 ///
2617 /// Special cases:
2618 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
2619 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
2620 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
2621 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
2622 ///
2623 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
2624 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
2625 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2626 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2627 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2628 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2629 ///
2630 /// If you know you'll be using `Nearest`, consider using [`Float::ieee_remainder_prec`]
2631 /// instead. If you know that your target precision is the maximum of the precisions of the two
2632 /// inputs, consider using [`Float::ieee_remainder_round`] instead. If both of these things are
2633 /// true, consider using [`Float::ieee_remainder`] instead.
2634 ///
2635 /// # Worst-case complexity
2636 /// $T(n) = O(n \log n \log\log n)$
2637 ///
2638 /// $M(n) = O(n)$
2639 ///
2640 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2641 /// other.complexity(), prec)`.
2642 ///
2643 /// # Panics
2644 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
2645 /// with `prec` bits.
2646 ///
2647 /// # Examples
2648 /// ```
2649 /// use core::cmp::Ordering::*;
2650 /// use malachite_base::rounding_modes::RoundingMode::*;
2651 /// use malachite_float::Float;
2652 ///
2653 /// let (r, o) = Float::from(14u32).ieee_remainder_prec_round(Float::from(3u32), 10, Nearest);
2654 /// assert_eq!(r.to_string(), "-1.0000");
2655 /// assert_eq!(o, Equal);
2656 ///
2657 /// let (r, o) = Float::from(10u32).ieee_remainder_prec_round(Float::from(7u32), 1, Floor);
2658 /// assert_eq!(r.to_string(), "2.0");
2659 /// assert_eq!(o, Less);
2660 /// ```
2661 #[allow(clippy::needless_pass_by_value)]
2662 pub fn ieee_remainder_prec_round(
2663 self,
2664 other: Self,
2665 prec: u64,
2666 rm: RoundingMode,
2667 ) -> (Self, Ordering) {
2668 let (r, o, _) = rem1_helper(&self, &other, true, false, prec, rm);
2669 (r, o)
2670 }
2671
2672 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
2673 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
2674 /// remainder to the specified precision and with the specified rounding mode. The first
2675 /// [`Float`] is taken by value and the second by reference. An [`Ordering`] is also returned,
2676 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
2677 /// remainder. Although `NaN`s are not comparable to any [`Float`], whenever this function
2678 /// returns a `NaN` it also returns `Equal`.
2679 ///
2680 /// $$
2681 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
2682 /// $$
2683 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
2684 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
2685 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
2686 ///
2687 /// If the output has a precision, it is `prec`.
2688 ///
2689 /// Special cases:
2690 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
2691 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
2692 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
2693 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
2694 ///
2695 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
2696 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
2697 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2698 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2699 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2700 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2701 ///
2702 /// If you know you'll be using `Nearest`, consider using [`Float::ieee_remainder_prec`]
2703 /// instead. If you know that your target precision is the maximum of the precisions of the two
2704 /// inputs, consider using [`Float::ieee_remainder_round`] instead. If both of these things are
2705 /// true, consider using [`Float::ieee_remainder`] instead.
2706 ///
2707 /// # Worst-case complexity
2708 /// $T(n) = O(n \log n \log\log n)$
2709 ///
2710 /// $M(n) = O(n)$
2711 ///
2712 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2713 /// other.complexity(), prec)`.
2714 ///
2715 /// # Panics
2716 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
2717 /// with `prec` bits.
2718 ///
2719 /// # Examples
2720 /// ```
2721 /// use core::cmp::Ordering::*;
2722 /// use malachite_base::rounding_modes::RoundingMode::*;
2723 /// use malachite_float::Float;
2724 ///
2725 /// let (r, o) =
2726 /// Float::from(14u32).ieee_remainder_prec_round_val_ref(&Float::from(3u32), 10, Nearest);
2727 /// assert_eq!(r.to_string(), "-1.0000");
2728 /// assert_eq!(o, Equal);
2729 ///
2730 /// let (r, o) =
2731 /// Float::from(10u32).ieee_remainder_prec_round_val_ref(&Float::from(7u32), 1, Floor);
2732 /// assert_eq!(r.to_string(), "2.0");
2733 /// assert_eq!(o, Less);
2734 /// ```
2735 pub fn ieee_remainder_prec_round_val_ref(
2736 self,
2737 other: &Self,
2738 prec: u64,
2739 rm: RoundingMode,
2740 ) -> (Self, Ordering) {
2741 let (r, o, _) = rem1_helper(&self, other, true, false, prec, rm);
2742 (r, o)
2743 }
2744
2745 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
2746 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
2747 /// remainder to the specified precision and with the specified rounding mode. The first
2748 /// [`Float`] is taken by reference and the second by value. An [`Ordering`] is also returned,
2749 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
2750 /// remainder. Although `NaN`s are not comparable to any [`Float`], whenever this function
2751 /// returns a `NaN` it also returns `Equal`.
2752 ///
2753 /// $$
2754 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
2755 /// $$
2756 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
2757 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
2758 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
2759 ///
2760 /// If the output has a precision, it is `prec`.
2761 ///
2762 /// Special cases:
2763 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
2764 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
2765 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
2766 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
2767 ///
2768 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
2769 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
2770 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2771 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2772 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2773 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2774 ///
2775 /// If you know you'll be using `Nearest`, consider using [`Float::ieee_remainder_prec`]
2776 /// instead. If you know that your target precision is the maximum of the precisions of the two
2777 /// inputs, consider using [`Float::ieee_remainder_round`] instead. If both of these things are
2778 /// true, consider using [`Float::ieee_remainder`] instead.
2779 ///
2780 /// # Worst-case complexity
2781 /// $T(n) = O(n \log n \log\log n)$
2782 ///
2783 /// $M(n) = O(n)$
2784 ///
2785 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2786 /// other.complexity(), prec)`.
2787 ///
2788 /// # Panics
2789 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
2790 /// with `prec` bits.
2791 ///
2792 /// # Examples
2793 /// ```
2794 /// use core::cmp::Ordering::*;
2795 /// use malachite_base::rounding_modes::RoundingMode::*;
2796 /// use malachite_float::Float;
2797 ///
2798 /// let (r, o) =
2799 /// Float::from(14u32).ieee_remainder_prec_round_ref_val(Float::from(3u32), 10, Nearest);
2800 /// assert_eq!(r.to_string(), "-1.0000");
2801 /// assert_eq!(o, Equal);
2802 ///
2803 /// let (r, o) =
2804 /// Float::from(10u32).ieee_remainder_prec_round_ref_val(Float::from(7u32), 1, Floor);
2805 /// assert_eq!(r.to_string(), "2.0");
2806 /// assert_eq!(o, Less);
2807 /// ```
2808 #[allow(clippy::needless_pass_by_value)]
2809 pub fn ieee_remainder_prec_round_ref_val(
2810 &self,
2811 other: Self,
2812 prec: u64,
2813 rm: RoundingMode,
2814 ) -> (Self, Ordering) {
2815 let (r, o, _) = rem1_helper(self, &other, true, false, prec, rm);
2816 (r, o)
2817 }
2818
2819 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
2820 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
2821 /// remainder to the specified precision and with the specified rounding mode. Both [`Float`]s
2822 /// are taken by reference. An [`Ordering`] is also returned, indicating whether the rounded
2823 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
2824 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
2825 /// `Equal`.
2826 ///
2827 /// $$
2828 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
2829 /// $$
2830 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
2831 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
2832 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
2833 ///
2834 /// If the output has a precision, it is `prec`.
2835 ///
2836 /// Special cases:
2837 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
2838 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
2839 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
2840 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
2841 ///
2842 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
2843 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
2844 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2845 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2846 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2847 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2848 ///
2849 /// If you know you'll be using `Nearest`, consider using [`Float::ieee_remainder_prec`]
2850 /// instead. If you know that your target precision is the maximum of the precisions of the two
2851 /// inputs, consider using [`Float::ieee_remainder_round`] instead. If both of these things are
2852 /// true, consider using [`Float::ieee_remainder`] instead.
2853 ///
2854 /// # Worst-case complexity
2855 /// $T(n) = O(n \log n \log\log n)$
2856 ///
2857 /// $M(n) = O(n)$
2858 ///
2859 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2860 /// other.complexity(), prec)`.
2861 ///
2862 /// # Panics
2863 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
2864 /// with `prec` bits.
2865 ///
2866 /// # Examples
2867 /// ```
2868 /// use core::cmp::Ordering::*;
2869 /// use malachite_base::rounding_modes::RoundingMode::*;
2870 /// use malachite_float::Float;
2871 ///
2872 /// let (r, o) =
2873 /// Float::from(14u32).ieee_remainder_prec_round_ref_ref(&Float::from(3u32), 10, Nearest);
2874 /// assert_eq!(r.to_string(), "-1.0000");
2875 /// assert_eq!(o, Equal);
2876 ///
2877 /// let (r, o) =
2878 /// Float::from(10u32).ieee_remainder_prec_round_ref_ref(&Float::from(7u32), 1, Floor);
2879 /// assert_eq!(r.to_string(), "2.0");
2880 /// assert_eq!(o, Less);
2881 /// ```
2882 pub fn ieee_remainder_prec_round_ref_ref(
2883 &self,
2884 other: &Self,
2885 prec: u64,
2886 rm: RoundingMode,
2887 ) -> (Self, Ordering) {
2888 let (r, o, _) = rem1_helper(self, other, true, false, prec, rm);
2889 (r, o)
2890 }
2891
2892 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
2893 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
2894 /// remainder to the nearest value of the specified precision. Both [`Float`]s are taken by
2895 /// value. An [`Ordering`] is also returned, indicating whether the rounded remainder is less
2896 /// than, equal to, or greater than the exact remainder. Although `NaN`s are not comparable to
2897 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2898 ///
2899 /// $$
2900 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
2901 /// $$
2902 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
2903 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
2904 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
2905 ///
2906 /// If the output has a precision, it is `prec`.
2907 ///
2908 /// Special cases:
2909 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
2910 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
2911 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
2912 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
2913 ///
2914 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
2915 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
2916 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2917 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2918 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2919 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2920 ///
2921 /// If you want to use a rounding mode other than `Nearest`, consider using
2922 /// [`Float::ieee_remainder_prec_round`] instead. If you know that your target precision is the
2923 /// maximum of the precisions of the two inputs, consider using [`Float::ieee_remainder`]
2924 /// instead.
2925 ///
2926 /// # Worst-case complexity
2927 /// $T(n) = O(n \log n \log\log n)$
2928 ///
2929 /// $M(n) = O(n)$
2930 ///
2931 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2932 /// other.complexity(), prec)`.
2933 ///
2934 /// # Panics
2935 /// Panics if `prec` is zero.
2936 ///
2937 /// # Examples
2938 /// ```
2939 /// use core::cmp::Ordering::*;
2940 /// use malachite_float::Float;
2941 ///
2942 /// let (r, o) = Float::from(14u32).ieee_remainder_prec(Float::from(3u32), 10);
2943 /// assert_eq!(r.to_string(), "-1.0000");
2944 /// assert_eq!(o, Equal);
2945 /// ```
2946 #[inline]
2947 pub fn ieee_remainder_prec(self, other: Self, prec: u64) -> (Self, Ordering) {
2948 self.ieee_remainder_prec_round(other, prec, Nearest)
2949 }
2950
2951 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
2952 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
2953 /// remainder to the nearest value of the specified precision. The first [`Float`] is taken by
2954 /// value and the second by reference. An [`Ordering`] is also returned, indicating whether the
2955 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
2956 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
2957 /// returns `Equal`.
2958 ///
2959 /// $$
2960 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
2961 /// $$
2962 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
2963 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
2964 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
2965 ///
2966 /// If the output has a precision, it is `prec`.
2967 ///
2968 /// Special cases:
2969 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
2970 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
2971 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
2972 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
2973 ///
2974 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
2975 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
2976 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2977 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2978 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2979 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2980 ///
2981 /// If you want to use a rounding mode other than `Nearest`, consider using
2982 /// [`Float::ieee_remainder_prec_round`] instead. If you know that your target precision is the
2983 /// maximum of the precisions of the two inputs, consider using [`Float::ieee_remainder`]
2984 /// instead.
2985 ///
2986 /// # Worst-case complexity
2987 /// $T(n) = O(n \log n \log\log n)$
2988 ///
2989 /// $M(n) = O(n)$
2990 ///
2991 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2992 /// other.complexity(), prec)`.
2993 ///
2994 /// # Panics
2995 /// Panics if `prec` is zero.
2996 ///
2997 /// # Examples
2998 /// ```
2999 /// use core::cmp::Ordering::*;
3000 /// use malachite_float::Float;
3001 ///
3002 /// let (r, o) = Float::from(14u32).ieee_remainder_prec_val_ref(&Float::from(3u32), 10);
3003 /// assert_eq!(r.to_string(), "-1.0000");
3004 /// assert_eq!(o, Equal);
3005 /// ```
3006 #[inline]
3007 pub fn ieee_remainder_prec_val_ref(self, other: &Self, prec: u64) -> (Self, Ordering) {
3008 self.ieee_remainder_prec_round_val_ref(other, prec, Nearest)
3009 }
3010
3011 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
3012 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
3013 /// remainder to the nearest value of the specified precision. The first [`Float`] is taken by
3014 /// reference and the second by value. An [`Ordering`] is also returned, indicating whether the
3015 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
3016 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
3017 /// returns `Equal`.
3018 ///
3019 /// $$
3020 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3021 /// $$
3022 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3023 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3024 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
3025 ///
3026 /// If the output has a precision, it is `prec`.
3027 ///
3028 /// Special cases:
3029 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3030 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3031 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3032 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3033 ///
3034 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3035 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3036 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3037 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3038 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3039 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3040 ///
3041 /// If you want to use a rounding mode other than `Nearest`, consider using
3042 /// [`Float::ieee_remainder_prec_round`] instead. If you know that your target precision is the
3043 /// maximum of the precisions of the two inputs, consider using [`Float::ieee_remainder`]
3044 /// instead.
3045 ///
3046 /// # Worst-case complexity
3047 /// $T(n) = O(n \log n \log\log n)$
3048 ///
3049 /// $M(n) = O(n)$
3050 ///
3051 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3052 /// other.complexity(), prec)`.
3053 ///
3054 /// # Panics
3055 /// Panics if `prec` is zero.
3056 ///
3057 /// # Examples
3058 /// ```
3059 /// use core::cmp::Ordering::*;
3060 /// use malachite_float::Float;
3061 ///
3062 /// let (r, o) = Float::from(14u32).ieee_remainder_prec_ref_val(Float::from(3u32), 10);
3063 /// assert_eq!(r.to_string(), "-1.0000");
3064 /// assert_eq!(o, Equal);
3065 /// ```
3066 #[inline]
3067 pub fn ieee_remainder_prec_ref_val(&self, other: Self, prec: u64) -> (Self, Ordering) {
3068 self.ieee_remainder_prec_round_ref_val(other, prec, Nearest)
3069 }
3070
3071 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
3072 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
3073 /// remainder to the nearest value of the specified precision. Both [`Float`]s are taken by
3074 /// reference. An [`Ordering`] is also returned, indicating whether the rounded remainder is
3075 /// less than, equal to, or greater than the exact remainder. Although `NaN`s are not comparable
3076 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3077 ///
3078 /// $$
3079 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3080 /// $$
3081 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3082 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3083 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
3084 ///
3085 /// If the output has a precision, it is `prec`.
3086 ///
3087 /// Special cases:
3088 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3089 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3090 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3091 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3092 ///
3093 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3094 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3095 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3096 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3097 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3098 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3099 ///
3100 /// If you want to use a rounding mode other than `Nearest`, consider using
3101 /// [`Float::ieee_remainder_prec_round`] instead. If you know that your target precision is the
3102 /// maximum of the precisions of the two inputs, consider using [`Float::ieee_remainder`]
3103 /// instead.
3104 ///
3105 /// # Worst-case complexity
3106 /// $T(n) = O(n \log n \log\log n)$
3107 ///
3108 /// $M(n) = O(n)$
3109 ///
3110 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3111 /// other.complexity(), prec)`.
3112 ///
3113 /// # Panics
3114 /// Panics if `prec` is zero.
3115 ///
3116 /// # Examples
3117 /// ```
3118 /// use core::cmp::Ordering::*;
3119 /// use malachite_float::Float;
3120 ///
3121 /// let (r, o) = Float::from(14u32).ieee_remainder_prec_ref_ref(&Float::from(3u32), 10);
3122 /// assert_eq!(r.to_string(), "-1.0000");
3123 /// assert_eq!(o, Equal);
3124 /// ```
3125 #[inline]
3126 pub fn ieee_remainder_prec_ref_ref(&self, other: &Self, prec: u64) -> (Self, Ordering) {
3127 self.ieee_remainder_prec_round_ref_ref(other, prec, Nearest)
3128 }
3129
3130 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
3131 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
3132 /// remainder to the maximum of the precisions of the inputs, with the specified rounding mode.
3133 /// Both [`Float`]s are taken by value. An [`Ordering`] is also returned, indicating whether the
3134 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
3135 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
3136 /// returns `Equal`.
3137 ///
3138 /// $$
3139 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3140 /// $$
3141 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3142 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3143 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
3144 ///
3145 /// If the output has a precision, it is the maximum of the precisions of the inputs.
3146 ///
3147 /// Special cases:
3148 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3149 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3150 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3151 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3152 ///
3153 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3154 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3155 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3156 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3157 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3158 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3159 ///
3160 /// If you want to specify an output precision, consider using
3161 /// [`Float::ieee_remainder_prec_round`] instead. If you know you'll be using the `Nearest`
3162 /// rounding mode, consider using [`Float::ieee_remainder`] instead.
3163 ///
3164 /// # Worst-case complexity
3165 /// $T(n) = O(n \log n \log\log n)$
3166 ///
3167 /// $M(n) = O(n)$
3168 ///
3169 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3170 /// other.complexity())`.
3171 ///
3172 /// # Panics
3173 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
3174 /// precision.
3175 ///
3176 /// # Examples
3177 /// ```
3178 /// use core::cmp::Ordering::*;
3179 /// use malachite_base::rounding_modes::RoundingMode::*;
3180 /// use malachite_float::Float;
3181 ///
3182 /// let (r, o) = Float::from(14u32).ieee_remainder_round(Float::from(3u32), Floor);
3183 /// assert_eq!(r.to_string(), "-1.0");
3184 /// assert_eq!(o, Equal);
3185 /// ```
3186 pub fn ieee_remainder_round(self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
3187 let prec = max(self.significant_bits(), other.significant_bits());
3188 self.ieee_remainder_prec_round(other, prec, rm)
3189 }
3190
3191 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
3192 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
3193 /// remainder to the maximum of the precisions of the inputs, with the specified rounding mode.
3194 /// The first [`Float`] is taken by value and the second by reference. An [`Ordering`] is also
3195 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
3196 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
3197 /// function returns a `NaN` it also returns `Equal`.
3198 ///
3199 /// $$
3200 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3201 /// $$
3202 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3203 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3204 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
3205 ///
3206 /// If the output has a precision, it is the maximum of the precisions of the inputs.
3207 ///
3208 /// Special cases:
3209 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3210 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3211 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3212 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3213 ///
3214 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3215 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3216 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3217 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3218 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3219 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3220 ///
3221 /// If you want to specify an output precision, consider using
3222 /// [`Float::ieee_remainder_prec_round`] instead. If you know you'll be using the `Nearest`
3223 /// rounding mode, consider using [`Float::ieee_remainder`] instead.
3224 ///
3225 /// # Worst-case complexity
3226 /// $T(n) = O(n \log n \log\log n)$
3227 ///
3228 /// $M(n) = O(n)$
3229 ///
3230 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3231 /// other.complexity())`.
3232 ///
3233 /// # Panics
3234 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
3235 /// precision.
3236 ///
3237 /// # Examples
3238 /// ```
3239 /// use core::cmp::Ordering::*;
3240 /// use malachite_base::rounding_modes::RoundingMode::*;
3241 /// use malachite_float::Float;
3242 ///
3243 /// let (r, o) = Float::from(14u32).ieee_remainder_round_val_ref(&Float::from(3u32), Floor);
3244 /// assert_eq!(r.to_string(), "-1.0");
3245 /// assert_eq!(o, Equal);
3246 /// ```
3247 pub fn ieee_remainder_round_val_ref(self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
3248 let prec = max(self.significant_bits(), other.significant_bits());
3249 self.ieee_remainder_prec_round_val_ref(other, prec, rm)
3250 }
3251
3252 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
3253 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
3254 /// remainder to the maximum of the precisions of the inputs, with the specified rounding mode.
3255 /// The first [`Float`] is taken by reference and the second by value. An [`Ordering`] is also
3256 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
3257 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
3258 /// function returns a `NaN` it also returns `Equal`.
3259 ///
3260 /// $$
3261 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3262 /// $$
3263 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3264 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3265 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
3266 ///
3267 /// If the output has a precision, it is the maximum of the precisions of the inputs.
3268 ///
3269 /// Special cases:
3270 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3271 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3272 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3273 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3274 ///
3275 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3276 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3277 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3278 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3279 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3280 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3281 ///
3282 /// If you want to specify an output precision, consider using
3283 /// [`Float::ieee_remainder_prec_round`] instead. If you know you'll be using the `Nearest`
3284 /// rounding mode, consider using [`Float::ieee_remainder`] instead.
3285 ///
3286 /// # Worst-case complexity
3287 /// $T(n) = O(n \log n \log\log n)$
3288 ///
3289 /// $M(n) = O(n)$
3290 ///
3291 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3292 /// other.complexity())`.
3293 ///
3294 /// # Panics
3295 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
3296 /// precision.
3297 ///
3298 /// # Examples
3299 /// ```
3300 /// use core::cmp::Ordering::*;
3301 /// use malachite_base::rounding_modes::RoundingMode::*;
3302 /// use malachite_float::Float;
3303 ///
3304 /// let (r, o) = Float::from(14u32).ieee_remainder_round_ref_val(Float::from(3u32), Floor);
3305 /// assert_eq!(r.to_string(), "-1.0");
3306 /// assert_eq!(o, Equal);
3307 /// ```
3308 pub fn ieee_remainder_round_ref_val(&self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
3309 let prec = max(self.significant_bits(), other.significant_bits());
3310 self.ieee_remainder_prec_round_ref_val(other, prec, rm)
3311 }
3312
3313 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
3314 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
3315 /// remainder to the maximum of the precisions of the inputs, with the specified rounding mode.
3316 /// Both [`Float`]s are taken by reference. An [`Ordering`] is also returned, indicating whether
3317 /// the rounded remainder is less than, equal to, or greater than the exact remainder. Although
3318 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
3319 /// returns `Equal`.
3320 ///
3321 /// $$
3322 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3323 /// $$
3324 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3325 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3326 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
3327 ///
3328 /// If the output has a precision, it is the maximum of the precisions of the inputs.
3329 ///
3330 /// Special cases:
3331 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3332 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3333 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3334 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3335 ///
3336 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3337 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3338 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3339 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3340 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3341 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3342 ///
3343 /// If you want to specify an output precision, consider using
3344 /// [`Float::ieee_remainder_prec_round`] instead. If you know you'll be using the `Nearest`
3345 /// rounding mode, consider using [`Float::ieee_remainder`] instead.
3346 ///
3347 /// # Worst-case complexity
3348 /// $T(n) = O(n \log n \log\log n)$
3349 ///
3350 /// $M(n) = O(n)$
3351 ///
3352 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3353 /// other.complexity())`.
3354 ///
3355 /// # Panics
3356 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
3357 /// precision.
3358 ///
3359 /// # Examples
3360 /// ```
3361 /// use core::cmp::Ordering::*;
3362 /// use malachite_base::rounding_modes::RoundingMode::*;
3363 /// use malachite_float::Float;
3364 ///
3365 /// let (r, o) = Float::from(14u32).ieee_remainder_round_ref_ref(&Float::from(3u32), Floor);
3366 /// assert_eq!(r.to_string(), "-1.0");
3367 /// assert_eq!(o, Equal);
3368 /// ```
3369 pub fn ieee_remainder_round_ref_ref(&self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
3370 let prec = max(self.significant_bits(), other.significant_bits());
3371 self.ieee_remainder_prec_round_ref_ref(other, prec, rm)
3372 }
3373
3374 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
3375 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
3376 /// remainder to the nearest value of the maximum of the precisions of the inputs. Both
3377 /// [`Float`]s are taken by value. An [`Ordering`] is also returned, indicating whether the
3378 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
3379 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
3380 /// returns `Equal`.
3381 ///
3382 /// $$
3383 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3384 /// $$
3385 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3386 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3387 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
3388 ///
3389 /// If the output has a precision, it is the maximum of the precisions of the inputs.
3390 ///
3391 /// Special cases:
3392 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3393 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3394 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3395 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3396 ///
3397 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3398 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3399 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3400 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3401 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3402 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3403 ///
3404 /// If you want to specify an output precision, consider using [`Float::ieee_remainder_prec`]
3405 /// instead. If you want to use a rounding mode other than `Nearest`, consider using
3406 /// [`Float::ieee_remainder_round`] instead.
3407 ///
3408 /// # Worst-case complexity
3409 /// $T(n) = O(n \log n \log\log n)$
3410 ///
3411 /// $M(n) = O(n)$
3412 ///
3413 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3414 /// other.complexity())`.
3415 ///
3416 /// # Examples
3417 /// ```
3418 /// use malachite_float::Float;
3419 ///
3420 /// assert_eq!(
3421 /// Float::from(14u32)
3422 /// .ieee_remainder(Float::from(3u32))
3423 /// .to_string(),
3424 /// "-1.0"
3425 /// );
3426 ///
3427 /// assert_eq!(
3428 /// Float::from(10u32)
3429 /// .ieee_remainder(Float::from(3u32))
3430 /// .to_string(),
3431 /// "1.0"
3432 /// );
3433 /// ```
3434 #[inline]
3435 pub fn ieee_remainder(self, other: Self) -> Self {
3436 self.ieee_remainder_round(other, Nearest).0
3437 }
3438
3439 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
3440 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
3441 /// remainder to the nearest value of the maximum of the precisions of the inputs. The first
3442 /// [`Float`] is taken by value and the second by reference. An [`Ordering`] is also returned,
3443 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
3444 /// remainder. Although `NaN`s are not comparable to any [`Float`], whenever this function
3445 /// returns a `NaN` it also returns `Equal`.
3446 ///
3447 /// $$
3448 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3449 /// $$
3450 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3451 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3452 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
3453 ///
3454 /// If the output has a precision, it is the maximum of the precisions of the inputs.
3455 ///
3456 /// Special cases:
3457 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3458 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3459 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3460 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3461 ///
3462 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3463 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3464 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3465 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3466 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3467 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3468 ///
3469 /// If you want to specify an output precision, consider using [`Float::ieee_remainder_prec`]
3470 /// instead. If you want to use a rounding mode other than `Nearest`, consider using
3471 /// [`Float::ieee_remainder_round`] instead.
3472 ///
3473 /// # Worst-case complexity
3474 /// $T(n) = O(n \log n \log\log n)$
3475 ///
3476 /// $M(n) = O(n)$
3477 ///
3478 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3479 /// other.complexity())`.
3480 ///
3481 /// # Examples
3482 /// ```
3483 /// use malachite_float::Float;
3484 ///
3485 /// let r = Float::from(14u32).ieee_remainder_val_ref(&Float::from(3u32));
3486 /// assert_eq!(r.to_string(), "-1.0");
3487 ///
3488 /// let r = Float::from(10u32).ieee_remainder_val_ref(&Float::from(3u32));
3489 /// assert_eq!(r.to_string(), "1.0");
3490 /// ```
3491 #[inline]
3492 pub fn ieee_remainder_val_ref(self, other: &Self) -> Self {
3493 self.ieee_remainder_round_val_ref(other, Nearest).0
3494 }
3495
3496 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
3497 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
3498 /// remainder to the nearest value of the maximum of the precisions of the inputs. The first
3499 /// [`Float`] is taken by reference and the second by value. An [`Ordering`] is also returned,
3500 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
3501 /// remainder. Although `NaN`s are not comparable to any [`Float`], whenever this function
3502 /// returns a `NaN` it also returns `Equal`.
3503 ///
3504 /// $$
3505 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3506 /// $$
3507 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3508 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3509 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
3510 ///
3511 /// If the output has a precision, it is the maximum of the precisions of the inputs.
3512 ///
3513 /// Special cases:
3514 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3515 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3516 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3517 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3518 ///
3519 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3520 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3521 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3522 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3523 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3524 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3525 ///
3526 /// If you want to specify an output precision, consider using [`Float::ieee_remainder_prec`]
3527 /// instead. If you want to use a rounding mode other than `Nearest`, consider using
3528 /// [`Float::ieee_remainder_round`] instead.
3529 ///
3530 /// # Worst-case complexity
3531 /// $T(n) = O(n \log n \log\log n)$
3532 ///
3533 /// $M(n) = O(n)$
3534 ///
3535 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3536 /// other.complexity())`.
3537 ///
3538 /// # Examples
3539 /// ```
3540 /// use malachite_float::Float;
3541 ///
3542 /// let r = Float::from(14u32).ieee_remainder_ref_val(Float::from(3u32));
3543 /// assert_eq!(r.to_string(), "-1.0");
3544 ///
3545 /// assert_eq!(
3546 /// Float::from(10u32)
3547 /// .ieee_remainder_ref_val(Float::from(3u32))
3548 /// .to_string(),
3549 /// "1.0"
3550 /// );
3551 /// ```
3552 #[inline]
3553 pub fn ieee_remainder_ref_val(&self, other: Self) -> Self {
3554 self.ieee_remainder_round_ref_val(other, Nearest).0
3555 }
3556
3557 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
3558 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
3559 /// remainder to the nearest value of the maximum of the precisions of the inputs. Both
3560 /// [`Float`]s are taken by reference. An [`Ordering`] is also returned, indicating whether the
3561 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
3562 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
3563 /// returns `Equal`.
3564 ///
3565 /// $$
3566 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3567 /// $$
3568 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3569 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3570 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
3571 ///
3572 /// If the output has a precision, it is the maximum of the precisions of the inputs.
3573 ///
3574 /// Special cases:
3575 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3576 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3577 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3578 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3579 ///
3580 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3581 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3582 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3583 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3584 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3585 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3586 ///
3587 /// If you want to specify an output precision, consider using [`Float::ieee_remainder_prec`]
3588 /// instead. If you want to use a rounding mode other than `Nearest`, consider using
3589 /// [`Float::ieee_remainder_round`] instead.
3590 ///
3591 /// # Worst-case complexity
3592 /// $T(n) = O(n \log n \log\log n)$
3593 ///
3594 /// $M(n) = O(n)$
3595 ///
3596 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3597 /// other.complexity())`.
3598 ///
3599 /// # Examples
3600 /// ```
3601 /// use malachite_float::Float;
3602 ///
3603 /// let r = Float::from(14u32).ieee_remainder_ref_ref(&Float::from(3u32));
3604 /// assert_eq!(r.to_string(), "-1.0");
3605 ///
3606 /// let r = Float::from(10u32).ieee_remainder_ref_ref(&Float::from(3u32));
3607 /// assert_eq!(r.to_string(), "1.0");
3608 /// ```
3609 #[inline]
3610 pub fn ieee_remainder_ref_ref(&self, other: &Self) -> Self {
3611 self.ieee_remainder_round_ref_ref(other, Nearest).0
3612 }
3613
3614 /// Computes the remainder of two [`Float`]s in place, with the quotient rounded to the nearest
3615 /// integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding
3616 /// the remainder to the specified precision and with the specified rounding mode. The [`Float`]
3617 /// on the right-hand side is taken by value. An [`Ordering`] is returned, indicating whether
3618 /// the rounded remainder is less than, equal to, or greater than the exact remainder. Although
3619 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
3620 /// returns `Equal`.
3621 ///
3622 /// $$
3623 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3624 /// $$
3625 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3626 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3627 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
3628 ///
3629 /// If the output has a precision, it is `prec`.
3630 ///
3631 /// Special cases:
3632 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3633 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3634 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3635 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3636 ///
3637 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3638 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3639 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3640 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3641 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3642 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3643 ///
3644 /// # Worst-case complexity
3645 /// $T(n) = O(n \log n \log\log n)$
3646 ///
3647 /// $M(n) = O(n)$
3648 ///
3649 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3650 /// other.complexity(), prec)`.
3651 ///
3652 /// # Panics
3653 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
3654 /// with `prec` bits.
3655 ///
3656 /// # Examples
3657 /// ```
3658 /// use core::cmp::Ordering::*;
3659 /// use malachite_base::rounding_modes::RoundingMode::*;
3660 /// use malachite_float::Float;
3661 ///
3662 /// let mut x = Float::from(14u32);
3663 /// assert_eq!(
3664 /// x.ieee_remainder_prec_round_assign(Float::from(3u32), 10, Nearest),
3665 /// Equal
3666 /// );
3667 /// assert_eq!(x.to_string(), "-1.0000");
3668 /// ```
3669 #[allow(clippy::needless_pass_by_value)]
3670 pub fn ieee_remainder_prec_round_assign(
3671 &mut self,
3672 other: Self,
3673 prec: u64,
3674 rm: RoundingMode,
3675 ) -> Ordering {
3676 let (r, o, _) = rem1_helper(self, &other, true, false, prec, rm);
3677 *self = r;
3678 o
3679 }
3680
3681 /// Computes the remainder of two [`Float`]s in place, with the quotient rounded to the nearest
3682 /// integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding
3683 /// the remainder to the specified precision and with the specified rounding mode. The [`Float`]
3684 /// on the right-hand side is taken by reference. An [`Ordering`] is returned, indicating
3685 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder.
3686 /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
3687 /// it also returns `Equal`.
3688 ///
3689 /// $$
3690 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3691 /// $$
3692 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3693 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3694 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
3695 ///
3696 /// If the output has a precision, it is `prec`.
3697 ///
3698 /// Special cases:
3699 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3700 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3701 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3702 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3703 ///
3704 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3705 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3706 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3707 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3708 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3709 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3710 ///
3711 /// # Worst-case complexity
3712 /// $T(n) = O(n \log n \log\log n)$
3713 ///
3714 /// $M(n) = O(n)$
3715 ///
3716 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3717 /// other.complexity(), prec)`.
3718 ///
3719 /// # Panics
3720 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
3721 /// with `prec` bits.
3722 ///
3723 /// # Examples
3724 /// ```
3725 /// use core::cmp::Ordering::*;
3726 /// use malachite_base::rounding_modes::RoundingMode::*;
3727 /// use malachite_float::Float;
3728 ///
3729 /// let mut x = Float::from(14u32);
3730 /// assert_eq!(
3731 /// x.ieee_remainder_prec_round_assign_ref(&Float::from(3u32), 10, Nearest),
3732 /// Equal
3733 /// );
3734 /// assert_eq!(x.to_string(), "-1.0000");
3735 /// ```
3736 pub fn ieee_remainder_prec_round_assign_ref(
3737 &mut self,
3738 other: &Self,
3739 prec: u64,
3740 rm: RoundingMode,
3741 ) -> Ordering {
3742 let (r, o, _) = rem1_helper(self, other, true, false, prec, rm);
3743 *self = r;
3744 o
3745 }
3746
3747 /// Computes the remainder of two [`Float`]s in place, with the quotient rounded to the nearest
3748 /// integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding
3749 /// the remainder to the nearest value of the specified precision. The [`Float`] on the
3750 /// right-hand side is taken by value. An [`Ordering`] is returned, indicating whether the
3751 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
3752 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
3753 /// returns `Equal`.
3754 ///
3755 /// $$
3756 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3757 /// $$
3758 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3759 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3760 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
3761 ///
3762 /// If the output has a precision, it is `prec`.
3763 ///
3764 /// Special cases:
3765 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3766 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3767 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3768 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3769 ///
3770 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3771 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3772 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3773 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3774 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3775 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3776 ///
3777 /// # Worst-case complexity
3778 /// $T(n) = O(n \log n \log\log n)$
3779 ///
3780 /// $M(n) = O(n)$
3781 ///
3782 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3783 /// other.complexity(), prec)`.
3784 ///
3785 /// # Panics
3786 /// Panics if `prec` is zero.
3787 ///
3788 /// # Examples
3789 /// ```
3790 /// use core::cmp::Ordering::*;
3791 /// use malachite_float::Float;
3792 ///
3793 /// let mut x = Float::from(14u32);
3794 /// assert_eq!(x.ieee_remainder_prec_assign(Float::from(3u32), 10), Equal);
3795 /// assert_eq!(x.to_string(), "-1.0000");
3796 /// ```
3797 #[inline]
3798 pub fn ieee_remainder_prec_assign(&mut self, other: Self, prec: u64) -> Ordering {
3799 self.ieee_remainder_prec_round_assign(other, prec, Nearest)
3800 }
3801
3802 /// Computes the remainder of two [`Float`]s in place, with the quotient rounded to the nearest
3803 /// integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding
3804 /// the remainder to the nearest value of the specified precision. The [`Float`] on the
3805 /// right-hand side is taken by reference. An [`Ordering`] is returned, indicating whether the
3806 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
3807 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
3808 /// returns `Equal`.
3809 ///
3810 /// $$
3811 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3812 /// $$
3813 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3814 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3815 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
3816 ///
3817 /// If the output has a precision, it is `prec`.
3818 ///
3819 /// Special cases:
3820 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3821 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3822 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3823 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3824 ///
3825 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3826 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3827 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3828 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3829 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3830 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3831 ///
3832 /// # Worst-case complexity
3833 /// $T(n) = O(n \log n \log\log n)$
3834 ///
3835 /// $M(n) = O(n)$
3836 ///
3837 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3838 /// other.complexity(), prec)`.
3839 ///
3840 /// # Panics
3841 /// Panics if `prec` is zero.
3842 ///
3843 /// # Examples
3844 /// ```
3845 /// use core::cmp::Ordering::*;
3846 /// use malachite_float::Float;
3847 ///
3848 /// let mut x = Float::from(14u32);
3849 /// assert_eq!(
3850 /// x.ieee_remainder_prec_assign_ref(&Float::from(3u32), 10),
3851 /// Equal
3852 /// );
3853 /// assert_eq!(x.to_string(), "-1.0000");
3854 /// ```
3855 #[inline]
3856 pub fn ieee_remainder_prec_assign_ref(&mut self, other: &Self, prec: u64) -> Ordering {
3857 self.ieee_remainder_prec_round_assign_ref(other, prec, Nearest)
3858 }
3859
3860 /// Computes the remainder of two [`Float`]s in place, with the quotient rounded to the nearest
3861 /// integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding
3862 /// the remainder to the maximum of the precisions of the inputs, with the specified rounding
3863 /// mode. The [`Float`] on the right-hand side is taken by value. An [`Ordering`] is returned,
3864 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
3865 /// remainder. Although `NaN`s are not comparable to any [`Float`], whenever this function
3866 /// returns a `NaN` it also returns `Equal`.
3867 ///
3868 /// $$
3869 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3870 /// $$
3871 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3872 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3873 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
3874 ///
3875 /// If the output has a precision, it is the maximum of the precisions of the inputs.
3876 ///
3877 /// Special cases:
3878 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3879 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3880 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3881 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3882 ///
3883 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3884 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3885 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3886 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3887 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3888 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3889 ///
3890 /// # Worst-case complexity
3891 /// $T(n) = O(n \log n \log\log n)$
3892 ///
3893 /// $M(n) = O(n)$
3894 ///
3895 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3896 /// other.complexity())`.
3897 ///
3898 /// # Panics
3899 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
3900 /// precision.
3901 ///
3902 /// # Examples
3903 /// ```
3904 /// use core::cmp::Ordering::*;
3905 /// use malachite_base::rounding_modes::RoundingMode::*;
3906 /// use malachite_float::Float;
3907 ///
3908 /// let mut x = Float::from(14u32);
3909 /// assert_eq!(
3910 /// x.ieee_remainder_round_assign(Float::from(3u32), Floor),
3911 /// Equal
3912 /// );
3913 /// assert_eq!(x.to_string(), "-1.0");
3914 /// ```
3915 pub fn ieee_remainder_round_assign(&mut self, other: Self, rm: RoundingMode) -> Ordering {
3916 let prec = max(self.significant_bits(), other.significant_bits());
3917 self.ieee_remainder_prec_round_assign(other, prec, rm)
3918 }
3919
3920 /// Computes the remainder of two [`Float`]s in place, with the quotient rounded to the nearest
3921 /// integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding
3922 /// the remainder to the maximum of the precisions of the inputs, with the specified rounding
3923 /// mode. The [`Float`] on the right-hand side is taken by reference. An [`Ordering`] is
3924 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
3925 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
3926 /// function returns a `NaN` it also returns `Equal`.
3927 ///
3928 /// $$
3929 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3930 /// $$
3931 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3932 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3933 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
3934 ///
3935 /// If the output has a precision, it is the maximum of the precisions of the inputs.
3936 ///
3937 /// Special cases:
3938 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3939 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
3940 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
3941 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
3942 ///
3943 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
3944 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
3945 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3946 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3947 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3948 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3949 ///
3950 /// # Worst-case complexity
3951 /// $T(n) = O(n \log n \log\log n)$
3952 ///
3953 /// $M(n) = O(n)$
3954 ///
3955 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3956 /// other.complexity())`.
3957 ///
3958 /// # Panics
3959 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
3960 /// precision.
3961 ///
3962 /// # Examples
3963 /// ```
3964 /// use core::cmp::Ordering::*;
3965 /// use malachite_base::rounding_modes::RoundingMode::*;
3966 /// use malachite_float::Float;
3967 ///
3968 /// let mut x = Float::from(14u32);
3969 /// assert_eq!(
3970 /// x.ieee_remainder_round_assign_ref(&Float::from(3u32), Floor),
3971 /// Equal
3972 /// );
3973 /// assert_eq!(x.to_string(), "-1.0");
3974 /// ```
3975 pub fn ieee_remainder_round_assign_ref(&mut self, other: &Self, rm: RoundingMode) -> Ordering {
3976 let prec = max(self.significant_bits(), other.significant_bits());
3977 self.ieee_remainder_prec_round_assign_ref(other, prec, rm)
3978 }
3979
3980 /// Computes the remainder of two [`Float`]s in place, with the quotient rounded to the nearest
3981 /// integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding
3982 /// the remainder to the nearest value of the maximum of the precisions of the inputs. The
3983 /// [`Float`] on the right-hand side is taken by value. An [`Ordering`] is returned, indicating
3984 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder.
3985 /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
3986 /// it also returns `Equal`.
3987 ///
3988 /// $$
3989 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
3990 /// $$
3991 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
3992 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
3993 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
3994 ///
3995 /// If the output has a precision, it is the maximum of the precisions of the inputs.
3996 ///
3997 /// Special cases:
3998 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
3999 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
4000 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
4001 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
4002 ///
4003 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
4004 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
4005 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4006 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4007 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4008 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4009 ///
4010 /// # Worst-case complexity
4011 /// $T(n) = O(n \log n \log\log n)$
4012 ///
4013 /// $M(n) = O(n)$
4014 ///
4015 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
4016 /// other.complexity())`.
4017 ///
4018 /// # Examples
4019 /// ```
4020 /// use malachite_float::Float;
4021 ///
4022 /// let mut x = Float::from(14u32);
4023 /// x.ieee_remainder_assign(Float::from(3u32));
4024 /// assert_eq!(x.to_string(), "-1.0");
4025 /// ```
4026 #[inline]
4027 pub fn ieee_remainder_assign(&mut self, other: Self) {
4028 self.ieee_remainder_round_assign(other, Nearest);
4029 }
4030
4031 /// Computes the remainder of two [`Float`]s in place, with the quotient rounded to the nearest
4032 /// integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding
4033 /// the remainder to the nearest value of the maximum of the precisions of the inputs. The
4034 /// [`Float`] on the right-hand side is taken by reference. An [`Ordering`] is returned,
4035 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
4036 /// remainder. Although `NaN`s are not comparable to any [`Float`], whenever this function
4037 /// returns a `NaN` it also returns `Equal`.
4038 ///
4039 /// $$
4040 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
4041 /// $$
4042 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
4043 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
4044 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
4045 ///
4046 /// If the output has a precision, it is the maximum of the precisions of the inputs.
4047 ///
4048 /// Special cases:
4049 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
4050 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
4051 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
4052 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
4053 ///
4054 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
4055 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
4056 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4057 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4058 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4059 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4060 ///
4061 /// # Worst-case complexity
4062 /// $T(n) = O(n \log n \log\log n)$
4063 ///
4064 /// $M(n) = O(n)$
4065 ///
4066 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
4067 /// other.complexity())`.
4068 ///
4069 /// # Examples
4070 /// ```
4071 /// use malachite_float::Float;
4072 ///
4073 /// let mut x = Float::from(14u32);
4074 /// x.ieee_remainder_assign_ref(&Float::from(3u32));
4075 /// assert_eq!(x.to_string(), "-1.0");
4076 /// ```
4077 #[inline]
4078 pub fn ieee_remainder_assign_ref(&mut self, other: &Self) {
4079 self.ieee_remainder_round_assign_ref(other, Nearest);
4080 }
4081
4082 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
4083 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
4084 /// remainder to the specified precision and with the specified rounding mode. Both [`Float`]s
4085 /// are taken by value. An [`Ordering`] is also returned, indicating whether the rounded
4086 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
4087 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
4088 /// whenever this function returns a `NaN` it also returns `Equal`.
4089 ///
4090 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
4091 /// it equals $\pm(|q|\bmod 2^{63})$. (MPFR documents the same contract for its `quo` output,
4092 /// but its C implementation can overflow a `long` when the low 63 bits are all ones and the
4093 /// quotient rounds away from zero; this implementation always keeps the modular contract.)
4094 ///
4095 /// $$
4096 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
4097 /// $$
4098 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
4099 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
4100 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
4101 ///
4102 /// If the output has a precision, it is `prec`.
4103 ///
4104 /// Special cases:
4105 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
4106 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
4107 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
4108 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
4109 /// - The quotient bits are 0 in all of the above special cases.
4110 ///
4111 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
4112 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
4113 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4114 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4115 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4116 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4117 ///
4118 /// If you know you'll be using `Nearest`, consider using
4119 /// [`Float::ieee_remainder_and_quotient_bits_prec`] instead. If you know that your target
4120 /// precision is the maximum of the precisions of the two inputs, consider using
4121 /// [`Float::ieee_remainder_and_quotient_bits_round`] instead. If both of these things are true,
4122 /// consider using [`Float::ieee_remainder_and_quotient_bits`] instead.
4123 ///
4124 /// # Worst-case complexity
4125 /// $T(n) = O(n \log n \log\log n)$
4126 ///
4127 /// $M(n) = O(n)$
4128 ///
4129 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
4130 /// other.complexity(), prec)`.
4131 ///
4132 /// # Panics
4133 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
4134 /// with `prec` bits.
4135 ///
4136 /// # Examples
4137 /// ```
4138 /// use core::cmp::Ordering::*;
4139 /// use malachite_base::rounding_modes::RoundingMode::*;
4140 /// use malachite_float::Float;
4141 ///
4142 /// let x = Float::from(14u32);
4143 /// let y = Float::from(3u32);
4144 /// let (r, o, q) = x.ieee_remainder_and_quotient_bits_prec_round(y, 10, Floor);
4145 /// assert_eq!(r.to_string(), "-1.0000");
4146 /// assert_eq!(o, Equal);
4147 /// assert_eq!(q, 5);
4148 /// ```
4149 #[allow(clippy::needless_pass_by_value)]
4150 #[inline]
4151 pub fn ieee_remainder_and_quotient_bits_prec_round(
4152 self,
4153 other: Self,
4154 prec: u64,
4155 rm: RoundingMode,
4156 ) -> (Self, Ordering, i64) {
4157 rem1_helper(&self, &other, true, true, prec, rm)
4158 }
4159
4160 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
4161 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
4162 /// remainder to the specified precision and with the specified rounding mode. The first
4163 /// [`Float`] is taken by value and the second by reference. An [`Ordering`] is also returned,
4164 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
4165 /// remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s are not
4166 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4167 ///
4168 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
4169 /// it equals $\pm(|q|\bmod 2^{63})$. (MPFR documents the same contract for its `quo` output,
4170 /// but its C implementation can overflow a `long` when the low 63 bits are all ones and the
4171 /// quotient rounds away from zero; this implementation always keeps the modular contract.)
4172 ///
4173 /// $$
4174 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
4175 /// $$
4176 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
4177 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
4178 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
4179 ///
4180 /// If the output has a precision, it is `prec`.
4181 ///
4182 /// Special cases:
4183 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
4184 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
4185 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
4186 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
4187 /// - The quotient bits are 0 in all of the above special cases.
4188 ///
4189 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
4190 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
4191 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4192 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4193 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4194 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4195 ///
4196 /// If you know you'll be using `Nearest`, consider using
4197 /// [`Float::ieee_remainder_and_quotient_bits_prec`] instead. If you know that your target
4198 /// precision is the maximum of the precisions of the two inputs, consider using
4199 /// [`Float::ieee_remainder_and_quotient_bits_round`] instead. If both of these things are true,
4200 /// consider using [`Float::ieee_remainder_and_quotient_bits`] instead.
4201 ///
4202 /// # Worst-case complexity
4203 /// $T(n) = O(n \log n \log\log n)$
4204 ///
4205 /// $M(n) = O(n)$
4206 ///
4207 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
4208 /// other.complexity(), prec)`.
4209 ///
4210 /// # Panics
4211 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
4212 /// with `prec` bits.
4213 ///
4214 /// # Examples
4215 /// ```
4216 /// use core::cmp::Ordering::*;
4217 /// use malachite_base::rounding_modes::RoundingMode::*;
4218 /// use malachite_float::Float;
4219 ///
4220 /// let x = Float::from(14u32);
4221 /// let y = Float::from(3u32);
4222 /// let (r, o, q) = x.ieee_remainder_and_quotient_bits_prec_round_val_ref(&y, 10, Floor);
4223 /// assert_eq!(r.to_string(), "-1.0000");
4224 /// assert_eq!(o, Equal);
4225 /// assert_eq!(q, 5);
4226 /// ```
4227 #[inline]
4228 pub fn ieee_remainder_and_quotient_bits_prec_round_val_ref(
4229 self,
4230 other: &Self,
4231 prec: u64,
4232 rm: RoundingMode,
4233 ) -> (Self, Ordering, i64) {
4234 rem1_helper(&self, other, true, true, prec, rm)
4235 }
4236
4237 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
4238 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
4239 /// remainder to the specified precision and with the specified rounding mode. The first
4240 /// [`Float`] is taken by reference and the second by value. An [`Ordering`] is also returned,
4241 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
4242 /// remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s are not
4243 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4244 ///
4245 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
4246 /// it equals $\pm(|q|\bmod 2^{63})$. (MPFR documents the same contract for its `quo` output,
4247 /// but its C implementation can overflow a `long` when the low 63 bits are all ones and the
4248 /// quotient rounds away from zero; this implementation always keeps the modular contract.)
4249 ///
4250 /// $$
4251 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
4252 /// $$
4253 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
4254 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
4255 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
4256 ///
4257 /// If the output has a precision, it is `prec`.
4258 ///
4259 /// Special cases:
4260 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
4261 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
4262 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
4263 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
4264 /// - The quotient bits are 0 in all of the above special cases.
4265 ///
4266 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
4267 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
4268 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4269 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4270 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4271 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4272 ///
4273 /// If you know you'll be using `Nearest`, consider using
4274 /// [`Float::ieee_remainder_and_quotient_bits_prec`] instead. If you know that your target
4275 /// precision is the maximum of the precisions of the two inputs, consider using
4276 /// [`Float::ieee_remainder_and_quotient_bits_round`] instead. If both of these things are true,
4277 /// consider using [`Float::ieee_remainder_and_quotient_bits`] instead.
4278 ///
4279 /// # Worst-case complexity
4280 /// $T(n) = O(n \log n \log\log n)$
4281 ///
4282 /// $M(n) = O(n)$
4283 ///
4284 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
4285 /// other.complexity(), prec)`.
4286 ///
4287 /// # Panics
4288 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
4289 /// with `prec` bits.
4290 ///
4291 /// # Examples
4292 /// ```
4293 /// use core::cmp::Ordering::*;
4294 /// use malachite_base::rounding_modes::RoundingMode::*;
4295 /// use malachite_float::Float;
4296 ///
4297 /// let x = Float::from(14u32);
4298 /// let y = Float::from(3u32);
4299 /// let (r, o, q) = x.ieee_remainder_and_quotient_bits_prec_round_ref_val(y, 10, Floor);
4300 /// assert_eq!(r.to_string(), "-1.0000");
4301 /// assert_eq!(o, Equal);
4302 /// assert_eq!(q, 5);
4303 /// ```
4304 #[allow(clippy::needless_pass_by_value)]
4305 #[inline]
4306 pub fn ieee_remainder_and_quotient_bits_prec_round_ref_val(
4307 &self,
4308 other: Self,
4309 prec: u64,
4310 rm: RoundingMode,
4311 ) -> (Self, Ordering, i64) {
4312 rem1_helper(self, &other, true, true, prec, rm)
4313 }
4314
4315 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
4316 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
4317 /// remainder to the specified precision and with the specified rounding mode. Both [`Float`]s
4318 /// are taken by reference. An [`Ordering`] is also returned, indicating whether the rounded
4319 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
4320 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
4321 /// whenever this function returns a `NaN` it also returns `Equal`.
4322 ///
4323 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
4324 /// it equals $\pm(|q|\bmod 2^{63})$. (MPFR documents the same contract for its `quo` output,
4325 /// but its C implementation can overflow a `long` when the low 63 bits are all ones and the
4326 /// quotient rounds away from zero; this implementation always keeps the modular contract.)
4327 ///
4328 /// $$
4329 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
4330 /// $$
4331 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
4332 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
4333 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
4334 ///
4335 /// If the output has a precision, it is `prec`.
4336 ///
4337 /// Special cases:
4338 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
4339 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
4340 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
4341 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
4342 /// - The quotient bits are 0 in all of the above special cases.
4343 ///
4344 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
4345 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
4346 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4347 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4348 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4349 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4350 ///
4351 /// If you know you'll be using `Nearest`, consider using
4352 /// [`Float::ieee_remainder_and_quotient_bits_prec`] instead. If you know that your target
4353 /// precision is the maximum of the precisions of the two inputs, consider using
4354 /// [`Float::ieee_remainder_and_quotient_bits_round`] instead. If both of these things are true,
4355 /// consider using [`Float::ieee_remainder_and_quotient_bits`] instead.
4356 ///
4357 /// # Worst-case complexity
4358 /// $T(n) = O(n \log n \log\log n)$
4359 ///
4360 /// $M(n) = O(n)$
4361 ///
4362 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
4363 /// other.complexity(), prec)`.
4364 ///
4365 /// # Panics
4366 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
4367 /// with `prec` bits.
4368 ///
4369 /// # Examples
4370 /// ```
4371 /// use core::cmp::Ordering::*;
4372 /// use malachite_base::rounding_modes::RoundingMode::*;
4373 /// use malachite_float::Float;
4374 ///
4375 /// let x = Float::from(14u32);
4376 /// let y = Float::from(3u32);
4377 /// let (r, o, q) = x.ieee_remainder_and_quotient_bits_prec_round_ref_ref(&y, 10, Floor);
4378 /// assert_eq!(r.to_string(), "-1.0000");
4379 /// assert_eq!(o, Equal);
4380 /// assert_eq!(q, 5);
4381 /// ```
4382 #[inline]
4383 pub fn ieee_remainder_and_quotient_bits_prec_round_ref_ref(
4384 &self,
4385 other: &Self,
4386 prec: u64,
4387 rm: RoundingMode,
4388 ) -> (Self, Ordering, i64) {
4389 rem1_helper(self, other, true, true, prec, rm)
4390 }
4391
4392 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
4393 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
4394 /// remainder to the nearest value of the specified precision. Both [`Float`]s are taken by
4395 /// value. An [`Ordering`] is also returned, indicating whether the rounded remainder is less
4396 /// than, equal to, or greater than the exact remainder, along with the low bits of the quotient
4397 /// as an `i64`. Although `NaN`s are not comparable to any [`Float`], whenever this function
4398 /// returns a `NaN` it also returns `Equal`.
4399 ///
4400 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
4401 /// it equals $\pm(|q|\bmod 2^{63})$.
4402 ///
4403 /// $$
4404 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
4405 /// $$
4406 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
4407 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
4408 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
4409 ///
4410 /// If the output has a precision, it is `prec`.
4411 ///
4412 /// Special cases:
4413 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
4414 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
4415 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
4416 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
4417 /// - The quotient bits are 0 in all of the above special cases.
4418 ///
4419 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
4420 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
4421 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4422 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4423 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4424 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4425 ///
4426 /// If you want to use a rounding mode other than `Nearest`, consider using
4427 /// [`Float::ieee_remainder_and_quotient_bits_prec_round`] instead. If you know that your target
4428 /// precision is the maximum of the precisions of the two inputs, consider using
4429 /// [`Float::ieee_remainder_and_quotient_bits`] instead.
4430 ///
4431 /// # Worst-case complexity
4432 /// $T(n) = O(n \log n \log\log n)$
4433 ///
4434 /// $M(n) = O(n)$
4435 ///
4436 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
4437 /// other.complexity(), prec)`.
4438 ///
4439 /// # Panics
4440 /// Panics if `prec` is zero.
4441 ///
4442 /// # Examples
4443 /// ```
4444 /// use core::cmp::Ordering::*;
4445 /// use malachite_float::Float;
4446 ///
4447 /// let (r, o, q) =
4448 /// Float::from(14u32).ieee_remainder_and_quotient_bits_prec(Float::from(3u32), 10);
4449 /// assert_eq!(r.to_string(), "-1.0000");
4450 /// assert_eq!(o, Equal);
4451 /// assert_eq!(q, 5);
4452 /// ```
4453 #[inline]
4454 pub fn ieee_remainder_and_quotient_bits_prec(
4455 self,
4456 other: Self,
4457 prec: u64,
4458 ) -> (Self, Ordering, i64) {
4459 self.ieee_remainder_and_quotient_bits_prec_round(other, prec, Nearest)
4460 }
4461
4462 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
4463 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
4464 /// remainder to the nearest value of the specified precision. The first [`Float`] is taken by
4465 /// value and the second by reference. An [`Ordering`] is also returned, indicating whether the
4466 /// rounded remainder is less than, equal to, or greater than the exact remainder, along with
4467 /// the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
4468 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4469 ///
4470 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
4471 /// it equals $\pm(|q|\bmod 2^{63})$.
4472 ///
4473 /// $$
4474 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
4475 /// $$
4476 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
4477 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
4478 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
4479 ///
4480 /// If the output has a precision, it is `prec`.
4481 ///
4482 /// Special cases:
4483 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
4484 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
4485 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
4486 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
4487 /// - The quotient bits are 0 in all of the above special cases.
4488 ///
4489 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
4490 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
4491 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4492 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4493 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4494 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4495 ///
4496 /// If you want to use a rounding mode other than `Nearest`, consider using
4497 /// [`Float::ieee_remainder_and_quotient_bits_prec_round`] instead. If you know that your target
4498 /// precision is the maximum of the precisions of the two inputs, consider using
4499 /// [`Float::ieee_remainder_and_quotient_bits`] instead.
4500 ///
4501 /// # Worst-case complexity
4502 /// $T(n) = O(n \log n \log\log n)$
4503 ///
4504 /// $M(n) = O(n)$
4505 ///
4506 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
4507 /// other.complexity(), prec)`.
4508 ///
4509 /// # Panics
4510 /// Panics if `prec` is zero.
4511 ///
4512 /// # Examples
4513 /// ```
4514 /// use core::cmp::Ordering::*;
4515 /// use malachite_float::Float;
4516 ///
4517 /// let x = Float::from(14u32);
4518 /// let y = Float::from(3u32);
4519 /// let (r, o, q) = x.ieee_remainder_and_quotient_bits_prec_val_ref(&y, 10);
4520 /// assert_eq!(r.to_string(), "-1.0000");
4521 /// assert_eq!(o, Equal);
4522 /// assert_eq!(q, 5);
4523 /// ```
4524 #[inline]
4525 pub fn ieee_remainder_and_quotient_bits_prec_val_ref(
4526 self,
4527 other: &Self,
4528 prec: u64,
4529 ) -> (Self, Ordering, i64) {
4530 self.ieee_remainder_and_quotient_bits_prec_round_val_ref(other, prec, Nearest)
4531 }
4532
4533 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
4534 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
4535 /// remainder to the nearest value of the specified precision. The first [`Float`] is taken by
4536 /// reference and the second by value. An [`Ordering`] is also returned, indicating whether the
4537 /// rounded remainder is less than, equal to, or greater than the exact remainder, along with
4538 /// the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
4539 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4540 ///
4541 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
4542 /// it equals $\pm(|q|\bmod 2^{63})$.
4543 ///
4544 /// $$
4545 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
4546 /// $$
4547 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
4548 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
4549 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
4550 ///
4551 /// If the output has a precision, it is `prec`.
4552 ///
4553 /// Special cases:
4554 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
4555 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
4556 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
4557 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
4558 /// - The quotient bits are 0 in all of the above special cases.
4559 ///
4560 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
4561 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
4562 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4563 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4564 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4565 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4566 ///
4567 /// If you want to use a rounding mode other than `Nearest`, consider using
4568 /// [`Float::ieee_remainder_and_quotient_bits_prec_round`] instead. If you know that your target
4569 /// precision is the maximum of the precisions of the two inputs, consider using
4570 /// [`Float::ieee_remainder_and_quotient_bits`] instead.
4571 ///
4572 /// # Worst-case complexity
4573 /// $T(n) = O(n \log n \log\log n)$
4574 ///
4575 /// $M(n) = O(n)$
4576 ///
4577 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
4578 /// other.complexity(), prec)`.
4579 ///
4580 /// # Panics
4581 /// Panics if `prec` is zero.
4582 ///
4583 /// # Examples
4584 /// ```
4585 /// use core::cmp::Ordering::*;
4586 /// use malachite_float::Float;
4587 ///
4588 /// let (r, o, q) =
4589 /// Float::from(14u32).ieee_remainder_and_quotient_bits_prec_ref_val(Float::from(3u32), 10);
4590 /// assert_eq!(r.to_string(), "-1.0000");
4591 /// assert_eq!(o, Equal);
4592 /// assert_eq!(q, 5);
4593 /// ```
4594 #[inline]
4595 pub fn ieee_remainder_and_quotient_bits_prec_ref_val(
4596 &self,
4597 other: Self,
4598 prec: u64,
4599 ) -> (Self, Ordering, i64) {
4600 self.ieee_remainder_and_quotient_bits_prec_round_ref_val(other, prec, Nearest)
4601 }
4602
4603 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
4604 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
4605 /// remainder to the nearest value of the specified precision. Both [`Float`]s are taken by
4606 /// reference. An [`Ordering`] is also returned, indicating whether the rounded remainder is
4607 /// less than, equal to, or greater than the exact remainder, along with the low bits of the
4608 /// quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`], whenever this
4609 /// function returns a `NaN` it also returns `Equal`.
4610 ///
4611 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
4612 /// it equals $\pm(|q|\bmod 2^{63})$.
4613 ///
4614 /// $$
4615 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
4616 /// $$
4617 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
4618 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
4619 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
4620 ///
4621 /// If the output has a precision, it is `prec`.
4622 ///
4623 /// Special cases:
4624 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
4625 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
4626 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
4627 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
4628 /// - The quotient bits are 0 in all of the above special cases.
4629 ///
4630 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
4631 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
4632 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4633 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4634 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4635 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4636 ///
4637 /// If you want to use a rounding mode other than `Nearest`, consider using
4638 /// [`Float::ieee_remainder_and_quotient_bits_prec_round`] instead. If you know that your target
4639 /// precision is the maximum of the precisions of the two inputs, consider using
4640 /// [`Float::ieee_remainder_and_quotient_bits`] instead.
4641 ///
4642 /// # Worst-case complexity
4643 /// $T(n) = O(n \log n \log\log n)$
4644 ///
4645 /// $M(n) = O(n)$
4646 ///
4647 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
4648 /// other.complexity(), prec)`.
4649 ///
4650 /// # Panics
4651 /// Panics if `prec` is zero.
4652 ///
4653 /// # Examples
4654 /// ```
4655 /// use core::cmp::Ordering::*;
4656 /// use malachite_float::Float;
4657 ///
4658 /// let x = Float::from(14u32);
4659 /// let y = Float::from(3u32);
4660 /// let (r, o, q) = x.ieee_remainder_and_quotient_bits_prec_ref_ref(&y, 10);
4661 /// assert_eq!(r.to_string(), "-1.0000");
4662 /// assert_eq!(o, Equal);
4663 /// assert_eq!(q, 5);
4664 /// ```
4665 #[inline]
4666 pub fn ieee_remainder_and_quotient_bits_prec_ref_ref(
4667 &self,
4668 other: &Self,
4669 prec: u64,
4670 ) -> (Self, Ordering, i64) {
4671 self.ieee_remainder_and_quotient_bits_prec_round_ref_ref(other, prec, Nearest)
4672 }
4673
4674 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
4675 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
4676 /// remainder to the maximum of the precisions of the inputs, with the specified rounding mode.
4677 /// Both [`Float`]s are taken by value. An [`Ordering`] is also returned, indicating whether the
4678 /// rounded remainder is less than, equal to, or greater than the exact remainder, along with
4679 /// the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
4680 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4681 ///
4682 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
4683 /// it equals $\pm(|q|\bmod 2^{63})$.
4684 ///
4685 /// $$
4686 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
4687 /// $$
4688 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
4689 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
4690 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
4691 ///
4692 /// If the output has a precision, it is the maximum of the precisions of the inputs.
4693 ///
4694 /// Special cases:
4695 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
4696 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
4697 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
4698 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
4699 /// - The quotient bits are 0 in all of the above special cases.
4700 ///
4701 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
4702 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
4703 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4704 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4705 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4706 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4707 ///
4708 /// If you want to specify an output precision, consider using
4709 /// [`Float::ieee_remainder_and_quotient_bits_prec_round`] instead. If you know you'll be using
4710 /// the `Nearest` rounding mode, consider using [`Float::ieee_remainder_and_quotient_bits`]
4711 /// instead.
4712 ///
4713 /// # Worst-case complexity
4714 /// $T(n) = O(n \log n \log\log n)$
4715 ///
4716 /// $M(n) = O(n)$
4717 ///
4718 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
4719 /// other.complexity())`.
4720 ///
4721 /// # Panics
4722 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
4723 /// precision.
4724 ///
4725 /// # Examples
4726 /// ```
4727 /// use core::cmp::Ordering::*;
4728 /// use malachite_base::rounding_modes::RoundingMode::*;
4729 /// use malachite_float::Float;
4730 ///
4731 /// let (r, o, q) =
4732 /// Float::from(14u32).ieee_remainder_and_quotient_bits_round(Float::from(3u32), Floor);
4733 /// assert_eq!(r.to_string(), "-1.0");
4734 /// assert_eq!(o, Equal);
4735 /// assert_eq!(q, 5);
4736 /// ```
4737 pub fn ieee_remainder_and_quotient_bits_round(
4738 self,
4739 other: Self,
4740 rm: RoundingMode,
4741 ) -> (Self, Ordering, i64) {
4742 let prec = max(self.significant_bits(), other.significant_bits());
4743 self.ieee_remainder_and_quotient_bits_prec_round(other, prec, rm)
4744 }
4745
4746 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
4747 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
4748 /// remainder to the maximum of the precisions of the inputs, with the specified rounding mode.
4749 /// The first [`Float`] is taken by value and the second by reference. An [`Ordering`] is also
4750 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
4751 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
4752 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
4753 /// `Equal`.
4754 ///
4755 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
4756 /// it equals $\pm(|q|\bmod 2^{63})$.
4757 ///
4758 /// $$
4759 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
4760 /// $$
4761 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
4762 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
4763 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
4764 ///
4765 /// If the output has a precision, it is the maximum of the precisions of the inputs.
4766 ///
4767 /// Special cases:
4768 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
4769 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
4770 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
4771 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
4772 /// - The quotient bits are 0 in all of the above special cases.
4773 ///
4774 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
4775 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
4776 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4777 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4778 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4779 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4780 ///
4781 /// If you want to specify an output precision, consider using
4782 /// [`Float::ieee_remainder_and_quotient_bits_prec_round`] instead. If you know you'll be using
4783 /// the `Nearest` rounding mode, consider using [`Float::ieee_remainder_and_quotient_bits`]
4784 /// instead.
4785 ///
4786 /// # Worst-case complexity
4787 /// $T(n) = O(n \log n \log\log n)$
4788 ///
4789 /// $M(n) = O(n)$
4790 ///
4791 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
4792 /// other.complexity())`.
4793 ///
4794 /// # Panics
4795 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
4796 /// precision.
4797 ///
4798 /// # Examples
4799 /// ```
4800 /// use core::cmp::Ordering::*;
4801 /// use malachite_base::rounding_modes::RoundingMode::*;
4802 /// use malachite_float::Float;
4803 ///
4804 /// let x = Float::from(14u32);
4805 /// let y = Float::from(3u32);
4806 /// let (r, o, q) = x.ieee_remainder_and_quotient_bits_round_val_ref(&y, Floor);
4807 /// assert_eq!(r.to_string(), "-1.0");
4808 /// assert_eq!(o, Equal);
4809 /// assert_eq!(q, 5);
4810 /// ```
4811 pub fn ieee_remainder_and_quotient_bits_round_val_ref(
4812 self,
4813 other: &Self,
4814 rm: RoundingMode,
4815 ) -> (Self, Ordering, i64) {
4816 let prec = max(self.significant_bits(), other.significant_bits());
4817 self.ieee_remainder_and_quotient_bits_prec_round_val_ref(other, prec, rm)
4818 }
4819
4820 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
4821 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
4822 /// remainder to the maximum of the precisions of the inputs, with the specified rounding mode.
4823 /// The first [`Float`] is taken by reference and the second by value. An [`Ordering`] is also
4824 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
4825 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
4826 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
4827 /// `Equal`.
4828 ///
4829 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
4830 /// it equals $\pm(|q|\bmod 2^{63})$.
4831 ///
4832 /// $$
4833 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
4834 /// $$
4835 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
4836 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
4837 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
4838 ///
4839 /// If the output has a precision, it is the maximum of the precisions of the inputs.
4840 ///
4841 /// Special cases:
4842 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
4843 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
4844 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
4845 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
4846 /// - The quotient bits are 0 in all of the above special cases.
4847 ///
4848 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
4849 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
4850 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4851 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4852 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4853 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4854 ///
4855 /// If you want to specify an output precision, consider using
4856 /// [`Float::ieee_remainder_and_quotient_bits_prec_round`] instead. If you know you'll be using
4857 /// the `Nearest` rounding mode, consider using [`Float::ieee_remainder_and_quotient_bits`]
4858 /// instead.
4859 ///
4860 /// # Worst-case complexity
4861 /// $T(n) = O(n \log n \log\log n)$
4862 ///
4863 /// $M(n) = O(n)$
4864 ///
4865 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
4866 /// other.complexity())`.
4867 ///
4868 /// # Panics
4869 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
4870 /// precision.
4871 ///
4872 /// # Examples
4873 /// ```
4874 /// use core::cmp::Ordering::*;
4875 /// use malachite_base::rounding_modes::RoundingMode::*;
4876 /// use malachite_float::Float;
4877 ///
4878 /// let x = Float::from(14u32);
4879 /// let y = Float::from(3u32);
4880 /// let (r, o, q) = x.ieee_remainder_and_quotient_bits_round_ref_val(y, Floor);
4881 /// assert_eq!(r.to_string(), "-1.0");
4882 /// assert_eq!(o, Equal);
4883 /// assert_eq!(q, 5);
4884 /// ```
4885 pub fn ieee_remainder_and_quotient_bits_round_ref_val(
4886 &self,
4887 other: Self,
4888 rm: RoundingMode,
4889 ) -> (Self, Ordering, i64) {
4890 let prec = max(self.significant_bits(), other.significant_bits());
4891 self.ieee_remainder_and_quotient_bits_prec_round_ref_val(other, prec, rm)
4892 }
4893
4894 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
4895 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
4896 /// remainder to the maximum of the precisions of the inputs, with the specified rounding mode.
4897 /// Both [`Float`]s are taken by reference. An [`Ordering`] is also returned, indicating whether
4898 /// the rounded remainder is less than, equal to, or greater than the exact remainder, along
4899 /// with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
4900 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4901 ///
4902 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
4903 /// it equals $\pm(|q|\bmod 2^{63})$.
4904 ///
4905 /// $$
4906 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
4907 /// $$
4908 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
4909 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
4910 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
4911 ///
4912 /// If the output has a precision, it is the maximum of the precisions of the inputs.
4913 ///
4914 /// Special cases:
4915 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
4916 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
4917 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
4918 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
4919 /// - The quotient bits are 0 in all of the above special cases.
4920 ///
4921 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
4922 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
4923 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4924 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4925 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4926 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4927 ///
4928 /// If you want to specify an output precision, consider using
4929 /// [`Float::ieee_remainder_and_quotient_bits_prec_round`] instead. If you know you'll be using
4930 /// the `Nearest` rounding mode, consider using [`Float::ieee_remainder_and_quotient_bits`]
4931 /// instead.
4932 ///
4933 /// # Worst-case complexity
4934 /// $T(n) = O(n \log n \log\log n)$
4935 ///
4936 /// $M(n) = O(n)$
4937 ///
4938 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
4939 /// other.complexity())`.
4940 ///
4941 /// # Panics
4942 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
4943 /// precision.
4944 ///
4945 /// # Examples
4946 /// ```
4947 /// use core::cmp::Ordering::*;
4948 /// use malachite_base::rounding_modes::RoundingMode::*;
4949 /// use malachite_float::Float;
4950 ///
4951 /// let x = Float::from(14u32);
4952 /// let y = Float::from(3u32);
4953 /// let (r, o, q) = x.ieee_remainder_and_quotient_bits_round_ref_ref(&y, Floor);
4954 /// assert_eq!(r.to_string(), "-1.0");
4955 /// assert_eq!(o, Equal);
4956 /// assert_eq!(q, 5);
4957 /// ```
4958 pub fn ieee_remainder_and_quotient_bits_round_ref_ref(
4959 &self,
4960 other: &Self,
4961 rm: RoundingMode,
4962 ) -> (Self, Ordering, i64) {
4963 let prec = max(self.significant_bits(), other.significant_bits());
4964 self.ieee_remainder_and_quotient_bits_prec_round_ref_ref(other, prec, rm)
4965 }
4966
4967 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
4968 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
4969 /// remainder to the nearest value of the maximum of the precisions of the inputs. Both
4970 /// [`Float`]s are taken by value. An [`Ordering`] is also returned, indicating whether the
4971 /// rounded remainder is less than, equal to, or greater than the exact remainder, along with
4972 /// the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
4973 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4974 ///
4975 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
4976 /// it equals $\pm(|q|\bmod 2^{63})$.
4977 ///
4978 /// $$
4979 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
4980 /// $$
4981 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
4982 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
4983 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
4984 ///
4985 /// If the output has a precision, it is the maximum of the precisions of the inputs.
4986 ///
4987 /// Special cases:
4988 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
4989 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
4990 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
4991 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
4992 /// - The quotient bits are 0 in all of the above special cases.
4993 ///
4994 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
4995 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
4996 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4997 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4998 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4999 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5000 ///
5001 /// If you want to specify an output precision, consider using
5002 /// [`Float::ieee_remainder_and_quotient_bits_prec`] instead. If you want to use a rounding mode
5003 /// other than `Nearest`, consider using [`Float::ieee_remainder_and_quotient_bits_round`]
5004 /// instead.
5005 ///
5006 /// # Worst-case complexity
5007 /// $T(n) = O(n \log n \log\log n)$
5008 ///
5009 /// $M(n) = O(n)$
5010 ///
5011 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
5012 /// other.complexity())`.
5013 ///
5014 /// # Examples
5015 /// ```
5016 /// use core::cmp::Ordering::*;
5017 /// use malachite_float::Float;
5018 ///
5019 /// let (r, o, q) = Float::from(14u32).ieee_remainder_and_quotient_bits(Float::from(3u32));
5020 /// assert_eq!(r.to_string(), "-1.0");
5021 /// assert_eq!(o, Equal);
5022 /// assert_eq!(q, 5);
5023 /// ```
5024 #[inline]
5025 pub fn ieee_remainder_and_quotient_bits(self, other: Self) -> (Self, Ordering, i64) {
5026 self.ieee_remainder_and_quotient_bits_round(other, Nearest)
5027 }
5028
5029 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
5030 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
5031 /// remainder to the nearest value of the maximum of the precisions of the inputs. The first
5032 /// [`Float`] is taken by value and the second by reference. An [`Ordering`] is also returned,
5033 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
5034 /// remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s are not
5035 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
5036 ///
5037 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
5038 /// it equals $\pm(|q|\bmod 2^{63})$.
5039 ///
5040 /// $$
5041 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
5042 /// $$
5043 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
5044 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
5045 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
5046 ///
5047 /// If the output has a precision, it is the maximum of the precisions of the inputs.
5048 ///
5049 /// Special cases:
5050 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
5051 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
5052 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
5053 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
5054 /// - The quotient bits are 0 in all of the above special cases.
5055 ///
5056 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
5057 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
5058 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5059 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5060 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5061 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5062 ///
5063 /// If you want to specify an output precision, consider using
5064 /// [`Float::ieee_remainder_and_quotient_bits_prec`] instead. If you want to use a rounding mode
5065 /// other than `Nearest`, consider using [`Float::ieee_remainder_and_quotient_bits_round`]
5066 /// instead.
5067 ///
5068 /// # Worst-case complexity
5069 /// $T(n) = O(n \log n \log\log n)$
5070 ///
5071 /// $M(n) = O(n)$
5072 ///
5073 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
5074 /// other.complexity())`.
5075 ///
5076 /// # Examples
5077 /// ```
5078 /// use core::cmp::Ordering::*;
5079 /// use malachite_float::Float;
5080 ///
5081 /// let (r, o, q) =
5082 /// Float::from(14u32).ieee_remainder_and_quotient_bits_val_ref(&Float::from(3u32));
5083 /// assert_eq!(r.to_string(), "-1.0");
5084 /// assert_eq!(o, Equal);
5085 /// assert_eq!(q, 5);
5086 /// ```
5087 #[inline]
5088 pub fn ieee_remainder_and_quotient_bits_val_ref(self, other: &Self) -> (Self, Ordering, i64) {
5089 self.ieee_remainder_and_quotient_bits_round_val_ref(other, Nearest)
5090 }
5091
5092 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
5093 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
5094 /// remainder to the nearest value of the maximum of the precisions of the inputs. The first
5095 /// [`Float`] is taken by reference and the second by value. An [`Ordering`] is also returned,
5096 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
5097 /// remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s are not
5098 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
5099 ///
5100 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
5101 /// it equals $\pm(|q|\bmod 2^{63})$.
5102 ///
5103 /// $$
5104 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
5105 /// $$
5106 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
5107 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
5108 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
5109 ///
5110 /// If the output has a precision, it is the maximum of the precisions of the inputs.
5111 ///
5112 /// Special cases:
5113 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
5114 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
5115 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
5116 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
5117 /// - The quotient bits are 0 in all of the above special cases.
5118 ///
5119 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
5120 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
5121 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5122 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5123 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5124 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5125 ///
5126 /// If you want to specify an output precision, consider using
5127 /// [`Float::ieee_remainder_and_quotient_bits_prec`] instead. If you want to use a rounding mode
5128 /// other than `Nearest`, consider using [`Float::ieee_remainder_and_quotient_bits_round`]
5129 /// instead.
5130 ///
5131 /// # Worst-case complexity
5132 /// $T(n) = O(n \log n \log\log n)$
5133 ///
5134 /// $M(n) = O(n)$
5135 ///
5136 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
5137 /// other.complexity())`.
5138 ///
5139 /// # Examples
5140 /// ```
5141 /// use core::cmp::Ordering::*;
5142 /// use malachite_float::Float;
5143 ///
5144 /// let (r, o, q) =
5145 /// Float::from(14u32).ieee_remainder_and_quotient_bits_ref_val(Float::from(3u32));
5146 /// assert_eq!(r.to_string(), "-1.0");
5147 /// assert_eq!(o, Equal);
5148 /// assert_eq!(q, 5);
5149 /// ```
5150 #[inline]
5151 pub fn ieee_remainder_and_quotient_bits_ref_val(&self, other: Self) -> (Self, Ordering, i64) {
5152 self.ieee_remainder_and_quotient_bits_round_ref_val(other, Nearest)
5153 }
5154
5155 /// Computes the remainder of two [`Float`]s, with the quotient rounded to the nearest integer,
5156 /// ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`, rounding the
5157 /// remainder to the nearest value of the maximum of the precisions of the inputs. Both
5158 /// [`Float`]s are taken by reference. An [`Ordering`] is also returned, indicating whether the
5159 /// rounded remainder is less than, equal to, or greater than the exact remainder, along with
5160 /// the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
5161 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
5162 ///
5163 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
5164 /// it equals $\pm(|q|\bmod 2^{63})$.
5165 ///
5166 /// $$
5167 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
5168 /// $$
5169 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
5170 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
5171 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
5172 ///
5173 /// If the output has a precision, it is the maximum of the precisions of the inputs.
5174 ///
5175 /// Special cases:
5176 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
5177 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
5178 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
5179 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
5180 /// - The quotient bits are 0 in all of the above special cases.
5181 ///
5182 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
5183 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
5184 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5185 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5186 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5187 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5188 ///
5189 /// If you want to specify an output precision, consider using
5190 /// [`Float::ieee_remainder_and_quotient_bits_prec`] instead. If you want to use a rounding mode
5191 /// other than `Nearest`, consider using [`Float::ieee_remainder_and_quotient_bits_round`]
5192 /// instead.
5193 ///
5194 /// # Worst-case complexity
5195 /// $T(n) = O(n \log n \log\log n)$
5196 ///
5197 /// $M(n) = O(n)$
5198 ///
5199 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
5200 /// other.complexity())`.
5201 ///
5202 /// # Examples
5203 /// ```
5204 /// use core::cmp::Ordering::*;
5205 /// use malachite_float::Float;
5206 ///
5207 /// let (r, o, q) =
5208 /// Float::from(14u32).ieee_remainder_and_quotient_bits_ref_ref(&Float::from(3u32));
5209 /// assert_eq!(r.to_string(), "-1.0");
5210 /// assert_eq!(o, Equal);
5211 /// assert_eq!(q, 5);
5212 /// ```
5213 #[inline]
5214 pub fn ieee_remainder_and_quotient_bits_ref_ref(&self, other: &Self) -> (Self, Ordering, i64) {
5215 self.ieee_remainder_and_quotient_bits_round_ref_ref(other, Nearest)
5216 }
5217
5218 /// Computes the remainder of a [`Float`] by a `u64`, with the quotient rounded toward zero,
5219 /// rounding the remainder to the specified precision and with the specified rounding mode. The
5220 /// [`Float`] is taken by value. An [`Ordering`] is also returned, indicating whether the
5221 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
5222 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
5223 /// returns `Equal`.
5224 ///
5225 /// The conversion of the modulus to a [`Float`] is exact, so this behaves exactly like the
5226 /// corresponding `rem` function with `Float::from(other)`, except that a zero modulus yields
5227 /// `NaN` (matching `mpfr_fmod_ui`) rather than following the `Float` special cases.
5228 ///
5229 /// # Worst-case complexity
5230 /// $T(n) = O(n \log n \log\log n)$
5231 ///
5232 /// $M(n) = O(n)$
5233 ///
5234 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(), prec)`.
5235 ///
5236 /// # Panics
5237 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
5238 /// with `prec` bits.
5239 ///
5240 /// # Examples
5241 /// ```
5242 /// use core::cmp::Ordering::*;
5243 /// use malachite_base::rounding_modes::RoundingMode::*;
5244 /// use malachite_float::Float;
5245 ///
5246 /// let (r, o) = Float::from(10u32).rem_unsigned_prec_round(7, 1, Floor);
5247 /// assert_eq!(r.to_string(), "2.0");
5248 /// assert_eq!(o, Less);
5249 ///
5250 /// let (r, o) = Float::from(10u32).rem_unsigned_prec_round(7, 1, Ceiling);
5251 /// assert_eq!(r.to_string(), "4.0");
5252 /// assert_eq!(o, Greater);
5253 /// ```
5254 #[inline]
5255 pub fn rem_unsigned_prec_round(
5256 self,
5257 other: u64,
5258 prec: u64,
5259 rm: RoundingMode,
5260 ) -> (Self, Ordering) {
5261 rem_unsigned_helper(&self, other, false, prec, rm)
5262 }
5263
5264 /// Computes the remainder of a [`Float`] by a `u64`, with the quotient rounded toward zero,
5265 /// rounding the remainder to the specified precision and with the specified rounding mode. The
5266 /// [`Float`] is taken by reference. An [`Ordering`] is also returned, indicating whether the
5267 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
5268 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
5269 /// returns `Equal`.
5270 ///
5271 /// The conversion of the modulus to a [`Float`] is exact, so this behaves exactly like the
5272 /// corresponding `rem` function with `Float::from(other)`, except that a zero modulus yields
5273 /// `NaN` (matching `mpfr_fmod_ui`) rather than following the `Float` special cases.
5274 ///
5275 /// # Worst-case complexity
5276 /// $T(n) = O(n \log n \log\log n)$
5277 ///
5278 /// $M(n) = O(n)$
5279 ///
5280 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(), prec)`.
5281 ///
5282 /// # Panics
5283 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
5284 /// with `prec` bits.
5285 ///
5286 /// # Examples
5287 /// ```
5288 /// use core::cmp::Ordering::*;
5289 /// use malachite_base::rounding_modes::RoundingMode::*;
5290 /// use malachite_float::Float;
5291 ///
5292 /// let (r, o) = Float::from(10u32).rem_unsigned_prec_round_ref(7, 1, Floor);
5293 /// assert_eq!(r.to_string(), "2.0");
5294 /// assert_eq!(o, Less);
5295 ///
5296 /// let (r, o) = Float::from(10u32).rem_unsigned_prec_round_ref(7, 1, Ceiling);
5297 /// assert_eq!(r.to_string(), "4.0");
5298 /// assert_eq!(o, Greater);
5299 /// ```
5300 #[inline]
5301 pub fn rem_unsigned_prec_round_ref(
5302 &self,
5303 other: u64,
5304 prec: u64,
5305 rm: RoundingMode,
5306 ) -> (Self, Ordering) {
5307 rem_unsigned_helper(self, other, false, prec, rm)
5308 }
5309
5310 /// Computes the remainder of a [`Float`] by a `u64`, with the quotient rounded toward zero,
5311 /// rounding the remainder to the nearest value of the specified precision. The [`Float`] is
5312 /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded remainder
5313 /// is less than, equal to, or greater than the exact remainder. Although `NaN`s are not
5314 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
5315 ///
5316 /// The conversion of the modulus to a [`Float`] is exact, so this behaves exactly like the
5317 /// corresponding `rem` function with `Float::from(other)`, except that a zero modulus yields
5318 /// `NaN` (matching `mpfr_fmod_ui`) rather than following the `Float` special cases.
5319 ///
5320 /// # Worst-case complexity
5321 /// $T(n) = O(n \log n \log\log n)$
5322 ///
5323 /// $M(n) = O(n)$
5324 ///
5325 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(), prec)`.
5326 ///
5327 /// # Panics
5328 /// Panics if `prec` is zero.
5329 ///
5330 /// # Examples
5331 /// ```
5332 /// use core::cmp::Ordering::*;
5333 /// use malachite_float::Float;
5334 ///
5335 /// let (r, o) = Float::from(10u32).rem_unsigned_prec(3, 10);
5336 /// assert_eq!(r.to_string(), "1.0000");
5337 /// assert_eq!(o, Equal);
5338 /// ```
5339 #[inline]
5340 pub fn rem_unsigned_prec(self, other: u64, prec: u64) -> (Self, Ordering) {
5341 rem_unsigned_helper(&self, other, false, prec, Nearest)
5342 }
5343
5344 /// Computes the remainder of a [`Float`] by a `u64`, with the quotient rounded toward zero,
5345 /// rounding the remainder to the nearest value of the specified precision. The [`Float`] is
5346 /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded
5347 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
5348 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
5349 /// `Equal`.
5350 ///
5351 /// The conversion of the modulus to a [`Float`] is exact, so this behaves exactly like the
5352 /// corresponding `rem` function with `Float::from(other)`, except that a zero modulus yields
5353 /// `NaN` (matching `mpfr_fmod_ui`) rather than following the `Float` special cases.
5354 ///
5355 /// # Worst-case complexity
5356 /// $T(n) = O(n \log n \log\log n)$
5357 ///
5358 /// $M(n) = O(n)$
5359 ///
5360 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(), prec)`.
5361 ///
5362 /// # Panics
5363 /// Panics if `prec` is zero.
5364 ///
5365 /// # Examples
5366 /// ```
5367 /// use core::cmp::Ordering::*;
5368 /// use malachite_float::Float;
5369 ///
5370 /// let (r, o) = Float::from(10u32).rem_unsigned_prec_ref(3, 10);
5371 /// assert_eq!(r.to_string(), "1.0000");
5372 /// assert_eq!(o, Equal);
5373 /// ```
5374 #[inline]
5375 pub fn rem_unsigned_prec_ref(&self, other: u64, prec: u64) -> (Self, Ordering) {
5376 rem_unsigned_helper(self, other, false, prec, Nearest)
5377 }
5378
5379 /// Computes the remainder of a [`Float`] by a `u64`, with the quotient rounded toward zero,
5380 /// rounding the remainder to `self.significant_bits()` bits, with the specified rounding mode.
5381 /// The [`Float`] is taken by value. An [`Ordering`] is also returned, indicating whether the
5382 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
5383 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
5384 /// returns `Equal`.
5385 ///
5386 /// The conversion of the modulus to a [`Float`] is exact, so this behaves exactly like the
5387 /// corresponding `rem` function with `Float::from(other)`, except that a zero modulus yields
5388 /// `NaN` (matching `mpfr_fmod_ui`) rather than following the `Float` special cases.
5389 ///
5390 /// # Worst-case complexity
5391 /// $T(n) = O(n \log n \log\log n)$
5392 ///
5393 /// $M(n) = O(n)$
5394 ///
5395 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.complexity()`.
5396 ///
5397 /// # Panics
5398 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
5399 /// precision.
5400 ///
5401 /// # Examples
5402 /// ```
5403 /// use core::cmp::Ordering::*;
5404 /// use malachite_base::rounding_modes::RoundingMode::*;
5405 /// use malachite_float::Float;
5406 ///
5407 /// let (r, o) = Float::from(10u32).rem_unsigned_round(3, Floor);
5408 /// assert_eq!(r.to_string(), "1.0");
5409 /// assert_eq!(o, Equal);
5410 /// ```
5411 pub fn rem_unsigned_round(self, other: u64, rm: RoundingMode) -> (Self, Ordering) {
5412 let prec = self.significant_bits();
5413 rem_unsigned_helper(&self, other, false, prec, rm)
5414 }
5415
5416 /// Computes the remainder of a [`Float`] by a `u64`, with the quotient rounded toward zero,
5417 /// rounding the remainder to `self.significant_bits()` bits, with the specified rounding mode.
5418 /// The [`Float`] is taken by reference. An [`Ordering`] is also returned, indicating whether
5419 /// the rounded remainder is less than, equal to, or greater than the exact remainder. Although
5420 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
5421 /// returns `Equal`.
5422 ///
5423 /// The conversion of the modulus to a [`Float`] is exact, so this behaves exactly like the
5424 /// corresponding `rem` function with `Float::from(other)`, except that a zero modulus yields
5425 /// `NaN` (matching `mpfr_fmod_ui`) rather than following the `Float` special cases.
5426 ///
5427 /// # Worst-case complexity
5428 /// $T(n) = O(n \log n \log\log n)$
5429 ///
5430 /// $M(n) = O(n)$
5431 ///
5432 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.complexity()`.
5433 ///
5434 /// # Panics
5435 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
5436 /// precision.
5437 ///
5438 /// # Examples
5439 /// ```
5440 /// use core::cmp::Ordering::*;
5441 /// use malachite_base::rounding_modes::RoundingMode::*;
5442 /// use malachite_float::Float;
5443 ///
5444 /// let (r, o) = Float::from(10u32).rem_unsigned_round_ref(3, Floor);
5445 /// assert_eq!(r.to_string(), "1.0");
5446 /// assert_eq!(o, Equal);
5447 /// ```
5448 pub fn rem_unsigned_round_ref(&self, other: u64, rm: RoundingMode) -> (Self, Ordering) {
5449 let prec = self.significant_bits();
5450 rem_unsigned_helper(self, other, false, prec, rm)
5451 }
5452
5453 /// Computes the remainder of a [`Float`] by a `u64`, with the quotient rounded toward zero,
5454 /// rounding the remainder to the nearest value of `self.significant_bits()` bits. The [`Float`]
5455 /// is taken by value.
5456 ///
5457 /// The conversion of the modulus to a [`Float`] is exact, so this behaves exactly like the
5458 /// corresponding `rem` function with `Float::from(other)`, except that a zero modulus yields
5459 /// `NaN` (matching `mpfr_fmod_ui`) rather than following the `Float` special cases.
5460 ///
5461 /// # Worst-case complexity
5462 /// $T(n) = O(n \log n \log\log n)$
5463 ///
5464 /// $M(n) = O(n)$
5465 ///
5466 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.complexity()`.
5467 ///
5468 /// # Examples
5469 /// ```
5470 /// use malachite_float::Float;
5471 ///
5472 /// assert_eq!(Float::from(10u32).rem_unsigned(3).to_string(), "1.0");
5473 ///
5474 /// assert_eq!(Float::from(10u32).rem_unsigned(0).to_string(), "NaN");
5475 /// ```
5476 #[inline]
5477 pub fn rem_unsigned(self, other: u64) -> Self {
5478 self.rem_unsigned_round(other, Nearest).0
5479 }
5480
5481 /// Computes the remainder of a [`Float`] by a `u64`, with the quotient rounded toward zero,
5482 /// rounding the remainder to the nearest value of `self.significant_bits()` bits. The [`Float`]
5483 /// is taken by reference.
5484 ///
5485 /// The conversion of the modulus to a [`Float`] is exact, so this behaves exactly like the
5486 /// corresponding `rem` function with `Float::from(other)`, except that a zero modulus yields
5487 /// `NaN` (matching `mpfr_fmod_ui`) rather than following the `Float` special cases.
5488 ///
5489 /// # Worst-case complexity
5490 /// $T(n) = O(n \log n \log\log n)$
5491 ///
5492 /// $M(n) = O(n)$
5493 ///
5494 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.complexity()`.
5495 ///
5496 /// # Examples
5497 /// ```
5498 /// use malachite_float::Float;
5499 ///
5500 /// assert_eq!(Float::from(10u32).rem_unsigned_ref(3).to_string(), "1.0");
5501 ///
5502 /// assert_eq!(Float::from(10u32).rem_unsigned_ref(0).to_string(), "NaN");
5503 /// ```
5504 #[inline]
5505 pub fn rem_unsigned_ref(&self, other: u64) -> Self {
5506 self.rem_unsigned_round_ref(other, Nearest).0
5507 }
5508}
5509
5510impl Float {
5511 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
5512 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
5513 /// specified precision and with the specified rounding mode. The [`Float`] and the [`Rational`]
5514 /// are both taken by value. An [`Ordering`] is also returned, indicating whether the rounded
5515 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
5516 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
5517 /// `Equal`.
5518 ///
5519 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
5520 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
5521 /// remainder by up to the quotient times the conversion error.
5522 ///
5523 /// $$
5524 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
5525 /// $$
5526 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
5527 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
5528 ///
5529 /// Special cases:
5530 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
5531 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
5532 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
5533 ///
5534 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
5535 /// the minimum positive [`Float`]:
5536 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5537 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5538 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5539 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5540 ///
5541 /// # Worst-case complexity
5542 /// $T(n) = O(n \log n \log\log n)$
5543 ///
5544 /// $M(n) = O(n)$
5545 ///
5546 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
5547 /// other.significant_bits(), prec)`.
5548 ///
5549 /// # Panics
5550 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
5551 /// with `prec` bits.
5552 ///
5553 /// # Examples
5554 /// ```
5555 /// use core::cmp::Ordering::*;
5556 /// use malachite_base::rounding_modes::RoundingMode::*;
5557 /// use malachite_float::Float;
5558 /// use malachite_q::Rational;
5559 ///
5560 /// let (r, o) =
5561 /// Float::from(10u32).rem_rational_prec_round(Rational::from_signeds(22, 7), 5, Floor);
5562 /// assert_eq!(r.to_string(), "0.562");
5563 /// assert_eq!(o, Less);
5564 ///
5565 /// let (r, o) =
5566 /// Float::from(10u32).rem_rational_prec_round(Rational::from_signeds(22, 7), 5, Ceiling);
5567 /// assert_eq!(r.to_string(), "0.594");
5568 /// assert_eq!(o, Greater);
5569 /// ```
5570 #[allow(clippy::needless_pass_by_value)]
5571 pub fn rem_rational_prec_round(
5572 self,
5573 other: Rational,
5574 prec: u64,
5575 rm: RoundingMode,
5576 ) -> (Self, Ordering) {
5577 let (r, o, _) = rem_rational_helper(&self, &other, false, false, prec, rm);
5578 (r, o)
5579 }
5580
5581 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
5582 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
5583 /// specified precision and with the specified rounding mode. The [`Float`] is taken by value
5584 /// and the [`Rational`] by reference. An [`Ordering`] is also returned, indicating whether the
5585 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
5586 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
5587 /// returns `Equal`.
5588 ///
5589 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
5590 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
5591 /// remainder by up to the quotient times the conversion error.
5592 ///
5593 /// $$
5594 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
5595 /// $$
5596 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
5597 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
5598 ///
5599 /// Special cases:
5600 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
5601 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
5602 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
5603 ///
5604 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
5605 /// the minimum positive [`Float`]:
5606 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5607 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5608 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5609 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5610 ///
5611 /// # Worst-case complexity
5612 /// $T(n) = O(n \log n \log\log n)$
5613 ///
5614 /// $M(n) = O(n)$
5615 ///
5616 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
5617 /// other.significant_bits(), prec)`.
5618 ///
5619 /// # Panics
5620 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
5621 /// with `prec` bits.
5622 ///
5623 /// # Examples
5624 /// ```
5625 /// use core::cmp::Ordering::*;
5626 /// use malachite_base::rounding_modes::RoundingMode::*;
5627 /// use malachite_float::Float;
5628 /// use malachite_q::Rational;
5629 ///
5630 /// let x = Float::from(10u32);
5631 /// let y = Rational::from_signeds(22, 7);
5632 /// let (r, o) = x.rem_rational_prec_round_val_ref(&y, 5, Floor);
5633 /// assert_eq!(r.to_string(), "0.562");
5634 /// assert_eq!(o, Less);
5635 ///
5636 /// let x = Float::from(10u32);
5637 /// let y = Rational::from_signeds(22, 7);
5638 /// let (r, o) = x.rem_rational_prec_round_val_ref(&y, 5, Ceiling);
5639 /// assert_eq!(r.to_string(), "0.594");
5640 /// assert_eq!(o, Greater);
5641 /// ```
5642 pub fn rem_rational_prec_round_val_ref(
5643 self,
5644 other: &Rational,
5645 prec: u64,
5646 rm: RoundingMode,
5647 ) -> (Self, Ordering) {
5648 let (r, o, _) = rem_rational_helper(&self, other, false, false, prec, rm);
5649 (r, o)
5650 }
5651
5652 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
5653 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
5654 /// specified precision and with the specified rounding mode. The [`Float`] is taken by
5655 /// reference and the [`Rational`] by value. An [`Ordering`] is also returned, indicating
5656 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder.
5657 /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
5658 /// it also returns `Equal`.
5659 ///
5660 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
5661 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
5662 /// remainder by up to the quotient times the conversion error.
5663 ///
5664 /// $$
5665 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
5666 /// $$
5667 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
5668 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
5669 ///
5670 /// Special cases:
5671 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
5672 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
5673 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
5674 ///
5675 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
5676 /// the minimum positive [`Float`]:
5677 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5678 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5679 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5680 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5681 ///
5682 /// # Worst-case complexity
5683 /// $T(n) = O(n \log n \log\log n)$
5684 ///
5685 /// $M(n) = O(n)$
5686 ///
5687 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
5688 /// other.significant_bits(), prec)`.
5689 ///
5690 /// # Panics
5691 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
5692 /// with `prec` bits.
5693 ///
5694 /// # Examples
5695 /// ```
5696 /// use core::cmp::Ordering::*;
5697 /// use malachite_base::rounding_modes::RoundingMode::*;
5698 /// use malachite_float::Float;
5699 /// use malachite_q::Rational;
5700 ///
5701 /// let x = Float::from(10u32);
5702 /// let y = Rational::from_signeds(22, 7);
5703 /// let (r, o) = x.rem_rational_prec_round_ref_val(y, 5, Floor);
5704 /// assert_eq!(r.to_string(), "0.562");
5705 /// assert_eq!(o, Less);
5706 ///
5707 /// let x = Float::from(10u32);
5708 /// let y = Rational::from_signeds(22, 7);
5709 /// let (r, o) = x.rem_rational_prec_round_ref_val(y, 5, Ceiling);
5710 /// assert_eq!(r.to_string(), "0.594");
5711 /// assert_eq!(o, Greater);
5712 /// ```
5713 #[allow(clippy::needless_pass_by_value)]
5714 pub fn rem_rational_prec_round_ref_val(
5715 &self,
5716 other: Rational,
5717 prec: u64,
5718 rm: RoundingMode,
5719 ) -> (Self, Ordering) {
5720 let (r, o, _) = rem_rational_helper(self, &other, false, false, prec, rm);
5721 (r, o)
5722 }
5723
5724 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
5725 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
5726 /// specified precision and with the specified rounding mode. The [`Float`] and the [`Rational`]
5727 /// are both taken by reference. An [`Ordering`] is also returned, indicating whether the
5728 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
5729 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
5730 /// returns `Equal`.
5731 ///
5732 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
5733 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
5734 /// remainder by up to the quotient times the conversion error.
5735 ///
5736 /// $$
5737 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
5738 /// $$
5739 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
5740 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
5741 ///
5742 /// Special cases:
5743 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
5744 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
5745 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
5746 ///
5747 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
5748 /// the minimum positive [`Float`]:
5749 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5750 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5751 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5752 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5753 ///
5754 /// # Worst-case complexity
5755 /// $T(n) = O(n \log n \log\log n)$
5756 ///
5757 /// $M(n) = O(n)$
5758 ///
5759 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
5760 /// other.significant_bits(), prec)`.
5761 ///
5762 /// # Panics
5763 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
5764 /// with `prec` bits.
5765 ///
5766 /// # Examples
5767 /// ```
5768 /// use core::cmp::Ordering::*;
5769 /// use malachite_base::rounding_modes::RoundingMode::*;
5770 /// use malachite_float::Float;
5771 /// use malachite_q::Rational;
5772 ///
5773 /// let x = Float::from(10u32);
5774 /// let y = Rational::from_signeds(22, 7);
5775 /// let (r, o) = x.rem_rational_prec_round_ref_ref(&y, 5, Floor);
5776 /// assert_eq!(r.to_string(), "0.562");
5777 /// assert_eq!(o, Less);
5778 ///
5779 /// let x = Float::from(10u32);
5780 /// let y = Rational::from_signeds(22, 7);
5781 /// let (r, o) = x.rem_rational_prec_round_ref_ref(&y, 5, Ceiling);
5782 /// assert_eq!(r.to_string(), "0.594");
5783 /// assert_eq!(o, Greater);
5784 /// ```
5785 pub fn rem_rational_prec_round_ref_ref(
5786 &self,
5787 other: &Rational,
5788 prec: u64,
5789 rm: RoundingMode,
5790 ) -> (Self, Ordering) {
5791 let (r, o, _) = rem_rational_helper(self, other, false, false, prec, rm);
5792 (r, o)
5793 }
5794
5795 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
5796 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
5797 /// nearest value of the specified precision. The [`Float`] and the [`Rational`] are both taken
5798 /// by value. An [`Ordering`] is also returned, indicating whether the rounded remainder is less
5799 /// than, equal to, or greater than the exact remainder. Although `NaN`s are not comparable to
5800 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
5801 ///
5802 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
5803 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
5804 /// remainder by up to the quotient times the conversion error.
5805 ///
5806 /// $$
5807 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
5808 /// $$
5809 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
5810 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
5811 ///
5812 /// Special cases:
5813 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
5814 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
5815 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
5816 ///
5817 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
5818 /// the minimum positive [`Float`]:
5819 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5820 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5821 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5822 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5823 ///
5824 /// # Worst-case complexity
5825 /// $T(n) = O(n \log n \log\log n)$
5826 ///
5827 /// $M(n) = O(n)$
5828 ///
5829 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
5830 /// other.significant_bits(), prec)`.
5831 ///
5832 /// # Panics
5833 /// Panics if `prec` is zero.
5834 ///
5835 /// # Examples
5836 /// ```
5837 /// use core::cmp::Ordering::*;
5838 /// use malachite_float::Float;
5839 /// use malachite_q::Rational;
5840 ///
5841 /// let (r, o) = Float::from(10u32).rem_rational_prec(Rational::from_signeds(22, 7), 5);
5842 /// assert_eq!(r.to_string(), "0.562");
5843 /// assert_eq!(o, Less);
5844 /// ```
5845 #[inline]
5846 pub fn rem_rational_prec(self, other: Rational, prec: u64) -> (Self, Ordering) {
5847 self.rem_rational_prec_round(other, prec, Nearest)
5848 }
5849
5850 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
5851 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
5852 /// nearest value of the specified precision. The [`Float`] is taken by value and the
5853 /// [`Rational`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
5854 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
5855 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
5856 /// `Equal`.
5857 ///
5858 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
5859 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
5860 /// remainder by up to the quotient times the conversion error.
5861 ///
5862 /// $$
5863 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
5864 /// $$
5865 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
5866 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
5867 ///
5868 /// Special cases:
5869 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
5870 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
5871 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
5872 ///
5873 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
5874 /// the minimum positive [`Float`]:
5875 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5876 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5877 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5878 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5879 ///
5880 /// # Worst-case complexity
5881 /// $T(n) = O(n \log n \log\log n)$
5882 ///
5883 /// $M(n) = O(n)$
5884 ///
5885 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
5886 /// other.significant_bits(), prec)`.
5887 ///
5888 /// # Panics
5889 /// Panics if `prec` is zero.
5890 ///
5891 /// # Examples
5892 /// ```
5893 /// use core::cmp::Ordering::*;
5894 /// use malachite_float::Float;
5895 /// use malachite_q::Rational;
5896 ///
5897 /// let (r, o) =
5898 /// Float::from(10u32).rem_rational_prec_val_ref(&Rational::from_signeds(22, 7), 5);
5899 /// assert_eq!(r.to_string(), "0.562");
5900 /// assert_eq!(o, Less);
5901 /// ```
5902 #[inline]
5903 pub fn rem_rational_prec_val_ref(self, other: &Rational, prec: u64) -> (Self, Ordering) {
5904 self.rem_rational_prec_round_val_ref(other, prec, Nearest)
5905 }
5906
5907 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
5908 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
5909 /// nearest value of the specified precision. The [`Float`] is taken by reference and the
5910 /// [`Rational`] by value. An [`Ordering`] is also returned, indicating whether the rounded
5911 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
5912 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
5913 /// `Equal`.
5914 ///
5915 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
5916 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
5917 /// remainder by up to the quotient times the conversion error.
5918 ///
5919 /// $$
5920 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
5921 /// $$
5922 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
5923 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
5924 ///
5925 /// Special cases:
5926 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
5927 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
5928 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
5929 ///
5930 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
5931 /// the minimum positive [`Float`]:
5932 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5933 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5934 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5935 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5936 ///
5937 /// # Worst-case complexity
5938 /// $T(n) = O(n \log n \log\log n)$
5939 ///
5940 /// $M(n) = O(n)$
5941 ///
5942 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
5943 /// other.significant_bits(), prec)`.
5944 ///
5945 /// # Panics
5946 /// Panics if `prec` is zero.
5947 ///
5948 /// # Examples
5949 /// ```
5950 /// use core::cmp::Ordering::*;
5951 /// use malachite_float::Float;
5952 /// use malachite_q::Rational;
5953 ///
5954 /// let (r, o) = Float::from(10u32).rem_rational_prec_ref_val(Rational::from_signeds(22, 7), 5);
5955 /// assert_eq!(r.to_string(), "0.562");
5956 /// assert_eq!(o, Less);
5957 /// ```
5958 #[inline]
5959 pub fn rem_rational_prec_ref_val(&self, other: Rational, prec: u64) -> (Self, Ordering) {
5960 self.rem_rational_prec_round_ref_val(other, prec, Nearest)
5961 }
5962
5963 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
5964 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
5965 /// nearest value of the specified precision. The [`Float`] and the [`Rational`] are both taken
5966 /// by reference. An [`Ordering`] is also returned, indicating whether the rounded remainder is
5967 /// less than, equal to, or greater than the exact remainder. Although `NaN`s are not comparable
5968 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
5969 ///
5970 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
5971 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
5972 /// remainder by up to the quotient times the conversion error.
5973 ///
5974 /// $$
5975 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
5976 /// $$
5977 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
5978 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
5979 ///
5980 /// Special cases:
5981 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
5982 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
5983 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
5984 ///
5985 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
5986 /// the minimum positive [`Float`]:
5987 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5988 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5989 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5990 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5991 ///
5992 /// # Worst-case complexity
5993 /// $T(n) = O(n \log n \log\log n)$
5994 ///
5995 /// $M(n) = O(n)$
5996 ///
5997 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
5998 /// other.significant_bits(), prec)`.
5999 ///
6000 /// # Panics
6001 /// Panics if `prec` is zero.
6002 ///
6003 /// # Examples
6004 /// ```
6005 /// use core::cmp::Ordering::*;
6006 /// use malachite_float::Float;
6007 /// use malachite_q::Rational;
6008 ///
6009 /// let (r, o) =
6010 /// Float::from(10u32).rem_rational_prec_ref_ref(&Rational::from_signeds(22, 7), 5);
6011 /// assert_eq!(r.to_string(), "0.562");
6012 /// assert_eq!(o, Less);
6013 /// ```
6014 #[inline]
6015 pub fn rem_rational_prec_ref_ref(&self, other: &Rational, prec: u64) -> (Self, Ordering) {
6016 self.rem_rational_prec_round_ref_ref(other, prec, Nearest)
6017 }
6018
6019 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
6020 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
6021 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] and the [`Rational`]
6022 /// are both taken by value. An [`Ordering`] is also returned, indicating whether the rounded
6023 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
6024 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
6025 /// `Equal`.
6026 ///
6027 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
6028 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
6029 /// remainder by up to the quotient times the conversion error.
6030 ///
6031 /// $$
6032 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
6033 /// $$
6034 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
6035 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
6036 ///
6037 /// Special cases:
6038 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
6039 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
6040 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
6041 ///
6042 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
6043 /// the minimum positive [`Float`]:
6044 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6045 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6046 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
6047 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6048 ///
6049 /// # Worst-case complexity
6050 /// $T(n) = O(n \log n \log\log n)$
6051 ///
6052 /// $M(n) = O(n)$
6053 ///
6054 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
6055 /// other.significant_bits())`.
6056 ///
6057 /// # Panics
6058 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
6059 /// precision.
6060 ///
6061 /// # Examples
6062 /// ```
6063 /// use core::cmp::Ordering::*;
6064 /// use malachite_base::rounding_modes::RoundingMode::*;
6065 /// use malachite_float::Float;
6066 /// use malachite_q::Rational;
6067 ///
6068 /// let (r, o) = Float::from(10u32).rem_rational_round(Rational::from_signeds(22, 7), Floor);
6069 /// assert_eq!(r.to_string(), "0.50");
6070 /// assert_eq!(o, Less);
6071 /// ```
6072 #[inline]
6073 pub fn rem_rational_round(self, other: Rational, rm: RoundingMode) -> (Self, Ordering) {
6074 let prec = self.significant_bits();
6075 self.rem_rational_prec_round(other, prec, rm)
6076 }
6077
6078 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
6079 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
6080 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] is taken by value and
6081 /// the [`Rational`] by reference. An [`Ordering`] is also returned, indicating whether the
6082 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
6083 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
6084 /// returns `Equal`.
6085 ///
6086 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
6087 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
6088 /// remainder by up to the quotient times the conversion error.
6089 ///
6090 /// $$
6091 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
6092 /// $$
6093 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
6094 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
6095 ///
6096 /// Special cases:
6097 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
6098 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
6099 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
6100 ///
6101 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
6102 /// the minimum positive [`Float`]:
6103 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6104 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6105 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
6106 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6107 ///
6108 /// # Worst-case complexity
6109 /// $T(n) = O(n \log n \log\log n)$
6110 ///
6111 /// $M(n) = O(n)$
6112 ///
6113 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
6114 /// other.significant_bits())`.
6115 ///
6116 /// # Panics
6117 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
6118 /// precision.
6119 ///
6120 /// # Examples
6121 /// ```
6122 /// use core::cmp::Ordering::*;
6123 /// use malachite_base::rounding_modes::RoundingMode::*;
6124 /// use malachite_float::Float;
6125 /// use malachite_q::Rational;
6126 ///
6127 /// let (r, o) =
6128 /// Float::from(10u32).rem_rational_round_val_ref(&Rational::from_signeds(22, 7), Floor);
6129 /// assert_eq!(r.to_string(), "0.50");
6130 /// assert_eq!(o, Less);
6131 /// ```
6132 #[inline]
6133 pub fn rem_rational_round_val_ref(
6134 self,
6135 other: &Rational,
6136 rm: RoundingMode,
6137 ) -> (Self, Ordering) {
6138 let prec = self.significant_bits();
6139 self.rem_rational_prec_round_val_ref(other, prec, rm)
6140 }
6141
6142 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
6143 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
6144 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] is taken by reference
6145 /// and the [`Rational`] by value. An [`Ordering`] is also returned, indicating whether the
6146 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
6147 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
6148 /// returns `Equal`.
6149 ///
6150 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
6151 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
6152 /// remainder by up to the quotient times the conversion error.
6153 ///
6154 /// $$
6155 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
6156 /// $$
6157 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
6158 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
6159 ///
6160 /// Special cases:
6161 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
6162 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
6163 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
6164 ///
6165 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
6166 /// the minimum positive [`Float`]:
6167 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6168 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6169 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
6170 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6171 ///
6172 /// # Worst-case complexity
6173 /// $T(n) = O(n \log n \log\log n)$
6174 ///
6175 /// $M(n) = O(n)$
6176 ///
6177 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
6178 /// other.significant_bits())`.
6179 ///
6180 /// # Panics
6181 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
6182 /// precision.
6183 ///
6184 /// # Examples
6185 /// ```
6186 /// use core::cmp::Ordering::*;
6187 /// use malachite_base::rounding_modes::RoundingMode::*;
6188 /// use malachite_float::Float;
6189 /// use malachite_q::Rational;
6190 ///
6191 /// let (r, o) =
6192 /// Float::from(10u32).rem_rational_round_ref_val(Rational::from_signeds(22, 7), Floor);
6193 /// assert_eq!(r.to_string(), "0.50");
6194 /// assert_eq!(o, Less);
6195 /// ```
6196 #[inline]
6197 pub fn rem_rational_round_ref_val(
6198 &self,
6199 other: Rational,
6200 rm: RoundingMode,
6201 ) -> (Self, Ordering) {
6202 let prec = self.significant_bits();
6203 self.rem_rational_prec_round_ref_val(other, prec, rm)
6204 }
6205
6206 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
6207 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
6208 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] and the [`Rational`]
6209 /// are both taken by reference. An [`Ordering`] is also returned, indicating whether the
6210 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
6211 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
6212 /// returns `Equal`.
6213 ///
6214 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
6215 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
6216 /// remainder by up to the quotient times the conversion error.
6217 ///
6218 /// $$
6219 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
6220 /// $$
6221 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
6222 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
6223 ///
6224 /// Special cases:
6225 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
6226 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
6227 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
6228 ///
6229 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
6230 /// the minimum positive [`Float`]:
6231 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6232 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6233 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
6234 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6235 ///
6236 /// # Worst-case complexity
6237 /// $T(n) = O(n \log n \log\log n)$
6238 ///
6239 /// $M(n) = O(n)$
6240 ///
6241 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
6242 /// other.significant_bits())`.
6243 ///
6244 /// # Panics
6245 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
6246 /// precision.
6247 ///
6248 /// # Examples
6249 /// ```
6250 /// use core::cmp::Ordering::*;
6251 /// use malachite_base::rounding_modes::RoundingMode::*;
6252 /// use malachite_float::Float;
6253 /// use malachite_q::Rational;
6254 ///
6255 /// let (r, o) =
6256 /// Float::from(10u32).rem_rational_round_ref_ref(&Rational::from_signeds(22, 7), Floor);
6257 /// assert_eq!(r.to_string(), "0.50");
6258 /// assert_eq!(o, Less);
6259 /// ```
6260 #[inline]
6261 pub fn rem_rational_round_ref_ref(
6262 &self,
6263 other: &Rational,
6264 rm: RoundingMode,
6265 ) -> (Self, Ordering) {
6266 let prec = self.significant_bits();
6267 self.rem_rational_prec_round_ref_ref(other, prec, rm)
6268 }
6269
6270 /// Computes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
6271 /// toward zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result
6272 /// to the specified precision and with the specified rounding mode. The [`Rational`] is taken
6273 /// by value. An [`Ordering`] is returned, indicating whether the rounded remainder is less
6274 /// than, equal to, or greater than the exact remainder. Although `NaN`s are not comparable to
6275 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
6276 ///
6277 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
6278 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
6279 /// remainder by up to the quotient times the conversion error.
6280 ///
6281 /// $$
6282 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
6283 /// $$
6284 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
6285 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
6286 ///
6287 /// Special cases:
6288 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
6289 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
6290 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
6291 ///
6292 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
6293 /// the minimum positive [`Float`]:
6294 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6295 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6296 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
6297 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6298 ///
6299 /// # Worst-case complexity
6300 /// $T(n) = O(n \log n \log\log n)$
6301 ///
6302 /// $M(n) = O(n)$
6303 ///
6304 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
6305 /// other.significant_bits(), prec)`.
6306 ///
6307 /// # Panics
6308 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
6309 /// with `prec` bits.
6310 ///
6311 /// # Examples
6312 /// ```
6313 /// use core::cmp::Ordering::*;
6314 /// use malachite_base::rounding_modes::RoundingMode::*;
6315 /// use malachite_float::Float;
6316 /// use malachite_q::Rational;
6317 ///
6318 /// let mut x = Float::from(10u32);
6319 /// assert_eq!(
6320 /// x.rem_rational_prec_round_assign(Rational::from_signeds(22, 7), 5, Floor),
6321 /// Less
6322 /// );
6323 /// assert_eq!(x.to_string(), "0.562");
6324 /// ```
6325 #[allow(clippy::needless_pass_by_value)]
6326 pub fn rem_rational_prec_round_assign(
6327 &mut self,
6328 other: Rational,
6329 prec: u64,
6330 rm: RoundingMode,
6331 ) -> Ordering {
6332 let (r, o, _) = rem_rational_helper(self, &other, false, false, prec, rm);
6333 *self = r;
6334 o
6335 }
6336
6337 /// Computes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
6338 /// toward zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result
6339 /// to the specified precision and with the specified rounding mode. The [`Rational`] is taken
6340 /// by reference. An [`Ordering`] is returned, indicating whether the rounded remainder is less
6341 /// than, equal to, or greater than the exact remainder. Although `NaN`s are not comparable to
6342 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
6343 ///
6344 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
6345 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
6346 /// remainder by up to the quotient times the conversion error.
6347 ///
6348 /// $$
6349 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
6350 /// $$
6351 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
6352 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
6353 ///
6354 /// Special cases:
6355 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
6356 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
6357 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
6358 ///
6359 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
6360 /// the minimum positive [`Float`]:
6361 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6362 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6363 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
6364 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6365 ///
6366 /// # Worst-case complexity
6367 /// $T(n) = O(n \log n \log\log n)$
6368 ///
6369 /// $M(n) = O(n)$
6370 ///
6371 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
6372 /// other.significant_bits(), prec)`.
6373 ///
6374 /// # Panics
6375 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
6376 /// with `prec` bits.
6377 ///
6378 /// # Examples
6379 /// ```
6380 /// use core::cmp::Ordering::*;
6381 /// use malachite_base::rounding_modes::RoundingMode::*;
6382 /// use malachite_float::Float;
6383 /// use malachite_q::Rational;
6384 ///
6385 /// let mut x = Float::from(10u32);
6386 /// let y = Rational::from_signeds(22, 7);
6387 /// assert_eq!(x.rem_rational_prec_round_assign_ref(&y, 5, Floor), Less);
6388 /// assert_eq!(x.to_string(), "0.562");
6389 /// ```
6390 pub fn rem_rational_prec_round_assign_ref(
6391 &mut self,
6392 other: &Rational,
6393 prec: u64,
6394 rm: RoundingMode,
6395 ) -> Ordering {
6396 let (r, o, _) = rem_rational_helper(self, other, false, false, prec, rm);
6397 *self = r;
6398 o
6399 }
6400
6401 /// Computes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
6402 /// toward zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result
6403 /// to the nearest value of the specified precision. The [`Rational`] is taken by value. An
6404 /// [`Ordering`] is returned, indicating whether the rounded remainder is less than, equal to,
6405 /// or greater than the exact remainder. Although `NaN`s are not comparable to any [`Float`],
6406 /// whenever this function returns a `NaN` it also returns `Equal`.
6407 ///
6408 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
6409 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
6410 /// remainder by up to the quotient times the conversion error.
6411 ///
6412 /// $$
6413 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
6414 /// $$
6415 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
6416 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
6417 ///
6418 /// Special cases:
6419 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
6420 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
6421 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
6422 ///
6423 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
6424 /// the minimum positive [`Float`]:
6425 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6426 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6427 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
6428 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6429 ///
6430 /// # Worst-case complexity
6431 /// $T(n) = O(n \log n \log\log n)$
6432 ///
6433 /// $M(n) = O(n)$
6434 ///
6435 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
6436 /// other.significant_bits(), prec)`.
6437 ///
6438 /// # Panics
6439 /// Panics if `prec` is zero.
6440 ///
6441 /// # Examples
6442 /// ```
6443 /// use core::cmp::Ordering::*;
6444 /// use malachite_float::Float;
6445 /// use malachite_q::Rational;
6446 ///
6447 /// let mut x = Float::from(10u32);
6448 /// assert_eq!(
6449 /// x.rem_rational_prec_assign(Rational::from_signeds(22, 7), 5),
6450 /// Less
6451 /// );
6452 /// assert_eq!(x.to_string(), "0.562");
6453 /// ```
6454 #[inline]
6455 pub fn rem_rational_prec_assign(&mut self, other: Rational, prec: u64) -> Ordering {
6456 self.rem_rational_prec_round_assign(other, prec, Nearest)
6457 }
6458
6459 /// Computes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
6460 /// toward zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result
6461 /// to the nearest value of the specified precision. The [`Rational`] is taken by reference. An
6462 /// [`Ordering`] is returned, indicating whether the rounded remainder is less than, equal to,
6463 /// or greater than the exact remainder. Although `NaN`s are not comparable to any [`Float`],
6464 /// whenever this function returns a `NaN` it also returns `Equal`.
6465 ///
6466 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
6467 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
6468 /// remainder by up to the quotient times the conversion error.
6469 ///
6470 /// $$
6471 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
6472 /// $$
6473 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
6474 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
6475 ///
6476 /// Special cases:
6477 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
6478 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
6479 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
6480 ///
6481 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
6482 /// the minimum positive [`Float`]:
6483 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6484 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6485 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
6486 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6487 ///
6488 /// # Worst-case complexity
6489 /// $T(n) = O(n \log n \log\log n)$
6490 ///
6491 /// $M(n) = O(n)$
6492 ///
6493 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
6494 /// other.significant_bits(), prec)`.
6495 ///
6496 /// # Panics
6497 /// Panics if `prec` is zero.
6498 ///
6499 /// # Examples
6500 /// ```
6501 /// use core::cmp::Ordering::*;
6502 /// use malachite_float::Float;
6503 /// use malachite_q::Rational;
6504 ///
6505 /// let mut x = Float::from(10u32);
6506 /// assert_eq!(
6507 /// x.rem_rational_prec_assign_ref(&Rational::from_signeds(22, 7), 5),
6508 /// Less
6509 /// );
6510 /// assert_eq!(x.to_string(), "0.562");
6511 /// ```
6512 #[inline]
6513 pub fn rem_rational_prec_assign_ref(&mut self, other: &Rational, prec: u64) -> Ordering {
6514 self.rem_rational_prec_round_assign_ref(other, prec, Nearest)
6515 }
6516
6517 /// Computes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
6518 /// toward zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result
6519 /// to the [`Float`]'s precision, with the specified rounding mode. The [`Rational`] is taken by
6520 /// value. An [`Ordering`] is returned, indicating whether the rounded remainder is less than,
6521 /// equal to, or greater than the exact remainder. Although `NaN`s are not comparable to any
6522 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
6523 ///
6524 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
6525 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
6526 /// remainder by up to the quotient times the conversion error.
6527 ///
6528 /// $$
6529 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
6530 /// $$
6531 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
6532 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
6533 ///
6534 /// Special cases:
6535 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
6536 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
6537 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
6538 ///
6539 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
6540 /// the minimum positive [`Float`]:
6541 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6542 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6543 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
6544 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6545 ///
6546 /// # Worst-case complexity
6547 /// $T(n) = O(n \log n \log\log n)$
6548 ///
6549 /// $M(n) = O(n)$
6550 ///
6551 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
6552 /// other.significant_bits())`.
6553 ///
6554 /// # Panics
6555 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
6556 /// precision.
6557 ///
6558 /// # Examples
6559 /// ```
6560 /// use core::cmp::Ordering::*;
6561 /// use malachite_base::rounding_modes::RoundingMode::*;
6562 /// use malachite_float::Float;
6563 /// use malachite_q::Rational;
6564 ///
6565 /// let mut x = Float::from(10u32);
6566 /// assert_eq!(
6567 /// x.rem_rational_round_assign(Rational::from_signeds(22, 7), Floor),
6568 /// Less
6569 /// );
6570 /// assert_eq!(x.to_string(), "0.50");
6571 /// ```
6572 #[inline]
6573 pub fn rem_rational_round_assign(&mut self, other: Rational, rm: RoundingMode) -> Ordering {
6574 let prec = self.significant_bits();
6575 self.rem_rational_prec_round_assign(other, prec, rm)
6576 }
6577
6578 /// Computes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
6579 /// toward zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result
6580 /// to the [`Float`]'s precision, with the specified rounding mode. The [`Rational`] is taken by
6581 /// reference. An [`Ordering`] is returned, indicating whether the rounded remainder is less
6582 /// than, equal to, or greater than the exact remainder. Although `NaN`s are not comparable to
6583 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
6584 ///
6585 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
6586 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
6587 /// remainder by up to the quotient times the conversion error.
6588 ///
6589 /// $$
6590 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
6591 /// $$
6592 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
6593 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
6594 ///
6595 /// Special cases:
6596 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
6597 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
6598 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
6599 ///
6600 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
6601 /// the minimum positive [`Float`]:
6602 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6603 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6604 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
6605 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6606 ///
6607 /// # Worst-case complexity
6608 /// $T(n) = O(n \log n \log\log n)$
6609 ///
6610 /// $M(n) = O(n)$
6611 ///
6612 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
6613 /// other.significant_bits())`.
6614 ///
6615 /// # Panics
6616 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
6617 /// precision.
6618 ///
6619 /// # Examples
6620 /// ```
6621 /// use core::cmp::Ordering::*;
6622 /// use malachite_base::rounding_modes::RoundingMode::*;
6623 /// use malachite_float::Float;
6624 /// use malachite_q::Rational;
6625 ///
6626 /// let mut x = Float::from(10u32);
6627 /// assert_eq!(
6628 /// x.rem_rational_round_assign_ref(&Rational::from_signeds(22, 7), Floor),
6629 /// Less
6630 /// );
6631 /// assert_eq!(x.to_string(), "0.50");
6632 /// ```
6633 #[inline]
6634 pub fn rem_rational_round_assign_ref(
6635 &mut self,
6636 other: &Rational,
6637 rm: RoundingMode,
6638 ) -> Ordering {
6639 let prec = self.significant_bits();
6640 self.rem_rational_prec_round_assign_ref(other, prec, rm)
6641 }
6642
6643 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
6644 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
6645 /// specified precision and with the specified rounding mode. The [`Float`] and the [`Rational`]
6646 /// are both taken by value. An [`Ordering`] is also returned, indicating whether the rounded
6647 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
6648 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
6649 /// whenever this function returns a `NaN` it also returns `Equal`.
6650 ///
6651 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
6652 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
6653 /// remainder by up to the quotient times the conversion error.
6654 ///
6655 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
6656 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
6657 /// [`Float`]-[`Float`] functions.
6658 ///
6659 /// $$
6660 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
6661 /// $$
6662 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
6663 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
6664 ///
6665 /// Special cases:
6666 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
6667 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
6668 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
6669 /// - The quotient bits are 0 in all of the above special cases.
6670 ///
6671 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
6672 /// the minimum positive [`Float`]:
6673 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6674 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6675 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
6676 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6677 ///
6678 /// # Worst-case complexity
6679 /// $T(n) = O(n \log n \log\log n)$
6680 ///
6681 /// $M(n) = O(n)$
6682 ///
6683 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
6684 /// other.significant_bits(), prec)`.
6685 ///
6686 /// # Panics
6687 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
6688 /// with `prec` bits.
6689 ///
6690 /// # Examples
6691 /// ```
6692 /// use core::cmp::Ordering::*;
6693 /// use malachite_base::rounding_modes::RoundingMode::*;
6694 /// use malachite_float::Float;
6695 /// use malachite_q::Rational;
6696 ///
6697 /// let x = Float::from(10u32);
6698 /// let y = Rational::from_signeds(22, 7);
6699 /// let (r, o, q) = x.rem_rational_and_quotient_bits_prec_round(y, 5, Floor);
6700 /// assert_eq!(r.to_string(), "0.562");
6701 /// assert_eq!(o, Less);
6702 /// assert_eq!(q, 3);
6703 /// ```
6704 #[allow(clippy::needless_pass_by_value)]
6705 #[inline]
6706 pub fn rem_rational_and_quotient_bits_prec_round(
6707 self,
6708 other: Rational,
6709 prec: u64,
6710 rm: RoundingMode,
6711 ) -> (Self, Ordering, i64) {
6712 rem_rational_helper(&self, &other, false, true, prec, rm)
6713 }
6714
6715 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
6716 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
6717 /// specified precision and with the specified rounding mode. The [`Float`] is taken by value
6718 /// and the [`Rational`] by reference. An [`Ordering`] is also returned, indicating whether the
6719 /// rounded remainder is less than, equal to, or greater than the exact remainder, along with
6720 /// the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
6721 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
6722 ///
6723 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
6724 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
6725 /// remainder by up to the quotient times the conversion error.
6726 ///
6727 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
6728 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
6729 /// [`Float`]-[`Float`] functions.
6730 ///
6731 /// $$
6732 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
6733 /// $$
6734 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
6735 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
6736 ///
6737 /// Special cases:
6738 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
6739 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
6740 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
6741 /// - The quotient bits are 0 in all of the above special cases.
6742 ///
6743 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
6744 /// the minimum positive [`Float`]:
6745 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6746 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6747 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
6748 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6749 ///
6750 /// # Worst-case complexity
6751 /// $T(n) = O(n \log n \log\log n)$
6752 ///
6753 /// $M(n) = O(n)$
6754 ///
6755 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
6756 /// other.significant_bits(), prec)`.
6757 ///
6758 /// # Panics
6759 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
6760 /// with `prec` bits.
6761 ///
6762 /// # Examples
6763 /// ```
6764 /// use core::cmp::Ordering::*;
6765 /// use malachite_base::rounding_modes::RoundingMode::*;
6766 /// use malachite_float::Float;
6767 /// use malachite_q::Rational;
6768 ///
6769 /// let x = Float::from(10u32);
6770 /// let y = Rational::from_signeds(22, 7);
6771 /// let (r, o, q) = x.rem_rational_and_quotient_bits_prec_round_val_ref(&y, 5, Floor);
6772 /// assert_eq!(r.to_string(), "0.562");
6773 /// assert_eq!(o, Less);
6774 /// assert_eq!(q, 3);
6775 /// ```
6776 #[inline]
6777 pub fn rem_rational_and_quotient_bits_prec_round_val_ref(
6778 self,
6779 other: &Rational,
6780 prec: u64,
6781 rm: RoundingMode,
6782 ) -> (Self, Ordering, i64) {
6783 rem_rational_helper(&self, other, false, true, prec, rm)
6784 }
6785
6786 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
6787 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
6788 /// specified precision and with the specified rounding mode. The [`Float`] is taken by
6789 /// reference and the [`Rational`] by value. An [`Ordering`] is also returned, indicating
6790 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder,
6791 /// along with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to
6792 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
6793 ///
6794 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
6795 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
6796 /// remainder by up to the quotient times the conversion error.
6797 ///
6798 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
6799 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
6800 /// [`Float`]-[`Float`] functions.
6801 ///
6802 /// $$
6803 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
6804 /// $$
6805 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
6806 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
6807 ///
6808 /// Special cases:
6809 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
6810 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
6811 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
6812 /// - The quotient bits are 0 in all of the above special cases.
6813 ///
6814 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
6815 /// the minimum positive [`Float`]:
6816 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6817 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6818 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
6819 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6820 ///
6821 /// # Worst-case complexity
6822 /// $T(n) = O(n \log n \log\log n)$
6823 ///
6824 /// $M(n) = O(n)$
6825 ///
6826 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
6827 /// other.significant_bits(), prec)`.
6828 ///
6829 /// # Panics
6830 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
6831 /// with `prec` bits.
6832 ///
6833 /// # Examples
6834 /// ```
6835 /// use core::cmp::Ordering::*;
6836 /// use malachite_base::rounding_modes::RoundingMode::*;
6837 /// use malachite_float::Float;
6838 /// use malachite_q::Rational;
6839 ///
6840 /// let x = Float::from(10u32);
6841 /// let y = Rational::from_signeds(22, 7);
6842 /// let (r, o, q) = x.rem_rational_and_quotient_bits_prec_round_ref_val(y, 5, Floor);
6843 /// assert_eq!(r.to_string(), "0.562");
6844 /// assert_eq!(o, Less);
6845 /// assert_eq!(q, 3);
6846 /// ```
6847 #[allow(clippy::needless_pass_by_value)]
6848 #[inline]
6849 pub fn rem_rational_and_quotient_bits_prec_round_ref_val(
6850 &self,
6851 other: Rational,
6852 prec: u64,
6853 rm: RoundingMode,
6854 ) -> (Self, Ordering, i64) {
6855 rem_rational_helper(self, &other, false, true, prec, rm)
6856 }
6857
6858 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
6859 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
6860 /// specified precision and with the specified rounding mode. The [`Float`] and the [`Rational`]
6861 /// are both taken by reference. An [`Ordering`] is also returned, indicating whether the
6862 /// rounded remainder is less than, equal to, or greater than the exact remainder, along with
6863 /// the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
6864 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
6865 ///
6866 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
6867 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
6868 /// remainder by up to the quotient times the conversion error.
6869 ///
6870 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
6871 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
6872 /// [`Float`]-[`Float`] functions.
6873 ///
6874 /// $$
6875 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
6876 /// $$
6877 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
6878 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
6879 ///
6880 /// Special cases:
6881 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
6882 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
6883 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
6884 /// - The quotient bits are 0 in all of the above special cases.
6885 ///
6886 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
6887 /// the minimum positive [`Float`]:
6888 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6889 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6890 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
6891 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6892 ///
6893 /// # Worst-case complexity
6894 /// $T(n) = O(n \log n \log\log n)$
6895 ///
6896 /// $M(n) = O(n)$
6897 ///
6898 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
6899 /// other.significant_bits(), prec)`.
6900 ///
6901 /// # Panics
6902 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
6903 /// with `prec` bits.
6904 ///
6905 /// # Examples
6906 /// ```
6907 /// use core::cmp::Ordering::*;
6908 /// use malachite_base::rounding_modes::RoundingMode::*;
6909 /// use malachite_float::Float;
6910 /// use malachite_q::Rational;
6911 ///
6912 /// let x = Float::from(10u32);
6913 /// let y = Rational::from_signeds(22, 7);
6914 /// let (r, o, q) = x.rem_rational_and_quotient_bits_prec_round_ref_ref(&y, 5, Floor);
6915 /// assert_eq!(r.to_string(), "0.562");
6916 /// assert_eq!(o, Less);
6917 /// assert_eq!(q, 3);
6918 /// ```
6919 #[inline]
6920 pub fn rem_rational_and_quotient_bits_prec_round_ref_ref(
6921 &self,
6922 other: &Rational,
6923 prec: u64,
6924 rm: RoundingMode,
6925 ) -> (Self, Ordering, i64) {
6926 rem_rational_helper(self, other, false, true, prec, rm)
6927 }
6928
6929 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
6930 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
6931 /// nearest value of the specified precision. The [`Float`] and the [`Rational`] are both taken
6932 /// by value. An [`Ordering`] is also returned, indicating whether the rounded remainder is less
6933 /// than, equal to, or greater than the exact remainder, along with the low bits of the quotient
6934 /// as an `i64`. Although `NaN`s are not comparable to any [`Float`], whenever this function
6935 /// returns a `NaN` it also returns `Equal`.
6936 ///
6937 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
6938 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
6939 /// remainder by up to the quotient times the conversion error.
6940 ///
6941 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
6942 /// it equals $\pm(|q|\bmod 2^{63})$.
6943 ///
6944 /// $$
6945 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
6946 /// $$
6947 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
6948 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
6949 ///
6950 /// Special cases:
6951 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
6952 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
6953 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
6954 /// - The quotient bits are 0 in all of the above special cases.
6955 ///
6956 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
6957 /// the minimum positive [`Float`]:
6958 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6959 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6960 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
6961 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6962 ///
6963 /// # Worst-case complexity
6964 /// $T(n) = O(n \log n \log\log n)$
6965 ///
6966 /// $M(n) = O(n)$
6967 ///
6968 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
6969 /// other.significant_bits(), prec)`.
6970 ///
6971 /// # Panics
6972 /// Panics if `prec` is zero.
6973 ///
6974 /// # Examples
6975 /// ```
6976 /// use core::cmp::Ordering::*;
6977 /// use malachite_float::Float;
6978 /// use malachite_q::Rational;
6979 ///
6980 /// let x = Float::from(10u32);
6981 /// let y = Rational::from_signeds(22, 7);
6982 /// let (r, o, q) = x.rem_rational_and_quotient_bits_prec(y, 5);
6983 /// assert_eq!(r.to_string(), "0.562");
6984 /// assert_eq!(o, Less);
6985 /// assert_eq!(q, 3);
6986 /// ```
6987 #[inline]
6988 pub fn rem_rational_and_quotient_bits_prec(
6989 self,
6990 other: Rational,
6991 prec: u64,
6992 ) -> (Self, Ordering, i64) {
6993 self.rem_rational_and_quotient_bits_prec_round(other, prec, Nearest)
6994 }
6995
6996 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
6997 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
6998 /// nearest value of the specified precision. The [`Float`] is taken by value and the
6999 /// [`Rational`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
7000 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
7001 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
7002 /// whenever this function returns a `NaN` it also returns `Equal`.
7003 ///
7004 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7005 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7006 /// remainder by up to the quotient times the conversion error.
7007 ///
7008 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
7009 /// it equals $\pm(|q|\bmod 2^{63})$.
7010 ///
7011 /// $$
7012 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
7013 /// $$
7014 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
7015 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
7016 ///
7017 /// Special cases:
7018 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
7019 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
7020 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
7021 /// - The quotient bits are 0 in all of the above special cases.
7022 ///
7023 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
7024 /// the minimum positive [`Float`]:
7025 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7026 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7027 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
7028 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7029 ///
7030 /// # Worst-case complexity
7031 /// $T(n) = O(n \log n \log\log n)$
7032 ///
7033 /// $M(n) = O(n)$
7034 ///
7035 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
7036 /// other.significant_bits(), prec)`.
7037 ///
7038 /// # Panics
7039 /// Panics if `prec` is zero.
7040 ///
7041 /// # Examples
7042 /// ```
7043 /// use core::cmp::Ordering::*;
7044 /// use malachite_float::Float;
7045 /// use malachite_q::Rational;
7046 ///
7047 /// let x = Float::from(10u32);
7048 /// let y = Rational::from_signeds(22, 7);
7049 /// let (r, o, q) = x.rem_rational_and_quotient_bits_prec_val_ref(&y, 5);
7050 /// assert_eq!(r.to_string(), "0.562");
7051 /// assert_eq!(o, Less);
7052 /// assert_eq!(q, 3);
7053 /// ```
7054 #[inline]
7055 pub fn rem_rational_and_quotient_bits_prec_val_ref(
7056 self,
7057 other: &Rational,
7058 prec: u64,
7059 ) -> (Self, Ordering, i64) {
7060 self.rem_rational_and_quotient_bits_prec_round_val_ref(other, prec, Nearest)
7061 }
7062
7063 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
7064 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
7065 /// nearest value of the specified precision. The [`Float`] is taken by reference and the
7066 /// [`Rational`] by value. An [`Ordering`] is also returned, indicating whether the rounded
7067 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
7068 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
7069 /// whenever this function returns a `NaN` it also returns `Equal`.
7070 ///
7071 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7072 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7073 /// remainder by up to the quotient times the conversion error.
7074 ///
7075 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
7076 /// it equals $\pm(|q|\bmod 2^{63})$.
7077 ///
7078 /// $$
7079 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
7080 /// $$
7081 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
7082 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
7083 ///
7084 /// Special cases:
7085 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
7086 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
7087 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
7088 /// - The quotient bits are 0 in all of the above special cases.
7089 ///
7090 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
7091 /// the minimum positive [`Float`]:
7092 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7093 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7094 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
7095 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7096 ///
7097 /// # Worst-case complexity
7098 /// $T(n) = O(n \log n \log\log n)$
7099 ///
7100 /// $M(n) = O(n)$
7101 ///
7102 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
7103 /// other.significant_bits(), prec)`.
7104 ///
7105 /// # Panics
7106 /// Panics if `prec` is zero.
7107 ///
7108 /// # Examples
7109 /// ```
7110 /// use core::cmp::Ordering::*;
7111 /// use malachite_float::Float;
7112 /// use malachite_q::Rational;
7113 ///
7114 /// let x = Float::from(10u32);
7115 /// let y = Rational::from_signeds(22, 7);
7116 /// let (r, o, q) = x.rem_rational_and_quotient_bits_prec_ref_val(y, 5);
7117 /// assert_eq!(r.to_string(), "0.562");
7118 /// assert_eq!(o, Less);
7119 /// assert_eq!(q, 3);
7120 /// ```
7121 #[inline]
7122 pub fn rem_rational_and_quotient_bits_prec_ref_val(
7123 &self,
7124 other: Rational,
7125 prec: u64,
7126 ) -> (Self, Ordering, i64) {
7127 self.rem_rational_and_quotient_bits_prec_round_ref_val(other, prec, Nearest)
7128 }
7129
7130 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
7131 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
7132 /// nearest value of the specified precision. The [`Float`] and the [`Rational`] are both taken
7133 /// by reference. An [`Ordering`] is also returned, indicating whether the rounded remainder is
7134 /// less than, equal to, or greater than the exact remainder, along with the low bits of the
7135 /// quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`], whenever this
7136 /// function returns a `NaN` it also returns `Equal`.
7137 ///
7138 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7139 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7140 /// remainder by up to the quotient times the conversion error.
7141 ///
7142 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
7143 /// it equals $\pm(|q|\bmod 2^{63})$.
7144 ///
7145 /// $$
7146 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
7147 /// $$
7148 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
7149 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
7150 ///
7151 /// Special cases:
7152 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
7153 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
7154 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
7155 /// - The quotient bits are 0 in all of the above special cases.
7156 ///
7157 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
7158 /// the minimum positive [`Float`]:
7159 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7160 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7161 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
7162 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7163 ///
7164 /// # Worst-case complexity
7165 /// $T(n) = O(n \log n \log\log n)$
7166 ///
7167 /// $M(n) = O(n)$
7168 ///
7169 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
7170 /// other.significant_bits(), prec)`.
7171 ///
7172 /// # Panics
7173 /// Panics if `prec` is zero.
7174 ///
7175 /// # Examples
7176 /// ```
7177 /// use core::cmp::Ordering::*;
7178 /// use malachite_float::Float;
7179 /// use malachite_q::Rational;
7180 ///
7181 /// let x = Float::from(10u32);
7182 /// let y = Rational::from_signeds(22, 7);
7183 /// let (r, o, q) = x.rem_rational_and_quotient_bits_prec_ref_ref(&y, 5);
7184 /// assert_eq!(r.to_string(), "0.562");
7185 /// assert_eq!(o, Less);
7186 /// assert_eq!(q, 3);
7187 /// ```
7188 #[inline]
7189 pub fn rem_rational_and_quotient_bits_prec_ref_ref(
7190 &self,
7191 other: &Rational,
7192 prec: u64,
7193 ) -> (Self, Ordering, i64) {
7194 self.rem_rational_and_quotient_bits_prec_round_ref_ref(other, prec, Nearest)
7195 }
7196
7197 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
7198 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
7199 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] and the [`Rational`]
7200 /// are both taken by value. An [`Ordering`] is also returned, indicating whether the rounded
7201 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
7202 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
7203 /// whenever this function returns a `NaN` it also returns `Equal`.
7204 ///
7205 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7206 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7207 /// remainder by up to the quotient times the conversion error.
7208 ///
7209 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
7210 /// it equals $\pm(|q|\bmod 2^{63})$.
7211 ///
7212 /// $$
7213 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
7214 /// $$
7215 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
7216 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
7217 ///
7218 /// Special cases:
7219 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
7220 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
7221 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
7222 /// - The quotient bits are 0 in all of the above special cases.
7223 ///
7224 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
7225 /// the minimum positive [`Float`]:
7226 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7227 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7228 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
7229 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7230 ///
7231 /// # Worst-case complexity
7232 /// $T(n) = O(n \log n \log\log n)$
7233 ///
7234 /// $M(n) = O(n)$
7235 ///
7236 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
7237 /// other.significant_bits())`.
7238 ///
7239 /// # Panics
7240 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
7241 /// precision.
7242 ///
7243 /// # Examples
7244 /// ```
7245 /// use core::cmp::Ordering::*;
7246 /// use malachite_base::rounding_modes::RoundingMode::*;
7247 /// use malachite_float::Float;
7248 /// use malachite_q::Rational;
7249 ///
7250 /// let x = Float::from(10u32);
7251 /// let y = Rational::from_signeds(22, 7);
7252 /// let (r, o, q) = x.rem_rational_and_quotient_bits_round(y, Floor);
7253 /// assert_eq!(r.to_string(), "0.50");
7254 /// assert_eq!(o, Less);
7255 /// assert_eq!(q, 3);
7256 /// ```
7257 #[inline]
7258 pub fn rem_rational_and_quotient_bits_round(
7259 self,
7260 other: Rational,
7261 rm: RoundingMode,
7262 ) -> (Self, Ordering, i64) {
7263 let prec = self.significant_bits();
7264 self.rem_rational_and_quotient_bits_prec_round(other, prec, rm)
7265 }
7266
7267 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
7268 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
7269 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] is taken by value and
7270 /// the [`Rational`] by reference. An [`Ordering`] is also returned, indicating whether the
7271 /// rounded remainder is less than, equal to, or greater than the exact remainder, along with
7272 /// the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
7273 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
7274 ///
7275 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7276 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7277 /// remainder by up to the quotient times the conversion error.
7278 ///
7279 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
7280 /// it equals $\pm(|q|\bmod 2^{63})$.
7281 ///
7282 /// $$
7283 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
7284 /// $$
7285 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
7286 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
7287 ///
7288 /// Special cases:
7289 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
7290 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
7291 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
7292 /// - The quotient bits are 0 in all of the above special cases.
7293 ///
7294 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
7295 /// the minimum positive [`Float`]:
7296 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7297 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7298 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
7299 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7300 ///
7301 /// # Worst-case complexity
7302 /// $T(n) = O(n \log n \log\log n)$
7303 ///
7304 /// $M(n) = O(n)$
7305 ///
7306 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
7307 /// other.significant_bits())`.
7308 ///
7309 /// # Panics
7310 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
7311 /// precision.
7312 ///
7313 /// # Examples
7314 /// ```
7315 /// use core::cmp::Ordering::*;
7316 /// use malachite_base::rounding_modes::RoundingMode::*;
7317 /// use malachite_float::Float;
7318 /// use malachite_q::Rational;
7319 ///
7320 /// let x = Float::from(10u32);
7321 /// let y = Rational::from_signeds(22, 7);
7322 /// let (r, o, q) = x.rem_rational_and_quotient_bits_round_val_ref(&y, Floor);
7323 /// assert_eq!(r.to_string(), "0.50");
7324 /// assert_eq!(o, Less);
7325 /// assert_eq!(q, 3);
7326 /// ```
7327 #[inline]
7328 pub fn rem_rational_and_quotient_bits_round_val_ref(
7329 self,
7330 other: &Rational,
7331 rm: RoundingMode,
7332 ) -> (Self, Ordering, i64) {
7333 let prec = self.significant_bits();
7334 self.rem_rational_and_quotient_bits_prec_round_val_ref(other, prec, rm)
7335 }
7336
7337 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
7338 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
7339 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] is taken by reference
7340 /// and the [`Rational`] by value. An [`Ordering`] is also returned, indicating whether the
7341 /// rounded remainder is less than, equal to, or greater than the exact remainder, along with
7342 /// the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
7343 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
7344 ///
7345 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7346 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7347 /// remainder by up to the quotient times the conversion error.
7348 ///
7349 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
7350 /// it equals $\pm(|q|\bmod 2^{63})$.
7351 ///
7352 /// $$
7353 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
7354 /// $$
7355 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
7356 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
7357 ///
7358 /// Special cases:
7359 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
7360 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
7361 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
7362 /// - The quotient bits are 0 in all of the above special cases.
7363 ///
7364 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
7365 /// the minimum positive [`Float`]:
7366 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7367 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7368 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
7369 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7370 ///
7371 /// # Worst-case complexity
7372 /// $T(n) = O(n \log n \log\log n)$
7373 ///
7374 /// $M(n) = O(n)$
7375 ///
7376 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
7377 /// other.significant_bits())`.
7378 ///
7379 /// # Panics
7380 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
7381 /// precision.
7382 ///
7383 /// # Examples
7384 /// ```
7385 /// use core::cmp::Ordering::*;
7386 /// use malachite_base::rounding_modes::RoundingMode::*;
7387 /// use malachite_float::Float;
7388 /// use malachite_q::Rational;
7389 ///
7390 /// let x = Float::from(10u32);
7391 /// let y = Rational::from_signeds(22, 7);
7392 /// let (r, o, q) = x.rem_rational_and_quotient_bits_round_ref_val(y, Floor);
7393 /// assert_eq!(r.to_string(), "0.50");
7394 /// assert_eq!(o, Less);
7395 /// assert_eq!(q, 3);
7396 /// ```
7397 #[inline]
7398 pub fn rem_rational_and_quotient_bits_round_ref_val(
7399 &self,
7400 other: Rational,
7401 rm: RoundingMode,
7402 ) -> (Self, Ordering, i64) {
7403 let prec = self.significant_bits();
7404 self.rem_rational_and_quotient_bits_prec_round_ref_val(other, prec, rm)
7405 }
7406
7407 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
7408 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
7409 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] and the [`Rational`]
7410 /// are both taken by reference. An [`Ordering`] is also returned, indicating whether the
7411 /// rounded remainder is less than, equal to, or greater than the exact remainder, along with
7412 /// the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
7413 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
7414 ///
7415 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7416 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7417 /// remainder by up to the quotient times the conversion error.
7418 ///
7419 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
7420 /// it equals $\pm(|q|\bmod 2^{63})$.
7421 ///
7422 /// $$
7423 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
7424 /// $$
7425 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
7426 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
7427 ///
7428 /// Special cases:
7429 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
7430 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
7431 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
7432 /// - The quotient bits are 0 in all of the above special cases.
7433 ///
7434 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
7435 /// the minimum positive [`Float`]:
7436 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7437 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7438 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
7439 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7440 ///
7441 /// # Worst-case complexity
7442 /// $T(n) = O(n \log n \log\log n)$
7443 ///
7444 /// $M(n) = O(n)$
7445 ///
7446 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
7447 /// other.significant_bits())`.
7448 ///
7449 /// # Panics
7450 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
7451 /// precision.
7452 ///
7453 /// # Examples
7454 /// ```
7455 /// use core::cmp::Ordering::*;
7456 /// use malachite_base::rounding_modes::RoundingMode::*;
7457 /// use malachite_float::Float;
7458 /// use malachite_q::Rational;
7459 ///
7460 /// let x = Float::from(10u32);
7461 /// let y = Rational::from_signeds(22, 7);
7462 /// let (r, o, q) = x.rem_rational_and_quotient_bits_round_ref_ref(&y, Floor);
7463 /// assert_eq!(r.to_string(), "0.50");
7464 /// assert_eq!(o, Less);
7465 /// assert_eq!(q, 3);
7466 /// ```
7467 #[inline]
7468 pub fn rem_rational_and_quotient_bits_round_ref_ref(
7469 &self,
7470 other: &Rational,
7471 rm: RoundingMode,
7472 ) -> (Self, Ordering, i64) {
7473 let prec = self.significant_bits();
7474 self.rem_rational_and_quotient_bits_prec_round_ref_ref(other, prec, rm)
7475 }
7476
7477 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
7478 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
7479 /// nearest value of the [`Float`]'s precision. The [`Float`] and the [`Rational`] are both
7480 /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded remainder
7481 /// is less than, equal to, or greater than the exact remainder, along with the low bits of the
7482 /// quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`], whenever this
7483 /// function returns a `NaN` it also returns `Equal`.
7484 ///
7485 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7486 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7487 /// remainder by up to the quotient times the conversion error.
7488 ///
7489 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
7490 /// it equals $\pm(|q|\bmod 2^{63})$.
7491 ///
7492 /// $$
7493 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
7494 /// $$
7495 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
7496 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
7497 ///
7498 /// Special cases:
7499 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
7500 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
7501 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
7502 /// - The quotient bits are 0 in all of the above special cases.
7503 ///
7504 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
7505 /// the minimum positive [`Float`]:
7506 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7507 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7508 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
7509 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7510 ///
7511 /// # Worst-case complexity
7512 /// $T(n) = O(n \log n \log\log n)$
7513 ///
7514 /// $M(n) = O(n)$
7515 ///
7516 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
7517 /// other.significant_bits())`.
7518 ///
7519 /// # Examples
7520 /// ```
7521 /// use core::cmp::Ordering::*;
7522 /// use malachite_float::Float;
7523 /// use malachite_q::Rational;
7524 ///
7525 /// let (r, o, q) =
7526 /// Float::from(10u32).rem_rational_and_quotient_bits(Rational::from_signeds(22, 7));
7527 /// assert_eq!(r.to_string(), "0.62");
7528 /// assert_eq!(o, Greater);
7529 /// assert_eq!(q, 3);
7530 /// ```
7531 #[inline]
7532 pub fn rem_rational_and_quotient_bits(self, other: Rational) -> (Self, Ordering, i64) {
7533 self.rem_rational_and_quotient_bits_round(other, Nearest)
7534 }
7535
7536 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
7537 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
7538 /// nearest value of the [`Float`]'s precision. The [`Float`] is taken by value and the
7539 /// [`Rational`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
7540 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
7541 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
7542 /// whenever this function returns a `NaN` it also returns `Equal`.
7543 ///
7544 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7545 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7546 /// remainder by up to the quotient times the conversion error.
7547 ///
7548 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
7549 /// it equals $\pm(|q|\bmod 2^{63})$.
7550 ///
7551 /// $$
7552 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
7553 /// $$
7554 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
7555 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
7556 ///
7557 /// Special cases:
7558 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
7559 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
7560 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
7561 /// - The quotient bits are 0 in all of the above special cases.
7562 ///
7563 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
7564 /// the minimum positive [`Float`]:
7565 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7566 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7567 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
7568 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7569 ///
7570 /// # Worst-case complexity
7571 /// $T(n) = O(n \log n \log\log n)$
7572 ///
7573 /// $M(n) = O(n)$
7574 ///
7575 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
7576 /// other.significant_bits())`.
7577 ///
7578 /// # Examples
7579 /// ```
7580 /// use core::cmp::Ordering::*;
7581 /// use malachite_float::Float;
7582 /// use malachite_q::Rational;
7583 ///
7584 /// let x = Float::from(10u32);
7585 /// let y = Rational::from_signeds(22, 7);
7586 /// let (r, o, q) = x.rem_rational_and_quotient_bits_val_ref(&y);
7587 /// assert_eq!(r.to_string(), "0.62");
7588 /// assert_eq!(o, Greater);
7589 /// assert_eq!(q, 3);
7590 /// ```
7591 #[inline]
7592 pub fn rem_rational_and_quotient_bits_val_ref(self, other: &Rational) -> (Self, Ordering, i64) {
7593 self.rem_rational_and_quotient_bits_round_val_ref(other, Nearest)
7594 }
7595
7596 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
7597 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
7598 /// nearest value of the [`Float`]'s precision. The [`Float`] is taken by reference and the
7599 /// [`Rational`] by value. An [`Ordering`] is also returned, indicating whether the rounded
7600 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
7601 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
7602 /// whenever this function returns a `NaN` it also returns `Equal`.
7603 ///
7604 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7605 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7606 /// remainder by up to the quotient times the conversion error.
7607 ///
7608 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
7609 /// it equals $\pm(|q|\bmod 2^{63})$.
7610 ///
7611 /// $$
7612 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
7613 /// $$
7614 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
7615 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
7616 ///
7617 /// Special cases:
7618 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
7619 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
7620 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
7621 /// - The quotient bits are 0 in all of the above special cases.
7622 ///
7623 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
7624 /// the minimum positive [`Float`]:
7625 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7626 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7627 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
7628 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7629 ///
7630 /// # Worst-case complexity
7631 /// $T(n) = O(n \log n \log\log n)$
7632 ///
7633 /// $M(n) = O(n)$
7634 ///
7635 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
7636 /// other.significant_bits())`.
7637 ///
7638 /// # Examples
7639 /// ```
7640 /// use core::cmp::Ordering::*;
7641 /// use malachite_float::Float;
7642 /// use malachite_q::Rational;
7643 ///
7644 /// let x = Float::from(10u32);
7645 /// let y = Rational::from_signeds(22, 7);
7646 /// let (r, o, q) = x.rem_rational_and_quotient_bits_ref_val(y);
7647 /// assert_eq!(r.to_string(), "0.62");
7648 /// assert_eq!(o, Greater);
7649 /// assert_eq!(q, 3);
7650 /// ```
7651 #[inline]
7652 pub fn rem_rational_and_quotient_bits_ref_val(&self, other: Rational) -> (Self, Ordering, i64) {
7653 self.rem_rational_and_quotient_bits_round_ref_val(other, Nearest)
7654 }
7655
7656 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward
7657 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
7658 /// nearest value of the [`Float`]'s precision. The [`Float`] and the [`Rational`] are both
7659 /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded
7660 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
7661 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
7662 /// whenever this function returns a `NaN` it also returns `Equal`.
7663 ///
7664 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7665 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7666 /// remainder by up to the quotient times the conversion error.
7667 ///
7668 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
7669 /// it equals $\pm(|q|\bmod 2^{63})$.
7670 ///
7671 /// $$
7672 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
7673 /// $$
7674 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
7675 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
7676 ///
7677 /// Special cases:
7678 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
7679 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
7680 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
7681 /// - The quotient bits are 0 in all of the above special cases.
7682 ///
7683 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
7684 /// the minimum positive [`Float`]:
7685 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7686 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7687 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
7688 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7689 ///
7690 /// # Worst-case complexity
7691 /// $T(n) = O(n \log n \log\log n)$
7692 ///
7693 /// $M(n) = O(n)$
7694 ///
7695 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
7696 /// other.significant_bits())`.
7697 ///
7698 /// # Examples
7699 /// ```
7700 /// use core::cmp::Ordering::*;
7701 /// use malachite_float::Float;
7702 /// use malachite_q::Rational;
7703 ///
7704 /// let x = Float::from(10u32);
7705 /// let y = Rational::from_signeds(22, 7);
7706 /// let (r, o, q) = x.rem_rational_and_quotient_bits_ref_ref(&y);
7707 /// assert_eq!(r.to_string(), "0.62");
7708 /// assert_eq!(o, Greater);
7709 /// assert_eq!(q, 3);
7710 /// ```
7711 #[inline]
7712 pub fn rem_rational_and_quotient_bits_ref_ref(
7713 &self,
7714 other: &Rational,
7715 ) -> (Self, Ordering, i64) {
7716 self.rem_rational_and_quotient_bits_round_ref_ref(other, Nearest)
7717 }
7718
7719 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
7720 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
7721 /// rounding the result to the specified precision and with the specified rounding mode. The
7722 /// [`Float`] and the [`Rational`] are both taken by value. An [`Ordering`] is also returned,
7723 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
7724 /// remainder. Although `NaN`s are not comparable to any [`Float`], whenever this function
7725 /// returns a `NaN` it also returns `Equal`.
7726 ///
7727 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7728 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7729 /// remainder by up to the quotient times the conversion error.
7730 ///
7731 /// $$
7732 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
7733 /// $$
7734 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
7735 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
7736 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
7737 ///
7738 /// Special cases:
7739 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
7740 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
7741 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
7742 ///
7743 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
7744 /// the minimum positive [`Float`]:
7745 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7746 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7747 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
7748 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7749 ///
7750 /// # Worst-case complexity
7751 /// $T(n) = O(n \log n \log\log n)$
7752 ///
7753 /// $M(n) = O(n)$
7754 ///
7755 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
7756 /// other.significant_bits(), prec)`.
7757 ///
7758 /// # Panics
7759 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
7760 /// with `prec` bits.
7761 ///
7762 /// # Examples
7763 /// ```
7764 /// use core::cmp::Ordering::*;
7765 /// use malachite_base::rounding_modes::RoundingMode::*;
7766 /// use malachite_float::Float;
7767 /// use malachite_q::Rational;
7768 ///
7769 /// let x = Float::from(10u32);
7770 /// let y = Rational::from_signeds(22, 7);
7771 /// let (r, o) = x.ieee_remainder_rational_prec_round(y, 5, Floor);
7772 /// assert_eq!(r.to_string(), "0.562");
7773 /// assert_eq!(o, Less);
7774 /// ```
7775 #[allow(clippy::needless_pass_by_value)]
7776 pub fn ieee_remainder_rational_prec_round(
7777 self,
7778 other: Rational,
7779 prec: u64,
7780 rm: RoundingMode,
7781 ) -> (Self, Ordering) {
7782 let (r, o, _) = rem_rational_helper(&self, &other, true, false, prec, rm);
7783 (r, o)
7784 }
7785
7786 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
7787 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
7788 /// rounding the result to the specified precision and with the specified rounding mode. The
7789 /// [`Float`] is taken by value and the [`Rational`] by reference. An [`Ordering`] is also
7790 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
7791 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
7792 /// function returns a `NaN` it also returns `Equal`.
7793 ///
7794 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7795 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7796 /// remainder by up to the quotient times the conversion error.
7797 ///
7798 /// $$
7799 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
7800 /// $$
7801 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
7802 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
7803 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
7804 ///
7805 /// Special cases:
7806 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
7807 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
7808 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
7809 ///
7810 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
7811 /// the minimum positive [`Float`]:
7812 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7813 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7814 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
7815 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7816 ///
7817 /// # Worst-case complexity
7818 /// $T(n) = O(n \log n \log\log n)$
7819 ///
7820 /// $M(n) = O(n)$
7821 ///
7822 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
7823 /// other.significant_bits(), prec)`.
7824 ///
7825 /// # Panics
7826 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
7827 /// with `prec` bits.
7828 ///
7829 /// # Examples
7830 /// ```
7831 /// use core::cmp::Ordering::*;
7832 /// use malachite_base::rounding_modes::RoundingMode::*;
7833 /// use malachite_float::Float;
7834 /// use malachite_q::Rational;
7835 ///
7836 /// let x = Float::from(10u32);
7837 /// let y = Rational::from_signeds(22, 7);
7838 /// let (r, o) = x.ieee_remainder_rational_prec_round_val_ref(&y, 5, Floor);
7839 /// assert_eq!(r.to_string(), "0.562");
7840 /// assert_eq!(o, Less);
7841 /// ```
7842 pub fn ieee_remainder_rational_prec_round_val_ref(
7843 self,
7844 other: &Rational,
7845 prec: u64,
7846 rm: RoundingMode,
7847 ) -> (Self, Ordering) {
7848 let (r, o, _) = rem_rational_helper(&self, other, true, false, prec, rm);
7849 (r, o)
7850 }
7851
7852 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
7853 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
7854 /// rounding the result to the specified precision and with the specified rounding mode. The
7855 /// [`Float`] is taken by reference and the [`Rational`] by value. An [`Ordering`] is also
7856 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
7857 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
7858 /// function returns a `NaN` it also returns `Equal`.
7859 ///
7860 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7861 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7862 /// remainder by up to the quotient times the conversion error.
7863 ///
7864 /// $$
7865 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
7866 /// $$
7867 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
7868 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
7869 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
7870 ///
7871 /// Special cases:
7872 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
7873 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
7874 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
7875 ///
7876 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
7877 /// the minimum positive [`Float`]:
7878 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7879 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7880 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
7881 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7882 ///
7883 /// # Worst-case complexity
7884 /// $T(n) = O(n \log n \log\log n)$
7885 ///
7886 /// $M(n) = O(n)$
7887 ///
7888 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
7889 /// other.significant_bits(), prec)`.
7890 ///
7891 /// # Panics
7892 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
7893 /// with `prec` bits.
7894 ///
7895 /// # Examples
7896 /// ```
7897 /// use core::cmp::Ordering::*;
7898 /// use malachite_base::rounding_modes::RoundingMode::*;
7899 /// use malachite_float::Float;
7900 /// use malachite_q::Rational;
7901 ///
7902 /// let x = Float::from(10u32);
7903 /// let y = Rational::from_signeds(22, 7);
7904 /// let (r, o) = x.ieee_remainder_rational_prec_round_ref_val(y, 5, Floor);
7905 /// assert_eq!(r.to_string(), "0.562");
7906 /// assert_eq!(o, Less);
7907 /// ```
7908 #[allow(clippy::needless_pass_by_value)]
7909 pub fn ieee_remainder_rational_prec_round_ref_val(
7910 &self,
7911 other: Rational,
7912 prec: u64,
7913 rm: RoundingMode,
7914 ) -> (Self, Ordering) {
7915 let (r, o, _) = rem_rational_helper(self, &other, true, false, prec, rm);
7916 (r, o)
7917 }
7918
7919 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
7920 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
7921 /// rounding the result to the specified precision and with the specified rounding mode. The
7922 /// [`Float`] and the [`Rational`] are both taken by reference. An [`Ordering`] is also
7923 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
7924 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
7925 /// function returns a `NaN` it also returns `Equal`.
7926 ///
7927 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7928 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7929 /// remainder by up to the quotient times the conversion error.
7930 ///
7931 /// $$
7932 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
7933 /// $$
7934 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
7935 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
7936 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
7937 ///
7938 /// Special cases:
7939 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
7940 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
7941 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
7942 ///
7943 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
7944 /// the minimum positive [`Float`]:
7945 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7946 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7947 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
7948 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7949 ///
7950 /// # Worst-case complexity
7951 /// $T(n) = O(n \log n \log\log n)$
7952 ///
7953 /// $M(n) = O(n)$
7954 ///
7955 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
7956 /// other.significant_bits(), prec)`.
7957 ///
7958 /// # Panics
7959 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
7960 /// with `prec` bits.
7961 ///
7962 /// # Examples
7963 /// ```
7964 /// use core::cmp::Ordering::*;
7965 /// use malachite_base::rounding_modes::RoundingMode::*;
7966 /// use malachite_float::Float;
7967 /// use malachite_q::Rational;
7968 ///
7969 /// let x = Float::from(10u32);
7970 /// let y = Rational::from_signeds(22, 7);
7971 /// let (r, o) = x.ieee_remainder_rational_prec_round_ref_ref(&y, 5, Floor);
7972 /// assert_eq!(r.to_string(), "0.562");
7973 /// assert_eq!(o, Less);
7974 /// ```
7975 pub fn ieee_remainder_rational_prec_round_ref_ref(
7976 &self,
7977 other: &Rational,
7978 prec: u64,
7979 rm: RoundingMode,
7980 ) -> (Self, Ordering) {
7981 let (r, o, _) = rem_rational_helper(self, other, true, false, prec, rm);
7982 (r, o)
7983 }
7984
7985 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
7986 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
7987 /// rounding the result to the nearest value of the specified precision. The [`Float`] and the
7988 /// [`Rational`] are both taken by value. An [`Ordering`] is also returned, indicating whether
7989 /// the rounded remainder is less than, equal to, or greater than the exact remainder. Although
7990 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
7991 /// returns `Equal`.
7992 ///
7993 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
7994 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
7995 /// remainder by up to the quotient times the conversion error.
7996 ///
7997 /// $$
7998 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
7999 /// $$
8000 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8001 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8002 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
8003 ///
8004 /// Special cases:
8005 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8006 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8007 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8008 ///
8009 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8010 /// the minimum positive [`Float`]:
8011 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8012 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8013 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8014 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8015 ///
8016 /// # Worst-case complexity
8017 /// $T(n) = O(n \log n \log\log n)$
8018 ///
8019 /// $M(n) = O(n)$
8020 ///
8021 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8022 /// other.significant_bits(), prec)`.
8023 ///
8024 /// # Panics
8025 /// Panics if `prec` is zero.
8026 ///
8027 /// # Examples
8028 /// ```
8029 /// use core::cmp::Ordering::*;
8030 /// use malachite_float::Float;
8031 /// use malachite_q::Rational;
8032 ///
8033 /// let (r, o) =
8034 /// Float::from(10u32).ieee_remainder_rational_prec(Rational::from_signeds(22, 7), 5);
8035 /// assert_eq!(r.to_string(), "0.562");
8036 /// assert_eq!(o, Less);
8037 /// ```
8038 #[inline]
8039 pub fn ieee_remainder_rational_prec(self, other: Rational, prec: u64) -> (Self, Ordering) {
8040 self.ieee_remainder_rational_prec_round(other, prec, Nearest)
8041 }
8042
8043 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
8044 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
8045 /// rounding the result to the nearest value of the specified precision. The [`Float`] is taken
8046 /// by value and the [`Rational`] by reference. An [`Ordering`] is also returned, indicating
8047 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder.
8048 /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
8049 /// it also returns `Equal`.
8050 ///
8051 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8052 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8053 /// remainder by up to the quotient times the conversion error.
8054 ///
8055 /// $$
8056 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8057 /// $$
8058 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8059 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8060 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
8061 ///
8062 /// Special cases:
8063 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8064 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8065 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8066 ///
8067 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8068 /// the minimum positive [`Float`]:
8069 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8070 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8071 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8072 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8073 ///
8074 /// # Worst-case complexity
8075 /// $T(n) = O(n \log n \log\log n)$
8076 ///
8077 /// $M(n) = O(n)$
8078 ///
8079 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8080 /// other.significant_bits(), prec)`.
8081 ///
8082 /// # Panics
8083 /// Panics if `prec` is zero.
8084 ///
8085 /// # Examples
8086 /// ```
8087 /// use core::cmp::Ordering::*;
8088 /// use malachite_float::Float;
8089 /// use malachite_q::Rational;
8090 ///
8091 /// let x = Float::from(10u32);
8092 /// let y = Rational::from_signeds(22, 7);
8093 /// let (r, o) = x.ieee_remainder_rational_prec_val_ref(&y, 5);
8094 /// assert_eq!(r.to_string(), "0.562");
8095 /// assert_eq!(o, Less);
8096 /// ```
8097 #[inline]
8098 pub fn ieee_remainder_rational_prec_val_ref(
8099 self,
8100 other: &Rational,
8101 prec: u64,
8102 ) -> (Self, Ordering) {
8103 self.ieee_remainder_rational_prec_round_val_ref(other, prec, Nearest)
8104 }
8105
8106 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
8107 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
8108 /// rounding the result to the nearest value of the specified precision. The [`Float`] is taken
8109 /// by reference and the [`Rational`] by value. An [`Ordering`] is also returned, indicating
8110 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder.
8111 /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
8112 /// it also returns `Equal`.
8113 ///
8114 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8115 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8116 /// remainder by up to the quotient times the conversion error.
8117 ///
8118 /// $$
8119 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8120 /// $$
8121 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8122 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8123 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
8124 ///
8125 /// Special cases:
8126 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8127 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8128 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8129 ///
8130 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8131 /// the minimum positive [`Float`]:
8132 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8133 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8134 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8135 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8136 ///
8137 /// # Worst-case complexity
8138 /// $T(n) = O(n \log n \log\log n)$
8139 ///
8140 /// $M(n) = O(n)$
8141 ///
8142 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8143 /// other.significant_bits(), prec)`.
8144 ///
8145 /// # Panics
8146 /// Panics if `prec` is zero.
8147 ///
8148 /// # Examples
8149 /// ```
8150 /// use core::cmp::Ordering::*;
8151 /// use malachite_float::Float;
8152 /// use malachite_q::Rational;
8153 ///
8154 /// let x = Float::from(10u32);
8155 /// let y = Rational::from_signeds(22, 7);
8156 /// let (r, o) = x.ieee_remainder_rational_prec_ref_val(y, 5);
8157 /// assert_eq!(r.to_string(), "0.562");
8158 /// assert_eq!(o, Less);
8159 /// ```
8160 #[inline]
8161 pub fn ieee_remainder_rational_prec_ref_val(
8162 &self,
8163 other: Rational,
8164 prec: u64,
8165 ) -> (Self, Ordering) {
8166 self.ieee_remainder_rational_prec_round_ref_val(other, prec, Nearest)
8167 }
8168
8169 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
8170 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
8171 /// rounding the result to the nearest value of the specified precision. The [`Float`] and the
8172 /// [`Rational`] are both taken by reference. An [`Ordering`] is also returned, indicating
8173 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder.
8174 /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
8175 /// it also returns `Equal`.
8176 ///
8177 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8178 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8179 /// remainder by up to the quotient times the conversion error.
8180 ///
8181 /// $$
8182 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8183 /// $$
8184 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8185 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8186 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
8187 ///
8188 /// Special cases:
8189 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8190 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8191 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8192 ///
8193 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8194 /// the minimum positive [`Float`]:
8195 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8196 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8197 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8198 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8199 ///
8200 /// # Worst-case complexity
8201 /// $T(n) = O(n \log n \log\log n)$
8202 ///
8203 /// $M(n) = O(n)$
8204 ///
8205 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8206 /// other.significant_bits(), prec)`.
8207 ///
8208 /// # Panics
8209 /// Panics if `prec` is zero.
8210 ///
8211 /// # Examples
8212 /// ```
8213 /// use core::cmp::Ordering::*;
8214 /// use malachite_float::Float;
8215 /// use malachite_q::Rational;
8216 ///
8217 /// let x = Float::from(10u32);
8218 /// let y = Rational::from_signeds(22, 7);
8219 /// let (r, o) = x.ieee_remainder_rational_prec_ref_ref(&y, 5);
8220 /// assert_eq!(r.to_string(), "0.562");
8221 /// assert_eq!(o, Less);
8222 /// ```
8223 #[inline]
8224 pub fn ieee_remainder_rational_prec_ref_ref(
8225 &self,
8226 other: &Rational,
8227 prec: u64,
8228 ) -> (Self, Ordering) {
8229 self.ieee_remainder_rational_prec_round_ref_ref(other, prec, Nearest)
8230 }
8231
8232 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
8233 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
8234 /// rounding the result to the [`Float`]'s precision, with the specified rounding mode. The
8235 /// [`Float`] and the [`Rational`] are both taken by value. An [`Ordering`] is also returned,
8236 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
8237 /// remainder. Although `NaN`s are not comparable to any [`Float`], whenever this function
8238 /// returns a `NaN` it also returns `Equal`.
8239 ///
8240 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8241 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8242 /// remainder by up to the quotient times the conversion error.
8243 ///
8244 /// $$
8245 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8246 /// $$
8247 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8248 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8249 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
8250 ///
8251 /// Special cases:
8252 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8253 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8254 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8255 ///
8256 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8257 /// the minimum positive [`Float`]:
8258 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8259 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8260 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8261 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8262 ///
8263 /// # Worst-case complexity
8264 /// $T(n) = O(n \log n \log\log n)$
8265 ///
8266 /// $M(n) = O(n)$
8267 ///
8268 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8269 /// other.significant_bits())`.
8270 ///
8271 /// # Panics
8272 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
8273 /// precision.
8274 ///
8275 /// # Examples
8276 /// ```
8277 /// use core::cmp::Ordering::*;
8278 /// use malachite_base::rounding_modes::RoundingMode::*;
8279 /// use malachite_float::Float;
8280 /// use malachite_q::Rational;
8281 ///
8282 /// let (r, o) =
8283 /// Float::from(10u32).ieee_remainder_rational_round(Rational::from_signeds(22, 7), Floor);
8284 /// assert_eq!(r.to_string(), "0.50");
8285 /// assert_eq!(o, Less);
8286 /// ```
8287 #[inline]
8288 pub fn ieee_remainder_rational_round(
8289 self,
8290 other: Rational,
8291 rm: RoundingMode,
8292 ) -> (Self, Ordering) {
8293 let prec = self.significant_bits();
8294 self.ieee_remainder_rational_prec_round(other, prec, rm)
8295 }
8296
8297 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
8298 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
8299 /// rounding the result to the [`Float`]'s precision, with the specified rounding mode. The
8300 /// [`Float`] is taken by value and the [`Rational`] by reference. An [`Ordering`] is also
8301 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
8302 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
8303 /// function returns a `NaN` it also returns `Equal`.
8304 ///
8305 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8306 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8307 /// remainder by up to the quotient times the conversion error.
8308 ///
8309 /// $$
8310 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8311 /// $$
8312 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8313 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8314 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
8315 ///
8316 /// Special cases:
8317 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8318 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8319 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8320 ///
8321 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8322 /// the minimum positive [`Float`]:
8323 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8324 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8325 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8326 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8327 ///
8328 /// # Worst-case complexity
8329 /// $T(n) = O(n \log n \log\log n)$
8330 ///
8331 /// $M(n) = O(n)$
8332 ///
8333 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8334 /// other.significant_bits())`.
8335 ///
8336 /// # Panics
8337 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
8338 /// precision.
8339 ///
8340 /// # Examples
8341 /// ```
8342 /// use core::cmp::Ordering::*;
8343 /// use malachite_base::rounding_modes::RoundingMode::*;
8344 /// use malachite_float::Float;
8345 /// use malachite_q::Rational;
8346 ///
8347 /// let x = Float::from(10u32);
8348 /// let y = Rational::from_signeds(22, 7);
8349 /// let (r, o) = x.ieee_remainder_rational_round_val_ref(&y, Floor);
8350 /// assert_eq!(r.to_string(), "0.50");
8351 /// assert_eq!(o, Less);
8352 /// ```
8353 #[inline]
8354 pub fn ieee_remainder_rational_round_val_ref(
8355 self,
8356 other: &Rational,
8357 rm: RoundingMode,
8358 ) -> (Self, Ordering) {
8359 let prec = self.significant_bits();
8360 self.ieee_remainder_rational_prec_round_val_ref(other, prec, rm)
8361 }
8362
8363 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
8364 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
8365 /// rounding the result to the [`Float`]'s precision, with the specified rounding mode. The
8366 /// [`Float`] is taken by reference and the [`Rational`] by value. An [`Ordering`] is also
8367 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
8368 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
8369 /// function returns a `NaN` it also returns `Equal`.
8370 ///
8371 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8372 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8373 /// remainder by up to the quotient times the conversion error.
8374 ///
8375 /// $$
8376 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8377 /// $$
8378 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8379 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8380 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
8381 ///
8382 /// Special cases:
8383 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8384 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8385 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8386 ///
8387 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8388 /// the minimum positive [`Float`]:
8389 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8390 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8391 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8392 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8393 ///
8394 /// # Worst-case complexity
8395 /// $T(n) = O(n \log n \log\log n)$
8396 ///
8397 /// $M(n) = O(n)$
8398 ///
8399 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8400 /// other.significant_bits())`.
8401 ///
8402 /// # Panics
8403 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
8404 /// precision.
8405 ///
8406 /// # Examples
8407 /// ```
8408 /// use core::cmp::Ordering::*;
8409 /// use malachite_base::rounding_modes::RoundingMode::*;
8410 /// use malachite_float::Float;
8411 /// use malachite_q::Rational;
8412 ///
8413 /// let x = Float::from(10u32);
8414 /// let y = Rational::from_signeds(22, 7);
8415 /// let (r, o) = x.ieee_remainder_rational_round_ref_val(y, Floor);
8416 /// assert_eq!(r.to_string(), "0.50");
8417 /// assert_eq!(o, Less);
8418 /// ```
8419 #[inline]
8420 pub fn ieee_remainder_rational_round_ref_val(
8421 &self,
8422 other: Rational,
8423 rm: RoundingMode,
8424 ) -> (Self, Ordering) {
8425 let prec = self.significant_bits();
8426 self.ieee_remainder_rational_prec_round_ref_val(other, prec, rm)
8427 }
8428
8429 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
8430 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
8431 /// rounding the result to the [`Float`]'s precision, with the specified rounding mode. The
8432 /// [`Float`] and the [`Rational`] are both taken by reference. An [`Ordering`] is also
8433 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
8434 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
8435 /// function returns a `NaN` it also returns `Equal`.
8436 ///
8437 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8438 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8439 /// remainder by up to the quotient times the conversion error.
8440 ///
8441 /// $$
8442 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8443 /// $$
8444 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8445 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8446 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
8447 ///
8448 /// Special cases:
8449 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8450 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8451 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8452 ///
8453 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8454 /// the minimum positive [`Float`]:
8455 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8456 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8457 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8458 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8459 ///
8460 /// # Worst-case complexity
8461 /// $T(n) = O(n \log n \log\log n)$
8462 ///
8463 /// $M(n) = O(n)$
8464 ///
8465 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8466 /// other.significant_bits())`.
8467 ///
8468 /// # Panics
8469 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
8470 /// precision.
8471 ///
8472 /// # Examples
8473 /// ```
8474 /// use core::cmp::Ordering::*;
8475 /// use malachite_base::rounding_modes::RoundingMode::*;
8476 /// use malachite_float::Float;
8477 /// use malachite_q::Rational;
8478 ///
8479 /// let x = Float::from(10u32);
8480 /// let y = Rational::from_signeds(22, 7);
8481 /// let (r, o) = x.ieee_remainder_rational_round_ref_ref(&y, Floor);
8482 /// assert_eq!(r.to_string(), "0.50");
8483 /// assert_eq!(o, Less);
8484 /// ```
8485 #[inline]
8486 pub fn ieee_remainder_rational_round_ref_ref(
8487 &self,
8488 other: &Rational,
8489 rm: RoundingMode,
8490 ) -> (Self, Ordering) {
8491 let prec = self.significant_bits();
8492 self.ieee_remainder_rational_prec_round_ref_ref(other, prec, rm)
8493 }
8494
8495 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
8496 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
8497 /// rounding the result to the nearest value of the [`Float`]'s precision. The [`Float`] and the
8498 /// [`Rational`] are both taken by value.
8499 ///
8500 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8501 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8502 /// remainder by up to the quotient times the conversion error.
8503 ///
8504 /// $$
8505 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8506 /// $$
8507 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8508 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8509 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
8510 ///
8511 /// Special cases:
8512 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8513 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8514 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8515 ///
8516 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8517 /// the minimum positive [`Float`]:
8518 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8519 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8520 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8521 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8522 ///
8523 /// # Worst-case complexity
8524 /// $T(n) = O(n \log n \log\log n)$
8525 ///
8526 /// $M(n) = O(n)$
8527 ///
8528 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8529 /// other.significant_bits())`.
8530 ///
8531 /// # Examples
8532 /// ```
8533 /// use malachite_float::Float;
8534 /// use malachite_q::Rational;
8535 ///
8536 /// let r = Float::from(10u32).ieee_remainder_rational(Rational::from_signeds(22, 7));
8537 /// assert_eq!(r.to_string(), "0.62");
8538 /// ```
8539 #[inline]
8540 pub fn ieee_remainder_rational(self, other: Rational) -> Self {
8541 self.ieee_remainder_rational_round(other, Nearest).0
8542 }
8543
8544 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
8545 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
8546 /// rounding the result to the nearest value of the [`Float`]'s precision. The [`Float`] is
8547 /// taken by value and the [`Rational`] by reference.
8548 ///
8549 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8550 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8551 /// remainder by up to the quotient times the conversion error.
8552 ///
8553 /// $$
8554 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8555 /// $$
8556 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8557 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8558 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
8559 ///
8560 /// Special cases:
8561 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8562 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8563 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8564 ///
8565 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8566 /// the minimum positive [`Float`]:
8567 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8568 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8569 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8570 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8571 ///
8572 /// # Worst-case complexity
8573 /// $T(n) = O(n \log n \log\log n)$
8574 ///
8575 /// $M(n) = O(n)$
8576 ///
8577 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8578 /// other.significant_bits())`.
8579 ///
8580 /// # Examples
8581 /// ```
8582 /// use malachite_float::Float;
8583 /// use malachite_q::Rational;
8584 ///
8585 /// let r = Float::from(10u32).ieee_remainder_rational_val_ref(&Rational::from_signeds(22, 7));
8586 /// assert_eq!(r.to_string(), "0.62");
8587 /// ```
8588 #[inline]
8589 pub fn ieee_remainder_rational_val_ref(self, other: &Rational) -> Self {
8590 self.ieee_remainder_rational_round_val_ref(other, Nearest).0
8591 }
8592
8593 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
8594 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
8595 /// rounding the result to the nearest value of the [`Float`]'s precision. The [`Float`] is
8596 /// taken by reference and the [`Rational`] by value.
8597 ///
8598 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8599 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8600 /// remainder by up to the quotient times the conversion error.
8601 ///
8602 /// $$
8603 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8604 /// $$
8605 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8606 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8607 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
8608 ///
8609 /// Special cases:
8610 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8611 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8612 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8613 ///
8614 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8615 /// the minimum positive [`Float`]:
8616 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8617 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8618 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8619 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8620 ///
8621 /// # Worst-case complexity
8622 /// $T(n) = O(n \log n \log\log n)$
8623 ///
8624 /// $M(n) = O(n)$
8625 ///
8626 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8627 /// other.significant_bits())`.
8628 ///
8629 /// # Examples
8630 /// ```
8631 /// use malachite_float::Float;
8632 /// use malachite_q::Rational;
8633 ///
8634 /// let r = Float::from(10u32).ieee_remainder_rational_ref_val(Rational::from_signeds(22, 7));
8635 /// assert_eq!(r.to_string(), "0.62");
8636 /// ```
8637 #[inline]
8638 pub fn ieee_remainder_rational_ref_val(&self, other: Rational) -> Self {
8639 self.ieee_remainder_rational_round_ref_val(other, Nearest).0
8640 }
8641
8642 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
8643 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
8644 /// rounding the result to the nearest value of the [`Float`]'s precision. The [`Float`] and the
8645 /// [`Rational`] are both taken by reference.
8646 ///
8647 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8648 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8649 /// remainder by up to the quotient times the conversion error.
8650 ///
8651 /// $$
8652 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8653 /// $$
8654 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8655 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8656 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
8657 ///
8658 /// Special cases:
8659 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8660 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8661 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8662 ///
8663 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8664 /// the minimum positive [`Float`]:
8665 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8666 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8667 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8668 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8669 ///
8670 /// # Worst-case complexity
8671 /// $T(n) = O(n \log n \log\log n)$
8672 ///
8673 /// $M(n) = O(n)$
8674 ///
8675 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8676 /// other.significant_bits())`.
8677 ///
8678 /// # Examples
8679 /// ```
8680 /// use malachite_float::Float;
8681 /// use malachite_q::Rational;
8682 ///
8683 /// let r = Float::from(10u32).ieee_remainder_rational_ref_ref(&Rational::from_signeds(22, 7));
8684 /// assert_eq!(r.to_string(), "0.62");
8685 /// ```
8686 #[inline]
8687 pub fn ieee_remainder_rational_ref_ref(&self, other: &Rational) -> Self {
8688 self.ieee_remainder_rational_round_ref_ref(other, Nearest).0
8689 }
8690
8691 /// Computes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
8692 /// to the nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's
8693 /// `remainder`, rounding the result to the specified precision and with the specified rounding
8694 /// mode. The [`Rational`] is taken by value. An [`Ordering`] is returned, indicating whether
8695 /// the rounded remainder is less than, equal to, or greater than the exact remainder. Although
8696 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
8697 /// returns `Equal`.
8698 ///
8699 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8700 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8701 /// remainder by up to the quotient times the conversion error.
8702 ///
8703 /// $$
8704 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8705 /// $$
8706 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8707 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8708 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
8709 ///
8710 /// Special cases:
8711 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8712 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8713 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8714 ///
8715 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8716 /// the minimum positive [`Float`]:
8717 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8718 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8719 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8720 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8721 ///
8722 /// # Worst-case complexity
8723 /// $T(n) = O(n \log n \log\log n)$
8724 ///
8725 /// $M(n) = O(n)$
8726 ///
8727 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8728 /// other.significant_bits(), prec)`.
8729 ///
8730 /// # Panics
8731 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
8732 /// with `prec` bits.
8733 ///
8734 /// # Examples
8735 /// ```
8736 /// use core::cmp::Ordering::*;
8737 /// use malachite_base::rounding_modes::RoundingMode::*;
8738 /// use malachite_float::Float;
8739 /// use malachite_q::Rational;
8740 ///
8741 /// let mut x = Float::from(10u32);
8742 /// let y = Rational::from_signeds(22, 7);
8743 /// assert_eq!(
8744 /// x.ieee_remainder_rational_prec_round_assign(y, 5, Floor),
8745 /// Less
8746 /// );
8747 /// assert_eq!(x.to_string(), "0.562");
8748 /// ```
8749 #[allow(clippy::needless_pass_by_value)]
8750 pub fn ieee_remainder_rational_prec_round_assign(
8751 &mut self,
8752 other: Rational,
8753 prec: u64,
8754 rm: RoundingMode,
8755 ) -> Ordering {
8756 let (r, o, _) = rem_rational_helper(self, &other, true, false, prec, rm);
8757 *self = r;
8758 o
8759 }
8760
8761 /// Computes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
8762 /// to the nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's
8763 /// `remainder`, rounding the result to the specified precision and with the specified rounding
8764 /// mode. The [`Rational`] is taken by reference. An [`Ordering`] is returned, indicating
8765 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder.
8766 /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
8767 /// it also returns `Equal`.
8768 ///
8769 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8770 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8771 /// remainder by up to the quotient times the conversion error.
8772 ///
8773 /// $$
8774 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8775 /// $$
8776 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8777 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8778 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
8779 ///
8780 /// Special cases:
8781 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8782 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8783 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8784 ///
8785 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8786 /// the minimum positive [`Float`]:
8787 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8788 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8789 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8790 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8791 ///
8792 /// # Worst-case complexity
8793 /// $T(n) = O(n \log n \log\log n)$
8794 ///
8795 /// $M(n) = O(n)$
8796 ///
8797 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8798 /// other.significant_bits(), prec)`.
8799 ///
8800 /// # Panics
8801 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
8802 /// with `prec` bits.
8803 ///
8804 /// # Examples
8805 /// ```
8806 /// use core::cmp::Ordering::*;
8807 /// use malachite_base::rounding_modes::RoundingMode::*;
8808 /// use malachite_float::Float;
8809 /// use malachite_q::Rational;
8810 ///
8811 /// let mut x = Float::from(10u32);
8812 /// let y = Rational::from_signeds(22, 7);
8813 /// assert_eq!(
8814 /// x.ieee_remainder_rational_prec_round_assign_ref(&y, 5, Floor),
8815 /// Less
8816 /// );
8817 /// assert_eq!(x.to_string(), "0.562");
8818 /// ```
8819 pub fn ieee_remainder_rational_prec_round_assign_ref(
8820 &mut self,
8821 other: &Rational,
8822 prec: u64,
8823 rm: RoundingMode,
8824 ) -> Ordering {
8825 let (r, o, _) = rem_rational_helper(self, other, true, false, prec, rm);
8826 *self = r;
8827 o
8828 }
8829
8830 /// Computes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
8831 /// to the nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's
8832 /// `remainder`, rounding the result to the nearest value of the specified precision. The
8833 /// [`Rational`] is taken by value. An [`Ordering`] is returned, indicating whether the rounded
8834 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
8835 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
8836 /// `Equal`.
8837 ///
8838 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8839 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8840 /// remainder by up to the quotient times the conversion error.
8841 ///
8842 /// $$
8843 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8844 /// $$
8845 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8846 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8847 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
8848 ///
8849 /// Special cases:
8850 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8851 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8852 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8853 ///
8854 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8855 /// the minimum positive [`Float`]:
8856 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8857 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8858 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8859 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8860 ///
8861 /// # Worst-case complexity
8862 /// $T(n) = O(n \log n \log\log n)$
8863 ///
8864 /// $M(n) = O(n)$
8865 ///
8866 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8867 /// other.significant_bits(), prec)`.
8868 ///
8869 /// # Panics
8870 /// Panics if `prec` is zero.
8871 ///
8872 /// # Examples
8873 /// ```
8874 /// use core::cmp::Ordering::*;
8875 /// use malachite_float::Float;
8876 /// use malachite_q::Rational;
8877 ///
8878 /// let mut x = Float::from(10u32);
8879 /// assert_eq!(
8880 /// x.ieee_remainder_rational_prec_assign(Rational::from_signeds(22, 7), 5),
8881 /// Less
8882 /// );
8883 /// assert_eq!(x.to_string(), "0.562");
8884 /// ```
8885 #[inline]
8886 pub fn ieee_remainder_rational_prec_assign(&mut self, other: Rational, prec: u64) -> Ordering {
8887 self.ieee_remainder_rational_prec_round_assign(other, prec, Nearest)
8888 }
8889
8890 /// Computes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
8891 /// to the nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's
8892 /// `remainder`, rounding the result to the nearest value of the specified precision. The
8893 /// [`Rational`] is taken by reference. An [`Ordering`] is returned, indicating whether the
8894 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
8895 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
8896 /// returns `Equal`.
8897 ///
8898 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8899 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8900 /// remainder by up to the quotient times the conversion error.
8901 ///
8902 /// $$
8903 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8904 /// $$
8905 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8906 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8907 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
8908 ///
8909 /// Special cases:
8910 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8911 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8912 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8913 ///
8914 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8915 /// the minimum positive [`Float`]:
8916 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8917 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8918 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8919 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8920 ///
8921 /// # Worst-case complexity
8922 /// $T(n) = O(n \log n \log\log n)$
8923 ///
8924 /// $M(n) = O(n)$
8925 ///
8926 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8927 /// other.significant_bits(), prec)`.
8928 ///
8929 /// # Panics
8930 /// Panics if `prec` is zero.
8931 ///
8932 /// # Examples
8933 /// ```
8934 /// use core::cmp::Ordering::*;
8935 /// use malachite_float::Float;
8936 /// use malachite_q::Rational;
8937 ///
8938 /// let mut x = Float::from(10u32);
8939 /// let y = Rational::from_signeds(22, 7);
8940 /// assert_eq!(x.ieee_remainder_rational_prec_assign_ref(&y, 5), Less);
8941 /// assert_eq!(x.to_string(), "0.562");
8942 /// ```
8943 #[inline]
8944 pub fn ieee_remainder_rational_prec_assign_ref(
8945 &mut self,
8946 other: &Rational,
8947 prec: u64,
8948 ) -> Ordering {
8949 self.ieee_remainder_rational_prec_round_assign_ref(other, prec, Nearest)
8950 }
8951
8952 /// Computes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
8953 /// to the nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's
8954 /// `remainder`, rounding the result to the [`Float`]'s precision, with the specified rounding
8955 /// mode. The [`Rational`] is taken by value. An [`Ordering`] is returned, indicating whether
8956 /// the rounded remainder is less than, equal to, or greater than the exact remainder. Although
8957 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
8958 /// returns `Equal`.
8959 ///
8960 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
8961 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
8962 /// remainder by up to the quotient times the conversion error.
8963 ///
8964 /// $$
8965 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
8966 /// $$
8967 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
8968 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
8969 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
8970 ///
8971 /// Special cases:
8972 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
8973 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
8974 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
8975 ///
8976 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
8977 /// the minimum positive [`Float`]:
8978 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
8979 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
8980 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
8981 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
8982 ///
8983 /// # Worst-case complexity
8984 /// $T(n) = O(n \log n \log\log n)$
8985 ///
8986 /// $M(n) = O(n)$
8987 ///
8988 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
8989 /// other.significant_bits())`.
8990 ///
8991 /// # Panics
8992 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
8993 /// precision.
8994 ///
8995 /// # Examples
8996 /// ```
8997 /// use core::cmp::Ordering::*;
8998 /// use malachite_base::rounding_modes::RoundingMode::*;
8999 /// use malachite_float::Float;
9000 /// use malachite_q::Rational;
9001 ///
9002 /// let mut x = Float::from(10u32);
9003 /// let y = Rational::from_signeds(22, 7);
9004 /// assert_eq!(x.ieee_remainder_rational_round_assign(y, Floor), Less);
9005 /// assert_eq!(x.to_string(), "0.50");
9006 /// ```
9007 #[inline]
9008 pub fn ieee_remainder_rational_round_assign(
9009 &mut self,
9010 other: Rational,
9011 rm: RoundingMode,
9012 ) -> Ordering {
9013 let prec = self.significant_bits();
9014 self.ieee_remainder_rational_prec_round_assign(other, prec, rm)
9015 }
9016
9017 /// Computes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
9018 /// to the nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's
9019 /// `remainder`, rounding the result to the [`Float`]'s precision, with the specified rounding
9020 /// mode. The [`Rational`] is taken by reference. An [`Ordering`] is returned, indicating
9021 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder.
9022 /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
9023 /// it also returns `Equal`.
9024 ///
9025 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
9026 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
9027 /// remainder by up to the quotient times the conversion error.
9028 ///
9029 /// $$
9030 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
9031 /// $$
9032 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
9033 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
9034 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
9035 ///
9036 /// Special cases:
9037 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
9038 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
9039 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
9040 ///
9041 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
9042 /// the minimum positive [`Float`]:
9043 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
9044 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
9045 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
9046 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
9047 ///
9048 /// # Worst-case complexity
9049 /// $T(n) = O(n \log n \log\log n)$
9050 ///
9051 /// $M(n) = O(n)$
9052 ///
9053 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
9054 /// other.significant_bits())`.
9055 ///
9056 /// # Panics
9057 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
9058 /// precision.
9059 ///
9060 /// # Examples
9061 /// ```
9062 /// use core::cmp::Ordering::*;
9063 /// use malachite_base::rounding_modes::RoundingMode::*;
9064 /// use malachite_float::Float;
9065 /// use malachite_q::Rational;
9066 ///
9067 /// let mut x = Float::from(10u32);
9068 /// let y = Rational::from_signeds(22, 7);
9069 /// assert_eq!(x.ieee_remainder_rational_round_assign_ref(&y, Floor), Less);
9070 /// assert_eq!(x.to_string(), "0.50");
9071 /// ```
9072 #[inline]
9073 pub fn ieee_remainder_rational_round_assign_ref(
9074 &mut self,
9075 other: &Rational,
9076 rm: RoundingMode,
9077 ) -> Ordering {
9078 let prec = self.significant_bits();
9079 self.ieee_remainder_rational_prec_round_assign_ref(other, prec, rm)
9080 }
9081
9082 /// Computes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
9083 /// to the nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's
9084 /// `remainder`, rounding the result to the nearest value of the [`Float`]'s precision. The
9085 /// [`Rational`] is taken by value. An [`Ordering`] is returned, indicating whether the rounded
9086 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
9087 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
9088 /// `Equal`.
9089 ///
9090 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
9091 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
9092 /// remainder by up to the quotient times the conversion error.
9093 ///
9094 /// $$
9095 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
9096 /// $$
9097 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
9098 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
9099 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
9100 ///
9101 /// Special cases:
9102 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
9103 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
9104 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
9105 ///
9106 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
9107 /// the minimum positive [`Float`]:
9108 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
9109 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
9110 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
9111 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
9112 ///
9113 /// # Worst-case complexity
9114 /// $T(n) = O(n \log n \log\log n)$
9115 ///
9116 /// $M(n) = O(n)$
9117 ///
9118 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
9119 /// other.significant_bits())`.
9120 ///
9121 /// # Examples
9122 /// ```
9123 /// use malachite_float::Float;
9124 /// use malachite_q::Rational;
9125 ///
9126 /// let mut x = Float::from(10u32);
9127 /// x.ieee_remainder_rational_assign(Rational::from_signeds(22, 7));
9128 /// assert_eq!(x.to_string(), "0.62");
9129 /// ```
9130 #[inline]
9131 pub fn ieee_remainder_rational_assign(&mut self, other: Rational) {
9132 self.ieee_remainder_rational_round_assign(other, Nearest);
9133 }
9134
9135 /// Computes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
9136 /// to the nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's
9137 /// `remainder`, rounding the result to the nearest value of the [`Float`]'s precision. The
9138 /// [`Rational`] is taken by reference. An [`Ordering`] is returned, indicating whether the
9139 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
9140 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
9141 /// returns `Equal`.
9142 ///
9143 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
9144 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
9145 /// remainder by up to the quotient times the conversion error.
9146 ///
9147 /// $$
9148 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
9149 /// $$
9150 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
9151 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
9152 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
9153 ///
9154 /// Special cases:
9155 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
9156 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
9157 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
9158 ///
9159 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
9160 /// the minimum positive [`Float`]:
9161 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
9162 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
9163 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
9164 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
9165 ///
9166 /// # Worst-case complexity
9167 /// $T(n) = O(n \log n \log\log n)$
9168 ///
9169 /// $M(n) = O(n)$
9170 ///
9171 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
9172 /// other.significant_bits())`.
9173 ///
9174 /// # Examples
9175 /// ```
9176 /// use malachite_float::Float;
9177 /// use malachite_q::Rational;
9178 ///
9179 /// let mut x = Float::from(10u32);
9180 /// x.ieee_remainder_rational_assign_ref(&Rational::from_signeds(22, 7));
9181 /// assert_eq!(x.to_string(), "0.62");
9182 /// ```
9183 #[inline]
9184 pub fn ieee_remainder_rational_assign_ref(&mut self, other: &Rational) {
9185 self.ieee_remainder_rational_round_assign_ref(other, Nearest);
9186 }
9187
9188 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
9189 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
9190 /// rounding the result to the specified precision and with the specified rounding mode. The
9191 /// [`Float`] and the [`Rational`] are both taken by value. An [`Ordering`] is also returned,
9192 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
9193 /// remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s are not
9194 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
9195 ///
9196 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
9197 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
9198 /// remainder by up to the quotient times the conversion error.
9199 ///
9200 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
9201 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
9202 /// [`Float`]-[`Float`] functions.
9203 ///
9204 /// $$
9205 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
9206 /// $$
9207 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
9208 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
9209 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
9210 ///
9211 /// Special cases:
9212 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
9213 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
9214 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
9215 /// - The quotient bits are 0 in all of the above special cases.
9216 ///
9217 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
9218 /// the minimum positive [`Float`]:
9219 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
9220 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
9221 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
9222 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
9223 ///
9224 /// # Worst-case complexity
9225 /// $T(n) = O(n \log n \log\log n)$
9226 ///
9227 /// $M(n) = O(n)$
9228 ///
9229 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
9230 /// other.significant_bits(), prec)`.
9231 ///
9232 /// # Panics
9233 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
9234 /// with `prec` bits.
9235 ///
9236 /// # Examples
9237 /// ```
9238 /// use core::cmp::Ordering::*;
9239 /// use malachite_base::rounding_modes::RoundingMode::*;
9240 /// use malachite_float::Float;
9241 /// use malachite_q::Rational;
9242 ///
9243 /// let x = Float::from(10u32);
9244 /// let y = Rational::from_signeds(22, 7);
9245 /// let (r, o, q) = x.ieee_remainder_rational_and_quotient_bits_prec_round(y, 5, Floor);
9246 /// assert_eq!(r.to_string(), "0.562");
9247 /// assert_eq!(o, Less);
9248 /// assert_eq!(q, 3);
9249 /// ```
9250 #[allow(clippy::needless_pass_by_value)]
9251 #[inline]
9252 pub fn ieee_remainder_rational_and_quotient_bits_prec_round(
9253 self,
9254 other: Rational,
9255 prec: u64,
9256 rm: RoundingMode,
9257 ) -> (Self, Ordering, i64) {
9258 rem_rational_helper(&self, &other, true, true, prec, rm)
9259 }
9260
9261 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
9262 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
9263 /// rounding the result to the specified precision and with the specified rounding mode. The
9264 /// [`Float`] is taken by value and the [`Rational`] by reference. An [`Ordering`] is also
9265 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
9266 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
9267 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
9268 /// `Equal`.
9269 ///
9270 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
9271 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
9272 /// remainder by up to the quotient times the conversion error.
9273 ///
9274 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
9275 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
9276 /// [`Float`]-[`Float`] functions.
9277 ///
9278 /// $$
9279 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
9280 /// $$
9281 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
9282 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
9283 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
9284 ///
9285 /// Special cases:
9286 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
9287 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
9288 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
9289 /// - The quotient bits are 0 in all of the above special cases.
9290 ///
9291 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
9292 /// the minimum positive [`Float`]:
9293 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
9294 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
9295 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
9296 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
9297 ///
9298 /// # Worst-case complexity
9299 /// $T(n) = O(n \log n \log\log n)$
9300 ///
9301 /// $M(n) = O(n)$
9302 ///
9303 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
9304 /// other.significant_bits(), prec)`.
9305 ///
9306 /// # Panics
9307 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
9308 /// with `prec` bits.
9309 ///
9310 /// # Examples
9311 /// ```
9312 /// use core::cmp::Ordering::*;
9313 /// use malachite_base::rounding_modes::RoundingMode::*;
9314 /// use malachite_float::Float;
9315 /// use malachite_q::Rational;
9316 ///
9317 /// let x = Float::from(10u32);
9318 /// let y = Rational::from_signeds(22, 7);
9319 /// let (r, o, q) =
9320 /// x.ieee_remainder_rational_and_quotient_bits_prec_round_val_ref(&y, 5, Floor);
9321 /// assert_eq!(r.to_string(), "0.562");
9322 /// assert_eq!(o, Less);
9323 /// assert_eq!(q, 3);
9324 /// ```
9325 #[inline]
9326 pub fn ieee_remainder_rational_and_quotient_bits_prec_round_val_ref(
9327 self,
9328 other: &Rational,
9329 prec: u64,
9330 rm: RoundingMode,
9331 ) -> (Self, Ordering, i64) {
9332 rem_rational_helper(&self, other, true, true, prec, rm)
9333 }
9334
9335 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
9336 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
9337 /// rounding the result to the specified precision and with the specified rounding mode. The
9338 /// [`Float`] is taken by reference and the [`Rational`] by value. An [`Ordering`] is also
9339 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
9340 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
9341 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
9342 /// `Equal`.
9343 ///
9344 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
9345 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
9346 /// remainder by up to the quotient times the conversion error.
9347 ///
9348 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
9349 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
9350 /// [`Float`]-[`Float`] functions.
9351 ///
9352 /// $$
9353 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
9354 /// $$
9355 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
9356 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
9357 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
9358 ///
9359 /// Special cases:
9360 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
9361 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
9362 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
9363 /// - The quotient bits are 0 in all of the above special cases.
9364 ///
9365 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
9366 /// the minimum positive [`Float`]:
9367 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
9368 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
9369 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
9370 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
9371 ///
9372 /// # Worst-case complexity
9373 /// $T(n) = O(n \log n \log\log n)$
9374 ///
9375 /// $M(n) = O(n)$
9376 ///
9377 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
9378 /// other.significant_bits(), prec)`.
9379 ///
9380 /// # Panics
9381 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
9382 /// with `prec` bits.
9383 ///
9384 /// # Examples
9385 /// ```
9386 /// use core::cmp::Ordering::*;
9387 /// use malachite_base::rounding_modes::RoundingMode::*;
9388 /// use malachite_float::Float;
9389 /// use malachite_q::Rational;
9390 ///
9391 /// let x = Float::from(10u32);
9392 /// let y = Rational::from_signeds(22, 7);
9393 /// let (r, o, q) = x.ieee_remainder_rational_and_quotient_bits_prec_round_ref_val(y, 5, Floor);
9394 /// assert_eq!(r.to_string(), "0.562");
9395 /// assert_eq!(o, Less);
9396 /// assert_eq!(q, 3);
9397 /// ```
9398 #[allow(clippy::needless_pass_by_value)]
9399 #[inline]
9400 pub fn ieee_remainder_rational_and_quotient_bits_prec_round_ref_val(
9401 &self,
9402 other: Rational,
9403 prec: u64,
9404 rm: RoundingMode,
9405 ) -> (Self, Ordering, i64) {
9406 rem_rational_helper(self, &other, true, true, prec, rm)
9407 }
9408
9409 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
9410 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
9411 /// rounding the result to the specified precision and with the specified rounding mode. The
9412 /// [`Float`] and the [`Rational`] are both taken by reference. An [`Ordering`] is also
9413 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
9414 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
9415 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
9416 /// `Equal`.
9417 ///
9418 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
9419 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
9420 /// remainder by up to the quotient times the conversion error.
9421 ///
9422 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
9423 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
9424 /// [`Float`]-[`Float`] functions.
9425 ///
9426 /// $$
9427 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
9428 /// $$
9429 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
9430 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
9431 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
9432 ///
9433 /// Special cases:
9434 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
9435 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
9436 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
9437 /// - The quotient bits are 0 in all of the above special cases.
9438 ///
9439 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
9440 /// the minimum positive [`Float`]:
9441 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
9442 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
9443 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
9444 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
9445 ///
9446 /// # Worst-case complexity
9447 /// $T(n) = O(n \log n \log\log n)$
9448 ///
9449 /// $M(n) = O(n)$
9450 ///
9451 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
9452 /// other.significant_bits(), prec)`.
9453 ///
9454 /// # Panics
9455 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
9456 /// with `prec` bits.
9457 ///
9458 /// # Examples
9459 /// ```
9460 /// use core::cmp::Ordering::*;
9461 /// use malachite_base::rounding_modes::RoundingMode::*;
9462 /// use malachite_float::Float;
9463 /// use malachite_q::Rational;
9464 ///
9465 /// let x = Float::from(10u32);
9466 /// let y = Rational::from_signeds(22, 7);
9467 /// let (r, o, q) =
9468 /// x.ieee_remainder_rational_and_quotient_bits_prec_round_ref_ref(&y, 5, Floor);
9469 /// assert_eq!(r.to_string(), "0.562");
9470 /// assert_eq!(o, Less);
9471 /// assert_eq!(q, 3);
9472 /// ```
9473 #[inline]
9474 pub fn ieee_remainder_rational_and_quotient_bits_prec_round_ref_ref(
9475 &self,
9476 other: &Rational,
9477 prec: u64,
9478 rm: RoundingMode,
9479 ) -> (Self, Ordering, i64) {
9480 rem_rational_helper(self, other, true, true, prec, rm)
9481 }
9482
9483 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
9484 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
9485 /// rounding the result to the nearest value of the specified precision. The [`Float`] and the
9486 /// [`Rational`] are both taken by value. An [`Ordering`] is also returned, indicating whether
9487 /// the rounded remainder is less than, equal to, or greater than the exact remainder, along
9488 /// with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
9489 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
9490 ///
9491 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
9492 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
9493 /// remainder by up to the quotient times the conversion error.
9494 ///
9495 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
9496 /// it equals $\pm(|q|\bmod 2^{63})$.
9497 ///
9498 /// $$
9499 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
9500 /// $$
9501 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
9502 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
9503 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
9504 ///
9505 /// Special cases:
9506 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
9507 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
9508 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
9509 /// - The quotient bits are 0 in all of the above special cases.
9510 ///
9511 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
9512 /// the minimum positive [`Float`]:
9513 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
9514 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
9515 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
9516 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
9517 ///
9518 /// # Worst-case complexity
9519 /// $T(n) = O(n \log n \log\log n)$
9520 ///
9521 /// $M(n) = O(n)$
9522 ///
9523 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
9524 /// other.significant_bits(), prec)`.
9525 ///
9526 /// # Panics
9527 /// Panics if `prec` is zero.
9528 ///
9529 /// # Examples
9530 /// ```
9531 /// use core::cmp::Ordering::*;
9532 /// use malachite_float::Float;
9533 /// use malachite_q::Rational;
9534 ///
9535 /// let x = Float::from(10u32);
9536 /// let y = Rational::from_signeds(22, 7);
9537 /// let (r, o, q) = x.ieee_remainder_rational_and_quotient_bits_prec(y, 5);
9538 /// assert_eq!(r.to_string(), "0.562");
9539 /// assert_eq!(o, Less);
9540 /// assert_eq!(q, 3);
9541 /// ```
9542 #[inline]
9543 pub fn ieee_remainder_rational_and_quotient_bits_prec(
9544 self,
9545 other: Rational,
9546 prec: u64,
9547 ) -> (Self, Ordering, i64) {
9548 self.ieee_remainder_rational_and_quotient_bits_prec_round(other, prec, Nearest)
9549 }
9550
9551 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
9552 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
9553 /// rounding the result to the nearest value of the specified precision. The [`Float`] is taken
9554 /// by value and the [`Rational`] by reference. An [`Ordering`] is also returned, indicating
9555 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder,
9556 /// along with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to
9557 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
9558 ///
9559 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
9560 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
9561 /// remainder by up to the quotient times the conversion error.
9562 ///
9563 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
9564 /// it equals $\pm(|q|\bmod 2^{63})$.
9565 ///
9566 /// $$
9567 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
9568 /// $$
9569 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
9570 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
9571 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
9572 ///
9573 /// Special cases:
9574 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
9575 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
9576 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
9577 /// - The quotient bits are 0 in all of the above special cases.
9578 ///
9579 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
9580 /// the minimum positive [`Float`]:
9581 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
9582 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
9583 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
9584 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
9585 ///
9586 /// # Worst-case complexity
9587 /// $T(n) = O(n \log n \log\log n)$
9588 ///
9589 /// $M(n) = O(n)$
9590 ///
9591 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
9592 /// other.significant_bits(), prec)`.
9593 ///
9594 /// # Panics
9595 /// Panics if `prec` is zero.
9596 ///
9597 /// # Examples
9598 /// ```
9599 /// use core::cmp::Ordering::*;
9600 /// use malachite_float::Float;
9601 /// use malachite_q::Rational;
9602 ///
9603 /// let x = Float::from(10u32);
9604 /// let y = Rational::from_signeds(22, 7);
9605 /// let (r, o, q) = x.ieee_remainder_rational_and_quotient_bits_prec_val_ref(&y, 5);
9606 /// assert_eq!(r.to_string(), "0.562");
9607 /// assert_eq!(o, Less);
9608 /// assert_eq!(q, 3);
9609 /// ```
9610 #[inline]
9611 pub fn ieee_remainder_rational_and_quotient_bits_prec_val_ref(
9612 self,
9613 other: &Rational,
9614 prec: u64,
9615 ) -> (Self, Ordering, i64) {
9616 self.ieee_remainder_rational_and_quotient_bits_prec_round_val_ref(other, prec, Nearest)
9617 }
9618
9619 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
9620 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
9621 /// rounding the result to the nearest value of the specified precision. The [`Float`] is taken
9622 /// by reference and the [`Rational`] by value. An [`Ordering`] is also returned, indicating
9623 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder,
9624 /// along with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to
9625 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
9626 ///
9627 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
9628 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
9629 /// remainder by up to the quotient times the conversion error.
9630 ///
9631 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
9632 /// it equals $\pm(|q|\bmod 2^{63})$.
9633 ///
9634 /// $$
9635 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
9636 /// $$
9637 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
9638 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
9639 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
9640 ///
9641 /// Special cases:
9642 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
9643 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
9644 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
9645 /// - The quotient bits are 0 in all of the above special cases.
9646 ///
9647 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
9648 /// the minimum positive [`Float`]:
9649 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
9650 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
9651 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
9652 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
9653 ///
9654 /// # Worst-case complexity
9655 /// $T(n) = O(n \log n \log\log n)$
9656 ///
9657 /// $M(n) = O(n)$
9658 ///
9659 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
9660 /// other.significant_bits(), prec)`.
9661 ///
9662 /// # Panics
9663 /// Panics if `prec` is zero.
9664 ///
9665 /// # Examples
9666 /// ```
9667 /// use core::cmp::Ordering::*;
9668 /// use malachite_float::Float;
9669 /// use malachite_q::Rational;
9670 ///
9671 /// let x = Float::from(10u32);
9672 /// let y = Rational::from_signeds(22, 7);
9673 /// let (r, o, q) = x.ieee_remainder_rational_and_quotient_bits_prec_ref_val(y, 5);
9674 /// assert_eq!(r.to_string(), "0.562");
9675 /// assert_eq!(o, Less);
9676 /// assert_eq!(q, 3);
9677 /// ```
9678 #[inline]
9679 pub fn ieee_remainder_rational_and_quotient_bits_prec_ref_val(
9680 &self,
9681 other: Rational,
9682 prec: u64,
9683 ) -> (Self, Ordering, i64) {
9684 self.ieee_remainder_rational_and_quotient_bits_prec_round_ref_val(other, prec, Nearest)
9685 }
9686
9687 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
9688 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
9689 /// rounding the result to the nearest value of the specified precision. The [`Float`] and the
9690 /// [`Rational`] are both taken by reference. An [`Ordering`] is also returned, indicating
9691 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder,
9692 /// along with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to
9693 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
9694 ///
9695 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
9696 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
9697 /// remainder by up to the quotient times the conversion error.
9698 ///
9699 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
9700 /// it equals $\pm(|q|\bmod 2^{63})$.
9701 ///
9702 /// $$
9703 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
9704 /// $$
9705 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
9706 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
9707 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
9708 ///
9709 /// Special cases:
9710 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
9711 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
9712 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
9713 /// - The quotient bits are 0 in all of the above special cases.
9714 ///
9715 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
9716 /// the minimum positive [`Float`]:
9717 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
9718 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
9719 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
9720 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
9721 ///
9722 /// # Worst-case complexity
9723 /// $T(n) = O(n \log n \log\log n)$
9724 ///
9725 /// $M(n) = O(n)$
9726 ///
9727 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
9728 /// other.significant_bits(), prec)`.
9729 ///
9730 /// # Panics
9731 /// Panics if `prec` is zero.
9732 ///
9733 /// # Examples
9734 /// ```
9735 /// use core::cmp::Ordering::*;
9736 /// use malachite_float::Float;
9737 /// use malachite_q::Rational;
9738 ///
9739 /// let x = Float::from(10u32);
9740 /// let y = Rational::from_signeds(22, 7);
9741 /// let (r, o, q) = x.ieee_remainder_rational_and_quotient_bits_prec_ref_ref(&y, 5);
9742 /// assert_eq!(r.to_string(), "0.562");
9743 /// assert_eq!(o, Less);
9744 /// assert_eq!(q, 3);
9745 /// ```
9746 #[inline]
9747 pub fn ieee_remainder_rational_and_quotient_bits_prec_ref_ref(
9748 &self,
9749 other: &Rational,
9750 prec: u64,
9751 ) -> (Self, Ordering, i64) {
9752 self.ieee_remainder_rational_and_quotient_bits_prec_round_ref_ref(other, prec, Nearest)
9753 }
9754
9755 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
9756 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
9757 /// rounding the result to the [`Float`]'s precision, with the specified rounding mode. The
9758 /// [`Float`] and the [`Rational`] are both taken by value. An [`Ordering`] is also returned,
9759 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
9760 /// remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s are not
9761 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
9762 ///
9763 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
9764 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
9765 /// remainder by up to the quotient times the conversion error.
9766 ///
9767 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
9768 /// it equals $\pm(|q|\bmod 2^{63})$.
9769 ///
9770 /// $$
9771 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
9772 /// $$
9773 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
9774 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
9775 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
9776 ///
9777 /// Special cases:
9778 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
9779 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
9780 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
9781 /// - The quotient bits are 0 in all of the above special cases.
9782 ///
9783 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
9784 /// the minimum positive [`Float`]:
9785 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
9786 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
9787 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
9788 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
9789 ///
9790 /// # Worst-case complexity
9791 /// $T(n) = O(n \log n \log\log n)$
9792 ///
9793 /// $M(n) = O(n)$
9794 ///
9795 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
9796 /// other.significant_bits())`.
9797 ///
9798 /// # Panics
9799 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
9800 /// precision.
9801 ///
9802 /// # Examples
9803 /// ```
9804 /// use core::cmp::Ordering::*;
9805 /// use malachite_base::rounding_modes::RoundingMode::*;
9806 /// use malachite_float::Float;
9807 /// use malachite_q::Rational;
9808 ///
9809 /// let x = Float::from(10u32);
9810 /// let y = Rational::from_signeds(22, 7);
9811 /// let (r, o, q) = x.ieee_remainder_rational_and_quotient_bits_round(y, Floor);
9812 /// assert_eq!(r.to_string(), "0.50");
9813 /// assert_eq!(o, Less);
9814 /// assert_eq!(q, 3);
9815 /// ```
9816 #[inline]
9817 pub fn ieee_remainder_rational_and_quotient_bits_round(
9818 self,
9819 other: Rational,
9820 rm: RoundingMode,
9821 ) -> (Self, Ordering, i64) {
9822 let prec = self.significant_bits();
9823 self.ieee_remainder_rational_and_quotient_bits_prec_round(other, prec, rm)
9824 }
9825
9826 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
9827 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
9828 /// rounding the result to the [`Float`]'s precision, with the specified rounding mode. The
9829 /// [`Float`] is taken by value and the [`Rational`] by reference. An [`Ordering`] is also
9830 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
9831 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
9832 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
9833 /// `Equal`.
9834 ///
9835 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
9836 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
9837 /// remainder by up to the quotient times the conversion error.
9838 ///
9839 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
9840 /// it equals $\pm(|q|\bmod 2^{63})$.
9841 ///
9842 /// $$
9843 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
9844 /// $$
9845 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
9846 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
9847 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
9848 ///
9849 /// Special cases:
9850 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
9851 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
9852 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
9853 /// - The quotient bits are 0 in all of the above special cases.
9854 ///
9855 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
9856 /// the minimum positive [`Float`]:
9857 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
9858 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
9859 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
9860 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
9861 ///
9862 /// # Worst-case complexity
9863 /// $T(n) = O(n \log n \log\log n)$
9864 ///
9865 /// $M(n) = O(n)$
9866 ///
9867 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
9868 /// other.significant_bits())`.
9869 ///
9870 /// # Panics
9871 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
9872 /// precision.
9873 ///
9874 /// # Examples
9875 /// ```
9876 /// use core::cmp::Ordering::*;
9877 /// use malachite_base::rounding_modes::RoundingMode::*;
9878 /// use malachite_float::Float;
9879 /// use malachite_q::Rational;
9880 ///
9881 /// let x = Float::from(10u32);
9882 /// let y = Rational::from_signeds(22, 7);
9883 /// let (r, o, q) = x.ieee_remainder_rational_and_quotient_bits_round_val_ref(&y, Floor);
9884 /// assert_eq!(r.to_string(), "0.50");
9885 /// assert_eq!(o, Less);
9886 /// assert_eq!(q, 3);
9887 /// ```
9888 #[inline]
9889 pub fn ieee_remainder_rational_and_quotient_bits_round_val_ref(
9890 self,
9891 other: &Rational,
9892 rm: RoundingMode,
9893 ) -> (Self, Ordering, i64) {
9894 let prec = self.significant_bits();
9895 self.ieee_remainder_rational_and_quotient_bits_prec_round_val_ref(other, prec, rm)
9896 }
9897
9898 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
9899 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
9900 /// rounding the result to the [`Float`]'s precision, with the specified rounding mode. The
9901 /// [`Float`] is taken by reference and the [`Rational`] by value. An [`Ordering`] is also
9902 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
9903 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
9904 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
9905 /// `Equal`.
9906 ///
9907 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
9908 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
9909 /// remainder by up to the quotient times the conversion error.
9910 ///
9911 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
9912 /// it equals $\pm(|q|\bmod 2^{63})$.
9913 ///
9914 /// $$
9915 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
9916 /// $$
9917 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
9918 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
9919 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
9920 ///
9921 /// Special cases:
9922 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
9923 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
9924 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
9925 /// - The quotient bits are 0 in all of the above special cases.
9926 ///
9927 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
9928 /// the minimum positive [`Float`]:
9929 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
9930 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
9931 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
9932 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
9933 ///
9934 /// # Worst-case complexity
9935 /// $T(n) = O(n \log n \log\log n)$
9936 ///
9937 /// $M(n) = O(n)$
9938 ///
9939 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
9940 /// other.significant_bits())`.
9941 ///
9942 /// # Panics
9943 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
9944 /// precision.
9945 ///
9946 /// # Examples
9947 /// ```
9948 /// use core::cmp::Ordering::*;
9949 /// use malachite_base::rounding_modes::RoundingMode::*;
9950 /// use malachite_float::Float;
9951 /// use malachite_q::Rational;
9952 ///
9953 /// let x = Float::from(10u32);
9954 /// let y = Rational::from_signeds(22, 7);
9955 /// let (r, o, q) = x.ieee_remainder_rational_and_quotient_bits_round_ref_val(y, Floor);
9956 /// assert_eq!(r.to_string(), "0.50");
9957 /// assert_eq!(o, Less);
9958 /// assert_eq!(q, 3);
9959 /// ```
9960 #[inline]
9961 pub fn ieee_remainder_rational_and_quotient_bits_round_ref_val(
9962 &self,
9963 other: Rational,
9964 rm: RoundingMode,
9965 ) -> (Self, Ordering, i64) {
9966 let prec = self.significant_bits();
9967 self.ieee_remainder_rational_and_quotient_bits_prec_round_ref_val(other, prec, rm)
9968 }
9969
9970 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
9971 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
9972 /// rounding the result to the [`Float`]'s precision, with the specified rounding mode. The
9973 /// [`Float`] and the [`Rational`] are both taken by reference. An [`Ordering`] is also
9974 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
9975 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
9976 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
9977 /// `Equal`.
9978 ///
9979 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
9980 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
9981 /// remainder by up to the quotient times the conversion error.
9982 ///
9983 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
9984 /// it equals $\pm(|q|\bmod 2^{63})$.
9985 ///
9986 /// $$
9987 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
9988 /// $$
9989 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
9990 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
9991 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
9992 ///
9993 /// Special cases:
9994 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
9995 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
9996 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
9997 /// - The quotient bits are 0 in all of the above special cases.
9998 ///
9999 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10000 /// the minimum positive [`Float`]:
10001 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10002 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10003 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10004 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10005 ///
10006 /// # Worst-case complexity
10007 /// $T(n) = O(n \log n \log\log n)$
10008 ///
10009 /// $M(n) = O(n)$
10010 ///
10011 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
10012 /// other.significant_bits())`.
10013 ///
10014 /// # Panics
10015 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
10016 /// precision.
10017 ///
10018 /// # Examples
10019 /// ```
10020 /// use core::cmp::Ordering::*;
10021 /// use malachite_base::rounding_modes::RoundingMode::*;
10022 /// use malachite_float::Float;
10023 /// use malachite_q::Rational;
10024 ///
10025 /// let x = Float::from(10u32);
10026 /// let y = Rational::from_signeds(22, 7);
10027 /// let (r, o, q) = x.ieee_remainder_rational_and_quotient_bits_round_ref_ref(&y, Floor);
10028 /// assert_eq!(r.to_string(), "0.50");
10029 /// assert_eq!(o, Less);
10030 /// assert_eq!(q, 3);
10031 /// ```
10032 #[inline]
10033 pub fn ieee_remainder_rational_and_quotient_bits_round_ref_ref(
10034 &self,
10035 other: &Rational,
10036 rm: RoundingMode,
10037 ) -> (Self, Ordering, i64) {
10038 let prec = self.significant_bits();
10039 self.ieee_remainder_rational_and_quotient_bits_prec_round_ref_ref(other, prec, rm)
10040 }
10041
10042 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
10043 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
10044 /// rounding the result to the nearest value of the [`Float`]'s precision. The [`Float`] and the
10045 /// [`Rational`] are both taken by value. An [`Ordering`] is also returned, indicating whether
10046 /// the rounded remainder is less than, equal to, or greater than the exact remainder, along
10047 /// with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
10048 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
10049 ///
10050 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
10051 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
10052 /// remainder by up to the quotient times the conversion error.
10053 ///
10054 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
10055 /// it equals $\pm(|q|\bmod 2^{63})$.
10056 ///
10057 /// $$
10058 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
10059 /// $$
10060 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
10061 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
10062 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
10063 ///
10064 /// Special cases:
10065 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
10066 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
10067 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
10068 /// - The quotient bits are 0 in all of the above special cases.
10069 ///
10070 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10071 /// the minimum positive [`Float`]:
10072 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10073 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10074 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10075 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10076 ///
10077 /// # Worst-case complexity
10078 /// $T(n) = O(n \log n \log\log n)$
10079 ///
10080 /// $M(n) = O(n)$
10081 ///
10082 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
10083 /// other.significant_bits())`.
10084 ///
10085 /// # Examples
10086 /// ```
10087 /// use core::cmp::Ordering::*;
10088 /// use malachite_float::Float;
10089 /// use malachite_q::Rational;
10090 ///
10091 /// let x = Float::from(10u32);
10092 /// let y = Rational::from_signeds(22, 7);
10093 /// let (r, o, q) = x.ieee_remainder_rational_and_quotient_bits(y);
10094 /// assert_eq!(r.to_string(), "0.62");
10095 /// assert_eq!(o, Greater);
10096 /// assert_eq!(q, 3);
10097 /// ```
10098 #[inline]
10099 pub fn ieee_remainder_rational_and_quotient_bits(
10100 self,
10101 other: Rational,
10102 ) -> (Self, Ordering, i64) {
10103 self.ieee_remainder_rational_and_quotient_bits_round(other, Nearest)
10104 }
10105
10106 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
10107 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
10108 /// rounding the result to the nearest value of the [`Float`]'s precision. The [`Float`] is
10109 /// taken by value and the [`Rational`] by reference. An [`Ordering`] is also returned,
10110 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
10111 /// remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s are not
10112 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
10113 ///
10114 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
10115 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
10116 /// remainder by up to the quotient times the conversion error.
10117 ///
10118 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
10119 /// it equals $\pm(|q|\bmod 2^{63})$.
10120 ///
10121 /// $$
10122 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
10123 /// $$
10124 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
10125 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
10126 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
10127 ///
10128 /// Special cases:
10129 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
10130 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
10131 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
10132 /// - The quotient bits are 0 in all of the above special cases.
10133 ///
10134 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10135 /// the minimum positive [`Float`]:
10136 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10137 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10138 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10139 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10140 ///
10141 /// # Worst-case complexity
10142 /// $T(n) = O(n \log n \log\log n)$
10143 ///
10144 /// $M(n) = O(n)$
10145 ///
10146 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
10147 /// other.significant_bits())`.
10148 ///
10149 /// # Examples
10150 /// ```
10151 /// use core::cmp::Ordering::*;
10152 /// use malachite_float::Float;
10153 /// use malachite_q::Rational;
10154 ///
10155 /// let x = Float::from(10u32);
10156 /// let y = Rational::from_signeds(22, 7);
10157 /// let (r, o, q) = x.ieee_remainder_rational_and_quotient_bits_val_ref(&y);
10158 /// assert_eq!(r.to_string(), "0.62");
10159 /// assert_eq!(o, Greater);
10160 /// assert_eq!(q, 3);
10161 /// ```
10162 #[inline]
10163 pub fn ieee_remainder_rational_and_quotient_bits_val_ref(
10164 self,
10165 other: &Rational,
10166 ) -> (Self, Ordering, i64) {
10167 self.ieee_remainder_rational_and_quotient_bits_round_val_ref(other, Nearest)
10168 }
10169
10170 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
10171 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
10172 /// rounding the result to the nearest value of the [`Float`]'s precision. The [`Float`] is
10173 /// taken by reference and the [`Rational`] by value. An [`Ordering`] is also returned,
10174 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
10175 /// remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s are not
10176 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
10177 ///
10178 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
10179 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
10180 /// remainder by up to the quotient times the conversion error.
10181 ///
10182 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
10183 /// it equals $\pm(|q|\bmod 2^{63})$.
10184 ///
10185 /// $$
10186 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
10187 /// $$
10188 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
10189 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
10190 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
10191 ///
10192 /// Special cases:
10193 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
10194 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
10195 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
10196 /// - The quotient bits are 0 in all of the above special cases.
10197 ///
10198 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10199 /// the minimum positive [`Float`]:
10200 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10201 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10202 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10203 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10204 ///
10205 /// # Worst-case complexity
10206 /// $T(n) = O(n \log n \log\log n)$
10207 ///
10208 /// $M(n) = O(n)$
10209 ///
10210 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
10211 /// other.significant_bits())`.
10212 ///
10213 /// # Examples
10214 /// ```
10215 /// use core::cmp::Ordering::*;
10216 /// use malachite_float::Float;
10217 /// use malachite_q::Rational;
10218 ///
10219 /// let x = Float::from(10u32);
10220 /// let y = Rational::from_signeds(22, 7);
10221 /// let (r, o, q) = x.ieee_remainder_rational_and_quotient_bits_ref_val(y);
10222 /// assert_eq!(r.to_string(), "0.62");
10223 /// assert_eq!(o, Greater);
10224 /// assert_eq!(q, 3);
10225 /// ```
10226 #[inline]
10227 pub fn ieee_remainder_rational_and_quotient_bits_ref_val(
10228 &self,
10229 other: Rational,
10230 ) -> (Self, Ordering, i64) {
10231 self.ieee_remainder_rational_and_quotient_bits_round_ref_val(other, Nearest)
10232 }
10233
10234 /// Computes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded to the
10235 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
10236 /// rounding the result to the nearest value of the [`Float`]'s precision. The [`Float`] and the
10237 /// [`Rational`] are both taken by reference. An [`Ordering`] is also returned, indicating
10238 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder,
10239 /// along with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to
10240 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
10241 ///
10242 /// The [`Rational`] modulus is used exactly, so the result is the correctly-rounded remainder
10243 /// of the exact input values. Converting the modulus to a [`Float`] first would perturb the
10244 /// remainder by up to the quotient times the conversion error.
10245 ///
10246 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
10247 /// it equals $\pm(|q|\bmod 2^{63})$.
10248 ///
10249 /// $$
10250 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
10251 /// $$
10252 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
10253 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
10254 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
10255 ///
10256 /// Special cases:
10257 /// - $f(\text{NaN},y,p)=f(\pm\infty,y,p)=f(x,0,p)=\text{NaN}$
10258 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y\neq 0$
10259 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
10260 /// - The quotient bits are 0 in all of the above special cases.
10261 ///
10262 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10263 /// the minimum positive [`Float`]:
10264 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10265 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10266 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10267 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10268 ///
10269 /// # Worst-case complexity
10270 /// $T(n) = O(n \log n \log\log n)$
10271 ///
10272 /// $M(n) = O(n)$
10273 ///
10274 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
10275 /// other.significant_bits())`.
10276 ///
10277 /// # Examples
10278 /// ```
10279 /// use core::cmp::Ordering::*;
10280 /// use malachite_float::Float;
10281 /// use malachite_q::Rational;
10282 ///
10283 /// let x = Float::from(10u32);
10284 /// let y = Rational::from_signeds(22, 7);
10285 /// let (r, o, q) = x.ieee_remainder_rational_and_quotient_bits_ref_ref(&y);
10286 /// assert_eq!(r.to_string(), "0.62");
10287 /// assert_eq!(o, Greater);
10288 /// assert_eq!(q, 3);
10289 /// ```
10290 #[inline]
10291 pub fn ieee_remainder_rational_and_quotient_bits_ref_ref(
10292 &self,
10293 other: &Rational,
10294 ) -> (Self, Ordering, i64) {
10295 self.ieee_remainder_rational_and_quotient_bits_round_ref_ref(other, Nearest)
10296 }
10297
10298 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
10299 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
10300 /// specified precision and with the specified rounding mode. The [`Rational`] and the [`Float`]
10301 /// are both taken by value. An [`Ordering`] is also returned, indicating whether the rounded
10302 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
10303 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
10304 /// `Equal`.
10305 ///
10306 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
10307 /// of the exact input values.
10308 ///
10309 /// $$
10310 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
10311 /// $$
10312 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
10313 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
10314 ///
10315 /// Special cases:
10316 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
10317 /// - $f(x,\pm\infty,p)=x$
10318 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
10319 /// result is a positive zero)
10320 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
10321 ///
10322 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10323 /// the minimum positive [`Float`]:
10324 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10325 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10326 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10327 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10328 ///
10329 /// # Worst-case complexity
10330 /// $T(n) = O(n \log n \log\log n)$
10331 ///
10332 /// $M(n) = O(n)$
10333 ///
10334 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
10335 /// y.complexity(), prec)`.
10336 ///
10337 /// # Panics
10338 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
10339 /// with `prec` bits.
10340 ///
10341 /// # Examples
10342 /// ```
10343 /// use core::cmp::Ordering::*;
10344 /// use malachite_base::rounding_modes::RoundingMode::*;
10345 /// use malachite_float::Float;
10346 /// use malachite_q::Rational;
10347 ///
10348 /// let a = Rational::from_signeds(22, 7);
10349 /// let b = Float::from(3u32);
10350 /// let (r, o) = Float::rational_rem_float_prec_round(a, b, 5, Floor);
10351 /// assert_eq!(r.to_string(), "0.141");
10352 /// assert_eq!(o, Less);
10353 /// ```
10354 #[allow(clippy::needless_pass_by_value)]
10355 pub fn rational_rem_float_prec_round(
10356 x: Rational,
10357 y: Self,
10358 prec: u64,
10359 rm: RoundingMode,
10360 ) -> (Self, Ordering) {
10361 let (r, o, _) = rational_rem_float_helper(&x, &y, false, false, prec, rm);
10362 (r, o)
10363 }
10364
10365 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
10366 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
10367 /// specified precision and with the specified rounding mode. The [`Rational`] is taken by value
10368 /// and the [`Float`] by reference. An [`Ordering`] is also returned, indicating whether the
10369 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
10370 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
10371 /// returns `Equal`.
10372 ///
10373 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
10374 /// of the exact input values.
10375 ///
10376 /// $$
10377 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
10378 /// $$
10379 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
10380 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
10381 ///
10382 /// Special cases:
10383 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
10384 /// - $f(x,\pm\infty,p)=x$
10385 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
10386 /// result is a positive zero)
10387 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
10388 ///
10389 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10390 /// the minimum positive [`Float`]:
10391 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10392 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10393 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10394 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10395 ///
10396 /// # Worst-case complexity
10397 /// $T(n) = O(n \log n \log\log n)$
10398 ///
10399 /// $M(n) = O(n)$
10400 ///
10401 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
10402 /// y.complexity(), prec)`.
10403 ///
10404 /// # Panics
10405 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
10406 /// with `prec` bits.
10407 ///
10408 /// # Examples
10409 /// ```
10410 /// use core::cmp::Ordering::*;
10411 /// use malachite_base::rounding_modes::RoundingMode::*;
10412 /// use malachite_float::Float;
10413 /// use malachite_q::Rational;
10414 ///
10415 /// let a = Rational::from_signeds(22, 7);
10416 /// let b = Float::from(3u32);
10417 /// let (r, o) = Float::rational_rem_float_prec_round_val_ref(a, &b, 5, Floor);
10418 /// assert_eq!(r.to_string(), "0.141");
10419 /// assert_eq!(o, Less);
10420 /// ```
10421 #[allow(clippy::needless_pass_by_value)]
10422 pub fn rational_rem_float_prec_round_val_ref(
10423 x: Rational,
10424 y: &Self,
10425 prec: u64,
10426 rm: RoundingMode,
10427 ) -> (Self, Ordering) {
10428 let (r, o, _) = rational_rem_float_helper(&x, y, false, false, prec, rm);
10429 (r, o)
10430 }
10431
10432 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
10433 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
10434 /// specified precision and with the specified rounding mode. The [`Rational`] is taken by
10435 /// reference and the [`Float`] by value. An [`Ordering`] is also returned, indicating whether
10436 /// the rounded remainder is less than, equal to, or greater than the exact remainder. Although
10437 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
10438 /// returns `Equal`.
10439 ///
10440 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
10441 /// of the exact input values.
10442 ///
10443 /// $$
10444 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
10445 /// $$
10446 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
10447 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
10448 ///
10449 /// Special cases:
10450 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
10451 /// - $f(x,\pm\infty,p)=x$
10452 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
10453 /// result is a positive zero)
10454 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
10455 ///
10456 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10457 /// the minimum positive [`Float`]:
10458 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10459 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10460 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10461 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10462 ///
10463 /// # Worst-case complexity
10464 /// $T(n) = O(n \log n \log\log n)$
10465 ///
10466 /// $M(n) = O(n)$
10467 ///
10468 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
10469 /// y.complexity(), prec)`.
10470 ///
10471 /// # Panics
10472 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
10473 /// with `prec` bits.
10474 ///
10475 /// # Examples
10476 /// ```
10477 /// use core::cmp::Ordering::*;
10478 /// use malachite_base::rounding_modes::RoundingMode::*;
10479 /// use malachite_float::Float;
10480 /// use malachite_q::Rational;
10481 ///
10482 /// let a = Rational::from_signeds(22, 7);
10483 /// let b = Float::from(3u32);
10484 /// let (r, o) = Float::rational_rem_float_prec_round_ref_val(&a, b, 5, Floor);
10485 /// assert_eq!(r.to_string(), "0.141");
10486 /// assert_eq!(o, Less);
10487 /// ```
10488 #[allow(clippy::needless_pass_by_value)]
10489 pub fn rational_rem_float_prec_round_ref_val(
10490 x: &Rational,
10491 y: Self,
10492 prec: u64,
10493 rm: RoundingMode,
10494 ) -> (Self, Ordering) {
10495 let (r, o, _) = rational_rem_float_helper(x, &y, false, false, prec, rm);
10496 (r, o)
10497 }
10498
10499 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
10500 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
10501 /// specified precision and with the specified rounding mode. The [`Rational`] and the [`Float`]
10502 /// are both taken by reference. An [`Ordering`] is also returned, indicating whether the
10503 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
10504 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
10505 /// returns `Equal`.
10506 ///
10507 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
10508 /// of the exact input values.
10509 ///
10510 /// $$
10511 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
10512 /// $$
10513 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
10514 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
10515 ///
10516 /// Special cases:
10517 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
10518 /// - $f(x,\pm\infty,p)=x$
10519 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
10520 /// result is a positive zero)
10521 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
10522 ///
10523 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10524 /// the minimum positive [`Float`]:
10525 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10526 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10527 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10528 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10529 ///
10530 /// # Worst-case complexity
10531 /// $T(n) = O(n \log n \log\log n)$
10532 ///
10533 /// $M(n) = O(n)$
10534 ///
10535 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
10536 /// y.complexity(), prec)`.
10537 ///
10538 /// # Panics
10539 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
10540 /// with `prec` bits.
10541 ///
10542 /// # Examples
10543 /// ```
10544 /// use core::cmp::Ordering::*;
10545 /// use malachite_base::rounding_modes::RoundingMode::*;
10546 /// use malachite_float::Float;
10547 /// use malachite_q::Rational;
10548 ///
10549 /// let a = Rational::from_signeds(22, 7);
10550 /// let b = Float::from(3u32);
10551 /// let (r, o) = Float::rational_rem_float_prec_round_ref_ref(&a, &b, 5, Floor);
10552 /// assert_eq!(r.to_string(), "0.141");
10553 /// assert_eq!(o, Less);
10554 /// ```
10555 pub fn rational_rem_float_prec_round_ref_ref(
10556 x: &Rational,
10557 y: &Self,
10558 prec: u64,
10559 rm: RoundingMode,
10560 ) -> (Self, Ordering) {
10561 let (r, o, _) = rational_rem_float_helper(x, y, false, false, prec, rm);
10562 (r, o)
10563 }
10564
10565 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
10566 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
10567 /// nearest value of the specified precision. The [`Rational`] and the [`Float`] are both taken
10568 /// by value. An [`Ordering`] is also returned, indicating whether the rounded remainder is less
10569 /// than, equal to, or greater than the exact remainder. Although `NaN`s are not comparable to
10570 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
10571 ///
10572 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
10573 /// of the exact input values.
10574 ///
10575 /// $$
10576 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
10577 /// $$
10578 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
10579 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
10580 ///
10581 /// Special cases:
10582 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
10583 /// - $f(x,\pm\infty,p)=x$
10584 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
10585 /// result is a positive zero)
10586 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
10587 ///
10588 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10589 /// the minimum positive [`Float`]:
10590 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10591 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10592 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10593 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10594 ///
10595 /// # Worst-case complexity
10596 /// $T(n) = O(n \log n \log\log n)$
10597 ///
10598 /// $M(n) = O(n)$
10599 ///
10600 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
10601 /// y.complexity(), prec)`.
10602 ///
10603 /// # Panics
10604 /// Panics if `prec` is zero.
10605 ///
10606 /// # Examples
10607 /// ```
10608 /// use core::cmp::Ordering::*;
10609 /// use malachite_float::Float;
10610 /// use malachite_q::Rational;
10611 ///
10612 /// let (r, o) =
10613 /// Float::rational_rem_float_prec(Rational::from_signeds(22, 7), Float::from(3u32), 5);
10614 /// assert_eq!(r.to_string(), "0.141");
10615 /// assert_eq!(o, Less);
10616 /// ```
10617 #[inline]
10618 pub fn rational_rem_float_prec(x: Rational, y: Self, prec: u64) -> (Self, Ordering) {
10619 Self::rational_rem_float_prec_round(x, y, prec, Nearest)
10620 }
10621
10622 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
10623 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
10624 /// nearest value of the specified precision. The [`Rational`] is taken by value and the
10625 /// [`Float`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
10626 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
10627 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
10628 /// `Equal`.
10629 ///
10630 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
10631 /// of the exact input values.
10632 ///
10633 /// $$
10634 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
10635 /// $$
10636 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
10637 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
10638 ///
10639 /// Special cases:
10640 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
10641 /// - $f(x,\pm\infty,p)=x$
10642 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
10643 /// result is a positive zero)
10644 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
10645 ///
10646 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10647 /// the minimum positive [`Float`]:
10648 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10649 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10650 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10651 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10652 ///
10653 /// # Worst-case complexity
10654 /// $T(n) = O(n \log n \log\log n)$
10655 ///
10656 /// $M(n) = O(n)$
10657 ///
10658 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
10659 /// y.complexity(), prec)`.
10660 ///
10661 /// # Panics
10662 /// Panics if `prec` is zero.
10663 ///
10664 /// # Examples
10665 /// ```
10666 /// use core::cmp::Ordering::*;
10667 /// use malachite_float::Float;
10668 /// use malachite_q::Rational;
10669 ///
10670 /// let a = Rational::from_signeds(22, 7);
10671 /// let b = Float::from(3u32);
10672 /// let (r, o) = Float::rational_rem_float_prec_val_ref(a, &b, 5);
10673 /// assert_eq!(r.to_string(), "0.141");
10674 /// assert_eq!(o, Less);
10675 /// ```
10676 #[inline]
10677 pub fn rational_rem_float_prec_val_ref(x: Rational, y: &Self, prec: u64) -> (Self, Ordering) {
10678 Self::rational_rem_float_prec_round_val_ref(x, y, prec, Nearest)
10679 }
10680
10681 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
10682 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
10683 /// nearest value of the specified precision. The [`Rational`] is taken by reference and the
10684 /// [`Float`] by value. An [`Ordering`] is also returned, indicating whether the rounded
10685 /// remainder is less than, equal to, or greater than the exact remainder. Although `NaN`s are
10686 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
10687 /// `Equal`.
10688 ///
10689 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
10690 /// of the exact input values.
10691 ///
10692 /// $$
10693 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
10694 /// $$
10695 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
10696 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
10697 ///
10698 /// Special cases:
10699 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
10700 /// - $f(x,\pm\infty,p)=x$
10701 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
10702 /// result is a positive zero)
10703 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
10704 ///
10705 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10706 /// the minimum positive [`Float`]:
10707 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10708 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10709 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10710 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10711 ///
10712 /// # Worst-case complexity
10713 /// $T(n) = O(n \log n \log\log n)$
10714 ///
10715 /// $M(n) = O(n)$
10716 ///
10717 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
10718 /// y.complexity(), prec)`.
10719 ///
10720 /// # Panics
10721 /// Panics if `prec` is zero.
10722 ///
10723 /// # Examples
10724 /// ```
10725 /// use core::cmp::Ordering::*;
10726 /// use malachite_float::Float;
10727 /// use malachite_q::Rational;
10728 ///
10729 /// let a = Rational::from_signeds(22, 7);
10730 /// let b = Float::from(3u32);
10731 /// let (r, o) = Float::rational_rem_float_prec_ref_val(&a, b, 5);
10732 /// assert_eq!(r.to_string(), "0.141");
10733 /// assert_eq!(o, Less);
10734 /// ```
10735 #[inline]
10736 pub fn rational_rem_float_prec_ref_val(x: &Rational, y: Self, prec: u64) -> (Self, Ordering) {
10737 Self::rational_rem_float_prec_round_ref_val(x, y, prec, Nearest)
10738 }
10739
10740 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
10741 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
10742 /// nearest value of the specified precision. The [`Rational`] and the [`Float`] are both taken
10743 /// by reference. An [`Ordering`] is also returned, indicating whether the rounded remainder is
10744 /// less than, equal to, or greater than the exact remainder. Although `NaN`s are not comparable
10745 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
10746 ///
10747 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
10748 /// of the exact input values.
10749 ///
10750 /// $$
10751 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
10752 /// $$
10753 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
10754 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
10755 ///
10756 /// Special cases:
10757 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
10758 /// - $f(x,\pm\infty,p)=x$
10759 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
10760 /// result is a positive zero)
10761 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
10762 ///
10763 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10764 /// the minimum positive [`Float`]:
10765 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10766 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10767 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10768 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10769 ///
10770 /// # Worst-case complexity
10771 /// $T(n) = O(n \log n \log\log n)$
10772 ///
10773 /// $M(n) = O(n)$
10774 ///
10775 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
10776 /// y.complexity(), prec)`.
10777 ///
10778 /// # Panics
10779 /// Panics if `prec` is zero.
10780 ///
10781 /// # Examples
10782 /// ```
10783 /// use core::cmp::Ordering::*;
10784 /// use malachite_float::Float;
10785 /// use malachite_q::Rational;
10786 ///
10787 /// let a = Rational::from_signeds(22, 7);
10788 /// let b = Float::from(3u32);
10789 /// let (r, o) = Float::rational_rem_float_prec_ref_ref(&a, &b, 5);
10790 /// assert_eq!(r.to_string(), "0.141");
10791 /// assert_eq!(o, Less);
10792 /// ```
10793 #[inline]
10794 pub fn rational_rem_float_prec_ref_ref(x: &Rational, y: &Self, prec: u64) -> (Self, Ordering) {
10795 Self::rational_rem_float_prec_round_ref_ref(x, y, prec, Nearest)
10796 }
10797
10798 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
10799 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
10800 /// [`Float`] modulus's precision, with the specified rounding mode. The [`Rational`] and the
10801 /// [`Float`] are both taken by value. An [`Ordering`] is also returned, indicating whether the
10802 /// rounded remainder is less than, equal to, or greater than the exact remainder. Although
10803 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
10804 /// returns `Equal`.
10805 ///
10806 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
10807 /// of the exact input values.
10808 ///
10809 /// $$
10810 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
10811 /// $$
10812 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
10813 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
10814 ///
10815 /// Special cases:
10816 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
10817 /// - $f(x,\pm\infty,p)=x$
10818 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
10819 /// result is a positive zero)
10820 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
10821 ///
10822 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10823 /// the minimum positive [`Float`]:
10824 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10825 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10826 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10827 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10828 ///
10829 /// # Worst-case complexity
10830 /// $T(n) = O(n \log n \log\log n)$
10831 ///
10832 /// $M(n) = O(n)$
10833 ///
10834 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
10835 /// y.complexity())`.
10836 ///
10837 /// # Panics
10838 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
10839 /// precision.
10840 ///
10841 /// # Examples
10842 /// ```
10843 /// use core::cmp::Ordering::*;
10844 /// use malachite_base::rounding_modes::RoundingMode::*;
10845 /// use malachite_float::Float;
10846 /// use malachite_q::Rational;
10847 ///
10848 /// let a = Rational::from_signeds(22, 7);
10849 /// let b = Float::from(3u32);
10850 /// let (r, o) = Float::rational_rem_float_round(a, b, Floor);
10851 /// assert_eq!(r.to_string(), "0.12");
10852 /// assert_eq!(o, Less);
10853 /// ```
10854 #[inline]
10855 pub fn rational_rem_float_round(x: Rational, y: Self, rm: RoundingMode) -> (Self, Ordering) {
10856 let prec = y.significant_bits();
10857 Self::rational_rem_float_prec_round(x, y, prec, rm)
10858 }
10859
10860 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
10861 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
10862 /// [`Float`] modulus's precision, with the specified rounding mode. The [`Rational`] is taken
10863 /// by value and the [`Float`] by reference. An [`Ordering`] is also returned, indicating
10864 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder.
10865 /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
10866 /// it also returns `Equal`.
10867 ///
10868 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
10869 /// of the exact input values.
10870 ///
10871 /// $$
10872 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
10873 /// $$
10874 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
10875 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
10876 ///
10877 /// Special cases:
10878 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
10879 /// - $f(x,\pm\infty,p)=x$
10880 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
10881 /// result is a positive zero)
10882 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
10883 ///
10884 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10885 /// the minimum positive [`Float`]:
10886 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10887 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10888 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10889 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10890 ///
10891 /// # Worst-case complexity
10892 /// $T(n) = O(n \log n \log\log n)$
10893 ///
10894 /// $M(n) = O(n)$
10895 ///
10896 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
10897 /// y.complexity())`.
10898 ///
10899 /// # Panics
10900 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
10901 /// precision.
10902 ///
10903 /// # Examples
10904 /// ```
10905 /// use core::cmp::Ordering::*;
10906 /// use malachite_base::rounding_modes::RoundingMode::*;
10907 /// use malachite_float::Float;
10908 /// use malachite_q::Rational;
10909 ///
10910 /// let a = Rational::from_signeds(22, 7);
10911 /// let b = Float::from(3u32);
10912 /// let (r, o) = Float::rational_rem_float_round_val_ref(a, &b, Floor);
10913 /// assert_eq!(r.to_string(), "0.12");
10914 /// assert_eq!(o, Less);
10915 /// ```
10916 #[inline]
10917 pub fn rational_rem_float_round_val_ref(
10918 x: Rational,
10919 y: &Self,
10920 rm: RoundingMode,
10921 ) -> (Self, Ordering) {
10922 let prec = y.significant_bits();
10923 Self::rational_rem_float_prec_round_val_ref(x, y, prec, rm)
10924 }
10925
10926 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
10927 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
10928 /// [`Float`] modulus's precision, with the specified rounding mode. The [`Rational`] is taken
10929 /// by reference and the [`Float`] by value. An [`Ordering`] is also returned, indicating
10930 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder.
10931 /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
10932 /// it also returns `Equal`.
10933 ///
10934 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
10935 /// of the exact input values.
10936 ///
10937 /// $$
10938 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
10939 /// $$
10940 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
10941 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
10942 ///
10943 /// Special cases:
10944 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
10945 /// - $f(x,\pm\infty,p)=x$
10946 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
10947 /// result is a positive zero)
10948 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
10949 ///
10950 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
10951 /// the minimum positive [`Float`]:
10952 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
10953 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
10954 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
10955 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
10956 ///
10957 /// # Worst-case complexity
10958 /// $T(n) = O(n \log n \log\log n)$
10959 ///
10960 /// $M(n) = O(n)$
10961 ///
10962 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
10963 /// y.complexity())`.
10964 ///
10965 /// # Panics
10966 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
10967 /// precision.
10968 ///
10969 /// # Examples
10970 /// ```
10971 /// use core::cmp::Ordering::*;
10972 /// use malachite_base::rounding_modes::RoundingMode::*;
10973 /// use malachite_float::Float;
10974 /// use malachite_q::Rational;
10975 ///
10976 /// let a = Rational::from_signeds(22, 7);
10977 /// let b = Float::from(3u32);
10978 /// let (r, o) = Float::rational_rem_float_round_ref_val(&a, b, Floor);
10979 /// assert_eq!(r.to_string(), "0.12");
10980 /// assert_eq!(o, Less);
10981 /// ```
10982 #[inline]
10983 pub fn rational_rem_float_round_ref_val(
10984 x: &Rational,
10985 y: Self,
10986 rm: RoundingMode,
10987 ) -> (Self, Ordering) {
10988 let prec = y.significant_bits();
10989 Self::rational_rem_float_prec_round_ref_val(x, y, prec, rm)
10990 }
10991
10992 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
10993 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
10994 /// [`Float`] modulus's precision, with the specified rounding mode. The [`Rational`] and the
10995 /// [`Float`] are both taken by reference. An [`Ordering`] is also returned, indicating whether
10996 /// the rounded remainder is less than, equal to, or greater than the exact remainder. Although
10997 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
10998 /// returns `Equal`.
10999 ///
11000 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
11001 /// of the exact input values.
11002 ///
11003 /// $$
11004 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
11005 /// $$
11006 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
11007 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
11008 ///
11009 /// Special cases:
11010 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
11011 /// - $f(x,\pm\infty,p)=x$
11012 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
11013 /// result is a positive zero)
11014 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
11015 ///
11016 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
11017 /// the minimum positive [`Float`]:
11018 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
11019 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
11020 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
11021 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
11022 ///
11023 /// # Worst-case complexity
11024 /// $T(n) = O(n \log n \log\log n)$
11025 ///
11026 /// $M(n) = O(n)$
11027 ///
11028 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
11029 /// y.complexity())`.
11030 ///
11031 /// # Panics
11032 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
11033 /// precision.
11034 ///
11035 /// # Examples
11036 /// ```
11037 /// use core::cmp::Ordering::*;
11038 /// use malachite_base::rounding_modes::RoundingMode::*;
11039 /// use malachite_float::Float;
11040 /// use malachite_q::Rational;
11041 ///
11042 /// let a = Rational::from_signeds(22, 7);
11043 /// let b = Float::from(3u32);
11044 /// let (r, o) = Float::rational_rem_float_round_ref_ref(&a, &b, Floor);
11045 /// assert_eq!(r.to_string(), "0.12");
11046 /// assert_eq!(o, Less);
11047 /// ```
11048 #[inline]
11049 pub fn rational_rem_float_round_ref_ref(
11050 x: &Rational,
11051 y: &Self,
11052 rm: RoundingMode,
11053 ) -> (Self, Ordering) {
11054 let prec = y.significant_bits();
11055 Self::rational_rem_float_prec_round_ref_ref(x, y, prec, rm)
11056 }
11057
11058 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
11059 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
11060 /// specified precision and with the specified rounding mode. The [`Rational`] and the [`Float`]
11061 /// are both taken by value. An [`Ordering`] is also returned, indicating whether the rounded
11062 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
11063 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
11064 /// whenever this function returns a `NaN` it also returns `Equal`.
11065 ///
11066 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
11067 /// of the exact input values.
11068 ///
11069 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
11070 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
11071 /// [`Float`]-[`Float`] functions.
11072 ///
11073 /// $$
11074 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
11075 /// $$
11076 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
11077 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
11078 ///
11079 /// Special cases:
11080 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
11081 /// - $f(x,\pm\infty,p)=x$
11082 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
11083 /// result is a positive zero)
11084 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
11085 /// - The quotient bits are 0 in all of the above special cases.
11086 ///
11087 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
11088 /// the minimum positive [`Float`]:
11089 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
11090 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
11091 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
11092 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
11093 ///
11094 /// # Worst-case complexity
11095 /// $T(n) = O(n \log n \log\log n)$
11096 ///
11097 /// $M(n) = O(n)$
11098 ///
11099 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
11100 /// y.complexity(), prec)`.
11101 ///
11102 /// # Panics
11103 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
11104 /// with `prec` bits.
11105 ///
11106 /// # Examples
11107 /// ```
11108 /// use core::cmp::Ordering::*;
11109 /// use malachite_base::rounding_modes::RoundingMode::*;
11110 /// use malachite_float::Float;
11111 /// use malachite_q::Rational;
11112 ///
11113 /// let a = Rational::from_signeds(22, 7);
11114 /// let b = Float::from(3u32);
11115 /// let (r, o, q) = Float::rational_rem_float_and_quotient_bits_prec_round(a, b, 5, Floor);
11116 /// assert_eq!(r.to_string(), "0.141");
11117 /// assert_eq!(o, Less);
11118 /// assert_eq!(q, 1);
11119 /// ```
11120 #[allow(clippy::needless_pass_by_value)]
11121 #[inline]
11122 pub fn rational_rem_float_and_quotient_bits_prec_round(
11123 x: Rational,
11124 y: Self,
11125 prec: u64,
11126 rm: RoundingMode,
11127 ) -> (Self, Ordering, i64) {
11128 rational_rem_float_helper(&x, &y, false, true, prec, rm)
11129 }
11130
11131 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
11132 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
11133 /// specified precision and with the specified rounding mode. The [`Rational`] is taken by value
11134 /// and the [`Float`] by reference. An [`Ordering`] is also returned, indicating whether the
11135 /// rounded remainder is less than, equal to, or greater than the exact remainder, along with
11136 /// the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
11137 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
11138 ///
11139 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
11140 /// of the exact input values.
11141 ///
11142 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
11143 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
11144 /// [`Float`]-[`Float`] functions.
11145 ///
11146 /// $$
11147 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
11148 /// $$
11149 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
11150 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
11151 ///
11152 /// Special cases:
11153 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
11154 /// - $f(x,\pm\infty,p)=x$
11155 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
11156 /// result is a positive zero)
11157 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
11158 /// - The quotient bits are 0 in all of the above special cases.
11159 ///
11160 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
11161 /// the minimum positive [`Float`]:
11162 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
11163 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
11164 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
11165 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
11166 ///
11167 /// # Worst-case complexity
11168 /// $T(n) = O(n \log n \log\log n)$
11169 ///
11170 /// $M(n) = O(n)$
11171 ///
11172 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
11173 /// y.complexity(), prec)`.
11174 ///
11175 /// # Panics
11176 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
11177 /// with `prec` bits.
11178 ///
11179 /// # Examples
11180 /// ```
11181 /// use core::cmp::Ordering::*;
11182 /// use malachite_base::rounding_modes::RoundingMode::*;
11183 /// use malachite_float::Float;
11184 /// use malachite_q::Rational;
11185 ///
11186 /// let a = Rational::from_signeds(22, 7);
11187 /// let b = Float::from(3u32);
11188 /// let (r, o, q) =
11189 /// Float::rational_rem_float_and_quotient_bits_prec_round_val_ref(a, &b, 5, Floor);
11190 /// assert_eq!(r.to_string(), "0.141");
11191 /// assert_eq!(o, Less);
11192 /// assert_eq!(q, 1);
11193 /// ```
11194 #[allow(clippy::needless_pass_by_value)]
11195 #[inline]
11196 pub fn rational_rem_float_and_quotient_bits_prec_round_val_ref(
11197 x: Rational,
11198 y: &Self,
11199 prec: u64,
11200 rm: RoundingMode,
11201 ) -> (Self, Ordering, i64) {
11202 rational_rem_float_helper(&x, y, false, true, prec, rm)
11203 }
11204
11205 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
11206 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
11207 /// specified precision and with the specified rounding mode. The [`Rational`] is taken by
11208 /// reference and the [`Float`] by value. An [`Ordering`] is also returned, indicating whether
11209 /// the rounded remainder is less than, equal to, or greater than the exact remainder, along
11210 /// with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
11211 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
11212 ///
11213 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
11214 /// of the exact input values.
11215 ///
11216 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
11217 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
11218 /// [`Float`]-[`Float`] functions.
11219 ///
11220 /// $$
11221 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
11222 /// $$
11223 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
11224 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
11225 ///
11226 /// Special cases:
11227 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
11228 /// - $f(x,\pm\infty,p)=x$
11229 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
11230 /// result is a positive zero)
11231 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
11232 /// - The quotient bits are 0 in all of the above special cases.
11233 ///
11234 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
11235 /// the minimum positive [`Float`]:
11236 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
11237 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
11238 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
11239 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
11240 ///
11241 /// # Worst-case complexity
11242 /// $T(n) = O(n \log n \log\log n)$
11243 ///
11244 /// $M(n) = O(n)$
11245 ///
11246 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
11247 /// y.complexity(), prec)`.
11248 ///
11249 /// # Panics
11250 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
11251 /// with `prec` bits.
11252 ///
11253 /// # Examples
11254 /// ```
11255 /// use core::cmp::Ordering::*;
11256 /// use malachite_base::rounding_modes::RoundingMode::*;
11257 /// use malachite_float::Float;
11258 /// use malachite_q::Rational;
11259 ///
11260 /// let a = Rational::from_signeds(22, 7);
11261 /// let b = Float::from(3u32);
11262 /// let (r, o, q) =
11263 /// Float::rational_rem_float_and_quotient_bits_prec_round_ref_val(&a, b, 5, Floor);
11264 /// assert_eq!(r.to_string(), "0.141");
11265 /// assert_eq!(o, Less);
11266 /// assert_eq!(q, 1);
11267 /// ```
11268 #[allow(clippy::needless_pass_by_value)]
11269 #[inline]
11270 pub fn rational_rem_float_and_quotient_bits_prec_round_ref_val(
11271 x: &Rational,
11272 y: Self,
11273 prec: u64,
11274 rm: RoundingMode,
11275 ) -> (Self, Ordering, i64) {
11276 rational_rem_float_helper(x, &y, false, true, prec, rm)
11277 }
11278
11279 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
11280 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
11281 /// specified precision and with the specified rounding mode. The [`Rational`] and the [`Float`]
11282 /// are both taken by reference. An [`Ordering`] is also returned, indicating whether the
11283 /// rounded remainder is less than, equal to, or greater than the exact remainder, along with
11284 /// the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
11285 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
11286 ///
11287 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
11288 /// of the exact input values.
11289 ///
11290 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
11291 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
11292 /// [`Float`]-[`Float`] functions.
11293 ///
11294 /// $$
11295 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
11296 /// $$
11297 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
11298 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
11299 ///
11300 /// Special cases:
11301 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
11302 /// - $f(x,\pm\infty,p)=x$
11303 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
11304 /// result is a positive zero)
11305 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
11306 /// - The quotient bits are 0 in all of the above special cases.
11307 ///
11308 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
11309 /// the minimum positive [`Float`]:
11310 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
11311 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
11312 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
11313 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
11314 ///
11315 /// # Worst-case complexity
11316 /// $T(n) = O(n \log n \log\log n)$
11317 ///
11318 /// $M(n) = O(n)$
11319 ///
11320 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
11321 /// y.complexity(), prec)`.
11322 ///
11323 /// # Panics
11324 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
11325 /// with `prec` bits.
11326 ///
11327 /// # Examples
11328 /// ```
11329 /// use core::cmp::Ordering::*;
11330 /// use malachite_base::rounding_modes::RoundingMode::*;
11331 /// use malachite_float::Float;
11332 /// use malachite_q::Rational;
11333 ///
11334 /// let a = Rational::from_signeds(22, 7);
11335 /// let b = Float::from(3u32);
11336 /// let (r, o, q) =
11337 /// Float::rational_rem_float_and_quotient_bits_prec_round_ref_ref(&a, &b, 5, Floor);
11338 /// assert_eq!(r.to_string(), "0.141");
11339 /// assert_eq!(o, Less);
11340 /// assert_eq!(q, 1);
11341 /// ```
11342 #[inline]
11343 pub fn rational_rem_float_and_quotient_bits_prec_round_ref_ref(
11344 x: &Rational,
11345 y: &Self,
11346 prec: u64,
11347 rm: RoundingMode,
11348 ) -> (Self, Ordering, i64) {
11349 rational_rem_float_helper(x, y, false, true, prec, rm)
11350 }
11351
11352 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
11353 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
11354 /// nearest value of the specified precision. The [`Rational`] and the [`Float`] are both taken
11355 /// by value. An [`Ordering`] is also returned, indicating whether the rounded remainder is less
11356 /// than, equal to, or greater than the exact remainder, along with the low bits of the quotient
11357 /// as an `i64`. Although `NaN`s are not comparable to any [`Float`], whenever this function
11358 /// returns a `NaN` it also returns `Equal`.
11359 ///
11360 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
11361 /// of the exact input values.
11362 ///
11363 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
11364 /// it equals $\pm(|q|\bmod 2^{63})$.
11365 ///
11366 /// $$
11367 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
11368 /// $$
11369 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
11370 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
11371 ///
11372 /// Special cases:
11373 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
11374 /// - $f(x,\pm\infty,p)=x$
11375 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
11376 /// result is a positive zero)
11377 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
11378 /// - The quotient bits are 0 in all of the above special cases.
11379 ///
11380 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
11381 /// the minimum positive [`Float`]:
11382 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
11383 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
11384 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
11385 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
11386 ///
11387 /// # Worst-case complexity
11388 /// $T(n) = O(n \log n \log\log n)$
11389 ///
11390 /// $M(n) = O(n)$
11391 ///
11392 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
11393 /// y.complexity(), prec)`.
11394 ///
11395 /// # Panics
11396 /// Panics if `prec` is zero.
11397 ///
11398 /// # Examples
11399 /// ```
11400 /// use core::cmp::Ordering::*;
11401 /// use malachite_float::Float;
11402 /// use malachite_q::Rational;
11403 ///
11404 /// let a = Rational::from_signeds(22, 7);
11405 /// let b = Float::from(3u32);
11406 /// let (r, o, q) = Float::rational_rem_float_and_quotient_bits_prec(a, b, 5);
11407 /// assert_eq!(r.to_string(), "0.141");
11408 /// assert_eq!(o, Less);
11409 /// assert_eq!(q, 1);
11410 /// ```
11411 #[inline]
11412 pub fn rational_rem_float_and_quotient_bits_prec(
11413 x: Rational,
11414 y: Self,
11415 prec: u64,
11416 ) -> (Self, Ordering, i64) {
11417 Self::rational_rem_float_and_quotient_bits_prec_round(x, y, prec, Nearest)
11418 }
11419
11420 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
11421 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
11422 /// nearest value of the specified precision. The [`Rational`] is taken by value and the
11423 /// [`Float`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
11424 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
11425 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
11426 /// whenever this function returns a `NaN` it also returns `Equal`.
11427 ///
11428 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
11429 /// of the exact input values.
11430 ///
11431 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
11432 /// it equals $\pm(|q|\bmod 2^{63})$.
11433 ///
11434 /// $$
11435 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
11436 /// $$
11437 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
11438 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
11439 ///
11440 /// Special cases:
11441 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
11442 /// - $f(x,\pm\infty,p)=x$
11443 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
11444 /// result is a positive zero)
11445 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
11446 /// - The quotient bits are 0 in all of the above special cases.
11447 ///
11448 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
11449 /// the minimum positive [`Float`]:
11450 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
11451 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
11452 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
11453 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
11454 ///
11455 /// # Worst-case complexity
11456 /// $T(n) = O(n \log n \log\log n)$
11457 ///
11458 /// $M(n) = O(n)$
11459 ///
11460 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
11461 /// y.complexity(), prec)`.
11462 ///
11463 /// # Panics
11464 /// Panics if `prec` is zero.
11465 ///
11466 /// # Examples
11467 /// ```
11468 /// use core::cmp::Ordering::*;
11469 /// use malachite_float::Float;
11470 /// use malachite_q::Rational;
11471 ///
11472 /// let a = Rational::from_signeds(22, 7);
11473 /// let b = Float::from(3u32);
11474 /// let (r, o, q) = Float::rational_rem_float_and_quotient_bits_prec_val_ref(a, &b, 5);
11475 /// assert_eq!(r.to_string(), "0.141");
11476 /// assert_eq!(o, Less);
11477 /// assert_eq!(q, 1);
11478 /// ```
11479 #[inline]
11480 pub fn rational_rem_float_and_quotient_bits_prec_val_ref(
11481 x: Rational,
11482 y: &Self,
11483 prec: u64,
11484 ) -> (Self, Ordering, i64) {
11485 Self::rational_rem_float_and_quotient_bits_prec_round_val_ref(x, y, prec, Nearest)
11486 }
11487
11488 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
11489 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
11490 /// nearest value of the specified precision. The [`Rational`] is taken by reference and the
11491 /// [`Float`] by value. An [`Ordering`] is also returned, indicating whether the rounded
11492 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
11493 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
11494 /// whenever this function returns a `NaN` it also returns `Equal`.
11495 ///
11496 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
11497 /// of the exact input values.
11498 ///
11499 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
11500 /// it equals $\pm(|q|\bmod 2^{63})$.
11501 ///
11502 /// $$
11503 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
11504 /// $$
11505 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
11506 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
11507 ///
11508 /// Special cases:
11509 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
11510 /// - $f(x,\pm\infty,p)=x$
11511 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
11512 /// result is a positive zero)
11513 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
11514 /// - The quotient bits are 0 in all of the above special cases.
11515 ///
11516 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
11517 /// the minimum positive [`Float`]:
11518 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
11519 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
11520 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
11521 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
11522 ///
11523 /// # Worst-case complexity
11524 /// $T(n) = O(n \log n \log\log n)$
11525 ///
11526 /// $M(n) = O(n)$
11527 ///
11528 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
11529 /// y.complexity(), prec)`.
11530 ///
11531 /// # Panics
11532 /// Panics if `prec` is zero.
11533 ///
11534 /// # Examples
11535 /// ```
11536 /// use core::cmp::Ordering::*;
11537 /// use malachite_float::Float;
11538 /// use malachite_q::Rational;
11539 ///
11540 /// let a = Rational::from_signeds(22, 7);
11541 /// let b = Float::from(3u32);
11542 /// let (r, o, q) = Float::rational_rem_float_and_quotient_bits_prec_ref_val(&a, b, 5);
11543 /// assert_eq!(r.to_string(), "0.141");
11544 /// assert_eq!(o, Less);
11545 /// assert_eq!(q, 1);
11546 /// ```
11547 #[inline]
11548 pub fn rational_rem_float_and_quotient_bits_prec_ref_val(
11549 x: &Rational,
11550 y: Self,
11551 prec: u64,
11552 ) -> (Self, Ordering, i64) {
11553 Self::rational_rem_float_and_quotient_bits_prec_round_ref_val(x, y, prec, Nearest)
11554 }
11555
11556 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
11557 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
11558 /// nearest value of the specified precision. The [`Rational`] and the [`Float`] are both taken
11559 /// by reference. An [`Ordering`] is also returned, indicating whether the rounded remainder is
11560 /// less than, equal to, or greater than the exact remainder, along with the low bits of the
11561 /// quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`], whenever this
11562 /// function returns a `NaN` it also returns `Equal`.
11563 ///
11564 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
11565 /// of the exact input values.
11566 ///
11567 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
11568 /// it equals $\pm(|q|\bmod 2^{63})$.
11569 ///
11570 /// $$
11571 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
11572 /// $$
11573 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
11574 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
11575 ///
11576 /// Special cases:
11577 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
11578 /// - $f(x,\pm\infty,p)=x$
11579 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
11580 /// result is a positive zero)
11581 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
11582 /// - The quotient bits are 0 in all of the above special cases.
11583 ///
11584 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
11585 /// the minimum positive [`Float`]:
11586 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
11587 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
11588 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
11589 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
11590 ///
11591 /// # Worst-case complexity
11592 /// $T(n) = O(n \log n \log\log n)$
11593 ///
11594 /// $M(n) = O(n)$
11595 ///
11596 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
11597 /// y.complexity(), prec)`.
11598 ///
11599 /// # Panics
11600 /// Panics if `prec` is zero.
11601 ///
11602 /// # Examples
11603 /// ```
11604 /// use core::cmp::Ordering::*;
11605 /// use malachite_float::Float;
11606 /// use malachite_q::Rational;
11607 ///
11608 /// let a = Rational::from_signeds(22, 7);
11609 /// let b = Float::from(3u32);
11610 /// let (r, o, q) = Float::rational_rem_float_and_quotient_bits_prec_ref_ref(&a, &b, 5);
11611 /// assert_eq!(r.to_string(), "0.141");
11612 /// assert_eq!(o, Less);
11613 /// assert_eq!(q, 1);
11614 /// ```
11615 #[inline]
11616 pub fn rational_rem_float_and_quotient_bits_prec_ref_ref(
11617 x: &Rational,
11618 y: &Self,
11619 prec: u64,
11620 ) -> (Self, Ordering, i64) {
11621 Self::rational_rem_float_and_quotient_bits_prec_round_ref_ref(x, y, prec, Nearest)
11622 }
11623
11624 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
11625 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
11626 /// [`Float`] modulus's precision, with the specified rounding mode. The [`Rational`] and the
11627 /// [`Float`] are both taken by value. An [`Ordering`] is also returned, indicating whether the
11628 /// rounded remainder is less than, equal to, or greater than the exact remainder, along with
11629 /// the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
11630 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
11631 ///
11632 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
11633 /// of the exact input values.
11634 ///
11635 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
11636 /// it equals $\pm(|q|\bmod 2^{63})$.
11637 ///
11638 /// $$
11639 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
11640 /// $$
11641 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
11642 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
11643 ///
11644 /// Special cases:
11645 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
11646 /// - $f(x,\pm\infty,p)=x$
11647 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
11648 /// result is a positive zero)
11649 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
11650 /// - The quotient bits are 0 in all of the above special cases.
11651 ///
11652 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
11653 /// the minimum positive [`Float`]:
11654 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
11655 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
11656 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
11657 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
11658 ///
11659 /// # Worst-case complexity
11660 /// $T(n) = O(n \log n \log\log n)$
11661 ///
11662 /// $M(n) = O(n)$
11663 ///
11664 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
11665 /// y.complexity())`.
11666 ///
11667 /// # Panics
11668 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
11669 /// precision.
11670 ///
11671 /// # Examples
11672 /// ```
11673 /// use core::cmp::Ordering::*;
11674 /// use malachite_base::rounding_modes::RoundingMode::*;
11675 /// use malachite_float::Float;
11676 /// use malachite_q::Rational;
11677 ///
11678 /// let a = Rational::from_signeds(22, 7);
11679 /// let b = Float::from(3u32);
11680 /// let (r, o, q) = Float::rational_rem_float_and_quotient_bits_round(a, b, Floor);
11681 /// assert_eq!(r.to_string(), "0.12");
11682 /// assert_eq!(o, Less);
11683 /// assert_eq!(q, 1);
11684 /// ```
11685 #[inline]
11686 pub fn rational_rem_float_and_quotient_bits_round(
11687 x: Rational,
11688 y: Self,
11689 rm: RoundingMode,
11690 ) -> (Self, Ordering, i64) {
11691 let prec = y.significant_bits();
11692 Self::rational_rem_float_and_quotient_bits_prec_round(x, y, prec, rm)
11693 }
11694
11695 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
11696 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
11697 /// [`Float`] modulus's precision, with the specified rounding mode. The [`Rational`] is taken
11698 /// by value and the [`Float`] by reference. An [`Ordering`] is also returned, indicating
11699 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder,
11700 /// along with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to
11701 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
11702 ///
11703 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
11704 /// of the exact input values.
11705 ///
11706 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
11707 /// it equals $\pm(|q|\bmod 2^{63})$.
11708 ///
11709 /// $$
11710 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
11711 /// $$
11712 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
11713 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
11714 ///
11715 /// Special cases:
11716 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
11717 /// - $f(x,\pm\infty,p)=x$
11718 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
11719 /// result is a positive zero)
11720 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
11721 /// - The quotient bits are 0 in all of the above special cases.
11722 ///
11723 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
11724 /// the minimum positive [`Float`]:
11725 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
11726 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
11727 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
11728 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
11729 ///
11730 /// # Worst-case complexity
11731 /// $T(n) = O(n \log n \log\log n)$
11732 ///
11733 /// $M(n) = O(n)$
11734 ///
11735 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
11736 /// y.complexity())`.
11737 ///
11738 /// # Panics
11739 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
11740 /// precision.
11741 ///
11742 /// # Examples
11743 /// ```
11744 /// use core::cmp::Ordering::*;
11745 /// use malachite_base::rounding_modes::RoundingMode::*;
11746 /// use malachite_float::Float;
11747 /// use malachite_q::Rational;
11748 ///
11749 /// let a = Rational::from_signeds(22, 7);
11750 /// let b = Float::from(3u32);
11751 /// let (r, o, q) = Float::rational_rem_float_and_quotient_bits_round_val_ref(a, &b, Floor);
11752 /// assert_eq!(r.to_string(), "0.12");
11753 /// assert_eq!(o, Less);
11754 /// assert_eq!(q, 1);
11755 /// ```
11756 #[inline]
11757 pub fn rational_rem_float_and_quotient_bits_round_val_ref(
11758 x: Rational,
11759 y: &Self,
11760 rm: RoundingMode,
11761 ) -> (Self, Ordering, i64) {
11762 let prec = y.significant_bits();
11763 Self::rational_rem_float_and_quotient_bits_prec_round_val_ref(x, y, prec, rm)
11764 }
11765
11766 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
11767 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
11768 /// [`Float`] modulus's precision, with the specified rounding mode. The [`Rational`] is taken
11769 /// by reference and the [`Float`] by value. An [`Ordering`] is also returned, indicating
11770 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder,
11771 /// along with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to
11772 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
11773 ///
11774 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
11775 /// of the exact input values.
11776 ///
11777 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
11778 /// it equals $\pm(|q|\bmod 2^{63})$.
11779 ///
11780 /// $$
11781 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
11782 /// $$
11783 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
11784 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
11785 ///
11786 /// Special cases:
11787 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
11788 /// - $f(x,\pm\infty,p)=x$
11789 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
11790 /// result is a positive zero)
11791 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
11792 /// - The quotient bits are 0 in all of the above special cases.
11793 ///
11794 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
11795 /// the minimum positive [`Float`]:
11796 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
11797 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
11798 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
11799 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
11800 ///
11801 /// # Worst-case complexity
11802 /// $T(n) = O(n \log n \log\log n)$
11803 ///
11804 /// $M(n) = O(n)$
11805 ///
11806 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
11807 /// y.complexity())`.
11808 ///
11809 /// # Panics
11810 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
11811 /// precision.
11812 ///
11813 /// # Examples
11814 /// ```
11815 /// use core::cmp::Ordering::*;
11816 /// use malachite_base::rounding_modes::RoundingMode::*;
11817 /// use malachite_float::Float;
11818 /// use malachite_q::Rational;
11819 ///
11820 /// let a = Rational::from_signeds(22, 7);
11821 /// let b = Float::from(3u32);
11822 /// let (r, o, q) = Float::rational_rem_float_and_quotient_bits_round_ref_val(&a, b, Floor);
11823 /// assert_eq!(r.to_string(), "0.12");
11824 /// assert_eq!(o, Less);
11825 /// assert_eq!(q, 1);
11826 /// ```
11827 #[inline]
11828 pub fn rational_rem_float_and_quotient_bits_round_ref_val(
11829 x: &Rational,
11830 y: Self,
11831 rm: RoundingMode,
11832 ) -> (Self, Ordering, i64) {
11833 let prec = y.significant_bits();
11834 Self::rational_rem_float_and_quotient_bits_prec_round_ref_val(x, y, prec, rm)
11835 }
11836
11837 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
11838 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
11839 /// [`Float`] modulus's precision, with the specified rounding mode. The [`Rational`] and the
11840 /// [`Float`] are both taken by reference. An [`Ordering`] is also returned, indicating whether
11841 /// the rounded remainder is less than, equal to, or greater than the exact remainder, along
11842 /// with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
11843 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
11844 ///
11845 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
11846 /// of the exact input values.
11847 ///
11848 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
11849 /// it equals $\pm(|q|\bmod 2^{63})$.
11850 ///
11851 /// $$
11852 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
11853 /// $$
11854 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
11855 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p+1}$.
11856 ///
11857 /// Special cases:
11858 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
11859 /// - $f(x,\pm\infty,p)=x$
11860 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
11861 /// result is a positive zero)
11862 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
11863 /// - The quotient bits are 0 in all of the above special cases.
11864 ///
11865 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
11866 /// the minimum positive [`Float`]:
11867 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
11868 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
11869 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
11870 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
11871 ///
11872 /// # Worst-case complexity
11873 /// $T(n) = O(n \log n \log\log n)$
11874 ///
11875 /// $M(n) = O(n)$
11876 ///
11877 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
11878 /// y.complexity())`.
11879 ///
11880 /// # Panics
11881 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
11882 /// precision.
11883 ///
11884 /// # Examples
11885 /// ```
11886 /// use core::cmp::Ordering::*;
11887 /// use malachite_base::rounding_modes::RoundingMode::*;
11888 /// use malachite_float::Float;
11889 /// use malachite_q::Rational;
11890 ///
11891 /// let a = Rational::from_signeds(22, 7);
11892 /// let b = Float::from(3u32);
11893 /// let (r, o, q) = Float::rational_rem_float_and_quotient_bits_round_ref_ref(&a, &b, Floor);
11894 /// assert_eq!(r.to_string(), "0.12");
11895 /// assert_eq!(o, Less);
11896 /// assert_eq!(q, 1);
11897 /// ```
11898 #[inline]
11899 pub fn rational_rem_float_and_quotient_bits_round_ref_ref(
11900 x: &Rational,
11901 y: &Self,
11902 rm: RoundingMode,
11903 ) -> (Self, Ordering, i64) {
11904 let prec = y.significant_bits();
11905 Self::rational_rem_float_and_quotient_bits_prec_round_ref_ref(x, y, prec, rm)
11906 }
11907
11908 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
11909 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
11910 /// nearest value of the [`Float`] modulus's precision. The [`Rational`] and the [`Float`] are
11911 /// both taken by value. An [`Ordering`] is also returned, indicating whether the rounded
11912 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
11913 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
11914 /// whenever this function returns a `NaN` it also returns `Equal`.
11915 ///
11916 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
11917 /// of the exact input values.
11918 ///
11919 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
11920 /// it equals $\pm(|q|\bmod 2^{63})$.
11921 ///
11922 /// $$
11923 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
11924 /// $$
11925 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
11926 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
11927 ///
11928 /// Special cases:
11929 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
11930 /// - $f(x,\pm\infty,p)=x$
11931 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
11932 /// result is a positive zero)
11933 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
11934 /// - The quotient bits are 0 in all of the above special cases.
11935 ///
11936 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
11937 /// the minimum positive [`Float`]:
11938 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
11939 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
11940 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
11941 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
11942 ///
11943 /// # Worst-case complexity
11944 /// $T(n) = O(n \log n \log\log n)$
11945 ///
11946 /// $M(n) = O(n)$
11947 ///
11948 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
11949 /// y.complexity())`.
11950 ///
11951 /// # Examples
11952 /// ```
11953 /// use core::cmp::Ordering::*;
11954 /// use malachite_float::Float;
11955 /// use malachite_q::Rational;
11956 ///
11957 /// let a = Rational::from_signeds(22, 7);
11958 /// let b = Float::from(3u32);
11959 /// let (r, o, q) = Float::rational_rem_float_and_quotient_bits(a, b);
11960 /// assert_eq!(r.to_string(), "0.12");
11961 /// assert_eq!(o, Less);
11962 /// assert_eq!(q, 1);
11963 /// ```
11964 #[inline]
11965 pub fn rational_rem_float_and_quotient_bits(x: Rational, y: Self) -> (Self, Ordering, i64) {
11966 Self::rational_rem_float_and_quotient_bits_round(x, y, Nearest)
11967 }
11968
11969 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
11970 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
11971 /// nearest value of the [`Float`] modulus's precision. The [`Rational`] is taken by value and
11972 /// the [`Float`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
11973 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
11974 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
11975 /// whenever this function returns a `NaN` it also returns `Equal`.
11976 ///
11977 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
11978 /// of the exact input values.
11979 ///
11980 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
11981 /// it equals $\pm(|q|\bmod 2^{63})$.
11982 ///
11983 /// $$
11984 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
11985 /// $$
11986 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
11987 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
11988 ///
11989 /// Special cases:
11990 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
11991 /// - $f(x,\pm\infty,p)=x$
11992 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
11993 /// result is a positive zero)
11994 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
11995 /// - The quotient bits are 0 in all of the above special cases.
11996 ///
11997 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
11998 /// the minimum positive [`Float`]:
11999 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12000 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12001 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12002 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12003 ///
12004 /// # Worst-case complexity
12005 /// $T(n) = O(n \log n \log\log n)$
12006 ///
12007 /// $M(n) = O(n)$
12008 ///
12009 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12010 /// y.complexity())`.
12011 ///
12012 /// # Examples
12013 /// ```
12014 /// use core::cmp::Ordering::*;
12015 /// use malachite_float::Float;
12016 /// use malachite_q::Rational;
12017 ///
12018 /// let a = Rational::from_signeds(22, 7);
12019 /// let b = Float::from(3u32);
12020 /// let (r, o, q) = Float::rational_rem_float_and_quotient_bits_val_ref(a, &b);
12021 /// assert_eq!(r.to_string(), "0.12");
12022 /// assert_eq!(o, Less);
12023 /// assert_eq!(q, 1);
12024 /// ```
12025 #[inline]
12026 pub fn rational_rem_float_and_quotient_bits_val_ref(
12027 x: Rational,
12028 y: &Self,
12029 ) -> (Self, Ordering, i64) {
12030 Self::rational_rem_float_and_quotient_bits_round_val_ref(x, y, Nearest)
12031 }
12032
12033 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
12034 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
12035 /// nearest value of the [`Float`] modulus's precision. The [`Rational`] is taken by reference
12036 /// and the [`Float`] by value. An [`Ordering`] is also returned, indicating whether the rounded
12037 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
12038 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
12039 /// whenever this function returns a `NaN` it also returns `Equal`.
12040 ///
12041 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
12042 /// of the exact input values.
12043 ///
12044 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
12045 /// it equals $\pm(|q|\bmod 2^{63})$.
12046 ///
12047 /// $$
12048 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
12049 /// $$
12050 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
12051 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
12052 ///
12053 /// Special cases:
12054 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
12055 /// - $f(x,\pm\infty,p)=x$
12056 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
12057 /// result is a positive zero)
12058 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
12059 /// - The quotient bits are 0 in all of the above special cases.
12060 ///
12061 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
12062 /// the minimum positive [`Float`]:
12063 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12064 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12065 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12066 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12067 ///
12068 /// # Worst-case complexity
12069 /// $T(n) = O(n \log n \log\log n)$
12070 ///
12071 /// $M(n) = O(n)$
12072 ///
12073 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12074 /// y.complexity())`.
12075 ///
12076 /// # Examples
12077 /// ```
12078 /// use core::cmp::Ordering::*;
12079 /// use malachite_float::Float;
12080 /// use malachite_q::Rational;
12081 ///
12082 /// let a = Rational::from_signeds(22, 7);
12083 /// let b = Float::from(3u32);
12084 /// let (r, o, q) = Float::rational_rem_float_and_quotient_bits_ref_val(&a, b);
12085 /// assert_eq!(r.to_string(), "0.12");
12086 /// assert_eq!(o, Less);
12087 /// assert_eq!(q, 1);
12088 /// ```
12089 #[inline]
12090 pub fn rational_rem_float_and_quotient_bits_ref_val(
12091 x: &Rational,
12092 y: Self,
12093 ) -> (Self, Ordering, i64) {
12094 Self::rational_rem_float_and_quotient_bits_round_ref_val(x, y, Nearest)
12095 }
12096
12097 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward
12098 /// zero, as for the `%` operator on primitive floats and C's `fmod`, rounding the result to the
12099 /// nearest value of the [`Float`] modulus's precision. The [`Rational`] and the [`Float`] are
12100 /// both taken by reference. An [`Ordering`] is also returned, indicating whether the rounded
12101 /// remainder is less than, equal to, or greater than the exact remainder, along with the low
12102 /// bits of the quotient as an `i64`. Although `NaN`s are not comparable to any [`Float`],
12103 /// whenever this function returns a `NaN` it also returns `Equal`.
12104 ///
12105 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
12106 /// of the exact input values.
12107 ///
12108 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
12109 /// it equals $\pm(|q|\bmod 2^{63})$.
12110 ///
12111 /// $$
12112 /// f(x,y,p) = x - y\operatorname{trunc}(x/y) + \varepsilon.
12113 /// $$
12114 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
12115 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$.
12116 ///
12117 /// Special cases:
12118 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
12119 /// - $f(x,\pm\infty,p)=x$
12120 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
12121 /// result is a positive zero)
12122 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
12123 /// - The quotient bits are 0 in all of the above special cases.
12124 ///
12125 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
12126 /// the minimum positive [`Float`]:
12127 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12128 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12129 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12130 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12131 ///
12132 /// # Worst-case complexity
12133 /// $T(n) = O(n \log n \log\log n)$
12134 ///
12135 /// $M(n) = O(n)$
12136 ///
12137 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12138 /// y.complexity())`.
12139 ///
12140 /// # Examples
12141 /// ```
12142 /// use core::cmp::Ordering::*;
12143 /// use malachite_float::Float;
12144 /// use malachite_q::Rational;
12145 ///
12146 /// let a = Rational::from_signeds(22, 7);
12147 /// let b = Float::from(3u32);
12148 /// let (r, o, q) = Float::rational_rem_float_and_quotient_bits_ref_ref(&a, &b);
12149 /// assert_eq!(r.to_string(), "0.12");
12150 /// assert_eq!(o, Less);
12151 /// assert_eq!(q, 1);
12152 /// ```
12153 #[inline]
12154 pub fn rational_rem_float_and_quotient_bits_ref_ref(
12155 x: &Rational,
12156 y: &Self,
12157 ) -> (Self, Ordering, i64) {
12158 Self::rational_rem_float_and_quotient_bits_round_ref_ref(x, y, Nearest)
12159 }
12160
12161 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
12162 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
12163 /// rounding the result to the specified precision and with the specified rounding mode. The
12164 /// [`Rational`] and the [`Float`] are both taken by value. An [`Ordering`] is also returned,
12165 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
12166 /// remainder. Although `NaN`s are not comparable to any [`Float`], whenever this function
12167 /// returns a `NaN` it also returns `Equal`.
12168 ///
12169 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
12170 /// of the exact input values.
12171 ///
12172 /// $$
12173 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
12174 /// $$
12175 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
12176 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
12177 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
12178 ///
12179 /// Special cases:
12180 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
12181 /// - $f(x,\pm\infty,p)=x$
12182 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
12183 /// result is a positive zero)
12184 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
12185 ///
12186 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
12187 /// the minimum positive [`Float`]:
12188 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12189 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12190 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12191 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12192 ///
12193 /// # Worst-case complexity
12194 /// $T(n) = O(n \log n \log\log n)$
12195 ///
12196 /// $M(n) = O(n)$
12197 ///
12198 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12199 /// y.complexity(), prec)`.
12200 ///
12201 /// # Panics
12202 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
12203 /// with `prec` bits.
12204 ///
12205 /// # Examples
12206 /// ```
12207 /// use core::cmp::Ordering::*;
12208 /// use malachite_base::rounding_modes::RoundingMode::*;
12209 /// use malachite_float::Float;
12210 /// use malachite_q::Rational;
12211 ///
12212 /// let a = Rational::from_signeds(22, 7);
12213 /// let b = Float::from(3u32);
12214 /// let (r, o) = Float::rational_ieee_remainder_float_prec_round(a, b, 5, Floor);
12215 /// assert_eq!(r.to_string(), "0.141");
12216 /// assert_eq!(o, Less);
12217 /// ```
12218 #[allow(clippy::needless_pass_by_value)]
12219 pub fn rational_ieee_remainder_float_prec_round(
12220 x: Rational,
12221 y: Self,
12222 prec: u64,
12223 rm: RoundingMode,
12224 ) -> (Self, Ordering) {
12225 let (r, o, _) = rational_rem_float_helper(&x, &y, true, false, prec, rm);
12226 (r, o)
12227 }
12228
12229 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
12230 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
12231 /// rounding the result to the specified precision and with the specified rounding mode. The
12232 /// [`Rational`] is taken by value and the [`Float`] by reference. An [`Ordering`] is also
12233 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
12234 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
12235 /// function returns a `NaN` it also returns `Equal`.
12236 ///
12237 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
12238 /// of the exact input values.
12239 ///
12240 /// $$
12241 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
12242 /// $$
12243 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
12244 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
12245 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
12246 ///
12247 /// Special cases:
12248 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
12249 /// - $f(x,\pm\infty,p)=x$
12250 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
12251 /// result is a positive zero)
12252 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
12253 ///
12254 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
12255 /// the minimum positive [`Float`]:
12256 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12257 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12258 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12259 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12260 ///
12261 /// # Worst-case complexity
12262 /// $T(n) = O(n \log n \log\log n)$
12263 ///
12264 /// $M(n) = O(n)$
12265 ///
12266 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12267 /// y.complexity(), prec)`.
12268 ///
12269 /// # Panics
12270 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
12271 /// with `prec` bits.
12272 ///
12273 /// # Examples
12274 /// ```
12275 /// use core::cmp::Ordering::*;
12276 /// use malachite_base::rounding_modes::RoundingMode::*;
12277 /// use malachite_float::Float;
12278 /// use malachite_q::Rational;
12279 ///
12280 /// let a = Rational::from_signeds(22, 7);
12281 /// let b = Float::from(3u32);
12282 /// let (r, o) = Float::rational_ieee_remainder_float_prec_round_val_ref(a, &b, 5, Floor);
12283 /// assert_eq!(r.to_string(), "0.141");
12284 /// assert_eq!(o, Less);
12285 /// ```
12286 #[allow(clippy::needless_pass_by_value)]
12287 pub fn rational_ieee_remainder_float_prec_round_val_ref(
12288 x: Rational,
12289 y: &Self,
12290 prec: u64,
12291 rm: RoundingMode,
12292 ) -> (Self, Ordering) {
12293 let (r, o, _) = rational_rem_float_helper(&x, y, true, false, prec, rm);
12294 (r, o)
12295 }
12296
12297 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
12298 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
12299 /// rounding the result to the specified precision and with the specified rounding mode. The
12300 /// [`Rational`] is taken by reference and the [`Float`] by value. An [`Ordering`] is also
12301 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
12302 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
12303 /// function returns a `NaN` it also returns `Equal`.
12304 ///
12305 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
12306 /// of the exact input values.
12307 ///
12308 /// $$
12309 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
12310 /// $$
12311 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
12312 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
12313 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
12314 ///
12315 /// Special cases:
12316 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
12317 /// - $f(x,\pm\infty,p)=x$
12318 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
12319 /// result is a positive zero)
12320 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
12321 ///
12322 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
12323 /// the minimum positive [`Float`]:
12324 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12325 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12326 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12327 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12328 ///
12329 /// # Worst-case complexity
12330 /// $T(n) = O(n \log n \log\log n)$
12331 ///
12332 /// $M(n) = O(n)$
12333 ///
12334 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12335 /// y.complexity(), prec)`.
12336 ///
12337 /// # Panics
12338 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
12339 /// with `prec` bits.
12340 ///
12341 /// # Examples
12342 /// ```
12343 /// use core::cmp::Ordering::*;
12344 /// use malachite_base::rounding_modes::RoundingMode::*;
12345 /// use malachite_float::Float;
12346 /// use malachite_q::Rational;
12347 ///
12348 /// let a = Rational::from_signeds(22, 7);
12349 /// let b = Float::from(3u32);
12350 /// let (r, o) = Float::rational_ieee_remainder_float_prec_round_ref_val(&a, b, 5, Floor);
12351 /// assert_eq!(r.to_string(), "0.141");
12352 /// assert_eq!(o, Less);
12353 /// ```
12354 #[allow(clippy::needless_pass_by_value)]
12355 pub fn rational_ieee_remainder_float_prec_round_ref_val(
12356 x: &Rational,
12357 y: Self,
12358 prec: u64,
12359 rm: RoundingMode,
12360 ) -> (Self, Ordering) {
12361 let (r, o, _) = rational_rem_float_helper(x, &y, true, false, prec, rm);
12362 (r, o)
12363 }
12364
12365 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
12366 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
12367 /// rounding the result to the specified precision and with the specified rounding mode. The
12368 /// [`Rational`] and the [`Float`] are both taken by reference. An [`Ordering`] is also
12369 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
12370 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
12371 /// function returns a `NaN` it also returns `Equal`.
12372 ///
12373 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
12374 /// of the exact input values.
12375 ///
12376 /// $$
12377 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
12378 /// $$
12379 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
12380 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
12381 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
12382 ///
12383 /// Special cases:
12384 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
12385 /// - $f(x,\pm\infty,p)=x$
12386 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
12387 /// result is a positive zero)
12388 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
12389 ///
12390 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
12391 /// the minimum positive [`Float`]:
12392 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12393 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12394 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12395 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12396 ///
12397 /// # Worst-case complexity
12398 /// $T(n) = O(n \log n \log\log n)$
12399 ///
12400 /// $M(n) = O(n)$
12401 ///
12402 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12403 /// y.complexity(), prec)`.
12404 ///
12405 /// # Panics
12406 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
12407 /// with `prec` bits.
12408 ///
12409 /// # Examples
12410 /// ```
12411 /// use core::cmp::Ordering::*;
12412 /// use malachite_base::rounding_modes::RoundingMode::*;
12413 /// use malachite_float::Float;
12414 /// use malachite_q::Rational;
12415 ///
12416 /// let a = Rational::from_signeds(22, 7);
12417 /// let b = Float::from(3u32);
12418 /// let (r, o) = Float::rational_ieee_remainder_float_prec_round_ref_ref(&a, &b, 5, Floor);
12419 /// assert_eq!(r.to_string(), "0.141");
12420 /// assert_eq!(o, Less);
12421 /// ```
12422 pub fn rational_ieee_remainder_float_prec_round_ref_ref(
12423 x: &Rational,
12424 y: &Self,
12425 prec: u64,
12426 rm: RoundingMode,
12427 ) -> (Self, Ordering) {
12428 let (r, o, _) = rational_rem_float_helper(x, y, true, false, prec, rm);
12429 (r, o)
12430 }
12431
12432 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
12433 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
12434 /// rounding the result to the nearest value of the specified precision. The [`Rational`] and
12435 /// the [`Float`] are both taken by value. An [`Ordering`] is also returned, indicating whether
12436 /// the rounded remainder is less than, equal to, or greater than the exact remainder. Although
12437 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
12438 /// returns `Equal`.
12439 ///
12440 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
12441 /// of the exact input values.
12442 ///
12443 /// $$
12444 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
12445 /// $$
12446 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
12447 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
12448 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
12449 ///
12450 /// Special cases:
12451 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
12452 /// - $f(x,\pm\infty,p)=x$
12453 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
12454 /// result is a positive zero)
12455 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
12456 ///
12457 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
12458 /// the minimum positive [`Float`]:
12459 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12460 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12461 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12462 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12463 ///
12464 /// # Worst-case complexity
12465 /// $T(n) = O(n \log n \log\log n)$
12466 ///
12467 /// $M(n) = O(n)$
12468 ///
12469 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12470 /// y.complexity(), prec)`.
12471 ///
12472 /// # Panics
12473 /// Panics if `prec` is zero.
12474 ///
12475 /// # Examples
12476 /// ```
12477 /// use core::cmp::Ordering::*;
12478 /// use malachite_float::Float;
12479 /// use malachite_q::Rational;
12480 ///
12481 /// let a = Rational::from_signeds(22, 7);
12482 /// let b = Float::from(3u32);
12483 /// let (r, o) = Float::rational_ieee_remainder_float_prec(a, b, 5);
12484 /// assert_eq!(r.to_string(), "0.141");
12485 /// assert_eq!(o, Less);
12486 /// ```
12487 #[inline]
12488 pub fn rational_ieee_remainder_float_prec(x: Rational, y: Self, prec: u64) -> (Self, Ordering) {
12489 Self::rational_ieee_remainder_float_prec_round(x, y, prec, Nearest)
12490 }
12491
12492 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
12493 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
12494 /// rounding the result to the nearest value of the specified precision. The [`Rational`] is
12495 /// taken by value and the [`Float`] by reference. An [`Ordering`] is also returned, indicating
12496 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder.
12497 /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
12498 /// it also returns `Equal`.
12499 ///
12500 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
12501 /// of the exact input values.
12502 ///
12503 /// $$
12504 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
12505 /// $$
12506 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
12507 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
12508 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
12509 ///
12510 /// Special cases:
12511 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
12512 /// - $f(x,\pm\infty,p)=x$
12513 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
12514 /// result is a positive zero)
12515 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
12516 ///
12517 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
12518 /// the minimum positive [`Float`]:
12519 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12520 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12521 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12522 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12523 ///
12524 /// # Worst-case complexity
12525 /// $T(n) = O(n \log n \log\log n)$
12526 ///
12527 /// $M(n) = O(n)$
12528 ///
12529 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12530 /// y.complexity(), prec)`.
12531 ///
12532 /// # Panics
12533 /// Panics if `prec` is zero.
12534 ///
12535 /// # Examples
12536 /// ```
12537 /// use core::cmp::Ordering::*;
12538 /// use malachite_float::Float;
12539 /// use malachite_q::Rational;
12540 ///
12541 /// let a = Rational::from_signeds(22, 7);
12542 /// let b = Float::from(3u32);
12543 /// let (r, o) = Float::rational_ieee_remainder_float_prec_val_ref(a, &b, 5);
12544 /// assert_eq!(r.to_string(), "0.141");
12545 /// assert_eq!(o, Less);
12546 /// ```
12547 #[inline]
12548 pub fn rational_ieee_remainder_float_prec_val_ref(
12549 x: Rational,
12550 y: &Self,
12551 prec: u64,
12552 ) -> (Self, Ordering) {
12553 Self::rational_ieee_remainder_float_prec_round_val_ref(x, y, prec, Nearest)
12554 }
12555
12556 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
12557 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
12558 /// rounding the result to the nearest value of the specified precision. The [`Rational`] is
12559 /// taken by reference and the [`Float`] by value. An [`Ordering`] is also returned, indicating
12560 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder.
12561 /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
12562 /// it also returns `Equal`.
12563 ///
12564 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
12565 /// of the exact input values.
12566 ///
12567 /// $$
12568 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
12569 /// $$
12570 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
12571 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
12572 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
12573 ///
12574 /// Special cases:
12575 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
12576 /// - $f(x,\pm\infty,p)=x$
12577 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
12578 /// result is a positive zero)
12579 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
12580 ///
12581 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
12582 /// the minimum positive [`Float`]:
12583 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12584 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12585 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12586 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12587 ///
12588 /// # Worst-case complexity
12589 /// $T(n) = O(n \log n \log\log n)$
12590 ///
12591 /// $M(n) = O(n)$
12592 ///
12593 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12594 /// y.complexity(), prec)`.
12595 ///
12596 /// # Panics
12597 /// Panics if `prec` is zero.
12598 ///
12599 /// # Examples
12600 /// ```
12601 /// use core::cmp::Ordering::*;
12602 /// use malachite_float::Float;
12603 /// use malachite_q::Rational;
12604 ///
12605 /// let a = Rational::from_signeds(22, 7);
12606 /// let b = Float::from(3u32);
12607 /// let (r, o) = Float::rational_ieee_remainder_float_prec_ref_val(&a, b, 5);
12608 /// assert_eq!(r.to_string(), "0.141");
12609 /// assert_eq!(o, Less);
12610 /// ```
12611 #[inline]
12612 pub fn rational_ieee_remainder_float_prec_ref_val(
12613 x: &Rational,
12614 y: Self,
12615 prec: u64,
12616 ) -> (Self, Ordering) {
12617 Self::rational_ieee_remainder_float_prec_round_ref_val(x, y, prec, Nearest)
12618 }
12619
12620 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
12621 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
12622 /// rounding the result to the nearest value of the specified precision. The [`Rational`] and
12623 /// the [`Float`] are both taken by reference. An [`Ordering`] is also returned, indicating
12624 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder.
12625 /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
12626 /// it also returns `Equal`.
12627 ///
12628 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
12629 /// of the exact input values.
12630 ///
12631 /// $$
12632 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
12633 /// $$
12634 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
12635 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
12636 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
12637 ///
12638 /// Special cases:
12639 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
12640 /// - $f(x,\pm\infty,p)=x$
12641 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
12642 /// result is a positive zero)
12643 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
12644 ///
12645 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
12646 /// the minimum positive [`Float`]:
12647 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12648 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12649 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12650 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12651 ///
12652 /// # Worst-case complexity
12653 /// $T(n) = O(n \log n \log\log n)$
12654 ///
12655 /// $M(n) = O(n)$
12656 ///
12657 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12658 /// y.complexity(), prec)`.
12659 ///
12660 /// # Panics
12661 /// Panics if `prec` is zero.
12662 ///
12663 /// # Examples
12664 /// ```
12665 /// use core::cmp::Ordering::*;
12666 /// use malachite_float::Float;
12667 /// use malachite_q::Rational;
12668 ///
12669 /// let a = Rational::from_signeds(22, 7);
12670 /// let b = Float::from(3u32);
12671 /// let (r, o) = Float::rational_ieee_remainder_float_prec_ref_ref(&a, &b, 5);
12672 /// assert_eq!(r.to_string(), "0.141");
12673 /// assert_eq!(o, Less);
12674 /// ```
12675 #[inline]
12676 pub fn rational_ieee_remainder_float_prec_ref_ref(
12677 x: &Rational,
12678 y: &Self,
12679 prec: u64,
12680 ) -> (Self, Ordering) {
12681 Self::rational_ieee_remainder_float_prec_round_ref_ref(x, y, prec, Nearest)
12682 }
12683
12684 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
12685 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
12686 /// rounding the result to the [`Float`] modulus's precision, with the specified rounding mode.
12687 /// The [`Rational`] and the [`Float`] are both taken by value. An [`Ordering`] is also
12688 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
12689 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
12690 /// function returns a `NaN` it also returns `Equal`.
12691 ///
12692 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
12693 /// of the exact input values.
12694 ///
12695 /// $$
12696 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
12697 /// $$
12698 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
12699 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
12700 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
12701 ///
12702 /// Special cases:
12703 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
12704 /// - $f(x,\pm\infty,p)=x$
12705 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
12706 /// result is a positive zero)
12707 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
12708 ///
12709 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
12710 /// the minimum positive [`Float`]:
12711 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12712 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12713 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12714 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12715 ///
12716 /// # Worst-case complexity
12717 /// $T(n) = O(n \log n \log\log n)$
12718 ///
12719 /// $M(n) = O(n)$
12720 ///
12721 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12722 /// y.complexity())`.
12723 ///
12724 /// # Panics
12725 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
12726 /// precision.
12727 ///
12728 /// # Examples
12729 /// ```
12730 /// use core::cmp::Ordering::*;
12731 /// use malachite_base::rounding_modes::RoundingMode::*;
12732 /// use malachite_float::Float;
12733 /// use malachite_q::Rational;
12734 ///
12735 /// let a = Rational::from_signeds(22, 7);
12736 /// let b = Float::from(3u32);
12737 /// let (r, o) = Float::rational_ieee_remainder_float_round(a, b, Floor);
12738 /// assert_eq!(r.to_string(), "0.12");
12739 /// assert_eq!(o, Less);
12740 /// ```
12741 #[inline]
12742 pub fn rational_ieee_remainder_float_round(
12743 x: Rational,
12744 y: Self,
12745 rm: RoundingMode,
12746 ) -> (Self, Ordering) {
12747 let prec = y.significant_bits();
12748 Self::rational_ieee_remainder_float_prec_round(x, y, prec, rm)
12749 }
12750
12751 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
12752 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
12753 /// rounding the result to the [`Float`] modulus's precision, with the specified rounding mode.
12754 /// The [`Rational`] is taken by value and the [`Float`] by reference. An [`Ordering`] is also
12755 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
12756 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
12757 /// function returns a `NaN` it also returns `Equal`.
12758 ///
12759 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
12760 /// of the exact input values.
12761 ///
12762 /// $$
12763 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
12764 /// $$
12765 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
12766 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
12767 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
12768 ///
12769 /// Special cases:
12770 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
12771 /// - $f(x,\pm\infty,p)=x$
12772 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
12773 /// result is a positive zero)
12774 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
12775 ///
12776 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
12777 /// the minimum positive [`Float`]:
12778 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12779 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12780 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12781 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12782 ///
12783 /// # Worst-case complexity
12784 /// $T(n) = O(n \log n \log\log n)$
12785 ///
12786 /// $M(n) = O(n)$
12787 ///
12788 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12789 /// y.complexity())`.
12790 ///
12791 /// # Panics
12792 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
12793 /// precision.
12794 ///
12795 /// # Examples
12796 /// ```
12797 /// use core::cmp::Ordering::*;
12798 /// use malachite_base::rounding_modes::RoundingMode::*;
12799 /// use malachite_float::Float;
12800 /// use malachite_q::Rational;
12801 ///
12802 /// let a = Rational::from_signeds(22, 7);
12803 /// let b = Float::from(3u32);
12804 /// let (r, o) = Float::rational_ieee_remainder_float_round_val_ref(a, &b, Floor);
12805 /// assert_eq!(r.to_string(), "0.12");
12806 /// assert_eq!(o, Less);
12807 /// ```
12808 #[inline]
12809 pub fn rational_ieee_remainder_float_round_val_ref(
12810 x: Rational,
12811 y: &Self,
12812 rm: RoundingMode,
12813 ) -> (Self, Ordering) {
12814 let prec = y.significant_bits();
12815 Self::rational_ieee_remainder_float_prec_round_val_ref(x, y, prec, rm)
12816 }
12817
12818 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
12819 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
12820 /// rounding the result to the [`Float`] modulus's precision, with the specified rounding mode.
12821 /// The [`Rational`] is taken by reference and the [`Float`] by value. An [`Ordering`] is also
12822 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
12823 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
12824 /// function returns a `NaN` it also returns `Equal`.
12825 ///
12826 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
12827 /// of the exact input values.
12828 ///
12829 /// $$
12830 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
12831 /// $$
12832 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
12833 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
12834 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
12835 ///
12836 /// Special cases:
12837 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
12838 /// - $f(x,\pm\infty,p)=x$
12839 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
12840 /// result is a positive zero)
12841 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
12842 ///
12843 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
12844 /// the minimum positive [`Float`]:
12845 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12846 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12847 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12848 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12849 ///
12850 /// # Worst-case complexity
12851 /// $T(n) = O(n \log n \log\log n)$
12852 ///
12853 /// $M(n) = O(n)$
12854 ///
12855 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12856 /// y.complexity())`.
12857 ///
12858 /// # Panics
12859 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
12860 /// precision.
12861 ///
12862 /// # Examples
12863 /// ```
12864 /// use core::cmp::Ordering::*;
12865 /// use malachite_base::rounding_modes::RoundingMode::*;
12866 /// use malachite_float::Float;
12867 /// use malachite_q::Rational;
12868 ///
12869 /// let a = Rational::from_signeds(22, 7);
12870 /// let b = Float::from(3u32);
12871 /// let (r, o) = Float::rational_ieee_remainder_float_round_ref_val(&a, b, Floor);
12872 /// assert_eq!(r.to_string(), "0.12");
12873 /// assert_eq!(o, Less);
12874 /// ```
12875 #[inline]
12876 pub fn rational_ieee_remainder_float_round_ref_val(
12877 x: &Rational,
12878 y: Self,
12879 rm: RoundingMode,
12880 ) -> (Self, Ordering) {
12881 let prec = y.significant_bits();
12882 Self::rational_ieee_remainder_float_prec_round_ref_val(x, y, prec, rm)
12883 }
12884
12885 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
12886 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
12887 /// rounding the result to the [`Float`] modulus's precision, with the specified rounding mode.
12888 /// The [`Rational`] and the [`Float`] are both taken by reference. An [`Ordering`] is also
12889 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
12890 /// the exact remainder. Although `NaN`s are not comparable to any [`Float`], whenever this
12891 /// function returns a `NaN` it also returns `Equal`.
12892 ///
12893 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
12894 /// of the exact input values.
12895 ///
12896 /// $$
12897 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
12898 /// $$
12899 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
12900 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
12901 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
12902 ///
12903 /// Special cases:
12904 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
12905 /// - $f(x,\pm\infty,p)=x$
12906 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
12907 /// result is a positive zero)
12908 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
12909 ///
12910 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
12911 /// the minimum positive [`Float`]:
12912 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12913 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12914 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12915 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12916 ///
12917 /// # Worst-case complexity
12918 /// $T(n) = O(n \log n \log\log n)$
12919 ///
12920 /// $M(n) = O(n)$
12921 ///
12922 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12923 /// y.complexity())`.
12924 ///
12925 /// # Panics
12926 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
12927 /// precision.
12928 ///
12929 /// # Examples
12930 /// ```
12931 /// use core::cmp::Ordering::*;
12932 /// use malachite_base::rounding_modes::RoundingMode::*;
12933 /// use malachite_float::Float;
12934 /// use malachite_q::Rational;
12935 ///
12936 /// let a = Rational::from_signeds(22, 7);
12937 /// let b = Float::from(3u32);
12938 /// let (r, o) = Float::rational_ieee_remainder_float_round_ref_ref(&a, &b, Floor);
12939 /// assert_eq!(r.to_string(), "0.12");
12940 /// assert_eq!(o, Less);
12941 /// ```
12942 #[inline]
12943 pub fn rational_ieee_remainder_float_round_ref_ref(
12944 x: &Rational,
12945 y: &Self,
12946 rm: RoundingMode,
12947 ) -> (Self, Ordering) {
12948 let prec = y.significant_bits();
12949 Self::rational_ieee_remainder_float_prec_round_ref_ref(x, y, prec, rm)
12950 }
12951
12952 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
12953 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
12954 /// rounding the result to the nearest value of the [`Float`] modulus's precision. The
12955 /// [`Rational`] and the [`Float`] are both taken by value.
12956 ///
12957 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
12958 /// of the exact input values.
12959 ///
12960 /// $$
12961 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
12962 /// $$
12963 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
12964 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
12965 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
12966 ///
12967 /// Special cases:
12968 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
12969 /// - $f(x,\pm\infty,p)=x$
12970 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
12971 /// result is a positive zero)
12972 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
12973 ///
12974 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
12975 /// the minimum positive [`Float`]:
12976 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
12977 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
12978 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
12979 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
12980 ///
12981 /// # Worst-case complexity
12982 /// $T(n) = O(n \log n \log\log n)$
12983 ///
12984 /// $M(n) = O(n)$
12985 ///
12986 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
12987 /// y.complexity())`.
12988 ///
12989 /// # Examples
12990 /// ```
12991 /// use malachite_float::Float;
12992 /// use malachite_q::Rational;
12993 ///
12994 /// let q = Rational::from_signeds(22, 7);
12995 /// let f = Float::from(3u32);
12996 /// let r = Float::rational_ieee_remainder_float(q, f);
12997 /// assert_eq!(r.to_string(), "0.12");
12998 /// ```
12999 #[allow(clippy::needless_pass_by_value)]
13000 #[inline]
13001 pub fn rational_ieee_remainder_float(x: Rational, y: Self) -> Self {
13002 Self::rational_ieee_remainder_float_round(x, y, Nearest).0
13003 }
13004
13005 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
13006 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
13007 /// rounding the result to the nearest value of the [`Float`] modulus's precision. The
13008 /// [`Rational`] is taken by value and the [`Float`] by reference.
13009 ///
13010 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
13011 /// of the exact input values.
13012 ///
13013 /// $$
13014 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
13015 /// $$
13016 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
13017 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
13018 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
13019 ///
13020 /// Special cases:
13021 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
13022 /// - $f(x,\pm\infty,p)=x$
13023 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
13024 /// result is a positive zero)
13025 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
13026 ///
13027 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
13028 /// the minimum positive [`Float`]:
13029 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
13030 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
13031 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
13032 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
13033 ///
13034 /// # Worst-case complexity
13035 /// $T(n) = O(n \log n \log\log n)$
13036 ///
13037 /// $M(n) = O(n)$
13038 ///
13039 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
13040 /// y.complexity())`.
13041 ///
13042 /// # Examples
13043 /// ```
13044 /// use malachite_float::Float;
13045 /// use malachite_q::Rational;
13046 ///
13047 /// let q = Rational::from_signeds(22, 7);
13048 /// let f = Float::from(3u32);
13049 /// let r = Float::rational_ieee_remainder_float_val_ref(q, &f);
13050 /// assert_eq!(r.to_string(), "0.12");
13051 /// ```
13052 #[allow(clippy::needless_pass_by_value)]
13053 #[inline]
13054 pub fn rational_ieee_remainder_float_val_ref(x: Rational, y: &Self) -> Self {
13055 Self::rational_ieee_remainder_float_round_val_ref(x, y, Nearest).0
13056 }
13057
13058 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
13059 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
13060 /// rounding the result to the nearest value of the [`Float`] modulus's precision. The
13061 /// [`Rational`] is taken by reference and the [`Float`] by value.
13062 ///
13063 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
13064 /// of the exact input values.
13065 ///
13066 /// $$
13067 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
13068 /// $$
13069 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
13070 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
13071 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
13072 ///
13073 /// Special cases:
13074 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
13075 /// - $f(x,\pm\infty,p)=x$
13076 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
13077 /// result is a positive zero)
13078 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
13079 ///
13080 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
13081 /// the minimum positive [`Float`]:
13082 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
13083 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
13084 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
13085 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
13086 ///
13087 /// # Worst-case complexity
13088 /// $T(n) = O(n \log n \log\log n)$
13089 ///
13090 /// $M(n) = O(n)$
13091 ///
13092 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
13093 /// y.complexity())`.
13094 ///
13095 /// # Examples
13096 /// ```
13097 /// use malachite_float::Float;
13098 /// use malachite_q::Rational;
13099 ///
13100 /// let q = Rational::from_signeds(22, 7);
13101 /// let f = Float::from(3u32);
13102 /// let r = Float::rational_ieee_remainder_float_ref_val(&q, f);
13103 /// assert_eq!(r.to_string(), "0.12");
13104 /// ```
13105 #[allow(clippy::needless_pass_by_value)]
13106 #[inline]
13107 pub fn rational_ieee_remainder_float_ref_val(x: &Rational, y: Self) -> Self {
13108 Self::rational_ieee_remainder_float_round_ref_val(x, y, Nearest).0
13109 }
13110
13111 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
13112 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
13113 /// rounding the result to the nearest value of the [`Float`] modulus's precision. The
13114 /// [`Rational`] and the [`Float`] are both taken by reference.
13115 ///
13116 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
13117 /// of the exact input values.
13118 ///
13119 /// $$
13120 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
13121 /// $$
13122 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
13123 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
13124 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
13125 ///
13126 /// Special cases:
13127 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
13128 /// - $f(x,\pm\infty,p)=x$
13129 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
13130 /// result is a positive zero)
13131 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
13132 ///
13133 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
13134 /// the minimum positive [`Float`]:
13135 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
13136 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
13137 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
13138 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
13139 ///
13140 /// # Worst-case complexity
13141 /// $T(n) = O(n \log n \log\log n)$
13142 ///
13143 /// $M(n) = O(n)$
13144 ///
13145 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
13146 /// y.complexity())`.
13147 ///
13148 /// # Examples
13149 /// ```
13150 /// use malachite_float::Float;
13151 /// use malachite_q::Rational;
13152 ///
13153 /// let q = Rational::from_signeds(22, 7);
13154 /// let f = Float::from(3u32);
13155 /// let r = Float::rational_ieee_remainder_float_ref_ref(&q, &f);
13156 /// assert_eq!(r.to_string(), "0.12");
13157 /// ```
13158 #[inline]
13159 pub fn rational_ieee_remainder_float_ref_ref(x: &Rational, y: &Self) -> Self {
13160 Self::rational_ieee_remainder_float_round_ref_ref(x, y, Nearest).0
13161 }
13162
13163 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
13164 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
13165 /// rounding the result to the specified precision and with the specified rounding mode. The
13166 /// [`Rational`] and the [`Float`] are both taken by value. An [`Ordering`] is also returned,
13167 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
13168 /// remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s are not
13169 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
13170 ///
13171 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
13172 /// of the exact input values.
13173 ///
13174 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
13175 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
13176 /// [`Float`]-[`Float`] functions.
13177 ///
13178 /// $$
13179 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
13180 /// $$
13181 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
13182 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
13183 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
13184 ///
13185 /// Special cases:
13186 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
13187 /// - $f(x,\pm\infty,p)=x$
13188 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
13189 /// result is a positive zero)
13190 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
13191 /// - The quotient bits are 0 in all of the above special cases.
13192 ///
13193 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
13194 /// the minimum positive [`Float`]:
13195 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
13196 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
13197 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
13198 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
13199 ///
13200 /// # Worst-case complexity
13201 /// $T(n) = O(n \log n \log\log n)$
13202 ///
13203 /// $M(n) = O(n)$
13204 ///
13205 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
13206 /// y.complexity(), prec)`.
13207 ///
13208 /// # Panics
13209 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
13210 /// with `prec` bits.
13211 ///
13212 /// # Examples
13213 /// ```
13214 /// use core::cmp::Ordering::*;
13215 /// use malachite_base::rounding_modes::RoundingMode::*;
13216 /// use malachite_float::Float;
13217 /// use malachite_q::Rational;
13218 ///
13219 /// let a = Rational::from_signeds(22, 7);
13220 /// let b = Float::from(3u32);
13221 /// let (r, o, q) =
13222 /// Float::rational_ieee_remainder_float_and_quotient_bits_prec_round(a, b, 5, Floor);
13223 /// assert_eq!(r.to_string(), "0.141");
13224 /// assert_eq!(o, Less);
13225 /// assert_eq!(q, 1);
13226 /// ```
13227 #[allow(clippy::needless_pass_by_value)]
13228 #[inline]
13229 pub fn rational_ieee_remainder_float_and_quotient_bits_prec_round(
13230 x: Rational,
13231 y: Self,
13232 prec: u64,
13233 rm: RoundingMode,
13234 ) -> (Self, Ordering, i64) {
13235 rational_rem_float_helper(&x, &y, true, true, prec, rm)
13236 }
13237
13238 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
13239 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
13240 /// rounding the result to the specified precision and with the specified rounding mode. The
13241 /// [`Rational`] is taken by value and the [`Float`] by reference. An [`Ordering`] is also
13242 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
13243 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
13244 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
13245 /// `Equal`.
13246 ///
13247 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
13248 /// of the exact input values.
13249 ///
13250 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
13251 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
13252 /// [`Float`]-[`Float`] functions.
13253 ///
13254 /// $$
13255 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
13256 /// $$
13257 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
13258 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
13259 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
13260 ///
13261 /// Special cases:
13262 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
13263 /// - $f(x,\pm\infty,p)=x$
13264 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
13265 /// result is a positive zero)
13266 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
13267 /// - The quotient bits are 0 in all of the above special cases.
13268 ///
13269 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
13270 /// the minimum positive [`Float`]:
13271 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
13272 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
13273 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
13274 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
13275 ///
13276 /// # Worst-case complexity
13277 /// $T(n) = O(n \log n \log\log n)$
13278 ///
13279 /// $M(n) = O(n)$
13280 ///
13281 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
13282 /// y.complexity(), prec)`.
13283 ///
13284 /// # Panics
13285 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
13286 /// with `prec` bits.
13287 ///
13288 /// # Examples
13289 /// ```
13290 /// use core::cmp::Ordering::*;
13291 /// use malachite_base::rounding_modes::RoundingMode::*;
13292 /// use malachite_float::Float;
13293 /// use malachite_q::Rational;
13294 ///
13295 /// let a = Rational::from_signeds(22, 7);
13296 /// let b = Float::from(3u32);
13297 /// let f = Float::rational_ieee_remainder_float_and_quotient_bits_prec_round_val_ref;
13298 /// let (r, o, q) = f(a, &b, 5, Floor);
13299 /// assert_eq!(r.to_string(), "0.141");
13300 /// assert_eq!(o, Less);
13301 /// assert_eq!(q, 1);
13302 /// ```
13303 #[allow(clippy::needless_pass_by_value)]
13304 #[inline]
13305 pub fn rational_ieee_remainder_float_and_quotient_bits_prec_round_val_ref(
13306 x: Rational,
13307 y: &Self,
13308 prec: u64,
13309 rm: RoundingMode,
13310 ) -> (Self, Ordering, i64) {
13311 rational_rem_float_helper(&x, y, true, true, prec, rm)
13312 }
13313
13314 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
13315 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
13316 /// rounding the result to the specified precision and with the specified rounding mode. The
13317 /// [`Rational`] is taken by reference and the [`Float`] by value. An [`Ordering`] is also
13318 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
13319 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
13320 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
13321 /// `Equal`.
13322 ///
13323 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
13324 /// of the exact input values.
13325 ///
13326 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
13327 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
13328 /// [`Float`]-[`Float`] functions.
13329 ///
13330 /// $$
13331 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
13332 /// $$
13333 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
13334 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
13335 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
13336 ///
13337 /// Special cases:
13338 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
13339 /// - $f(x,\pm\infty,p)=x$
13340 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
13341 /// result is a positive zero)
13342 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
13343 /// - The quotient bits are 0 in all of the above special cases.
13344 ///
13345 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
13346 /// the minimum positive [`Float`]:
13347 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
13348 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
13349 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
13350 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
13351 ///
13352 /// # Worst-case complexity
13353 /// $T(n) = O(n \log n \log\log n)$
13354 ///
13355 /// $M(n) = O(n)$
13356 ///
13357 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
13358 /// y.complexity(), prec)`.
13359 ///
13360 /// # Panics
13361 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
13362 /// with `prec` bits.
13363 ///
13364 /// # Examples
13365 /// ```
13366 /// use core::cmp::Ordering::*;
13367 /// use malachite_base::rounding_modes::RoundingMode::*;
13368 /// use malachite_float::Float;
13369 /// use malachite_q::Rational;
13370 ///
13371 /// let a = Rational::from_signeds(22, 7);
13372 /// let b = Float::from(3u32);
13373 /// let f = Float::rational_ieee_remainder_float_and_quotient_bits_prec_round_ref_val;
13374 /// let (r, o, q) = f(&a, b, 5, Floor);
13375 /// assert_eq!(r.to_string(), "0.141");
13376 /// assert_eq!(o, Less);
13377 /// assert_eq!(q, 1);
13378 /// ```
13379 #[allow(clippy::needless_pass_by_value)]
13380 #[inline]
13381 pub fn rational_ieee_remainder_float_and_quotient_bits_prec_round_ref_val(
13382 x: &Rational,
13383 y: Self,
13384 prec: u64,
13385 rm: RoundingMode,
13386 ) -> (Self, Ordering, i64) {
13387 rational_rem_float_helper(x, &y, true, true, prec, rm)
13388 }
13389
13390 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
13391 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
13392 /// rounding the result to the specified precision and with the specified rounding mode. The
13393 /// [`Rational`] and the [`Float`] are both taken by reference. An [`Ordering`] is also
13394 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
13395 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
13396 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
13397 /// `Equal`.
13398 ///
13399 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
13400 /// of the exact input values.
13401 ///
13402 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
13403 /// it equals $\pm(|q|\bmod 2^{63})$. This is the same contract as the corresponding
13404 /// [`Float`]-[`Float`] functions.
13405 ///
13406 /// $$
13407 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
13408 /// $$
13409 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
13410 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
13411 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
13412 ///
13413 /// Special cases:
13414 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
13415 /// - $f(x,\pm\infty,p)=x$
13416 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
13417 /// result is a positive zero)
13418 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
13419 /// - The quotient bits are 0 in all of the above special cases.
13420 ///
13421 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
13422 /// the minimum positive [`Float`]:
13423 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
13424 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
13425 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
13426 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
13427 ///
13428 /// # Worst-case complexity
13429 /// $T(n) = O(n \log n \log\log n)$
13430 ///
13431 /// $M(n) = O(n)$
13432 ///
13433 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
13434 /// y.complexity(), prec)`.
13435 ///
13436 /// # Panics
13437 /// Panics if `prec` is zero, or if `rm` is `Exact` and the exact remainder is not representable
13438 /// with `prec` bits.
13439 ///
13440 /// # Examples
13441 /// ```
13442 /// use core::cmp::Ordering::*;
13443 /// use malachite_base::rounding_modes::RoundingMode::*;
13444 /// use malachite_float::Float;
13445 /// use malachite_q::Rational;
13446 ///
13447 /// let a = Rational::from_signeds(22, 7);
13448 /// let b = Float::from(3u32);
13449 /// let f = Float::rational_ieee_remainder_float_and_quotient_bits_prec_round_ref_ref;
13450 /// let (r, o, q) = f(&a, &b, 5, Floor);
13451 /// assert_eq!(r.to_string(), "0.141");
13452 /// assert_eq!(o, Less);
13453 /// assert_eq!(q, 1);
13454 /// ```
13455 #[inline]
13456 pub fn rational_ieee_remainder_float_and_quotient_bits_prec_round_ref_ref(
13457 x: &Rational,
13458 y: &Self,
13459 prec: u64,
13460 rm: RoundingMode,
13461 ) -> (Self, Ordering, i64) {
13462 rational_rem_float_helper(x, y, true, true, prec, rm)
13463 }
13464
13465 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
13466 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
13467 /// rounding the result to the nearest value of the specified precision. The [`Rational`] and
13468 /// the [`Float`] are both taken by value. An [`Ordering`] is also returned, indicating whether
13469 /// the rounded remainder is less than, equal to, or greater than the exact remainder, along
13470 /// with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to any
13471 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
13472 ///
13473 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
13474 /// of the exact input values.
13475 ///
13476 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
13477 /// it equals $\pm(|q|\bmod 2^{63})$.
13478 ///
13479 /// $$
13480 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
13481 /// $$
13482 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
13483 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
13484 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
13485 ///
13486 /// Special cases:
13487 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
13488 /// - $f(x,\pm\infty,p)=x$
13489 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
13490 /// result is a positive zero)
13491 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
13492 /// - The quotient bits are 0 in all of the above special cases.
13493 ///
13494 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
13495 /// the minimum positive [`Float`]:
13496 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
13497 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
13498 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
13499 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
13500 ///
13501 /// # Worst-case complexity
13502 /// $T(n) = O(n \log n \log\log n)$
13503 ///
13504 /// $M(n) = O(n)$
13505 ///
13506 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
13507 /// y.complexity(), prec)`.
13508 ///
13509 /// # Panics
13510 /// Panics if `prec` is zero.
13511 ///
13512 /// # Examples
13513 /// ```
13514 /// use core::cmp::Ordering::*;
13515 /// use malachite_float::Float;
13516 /// use malachite_q::Rational;
13517 ///
13518 /// let a = Rational::from_signeds(22, 7);
13519 /// let b = Float::from(3u32);
13520 /// let (r, o, q) = Float::rational_ieee_remainder_float_and_quotient_bits_prec(a, b, 5);
13521 /// assert_eq!(r.to_string(), "0.141");
13522 /// assert_eq!(o, Less);
13523 /// assert_eq!(q, 1);
13524 /// ```
13525 #[inline]
13526 pub fn rational_ieee_remainder_float_and_quotient_bits_prec(
13527 x: Rational,
13528 y: Self,
13529 prec: u64,
13530 ) -> (Self, Ordering, i64) {
13531 Self::rational_ieee_remainder_float_and_quotient_bits_prec_round(x, y, prec, Nearest)
13532 }
13533
13534 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
13535 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
13536 /// rounding the result to the nearest value of the specified precision. The [`Rational`] is
13537 /// taken by value and the [`Float`] by reference. An [`Ordering`] is also returned, indicating
13538 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder,
13539 /// along with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to
13540 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
13541 ///
13542 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
13543 /// of the exact input values.
13544 ///
13545 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
13546 /// it equals $\pm(|q|\bmod 2^{63})$.
13547 ///
13548 /// $$
13549 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
13550 /// $$
13551 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
13552 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
13553 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
13554 ///
13555 /// Special cases:
13556 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
13557 /// - $f(x,\pm\infty,p)=x$
13558 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
13559 /// result is a positive zero)
13560 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
13561 /// - The quotient bits are 0 in all of the above special cases.
13562 ///
13563 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
13564 /// the minimum positive [`Float`]:
13565 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
13566 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
13567 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
13568 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
13569 ///
13570 /// # Worst-case complexity
13571 /// $T(n) = O(n \log n \log\log n)$
13572 ///
13573 /// $M(n) = O(n)$
13574 ///
13575 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
13576 /// y.complexity(), prec)`.
13577 ///
13578 /// # Panics
13579 /// Panics if `prec` is zero.
13580 ///
13581 /// # Examples
13582 /// ```
13583 /// use core::cmp::Ordering::*;
13584 /// use malachite_float::Float;
13585 /// use malachite_q::Rational;
13586 ///
13587 /// let a = Rational::from_signeds(22, 7);
13588 /// let b = Float::from(3u32);
13589 /// let (r, o, q) =
13590 /// Float::rational_ieee_remainder_float_and_quotient_bits_prec_val_ref(a, &b, 5);
13591 /// assert_eq!(r.to_string(), "0.141");
13592 /// assert_eq!(o, Less);
13593 /// assert_eq!(q, 1);
13594 /// ```
13595 #[inline]
13596 pub fn rational_ieee_remainder_float_and_quotient_bits_prec_val_ref(
13597 x: Rational,
13598 y: &Self,
13599 prec: u64,
13600 ) -> (Self, Ordering, i64) {
13601 Self::rational_ieee_remainder_float_and_quotient_bits_prec_round_val_ref(
13602 x, y, prec, Nearest,
13603 )
13604 }
13605
13606 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
13607 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
13608 /// rounding the result to the nearest value of the specified precision. The [`Rational`] is
13609 /// taken by reference and the [`Float`] by value. An [`Ordering`] is also returned, indicating
13610 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder,
13611 /// along with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to
13612 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
13613 ///
13614 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
13615 /// of the exact input values.
13616 ///
13617 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
13618 /// it equals $\pm(|q|\bmod 2^{63})$.
13619 ///
13620 /// $$
13621 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
13622 /// $$
13623 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
13624 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
13625 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
13626 ///
13627 /// Special cases:
13628 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
13629 /// - $f(x,\pm\infty,p)=x$
13630 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
13631 /// result is a positive zero)
13632 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
13633 /// - The quotient bits are 0 in all of the above special cases.
13634 ///
13635 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
13636 /// the minimum positive [`Float`]:
13637 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
13638 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
13639 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
13640 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
13641 ///
13642 /// # Worst-case complexity
13643 /// $T(n) = O(n \log n \log\log n)$
13644 ///
13645 /// $M(n) = O(n)$
13646 ///
13647 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
13648 /// y.complexity(), prec)`.
13649 ///
13650 /// # Panics
13651 /// Panics if `prec` is zero.
13652 ///
13653 /// # Examples
13654 /// ```
13655 /// use core::cmp::Ordering::*;
13656 /// use malachite_float::Float;
13657 /// use malachite_q::Rational;
13658 ///
13659 /// let a = Rational::from_signeds(22, 7);
13660 /// let b = Float::from(3u32);
13661 /// let (r, o, q) =
13662 /// Float::rational_ieee_remainder_float_and_quotient_bits_prec_ref_val(&a, b, 5);
13663 /// assert_eq!(r.to_string(), "0.141");
13664 /// assert_eq!(o, Less);
13665 /// assert_eq!(q, 1);
13666 /// ```
13667 #[inline]
13668 pub fn rational_ieee_remainder_float_and_quotient_bits_prec_ref_val(
13669 x: &Rational,
13670 y: Self,
13671 prec: u64,
13672 ) -> (Self, Ordering, i64) {
13673 Self::rational_ieee_remainder_float_and_quotient_bits_prec_round_ref_val(
13674 x, y, prec, Nearest,
13675 )
13676 }
13677
13678 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
13679 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
13680 /// rounding the result to the nearest value of the specified precision. The [`Rational`] and
13681 /// the [`Float`] are both taken by reference. An [`Ordering`] is also returned, indicating
13682 /// whether the rounded remainder is less than, equal to, or greater than the exact remainder,
13683 /// along with the low bits of the quotient as an `i64`. Although `NaN`s are not comparable to
13684 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
13685 ///
13686 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
13687 /// of the exact input values.
13688 ///
13689 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
13690 /// it equals $\pm(|q|\bmod 2^{63})$.
13691 ///
13692 /// $$
13693 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
13694 /// $$
13695 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
13696 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
13697 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
13698 ///
13699 /// Special cases:
13700 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
13701 /// - $f(x,\pm\infty,p)=x$
13702 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
13703 /// result is a positive zero)
13704 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
13705 /// - The quotient bits are 0 in all of the above special cases.
13706 ///
13707 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
13708 /// the minimum positive [`Float`]:
13709 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
13710 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
13711 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
13712 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
13713 ///
13714 /// # Worst-case complexity
13715 /// $T(n) = O(n \log n \log\log n)$
13716 ///
13717 /// $M(n) = O(n)$
13718 ///
13719 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
13720 /// y.complexity(), prec)`.
13721 ///
13722 /// # Panics
13723 /// Panics if `prec` is zero.
13724 ///
13725 /// # Examples
13726 /// ```
13727 /// use core::cmp::Ordering::*;
13728 /// use malachite_float::Float;
13729 /// use malachite_q::Rational;
13730 ///
13731 /// let a = Rational::from_signeds(22, 7);
13732 /// let b = Float::from(3u32);
13733 /// let (r, o, q) =
13734 /// Float::rational_ieee_remainder_float_and_quotient_bits_prec_ref_ref(&a, &b, 5);
13735 /// assert_eq!(r.to_string(), "0.141");
13736 /// assert_eq!(o, Less);
13737 /// assert_eq!(q, 1);
13738 /// ```
13739 #[inline]
13740 pub fn rational_ieee_remainder_float_and_quotient_bits_prec_ref_ref(
13741 x: &Rational,
13742 y: &Self,
13743 prec: u64,
13744 ) -> (Self, Ordering, i64) {
13745 Self::rational_ieee_remainder_float_and_quotient_bits_prec_round_ref_ref(
13746 x, y, prec, Nearest,
13747 )
13748 }
13749
13750 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
13751 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
13752 /// rounding the result to the [`Float`] modulus's precision, with the specified rounding mode.
13753 /// The [`Rational`] and the [`Float`] are both taken by value. An [`Ordering`] is also
13754 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
13755 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
13756 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
13757 /// `Equal`.
13758 ///
13759 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
13760 /// of the exact input values.
13761 ///
13762 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
13763 /// it equals $\pm(|q|\bmod 2^{63})$.
13764 ///
13765 /// $$
13766 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
13767 /// $$
13768 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
13769 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
13770 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
13771 ///
13772 /// Special cases:
13773 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
13774 /// - $f(x,\pm\infty,p)=x$
13775 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
13776 /// result is a positive zero)
13777 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
13778 /// - The quotient bits are 0 in all of the above special cases.
13779 ///
13780 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
13781 /// the minimum positive [`Float`]:
13782 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
13783 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
13784 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
13785 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
13786 ///
13787 /// # Worst-case complexity
13788 /// $T(n) = O(n \log n \log\log n)$
13789 ///
13790 /// $M(n) = O(n)$
13791 ///
13792 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
13793 /// y.complexity())`.
13794 ///
13795 /// # Panics
13796 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
13797 /// precision.
13798 ///
13799 /// # Examples
13800 /// ```
13801 /// use core::cmp::Ordering::*;
13802 /// use malachite_base::rounding_modes::RoundingMode::*;
13803 /// use malachite_float::Float;
13804 /// use malachite_q::Rational;
13805 ///
13806 /// let a = Rational::from_signeds(22, 7);
13807 /// let b = Float::from(3u32);
13808 /// let (r, o, q) = Float::rational_ieee_remainder_float_and_quotient_bits_round(a, b, Floor);
13809 /// assert_eq!(r.to_string(), "0.12");
13810 /// assert_eq!(o, Less);
13811 /// assert_eq!(q, 1);
13812 /// ```
13813 #[inline]
13814 pub fn rational_ieee_remainder_float_and_quotient_bits_round(
13815 x: Rational,
13816 y: Self,
13817 rm: RoundingMode,
13818 ) -> (Self, Ordering, i64) {
13819 let prec = y.significant_bits();
13820 Self::rational_ieee_remainder_float_and_quotient_bits_prec_round(x, y, prec, rm)
13821 }
13822
13823 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
13824 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
13825 /// rounding the result to the [`Float`] modulus's precision, with the specified rounding mode.
13826 /// The [`Rational`] is taken by value and the [`Float`] by reference. An [`Ordering`] is also
13827 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
13828 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
13829 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
13830 /// `Equal`.
13831 ///
13832 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
13833 /// of the exact input values.
13834 ///
13835 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
13836 /// it equals $\pm(|q|\bmod 2^{63})$.
13837 ///
13838 /// $$
13839 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
13840 /// $$
13841 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
13842 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
13843 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
13844 ///
13845 /// Special cases:
13846 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
13847 /// - $f(x,\pm\infty,p)=x$
13848 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
13849 /// result is a positive zero)
13850 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
13851 /// - The quotient bits are 0 in all of the above special cases.
13852 ///
13853 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
13854 /// the minimum positive [`Float`]:
13855 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
13856 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
13857 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
13858 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
13859 ///
13860 /// # Worst-case complexity
13861 /// $T(n) = O(n \log n \log\log n)$
13862 ///
13863 /// $M(n) = O(n)$
13864 ///
13865 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
13866 /// y.complexity())`.
13867 ///
13868 /// # Panics
13869 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
13870 /// precision.
13871 ///
13872 /// # Examples
13873 /// ```
13874 /// use core::cmp::Ordering::*;
13875 /// use malachite_base::rounding_modes::RoundingMode::*;
13876 /// use malachite_float::Float;
13877 /// use malachite_q::Rational;
13878 ///
13879 /// let a = Rational::from_signeds(22, 7);
13880 /// let b = Float::from(3u32);
13881 /// let (r, o, q) =
13882 /// Float::rational_ieee_remainder_float_and_quotient_bits_round_val_ref(a, &b, Floor);
13883 /// assert_eq!(r.to_string(), "0.12");
13884 /// assert_eq!(o, Less);
13885 /// assert_eq!(q, 1);
13886 /// ```
13887 #[inline]
13888 pub fn rational_ieee_remainder_float_and_quotient_bits_round_val_ref(
13889 x: Rational,
13890 y: &Self,
13891 rm: RoundingMode,
13892 ) -> (Self, Ordering, i64) {
13893 let prec = y.significant_bits();
13894 Self::rational_ieee_remainder_float_and_quotient_bits_prec_round_val_ref(x, y, prec, rm)
13895 }
13896
13897 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
13898 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
13899 /// rounding the result to the [`Float`] modulus's precision, with the specified rounding mode.
13900 /// The [`Rational`] is taken by reference and the [`Float`] by value. An [`Ordering`] is also
13901 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
13902 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
13903 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
13904 /// `Equal`.
13905 ///
13906 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
13907 /// of the exact input values.
13908 ///
13909 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
13910 /// it equals $\pm(|q|\bmod 2^{63})$.
13911 ///
13912 /// $$
13913 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
13914 /// $$
13915 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
13916 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
13917 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
13918 ///
13919 /// Special cases:
13920 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
13921 /// - $f(x,\pm\infty,p)=x$
13922 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
13923 /// result is a positive zero)
13924 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
13925 /// - The quotient bits are 0 in all of the above special cases.
13926 ///
13927 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
13928 /// the minimum positive [`Float`]:
13929 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
13930 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
13931 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
13932 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
13933 ///
13934 /// # Worst-case complexity
13935 /// $T(n) = O(n \log n \log\log n)$
13936 ///
13937 /// $M(n) = O(n)$
13938 ///
13939 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
13940 /// y.complexity())`.
13941 ///
13942 /// # Panics
13943 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
13944 /// precision.
13945 ///
13946 /// # Examples
13947 /// ```
13948 /// use core::cmp::Ordering::*;
13949 /// use malachite_base::rounding_modes::RoundingMode::*;
13950 /// use malachite_float::Float;
13951 /// use malachite_q::Rational;
13952 ///
13953 /// let a = Rational::from_signeds(22, 7);
13954 /// let b = Float::from(3u32);
13955 /// let (r, o, q) =
13956 /// Float::rational_ieee_remainder_float_and_quotient_bits_round_ref_val(&a, b, Floor);
13957 /// assert_eq!(r.to_string(), "0.12");
13958 /// assert_eq!(o, Less);
13959 /// assert_eq!(q, 1);
13960 /// ```
13961 #[inline]
13962 pub fn rational_ieee_remainder_float_and_quotient_bits_round_ref_val(
13963 x: &Rational,
13964 y: Self,
13965 rm: RoundingMode,
13966 ) -> (Self, Ordering, i64) {
13967 let prec = y.significant_bits();
13968 Self::rational_ieee_remainder_float_and_quotient_bits_prec_round_ref_val(x, y, prec, rm)
13969 }
13970
13971 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
13972 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
13973 /// rounding the result to the [`Float`] modulus's precision, with the specified rounding mode.
13974 /// The [`Rational`] and the [`Float`] are both taken by reference. An [`Ordering`] is also
13975 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
13976 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
13977 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
13978 /// `Equal`.
13979 ///
13980 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
13981 /// of the exact input values.
13982 ///
13983 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
13984 /// it equals $\pm(|q|\bmod 2^{63})$.
13985 ///
13986 /// $$
13987 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
13988 /// $$
13989 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
13990 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
13991 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p+1}$.
13992 ///
13993 /// Special cases:
13994 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
13995 /// - $f(x,\pm\infty,p)=x$
13996 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
13997 /// result is a positive zero)
13998 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
13999 /// - The quotient bits are 0 in all of the above special cases.
14000 ///
14001 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
14002 /// the minimum positive [`Float`]:
14003 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
14004 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
14005 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
14006 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
14007 ///
14008 /// # Worst-case complexity
14009 /// $T(n) = O(n \log n \log\log n)$
14010 ///
14011 /// $M(n) = O(n)$
14012 ///
14013 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
14014 /// y.complexity())`.
14015 ///
14016 /// # Panics
14017 /// Panics if `rm` is `Exact` and the exact remainder is not representable with the output
14018 /// precision.
14019 ///
14020 /// # Examples
14021 /// ```
14022 /// use core::cmp::Ordering::*;
14023 /// use malachite_base::rounding_modes::RoundingMode::*;
14024 /// use malachite_float::Float;
14025 /// use malachite_q::Rational;
14026 ///
14027 /// let a = Rational::from_signeds(22, 7);
14028 /// let b = Float::from(3u32);
14029 /// let (r, o, q) =
14030 /// Float::rational_ieee_remainder_float_and_quotient_bits_round_ref_ref(&a, &b, Floor);
14031 /// assert_eq!(r.to_string(), "0.12");
14032 /// assert_eq!(o, Less);
14033 /// assert_eq!(q, 1);
14034 /// ```
14035 #[inline]
14036 pub fn rational_ieee_remainder_float_and_quotient_bits_round_ref_ref(
14037 x: &Rational,
14038 y: &Self,
14039 rm: RoundingMode,
14040 ) -> (Self, Ordering, i64) {
14041 let prec = y.significant_bits();
14042 Self::rational_ieee_remainder_float_and_quotient_bits_prec_round_ref_ref(x, y, prec, rm)
14043 }
14044
14045 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
14046 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
14047 /// rounding the result to the nearest value of the [`Float`] modulus's precision. The
14048 /// [`Rational`] and the [`Float`] are both taken by value. An [`Ordering`] is also returned,
14049 /// indicating whether the rounded remainder is less than, equal to, or greater than the exact
14050 /// remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s are not
14051 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
14052 ///
14053 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
14054 /// of the exact input values.
14055 ///
14056 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
14057 /// it equals $\pm(|q|\bmod 2^{63})$.
14058 ///
14059 /// $$
14060 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
14061 /// $$
14062 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
14063 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
14064 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
14065 ///
14066 /// Special cases:
14067 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
14068 /// - $f(x,\pm\infty,p)=x$
14069 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
14070 /// result is a positive zero)
14071 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
14072 /// - The quotient bits are 0 in all of the above special cases.
14073 ///
14074 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
14075 /// the minimum positive [`Float`]:
14076 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
14077 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
14078 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
14079 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
14080 ///
14081 /// # Worst-case complexity
14082 /// $T(n) = O(n \log n \log\log n)$
14083 ///
14084 /// $M(n) = O(n)$
14085 ///
14086 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
14087 /// y.complexity())`.
14088 ///
14089 /// # Examples
14090 /// ```
14091 /// use core::cmp::Ordering::*;
14092 /// use malachite_float::Float;
14093 /// use malachite_q::Rational;
14094 ///
14095 /// let a = Rational::from_signeds(22, 7);
14096 /// let b = Float::from(3u32);
14097 /// let (r, o, q) = Float::rational_ieee_remainder_float_and_quotient_bits(a, b);
14098 /// assert_eq!(r.to_string(), "0.12");
14099 /// assert_eq!(o, Less);
14100 /// assert_eq!(q, 1);
14101 /// ```
14102 #[inline]
14103 pub fn rational_ieee_remainder_float_and_quotient_bits(
14104 x: Rational,
14105 y: Self,
14106 ) -> (Self, Ordering, i64) {
14107 Self::rational_ieee_remainder_float_and_quotient_bits_round(x, y, Nearest)
14108 }
14109
14110 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
14111 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
14112 /// rounding the result to the nearest value of the [`Float`] modulus's precision. The
14113 /// [`Rational`] is taken by value and the [`Float`] by reference. An [`Ordering`] is also
14114 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
14115 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
14116 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
14117 /// `Equal`.
14118 ///
14119 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
14120 /// of the exact input values.
14121 ///
14122 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
14123 /// it equals $\pm(|q|\bmod 2^{63})$.
14124 ///
14125 /// $$
14126 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
14127 /// $$
14128 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
14129 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
14130 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
14131 ///
14132 /// Special cases:
14133 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
14134 /// - $f(x,\pm\infty,p)=x$
14135 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
14136 /// result is a positive zero)
14137 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
14138 /// - The quotient bits are 0 in all of the above special cases.
14139 ///
14140 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
14141 /// the minimum positive [`Float`]:
14142 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
14143 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
14144 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
14145 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
14146 ///
14147 /// # Worst-case complexity
14148 /// $T(n) = O(n \log n \log\log n)$
14149 ///
14150 /// $M(n) = O(n)$
14151 ///
14152 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
14153 /// y.complexity())`.
14154 ///
14155 /// # Examples
14156 /// ```
14157 /// use core::cmp::Ordering::*;
14158 /// use malachite_float::Float;
14159 /// use malachite_q::Rational;
14160 ///
14161 /// let a = Rational::from_signeds(22, 7);
14162 /// let b = Float::from(3u32);
14163 /// let (r, o, q) = Float::rational_ieee_remainder_float_and_quotient_bits_val_ref(a, &b);
14164 /// assert_eq!(r.to_string(), "0.12");
14165 /// assert_eq!(o, Less);
14166 /// assert_eq!(q, 1);
14167 /// ```
14168 #[inline]
14169 pub fn rational_ieee_remainder_float_and_quotient_bits_val_ref(
14170 x: Rational,
14171 y: &Self,
14172 ) -> (Self, Ordering, i64) {
14173 Self::rational_ieee_remainder_float_and_quotient_bits_round_val_ref(x, y, Nearest)
14174 }
14175
14176 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
14177 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
14178 /// rounding the result to the nearest value of the [`Float`] modulus's precision. The
14179 /// [`Rational`] is taken by reference and the [`Float`] by value. An [`Ordering`] is also
14180 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
14181 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
14182 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
14183 /// `Equal`.
14184 ///
14185 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
14186 /// of the exact input values.
14187 ///
14188 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
14189 /// it equals $\pm(|q|\bmod 2^{63})$.
14190 ///
14191 /// $$
14192 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
14193 /// $$
14194 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
14195 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
14196 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
14197 ///
14198 /// Special cases:
14199 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
14200 /// - $f(x,\pm\infty,p)=x$
14201 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
14202 /// result is a positive zero)
14203 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
14204 /// - The quotient bits are 0 in all of the above special cases.
14205 ///
14206 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
14207 /// the minimum positive [`Float`]:
14208 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
14209 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
14210 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
14211 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
14212 ///
14213 /// # Worst-case complexity
14214 /// $T(n) = O(n \log n \log\log n)$
14215 ///
14216 /// $M(n) = O(n)$
14217 ///
14218 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
14219 /// y.complexity())`.
14220 ///
14221 /// # Examples
14222 /// ```
14223 /// use core::cmp::Ordering::*;
14224 /// use malachite_float::Float;
14225 /// use malachite_q::Rational;
14226 ///
14227 /// let a = Rational::from_signeds(22, 7);
14228 /// let b = Float::from(3u32);
14229 /// let (r, o, q) = Float::rational_ieee_remainder_float_and_quotient_bits_ref_val(&a, b);
14230 /// assert_eq!(r.to_string(), "0.12");
14231 /// assert_eq!(o, Less);
14232 /// assert_eq!(q, 1);
14233 /// ```
14234 #[inline]
14235 pub fn rational_ieee_remainder_float_and_quotient_bits_ref_val(
14236 x: &Rational,
14237 y: Self,
14238 ) -> (Self, Ordering, i64) {
14239 Self::rational_ieee_remainder_float_and_quotient_bits_round_ref_val(x, y, Nearest)
14240 }
14241
14242 /// Computes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded to the
14243 /// nearest integer, ties to even; this is the IEEE 754 `remainder` operation, C's `remainder`,
14244 /// rounding the result to the nearest value of the [`Float`] modulus's precision. The
14245 /// [`Rational`] and the [`Float`] are both taken by reference. An [`Ordering`] is also
14246 /// returned, indicating whether the rounded remainder is less than, equal to, or greater than
14247 /// the exact remainder, along with the low bits of the quotient as an `i64`. Although `NaN`s
14248 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
14249 /// `Equal`.
14250 ///
14251 /// The [`Rational`] dividend is used exactly, so the result is the correctly-rounded remainder
14252 /// of the exact input values.
14253 ///
14254 /// The returned `i64` agrees with the exact quotient $q$ in its low 63 bits and has $q$'s sign:
14255 /// it equals $\pm(|q|\bmod 2^{63})$.
14256 ///
14257 /// $$
14258 /// f(x,y,p) = x - y\operatorname{roundeven}(x/y) + \varepsilon.
14259 /// $$
14260 /// - If the exact remainder is zero or an input is special, $\varepsilon$ is 0.
14261 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2
14262 /// |x-y\operatorname{roundeven}(x/y)|\rfloor-p}$.
14263 ///
14264 /// Special cases:
14265 /// - $f(x,\text{NaN},p)=f(x,\pm0.0,p)=\text{NaN}$
14266 /// - $f(x,\pm\infty,p)=x$
14267 /// - $f(0,y,p)=0.0$ if $y$ is not `NaN` and $y\neq 0$ (a zero [`Rational`] has no sign, so the
14268 /// result is a positive zero)
14269 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
14270 /// - The quotient bits are 0 in all of the above special cases.
14271 ///
14272 /// The remainder never overflows, but it can underflow, since its granularity may lie far below
14273 /// the minimum positive [`Float`]:
14274 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
14275 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
14276 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
14277 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
14278 ///
14279 /// # Worst-case complexity
14280 /// $T(n) = O(n \log n \log\log n)$
14281 ///
14282 /// $M(n) = O(n)$
14283 ///
14284 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
14285 /// y.complexity())`.
14286 ///
14287 /// # Examples
14288 /// ```
14289 /// use core::cmp::Ordering::*;
14290 /// use malachite_float::Float;
14291 /// use malachite_q::Rational;
14292 ///
14293 /// let a = Rational::from_signeds(22, 7);
14294 /// let b = Float::from(3u32);
14295 /// let (r, o, q) = Float::rational_ieee_remainder_float_and_quotient_bits_ref_ref(&a, &b);
14296 /// assert_eq!(r.to_string(), "0.12");
14297 /// assert_eq!(o, Less);
14298 /// assert_eq!(q, 1);
14299 /// ```
14300 #[inline]
14301 pub fn rational_ieee_remainder_float_and_quotient_bits_ref_ref(
14302 x: &Rational,
14303 y: &Self,
14304 ) -> (Self, Ordering, i64) {
14305 Self::rational_ieee_remainder_float_and_quotient_bits_round_ref_ref(x, y, Nearest)
14306 }
14307}
14308
14309impl Rem<Self> for Float {
14310 type Output = Self;
14311
14312 /// Takes the remainder of two [`Float`]s, with the quotient rounded toward zero (as for the `%`
14313 /// operator on primitive floats and C's `fmod`), taking both by value. The result is rounded to
14314 /// the nearest value of the maximum of the precisions of the inputs.
14315 ///
14316 /// $$
14317 /// x\%y = x - y\operatorname{trunc}(x/y) + \varepsilon.
14318 /// $$
14319 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
14320 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$,
14321 /// where $p$ is the maximum of the precisions of the inputs.
14322 ///
14323 /// Special cases:
14324 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
14325 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
14326 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
14327 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
14328 ///
14329 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
14330 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
14331 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
14332 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
14333 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
14334 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
14335 ///
14336 /// If you want to specify an output precision, consider using [`Float::rem_prec`] instead. If
14337 /// you want to use a rounding mode other than `Nearest`, consider using [`Float::rem_round`]
14338 /// instead. If you want both, consider using [`Float::rem_prec_round`] instead.
14339 ///
14340 /// # Worst-case complexity
14341 /// $T(n) = O(n \log n \log\log n)$
14342 ///
14343 /// $M(n) = O(n)$
14344 ///
14345 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
14346 /// other.complexity())`.
14347 ///
14348 /// # Examples
14349 /// ```
14350 /// use malachite_float::Float;
14351 ///
14352 /// assert_eq!((Float::from(10u32) % Float::from(7u32)).to_string(), "3.0");
14353 /// assert_eq!(
14354 /// (-Float::from(10u32) % Float::from(7u32)).to_string(),
14355 /// "-3.0"
14356 /// );
14357 /// ```
14358 #[inline]
14359 fn rem(self, other: Self) -> Self {
14360 let prec = max(self.significant_bits(), other.significant_bits());
14361 self.rem_prec_round(other, prec, Nearest).0
14362 }
14363}
14364
14365impl Rem<&Self> for Float {
14366 type Output = Self;
14367
14368 /// Takes the remainder of two [`Float`]s, with the quotient rounded toward zero (as for the `%`
14369 /// operator on primitive floats and C's `fmod`), taking the first by value and the second by
14370 /// reference. The result is rounded to the nearest value of the maximum of the precisions of
14371 /// the inputs.
14372 ///
14373 /// $$
14374 /// x\%y = x - y\operatorname{trunc}(x/y) + \varepsilon.
14375 /// $$
14376 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
14377 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$,
14378 /// where $p$ is the maximum of the precisions of the inputs.
14379 ///
14380 /// Special cases:
14381 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
14382 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
14383 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
14384 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
14385 ///
14386 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
14387 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
14388 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
14389 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
14390 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
14391 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
14392 ///
14393 /// If you want to specify an output precision, consider using [`Float::rem_prec`] instead. If
14394 /// you want to use a rounding mode other than `Nearest`, consider using [`Float::rem_round`]
14395 /// instead. If you want both, consider using [`Float::rem_prec_round`] instead.
14396 ///
14397 /// # Worst-case complexity
14398 /// $T(n) = O(n \log n \log\log n)$
14399 ///
14400 /// $M(n) = O(n)$
14401 ///
14402 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
14403 /// other.complexity())`.
14404 ///
14405 /// # Examples
14406 /// ```
14407 /// use malachite_float::Float;
14408 ///
14409 /// assert_eq!((Float::from(10u32) % &Float::from(7u32)).to_string(), "3.0");
14410 /// assert_eq!(
14411 /// (-Float::from(10u32) % &Float::from(7u32)).to_string(),
14412 /// "-3.0"
14413 /// );
14414 /// ```
14415 #[inline]
14416 fn rem(self, other: &Self) -> Self {
14417 let prec = max(self.significant_bits(), other.significant_bits());
14418 self.rem_prec_round_val_ref(other, prec, Nearest).0
14419 }
14420}
14421
14422impl Rem<Float> for &Float {
14423 type Output = Float;
14424
14425 /// Takes the remainder of two [`Float`]s, with the quotient rounded toward zero (as for the `%`
14426 /// operator on primitive floats and C's `fmod`), taking the first by reference and the second
14427 /// by value. The result is rounded to the nearest value of the maximum of the precisions of the
14428 /// inputs.
14429 ///
14430 /// $$
14431 /// x\%y = x - y\operatorname{trunc}(x/y) + \varepsilon.
14432 /// $$
14433 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
14434 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$,
14435 /// where $p$ is the maximum of the precisions of the inputs.
14436 ///
14437 /// Special cases:
14438 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
14439 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
14440 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
14441 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
14442 ///
14443 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
14444 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
14445 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
14446 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
14447 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
14448 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
14449 ///
14450 /// If you want to specify an output precision, consider using [`Float::rem_prec`] instead. If
14451 /// you want to use a rounding mode other than `Nearest`, consider using [`Float::rem_round`]
14452 /// instead. If you want both, consider using [`Float::rem_prec_round`] instead.
14453 ///
14454 /// # Worst-case complexity
14455 /// $T(n) = O(n \log n \log\log n)$
14456 ///
14457 /// $M(n) = O(n)$
14458 ///
14459 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
14460 /// other.complexity())`.
14461 ///
14462 /// # Examples
14463 /// ```
14464 /// use malachite_float::Float;
14465 ///
14466 /// assert_eq!((&Float::from(10u32) % Float::from(7u32)).to_string(), "3.0");
14467 /// assert_eq!(
14468 /// (-Float::from(10u32) % Float::from(7u32)).to_string(),
14469 /// "-3.0"
14470 /// );
14471 /// ```
14472 #[inline]
14473 fn rem(self, other: Float) -> Float {
14474 let prec = max(self.significant_bits(), other.significant_bits());
14475 self.rem_prec_round_ref_val(other, prec, Nearest).0
14476 }
14477}
14478
14479impl Rem<&Float> for &Float {
14480 type Output = Float;
14481
14482 /// Takes the remainder of two [`Float`]s, with the quotient rounded toward zero (as for the `%`
14483 /// operator on primitive floats and C's `fmod`), taking both by reference. The result is
14484 /// rounded to the nearest value of the maximum of the precisions of the inputs.
14485 ///
14486 /// $$
14487 /// x\%y = x - y\operatorname{trunc}(x/y) + \varepsilon.
14488 /// $$
14489 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
14490 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$,
14491 /// where $p$ is the maximum of the precisions of the inputs.
14492 ///
14493 /// Special cases:
14494 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
14495 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
14496 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
14497 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
14498 ///
14499 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
14500 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
14501 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
14502 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
14503 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
14504 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
14505 ///
14506 /// If you want to specify an output precision, consider using [`Float::rem_prec`] instead. If
14507 /// you want to use a rounding mode other than `Nearest`, consider using [`Float::rem_round`]
14508 /// instead. If you want both, consider using [`Float::rem_prec_round`] instead.
14509 ///
14510 /// # Worst-case complexity
14511 /// $T(n) = O(n \log n \log\log n)$
14512 ///
14513 /// $M(n) = O(n)$
14514 ///
14515 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
14516 /// other.complexity())`.
14517 ///
14518 /// # Examples
14519 /// ```
14520 /// use malachite_float::Float;
14521 ///
14522 /// assert_eq!(
14523 /// (&Float::from(10u32) % &Float::from(7u32)).to_string(),
14524 /// "3.0"
14525 /// );
14526 /// assert_eq!(
14527 /// (-Float::from(10u32) % &Float::from(7u32)).to_string(),
14528 /// "-3.0"
14529 /// );
14530 /// ```
14531 #[inline]
14532 fn rem(self, other: &Float) -> Float {
14533 let prec = max(self.significant_bits(), other.significant_bits());
14534 self.rem_prec_round_ref_ref(other, prec, Nearest).0
14535 }
14536}
14537
14538impl RemAssign<Self> for Float {
14539 /// Takes the remainder of two [`Float`]s in place, with the quotient rounded toward zero (as
14540 /// for the `%` operator on primitive floats and C's `fmod`); the [`Float`] on the right-hand
14541 /// side is taken by value. The result is rounded to the nearest value of the maximum of the
14542 /// precisions of the inputs.
14543 ///
14544 /// $$
14545 /// x\%y = x - y\operatorname{trunc}(x/y) + \varepsilon.
14546 /// $$
14547 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
14548 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$,
14549 /// where $p$ is the maximum of the precisions of the inputs.
14550 ///
14551 /// Special cases:
14552 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
14553 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
14554 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
14555 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
14556 ///
14557 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
14558 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
14559 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
14560 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
14561 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
14562 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
14563 ///
14564 /// If you want to specify an output precision, consider using [`Float::rem_prec`] instead. If
14565 /// you want to use a rounding mode other than `Nearest`, consider using [`Float::rem_round`]
14566 /// instead. If you want both, consider using [`Float::rem_prec_round`] instead.
14567 ///
14568 /// # Worst-case complexity
14569 /// $T(n) = O(n \log n \log\log n)$
14570 ///
14571 /// $M(n) = O(n)$
14572 ///
14573 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
14574 /// other.complexity())`.
14575 ///
14576 /// # Examples
14577 /// ```
14578 /// use malachite_float::Float;
14579 ///
14580 /// let mut x = Float::from(10u32);
14581 /// x %= Float::from(7u32);
14582 /// assert_eq!(x.to_string(), "3.0");
14583 /// ```
14584 #[inline]
14585 fn rem_assign(&mut self, other: Self) {
14586 let prec = max(self.significant_bits(), other.significant_bits());
14587 self.rem_prec_round_assign(other, prec, Nearest);
14588 }
14589}
14590
14591impl RemAssign<&Self> for Float {
14592 /// Takes the remainder of two [`Float`]s in place, with the quotient rounded toward zero (as
14593 /// for the `%` operator on primitive floats and C's `fmod`); the [`Float`] on the right-hand
14594 /// side is taken by reference. The result is rounded to the nearest value of the maximum of the
14595 /// precisions of the inputs.
14596 ///
14597 /// $$
14598 /// x\%y = x - y\operatorname{trunc}(x/y) + \varepsilon.
14599 /// $$
14600 /// - If the exact remainder is zero or the inputs are special, $\varepsilon$ is 0.
14601 /// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |x-y\operatorname{trunc}(x/y)|\rfloor-p}$,
14602 /// where $p$ is the maximum of the precisions of the inputs.
14603 ///
14604 /// Special cases:
14605 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=f(\pm\infty,y,p)=f(x,\pm0.0,p) = \text{NaN}$
14606 /// - $f(x,\pm\infty,p)=x$ if $x$ is finite
14607 /// - $f(\pm0.0,y,p)=\pm0.0$ if $y$ is not `NaN` and $y\neq 0$
14608 /// - If the exact remainder is zero, a zero with the sign of $x$ is returned.
14609 ///
14610 /// The remainder never overflows, since it is smaller than $y$ in magnitude, but it can
14611 /// underflow, since its granularity may lie far below the minimum positive [`Float`]:
14612 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
14613 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
14614 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
14615 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
14616 ///
14617 /// If you want to specify an output precision, consider using [`Float::rem_prec`] instead. If
14618 /// you want to use a rounding mode other than `Nearest`, consider using [`Float::rem_round`]
14619 /// instead. If you want both, consider using [`Float::rem_prec_round`] instead.
14620 ///
14621 /// # Worst-case complexity
14622 /// $T(n) = O(n \log n \log\log n)$
14623 ///
14624 /// $M(n) = O(n)$
14625 ///
14626 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
14627 /// other.complexity())`.
14628 ///
14629 /// # Examples
14630 /// ```
14631 /// use malachite_float::Float;
14632 ///
14633 /// let mut x = Float::from(10u32);
14634 /// x %= &Float::from(7u32);
14635 /// assert_eq!(x.to_string(), "3.0");
14636 /// ```
14637 #[inline]
14638 fn rem_assign(&mut self, other: &Self) {
14639 let prec = max(self.significant_bits(), other.significant_bits());
14640 self.rem_prec_round_assign_ref(other, prec, Nearest);
14641 }
14642}
14643
14644impl Rem<Rational> for Float {
14645 type Output = Self;
14646
14647 /// Takes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward zero
14648 /// (as for the `%` operator on primitive floats and C's `fmod`), taking both by value. The
14649 /// result is rounded to the nearest value of the [`Float`]'s precision.
14650 ///
14651 /// The [`Rational`] operand is used exactly, so the result is the correctly-rounded remainder
14652 /// of the exact input values.
14653 ///
14654 /// Special cases:
14655 /// - $f(\text{NaN},y)=f(\pm\infty,y)=f(x,0)=\text{NaN}$
14656 /// - $f(\pm0.0,y)=\pm0.0$ if $y\neq 0$
14657 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
14658 ///
14659 /// # Worst-case complexity
14660 /// $T(n) = O(n \log n \log\log n)$
14661 ///
14662 /// $M(n) = O(n)$
14663 ///
14664 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum of the operands'
14665 /// complexities.
14666 ///
14667 /// # Examples
14668 /// ```
14669 /// use malachite_float::Float;
14670 /// use malachite_q::Rational;
14671 ///
14672 /// let r = Float::from(10u32) % Rational::from_signeds(22, 7);
14673 /// assert_eq!(r.to_string(), "0.62");
14674 /// ```
14675 #[inline]
14676 fn rem(self, other: Rational) -> Self {
14677 let prec = self.significant_bits();
14678 self.rem_rational_prec_round(other, prec, Nearest).0
14679 }
14680}
14681
14682impl Rem<&Rational> for Float {
14683 type Output = Self;
14684
14685 /// Takes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward zero
14686 /// (as for the `%` operator on primitive floats and C's `fmod`), taking the [`Float`] by value
14687 /// and the [`Rational`] by reference. The result is rounded to the nearest value of the
14688 /// [`Float`]'s precision.
14689 ///
14690 /// The [`Rational`] operand is used exactly, so the result is the correctly-rounded remainder
14691 /// of the exact input values.
14692 ///
14693 /// Special cases:
14694 /// - $f(\text{NaN},y)=f(\pm\infty,y)=f(x,0)=\text{NaN}$
14695 /// - $f(\pm0.0,y)=\pm0.0$ if $y\neq 0$
14696 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
14697 ///
14698 /// # Worst-case complexity
14699 /// $T(n) = O(n \log n \log\log n)$
14700 ///
14701 /// $M(n) = O(n)$
14702 ///
14703 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum of the operands'
14704 /// complexities.
14705 ///
14706 /// # Examples
14707 /// ```
14708 /// use malachite_float::Float;
14709 /// use malachite_q::Rational;
14710 ///
14711 /// let r = Float::from(10u32) % &Rational::from_signeds(22, 7);
14712 /// assert_eq!(r.to_string(), "0.62");
14713 /// ```
14714 #[inline]
14715 fn rem(self, other: &Rational) -> Self {
14716 let prec = self.significant_bits();
14717 self.rem_rational_prec_round_val_ref(other, prec, Nearest).0
14718 }
14719}
14720
14721impl Rem<Rational> for &Float {
14722 type Output = Float;
14723
14724 /// Takes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward zero
14725 /// (as for the `%` operator on primitive floats and C's `fmod`), taking the [`Float`] by
14726 /// reference and the [`Rational`] by value. The result is rounded to the nearest value of the
14727 /// [`Float`]'s precision.
14728 ///
14729 /// The [`Rational`] operand is used exactly, so the result is the correctly-rounded remainder
14730 /// of the exact input values.
14731 ///
14732 /// Special cases:
14733 /// - $f(\text{NaN},y)=f(\pm\infty,y)=f(x,0)=\text{NaN}$
14734 /// - $f(\pm0.0,y)=\pm0.0$ if $y\neq 0$
14735 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
14736 ///
14737 /// # Worst-case complexity
14738 /// $T(n) = O(n \log n \log\log n)$
14739 ///
14740 /// $M(n) = O(n)$
14741 ///
14742 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum of the operands'
14743 /// complexities.
14744 ///
14745 /// # Examples
14746 /// ```
14747 /// use malachite_float::Float;
14748 /// use malachite_q::Rational;
14749 ///
14750 /// let r = &Float::from(10u32) % Rational::from_signeds(22, 7);
14751 /// assert_eq!(r.to_string(), "0.62");
14752 /// ```
14753 #[inline]
14754 fn rem(self, other: Rational) -> Float {
14755 let prec = self.significant_bits();
14756 self.rem_rational_prec_round_ref_val(other, prec, Nearest).0
14757 }
14758}
14759
14760impl Rem<&Rational> for &Float {
14761 type Output = Float;
14762
14763 /// Takes the remainder of a [`Float`] by a [`Rational`], with the quotient rounded toward zero
14764 /// (as for the `%` operator on primitive floats and C's `fmod`), taking both by reference. The
14765 /// result is rounded to the nearest value of the [`Float`]'s precision.
14766 ///
14767 /// The [`Rational`] operand is used exactly, so the result is the correctly-rounded remainder
14768 /// of the exact input values.
14769 ///
14770 /// Special cases:
14771 /// - $f(\text{NaN},y)=f(\pm\infty,y)=f(x,0)=\text{NaN}$
14772 /// - $f(\pm0.0,y)=\pm0.0$ if $y\neq 0$
14773 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
14774 ///
14775 /// # Worst-case complexity
14776 /// $T(n) = O(n \log n \log\log n)$
14777 ///
14778 /// $M(n) = O(n)$
14779 ///
14780 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum of the operands'
14781 /// complexities.
14782 ///
14783 /// # Examples
14784 /// ```
14785 /// use malachite_float::Float;
14786 /// use malachite_q::Rational;
14787 ///
14788 /// let r = &Float::from(10u32) % &Rational::from_signeds(22, 7);
14789 /// assert_eq!(r.to_string(), "0.62");
14790 /// ```
14791 #[inline]
14792 fn rem(self, other: &Rational) -> Float {
14793 let prec = self.significant_bits();
14794 self.rem_rational_prec_round_ref_ref(other, prec, Nearest).0
14795 }
14796}
14797
14798impl RemAssign<Rational> for Float {
14799 /// Takes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
14800 /// toward zero (as for the `%` operator on primitive floats and C's `fmod`); the [`Rational`]
14801 /// is taken by value. The result is rounded to the nearest value of the [`Float`]'s precision.
14802 ///
14803 /// The [`Rational`] operand is used exactly, so the result is the correctly-rounded remainder
14804 /// of the exact input values.
14805 ///
14806 /// Special cases:
14807 /// - $f(\text{NaN},y)=f(\pm\infty,y)=f(x,0)=\text{NaN}$
14808 /// - $f(\pm0.0,y)=\pm0.0$ if $y\neq 0$
14809 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
14810 ///
14811 /// # Worst-case complexity
14812 /// $T(n) = O(n \log n \log\log n)$
14813 ///
14814 /// $M(n) = O(n)$
14815 ///
14816 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum of the operands'
14817 /// complexities.
14818 ///
14819 /// # Examples
14820 /// ```
14821 /// use malachite_float::Float;
14822 /// use malachite_q::Rational;
14823 ///
14824 /// let mut x = Float::from(10u32);
14825 /// x %= Rational::from_signeds(22, 7);
14826 /// assert_eq!(x.to_string(), "0.62");
14827 /// ```
14828 #[inline]
14829 fn rem_assign(&mut self, other: Rational) {
14830 let prec = self.significant_bits();
14831 self.rem_rational_prec_round_assign(other, prec, Nearest);
14832 }
14833}
14834
14835impl RemAssign<&Rational> for Float {
14836 /// Takes the remainder of a [`Float`] by a [`Rational`] in place, with the quotient rounded
14837 /// toward zero (as for the `%` operator on primitive floats and C's `fmod`); the [`Rational`]
14838 /// is taken by reference. The result is rounded to the nearest value of the [`Float`]'s
14839 /// precision.
14840 ///
14841 /// The [`Rational`] operand is used exactly, so the result is the correctly-rounded remainder
14842 /// of the exact input values.
14843 ///
14844 /// Special cases:
14845 /// - $f(\text{NaN},y)=f(\pm\infty,y)=f(x,0)=\text{NaN}$
14846 /// - $f(\pm0.0,y)=\pm0.0$ if $y\neq 0$
14847 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
14848 ///
14849 /// # Worst-case complexity
14850 /// $T(n) = O(n \log n \log\log n)$
14851 ///
14852 /// $M(n) = O(n)$
14853 ///
14854 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum of the operands'
14855 /// complexities.
14856 ///
14857 /// # Examples
14858 /// ```
14859 /// use malachite_float::Float;
14860 /// use malachite_q::Rational;
14861 ///
14862 /// let mut x = Float::from(10u32);
14863 /// x %= &Rational::from_signeds(22, 7);
14864 /// assert_eq!(x.to_string(), "0.62");
14865 /// ```
14866 #[inline]
14867 fn rem_assign(&mut self, other: &Rational) {
14868 let prec = self.significant_bits();
14869 self.rem_rational_prec_round_assign_ref(other, prec, Nearest);
14870 }
14871}
14872
14873impl Rem<Float> for Rational {
14874 type Output = Float;
14875
14876 /// Takes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward zero
14877 /// (as for the `%` operator on primitive floats and C's `fmod`), taking both by value. The
14878 /// result is rounded to the nearest value of the [`Float`]'s precision.
14879 ///
14880 /// The [`Rational`] operand is used exactly, so the result is the correctly-rounded remainder
14881 /// of the exact input values.
14882 ///
14883 /// Special cases:
14884 /// - $f(x,\text{NaN})=f(x,\pm0.0)=\text{NaN}$
14885 /// - $f(x,\pm\infty)=x$
14886 /// - $f(0,y)=0.0$ if $y$ is not `NaN` and $y\neq 0$
14887 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
14888 ///
14889 /// # Worst-case complexity
14890 /// $T(n) = O(n \log n \log\log n)$
14891 ///
14892 /// $M(n) = O(n)$
14893 ///
14894 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum of the operands'
14895 /// complexities.
14896 ///
14897 /// # Examples
14898 /// ```
14899 /// use malachite_float::Float;
14900 /// use malachite_q::Rational;
14901 ///
14902 /// let r = Rational::from_signeds(22, 7) % Float::from(3u32);
14903 /// assert_eq!(r.to_string(), "0.12");
14904 /// ```
14905 #[inline]
14906 fn rem(self, other: Float) -> Float {
14907 let prec = other.significant_bits();
14908 Float::rational_rem_float_prec_round(self, other, prec, Nearest).0
14909 }
14910}
14911
14912impl Rem<&Float> for Rational {
14913 type Output = Float;
14914
14915 /// Takes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward zero
14916 /// (as for the `%` operator on primitive floats and C's `fmod`), taking the [`Rational`] by
14917 /// value and the [`Float`] by reference. The result is rounded to the nearest value of the
14918 /// [`Float`]'s precision.
14919 ///
14920 /// The [`Rational`] operand is used exactly, so the result is the correctly-rounded remainder
14921 /// of the exact input values.
14922 ///
14923 /// Special cases:
14924 /// - $f(x,\text{NaN})=f(x,\pm0.0)=\text{NaN}$
14925 /// - $f(x,\pm\infty)=x$
14926 /// - $f(0,y)=0.0$ if $y$ is not `NaN` and $y\neq 0$
14927 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
14928 ///
14929 /// # Worst-case complexity
14930 /// $T(n) = O(n \log n \log\log n)$
14931 ///
14932 /// $M(n) = O(n)$
14933 ///
14934 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum of the operands'
14935 /// complexities.
14936 ///
14937 /// # Examples
14938 /// ```
14939 /// use malachite_float::Float;
14940 /// use malachite_q::Rational;
14941 ///
14942 /// let r = Rational::from_signeds(22, 7) % &Float::from(3u32);
14943 /// assert_eq!(r.to_string(), "0.12");
14944 /// ```
14945 #[inline]
14946 fn rem(self, other: &Float) -> Float {
14947 let prec = other.significant_bits();
14948 Float::rational_rem_float_prec_round_val_ref(self, other, prec, Nearest).0
14949 }
14950}
14951
14952impl Rem<Float> for &Rational {
14953 type Output = Float;
14954
14955 /// Takes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward zero
14956 /// (as for the `%` operator on primitive floats and C's `fmod`), taking the [`Rational`] by
14957 /// reference and the [`Float`] by value. The result is rounded to the nearest value of the
14958 /// [`Float`]'s precision.
14959 ///
14960 /// The [`Rational`] operand is used exactly, so the result is the correctly-rounded remainder
14961 /// of the exact input values.
14962 ///
14963 /// Special cases:
14964 /// - $f(x,\text{NaN})=f(x,\pm0.0)=\text{NaN}$
14965 /// - $f(x,\pm\infty)=x$
14966 /// - $f(0,y)=0.0$ if $y$ is not `NaN` and $y\neq 0$
14967 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
14968 ///
14969 /// # Worst-case complexity
14970 /// $T(n) = O(n \log n \log\log n)$
14971 ///
14972 /// $M(n) = O(n)$
14973 ///
14974 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum of the operands'
14975 /// complexities.
14976 ///
14977 /// # Examples
14978 /// ```
14979 /// use malachite_float::Float;
14980 /// use malachite_q::Rational;
14981 ///
14982 /// let r = &Rational::from_signeds(22, 7) % Float::from(3u32);
14983 /// assert_eq!(r.to_string(), "0.12");
14984 /// ```
14985 #[inline]
14986 fn rem(self, other: Float) -> Float {
14987 let prec = other.significant_bits();
14988 Float::rational_rem_float_prec_round_ref_val(self, other, prec, Nearest).0
14989 }
14990}
14991
14992impl Rem<&Float> for &Rational {
14993 type Output = Float;
14994
14995 /// Takes the remainder of a [`Rational`] by a [`Float`], with the quotient rounded toward zero
14996 /// (as for the `%` operator on primitive floats and C's `fmod`), taking both by reference. The
14997 /// result is rounded to the nearest value of the [`Float`]'s precision.
14998 ///
14999 /// The [`Rational`] operand is used exactly, so the result is the correctly-rounded remainder
15000 /// of the exact input values.
15001 ///
15002 /// Special cases:
15003 /// - $f(x,\text{NaN})=f(x,\pm0.0)=\text{NaN}$
15004 /// - $f(x,\pm\infty)=x$
15005 /// - $f(0,y)=0.0$ if $y$ is not `NaN` and $y\neq 0$
15006 /// - Otherwise, if the exact remainder is zero, a zero with the sign of $x$ is returned.
15007 ///
15008 /// # Worst-case complexity
15009 /// $T(n) = O(n \log n \log\log n)$
15010 ///
15011 /// $M(n) = O(n)$
15012 ///
15013 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum of the operands'
15014 /// complexities.
15015 ///
15016 /// # Examples
15017 /// ```
15018 /// use malachite_float::Float;
15019 /// use malachite_q::Rational;
15020 ///
15021 /// let r = &Rational::from_signeds(22, 7) % &Float::from(3u32);
15022 /// assert_eq!(r.to_string(), "0.12");
15023 /// ```
15024 #[inline]
15025 fn rem(self, other: &Float) -> Float {
15026 let prec = other.significant_bits();
15027 Float::rational_rem_float_prec_round_ref_ref(self, other, prec, Nearest).0
15028 }
15029}
15030
15031/// Computes the remainder of two primitive floats, with the quotient rounded toward zero, using
15032/// emulated [`Float`] arithmetic.
15033///
15034/// The floating-point remainder of two values of the same format is always exactly representable,
15035/// so this function returns the same values as the `%` operator on primitive floats; it serves as a
15036/// reference implementation. NaN, infinite `x`, or zero `y` gives NaN; a zero remainder has the
15037/// sign of `x`.
15038///
15039/// # Worst-case complexity
15040/// Constant time and additional memory.
15041///
15042/// # Examples
15043/// ```
15044/// use malachite_base::num::float::NiceFloat;
15045/// use malachite_float::float::arithmetic::rem::primitive_float_rem;
15046///
15047/// assert_eq!(NiceFloat(primitive_float_rem(10.0, 7.0)), NiceFloat(3.0));
15048/// assert_eq!(NiceFloat(primitive_float_rem(10.5, 3.25)), NiceFloat(0.75));
15049/// ```
15050#[allow(clippy::type_repetition_in_bounds)]
15051#[inline]
15052pub fn primitive_float_rem<T: PrimitiveFloat>(x: T, y: T) -> T
15053where
15054 Float: From<T> + PartialOrd<T>,
15055 for<'a> T: ExactFrom<&'a Float>,
15056{
15057 emulate_float_float_to_float_fn(Float::rem_prec, x, y)
15058}
15059
15060/// Computes the IEEE 754 `remainder` of two primitive floats, with the quotient rounded to the
15061/// nearest integer (ties to even), using emulated [`Float`] arithmetic.
15062///
15063/// Like the truncated-quotient remainder, this value is always exactly representable. NaN, infinite
15064/// `x`, or zero `y` gives NaN; a zero remainder has the sign of `x`.
15065///
15066/// # Worst-case complexity
15067/// Constant time and additional memory.
15068///
15069/// # Examples
15070/// ```
15071/// use malachite_base::num::float::NiceFloat;
15072/// use malachite_float::float::arithmetic::rem::primitive_float_ieee_remainder;
15073///
15074/// assert_eq!(
15075/// NiceFloat(primitive_float_ieee_remainder(14.0, 3.0)),
15076/// NiceFloat(-1.0)
15077/// );
15078/// ```
15079#[allow(clippy::type_repetition_in_bounds)]
15080#[inline]
15081pub fn primitive_float_ieee_remainder<T: PrimitiveFloat>(x: T, y: T) -> T
15082where
15083 Float: From<T> + PartialOrd<T>,
15084 for<'a> T: ExactFrom<&'a Float>,
15085{
15086 emulate_float_float_to_float_fn(Float::ieee_remainder_prec, x, y)
15087}
15088
15089/// Computes the remainder of a primitive float by a [`Rational`], with the quotient rounded toward
15090/// zero, correctly rounding the result to the nearest value.
15091///
15092/// The [`Rational`] modulus is used exactly. A remainder is unusually sensitive to its modulus —
15093/// perturbing it by $\varepsilon$ moves the result by up to the quotient times $\varepsilon$ — so
15094/// no primitive-float approximation of the modulus could produce these values. NaN or infinite `x`,
15095/// or zero `y`, gives NaN.
15096///
15097/// # Worst-case complexity
15098/// $T(n) = O(n \log n \log\log n)$
15099///
15100/// $M(n) = O(n)$
15101///
15102/// where $T$ is time, $M$ is additional memory, and $n$ is `y.significant_bits()`.
15103///
15104/// # Examples
15105/// ```
15106/// use malachite_base::num::float::NiceFloat;
15107/// use malachite_float::float::arithmetic::rem::primitive_float_rem_rational;
15108/// use malachite_q::Rational;
15109///
15110/// // 10 mod 22/7 = 4/7
15111/// assert_eq!(
15112/// NiceFloat(primitive_float_rem_rational(
15113/// 10.0,
15114/// &Rational::from_signeds(22, 7)
15115/// )),
15116/// NiceFloat(0.5714285714285714)
15117/// );
15118/// ```
15119#[allow(clippy::type_repetition_in_bounds)]
15120#[inline]
15121pub fn primitive_float_rem_rational<T: PrimitiveFloat>(x: T, y: &Rational) -> T
15122where
15123 Float: From<T> + PartialOrd<T>,
15124 for<'a> T: ExactFrom<&'a Float>,
15125{
15126 emulate_float_to_float_fn(|x, prec| Float::rem_rational_prec_val_ref(x, y, prec), x)
15127}
15128
15129/// Computes the IEEE 754 `remainder` of a primitive float by a [`Rational`], with the quotient
15130/// rounded to the nearest integer (ties to even), correctly rounding the result to the nearest
15131/// value.
15132///
15133/// The [`Rational`] modulus is used exactly; see [`primitive_float_rem_rational`] for why this
15134/// matters.
15135///
15136/// # Worst-case complexity
15137/// $T(n) = O(n \log n \log\log n)$
15138///
15139/// $M(n) = O(n)$
15140///
15141/// where $T$ is time, $M$ is additional memory, and $n$ is `y.significant_bits()`.
15142///
15143/// # Examples
15144/// ```
15145/// use malachite_base::num::float::NiceFloat;
15146/// use malachite_float::float::arithmetic::rem::primitive_float_ieee_remainder_rational;
15147/// use malachite_q::Rational;
15148///
15149/// assert_eq!(
15150/// NiceFloat(primitive_float_ieee_remainder_rational(
15151/// 10.0,
15152/// &Rational::from_signeds(22, 7)
15153/// )),
15154/// NiceFloat(0.5714285714285714)
15155/// );
15156/// ```
15157#[allow(clippy::type_repetition_in_bounds)]
15158#[inline]
15159pub fn primitive_float_ieee_remainder_rational<T: PrimitiveFloat>(x: T, y: &Rational) -> T
15160where
15161 Float: From<T> + PartialOrd<T>,
15162 for<'a> T: ExactFrom<&'a Float>,
15163{
15164 emulate_float_to_float_fn(
15165 |x, prec| Float::ieee_remainder_rational_prec_val_ref(x, y, prec),
15166 x,
15167 )
15168}
15169
15170/// Computes the remainder of a [`Rational`] by a primitive float, with the quotient rounded toward
15171/// zero, correctly rounding the result to the nearest value.
15172///
15173/// The [`Rational`] dividend is used exactly. NaN or zero `y` gives NaN; an infinite `y` returns
15174/// the dividend, rounded.
15175///
15176/// # Worst-case complexity
15177/// $T(n) = O(n \log n \log\log n)$
15178///
15179/// $M(n) = O(n)$
15180///
15181/// where $T$ is time, $M$ is additional memory, and $n$ is `x.significant_bits()`.
15182///
15183/// # Examples
15184/// ```
15185/// use malachite_base::num::float::NiceFloat;
15186/// use malachite_float::float::arithmetic::rem::primitive_float_rational_rem_float;
15187/// use malachite_q::Rational;
15188///
15189/// // 22/7 mod 3 = 1/7
15190/// assert_eq!(
15191/// NiceFloat(primitive_float_rational_rem_float(
15192/// &Rational::from_signeds(22, 7),
15193/// 3.0
15194/// )),
15195/// NiceFloat(0.14285714285714285)
15196/// );
15197/// ```
15198#[allow(clippy::type_repetition_in_bounds)]
15199#[inline]
15200pub fn primitive_float_rational_rem_float<T: PrimitiveFloat>(x: &Rational, y: T) -> T
15201where
15202 Float: From<T> + PartialOrd<T>,
15203 for<'a> T: ExactFrom<&'a Float>,
15204{
15205 emulate_float_to_float_fn(
15206 |y, prec| Float::rational_rem_float_prec_ref_val(x, y, prec),
15207 y,
15208 )
15209}
15210
15211/// Computes the IEEE 754 `remainder` of a [`Rational`] by a primitive float, with the quotient
15212/// rounded to the nearest integer (ties to even), correctly rounding the result to the nearest
15213/// value.
15214///
15215/// The [`Rational`] dividend is used exactly.
15216///
15217/// # Worst-case complexity
15218/// $T(n) = O(n \log n \log\log n)$
15219///
15220/// $M(n) = O(n)$
15221///
15222/// where $T$ is time, $M$ is additional memory, and $n$ is `x.significant_bits()`.
15223///
15224/// # Examples
15225/// ```
15226/// use malachite_base::num::float::NiceFloat;
15227/// use malachite_float::float::arithmetic::rem::primitive_float_rational_ieee_remainder_float;
15228/// use malachite_q::Rational;
15229///
15230/// assert_eq!(
15231/// NiceFloat(primitive_float_rational_ieee_remainder_float(
15232/// &Rational::from_signeds(22, 7),
15233/// 3.0
15234/// )),
15235/// NiceFloat(0.14285714285714285)
15236/// );
15237/// ```
15238#[allow(clippy::type_repetition_in_bounds)]
15239#[inline]
15240pub fn primitive_float_rational_ieee_remainder_float<T: PrimitiveFloat>(x: &Rational, y: T) -> T
15241where
15242 Float: From<T> + PartialOrd<T>,
15243 for<'a> T: ExactFrom<&'a Float>,
15244{
15245 emulate_float_to_float_fn(
15246 |y, prec| Float::rational_ieee_remainder_float_prec_ref_val(x, y, prec),
15247 y,
15248 )
15249}
15250
15251/// Computes the remainder of a primitive float by a `u64`, with the quotient rounded toward zero,
15252/// correctly rounding the result to the nearest value.
15253///
15254/// The modulus is used exactly, even when it is not representable in the primitive float type (any
15255/// `u64` above $2^{T::MANTISSA\\_WIDTH+1}$ has neighbors that round to the same float). A zero
15256/// modulus gives NaN, matching `mpfr_fmod_ui`.
15257///
15258/// # Worst-case complexity
15259/// Constant time and additional memory.
15260///
15261/// # Examples
15262/// ```
15263/// use malachite_base::num::float::NiceFloat;
15264/// use malachite_float::float::arithmetic::rem::primitive_float_rem_unsigned;
15265///
15266/// assert_eq!(
15267/// NiceFloat(primitive_float_rem_unsigned(10.5, 3)),
15268/// NiceFloat(1.5)
15269/// );
15270/// // u64::MAX is not exactly representable as an f64, but the remainder is taken exactly
15271/// assert_eq!(
15272/// NiceFloat(primitive_float_rem_unsigned(1.0e30, u64::MAX)),
15273/// NiceFloat(5.076964209140211e18)
15274/// );
15275/// ```
15276#[allow(clippy::type_repetition_in_bounds)]
15277#[inline]
15278pub fn primitive_float_rem_unsigned<T: PrimitiveFloat>(x: T, y: u64) -> T
15279where
15280 Float: From<T> + PartialOrd<T>,
15281 for<'a> T: ExactFrom<&'a Float>,
15282{
15283 emulate_float_to_float_fn(|x, prec| x.rem_unsigned_prec(y, prec), x)
15284}
15285
15286/// Computes the remainder of two primitive floats along with the low bits of the quotient, with the
15287/// quotient rounded toward zero, using emulated [`Float`] arithmetic.
15288///
15289/// This is the analog of C's `fmodquo`-style functions: the `i64` agrees with the exact quotient
15290/// $q$ in its low 63 bits and has $q$'s sign.
15291///
15292/// # Worst-case complexity
15293/// Constant time and additional memory.
15294///
15295/// # Examples
15296/// ```
15297/// use malachite_base::num::float::NiceFloat;
15298/// use malachite_float::float::arithmetic::rem::primitive_float_rem_and_quotient_bits;
15299///
15300/// let (r, q) = primitive_float_rem_and_quotient_bits(100.0, 7.0);
15301/// assert_eq!(NiceFloat(r), NiceFloat(2.0));
15302/// assert_eq!(q, 14);
15303/// ```
15304#[allow(clippy::type_repetition_in_bounds)]
15305#[inline]
15306pub fn primitive_float_rem_and_quotient_bits<T: PrimitiveFloat>(x: T, y: T) -> (T, i64)
15307where
15308 Float: From<T> + PartialOrd<T>,
15309 for<'a> T: ExactFrom<&'a Float>,
15310{
15311 emulate_float_float_to_float_and_i64_fn(Float::rem_and_quotient_bits_prec, x, y)
15312}
15313
15314/// Computes the IEEE 754 `remainder` of two primitive floats along with the low bits of the
15315/// quotient, with the quotient rounded to the nearest integer (ties to even), using emulated
15316/// [`Float`] arithmetic.
15317///
15318/// This is the analog of C's `remquo`: the `i64` agrees with the exact quotient $q$ in its low 63
15319/// bits and has $q$'s sign.
15320///
15321/// # Worst-case complexity
15322/// Constant time and additional memory.
15323///
15324/// # Examples
15325/// ```
15326/// use malachite_base::num::float::NiceFloat;
15327/// use malachite_float::float::arithmetic::rem::primitive_float_ieee_remainder_and_quotient_bits;
15328///
15329/// let (r, q) = primitive_float_ieee_remainder_and_quotient_bits(14.0, 3.0);
15330/// assert_eq!(NiceFloat(r), NiceFloat(-1.0));
15331/// assert_eq!(q, 5);
15332/// ```
15333#[allow(clippy::type_repetition_in_bounds)]
15334#[inline]
15335pub fn primitive_float_ieee_remainder_and_quotient_bits<T: PrimitiveFloat>(x: T, y: T) -> (T, i64)
15336where
15337 Float: From<T> + PartialOrd<T>,
15338 for<'a> T: ExactFrom<&'a Float>,
15339{
15340 emulate_float_float_to_float_and_i64_fn(Float::ieee_remainder_and_quotient_bits_prec, x, y)
15341}
15342
15343/// Computes the remainder of a primitive float by a [`Rational`] along with the low bits of the
15344/// quotient, with the quotient rounded toward zero, correctly rounding the remainder to the nearest
15345/// value.
15346///
15347/// The [`Rational`] modulus is used exactly.
15348///
15349/// # Worst-case complexity
15350/// $T(n) = O(n \log n \log\log n)$
15351///
15352/// $M(n) = O(n)$
15353///
15354/// where $T$ is time, $M$ is additional memory, and $n$ is `y.significant_bits()`.
15355///
15356/// # Examples
15357/// ```
15358/// use malachite_base::num::float::NiceFloat;
15359/// use malachite_float::float::arithmetic::rem::primitive_float_rem_rational_and_quotient_bits;
15360/// use malachite_q::Rational;
15361///
15362/// let (r, q) =
15363/// primitive_float_rem_rational_and_quotient_bits(10.0, &Rational::from_signeds(22, 7));
15364/// assert_eq!(NiceFloat(r), NiceFloat(0.5714285714285714));
15365/// assert_eq!(q, 3);
15366/// ```
15367#[allow(clippy::type_repetition_in_bounds)]
15368#[inline]
15369pub fn primitive_float_rem_rational_and_quotient_bits<T: PrimitiveFloat>(
15370 x: T,
15371 y: &Rational,
15372) -> (T, i64)
15373where
15374 Float: From<T> + PartialOrd<T>,
15375 for<'a> T: ExactFrom<&'a Float>,
15376{
15377 emulate_float_to_float_and_i64_fn(
15378 |x, prec| Float::rem_rational_and_quotient_bits_prec_val_ref(x, y, prec),
15379 x,
15380 )
15381}
15382
15383/// Computes the IEEE 754 `remainder` of a primitive float by a [`Rational`] along with the low bits
15384/// of the quotient, with the quotient rounded to the nearest integer (ties to even), correctly
15385/// rounding the remainder to the nearest value.
15386///
15387/// The [`Rational`] modulus is used exactly. This is the natural tool for additive argument
15388/// reduction against a non-dyadic constant: reducing against a [`Rational`] approximation of, say,
15389/// $\pi/2$ yields the reduced argument and the quadrant bits in one call.
15390///
15391/// # Worst-case complexity
15392/// $T(n) = O(n \log n \log\log n)$
15393///
15394/// $M(n) = O(n)$
15395///
15396/// where $T$ is time, $M$ is additional memory, and $n$ is `y.significant_bits()`.
15397///
15398/// # Examples
15399/// ```
15400/// use malachite_base::num::float::NiceFloat;
15401/// use malachite_float::float::arithmetic::rem::*;
15402/// use malachite_q::Rational;
15403///
15404/// let (r, q) = primitive_float_ieee_remainder_rational_and_quotient_bits(
15405/// 10.0,
15406/// &Rational::from_signeds(22, 7),
15407/// );
15408/// assert_eq!(NiceFloat(r), NiceFloat(0.5714285714285714));
15409/// assert_eq!(q, 3);
15410/// ```
15411#[allow(clippy::type_repetition_in_bounds)]
15412#[inline]
15413pub fn primitive_float_ieee_remainder_rational_and_quotient_bits<T: PrimitiveFloat>(
15414 x: T,
15415 y: &Rational,
15416) -> (T, i64)
15417where
15418 Float: From<T> + PartialOrd<T>,
15419 for<'a> T: ExactFrom<&'a Float>,
15420{
15421 emulate_float_to_float_and_i64_fn(
15422 |x, prec| Float::ieee_remainder_rational_and_quotient_bits_prec_val_ref(x, y, prec),
15423 x,
15424 )
15425}