malachite_float/float/comparison/min_max.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// Uses code adopted from the GNU MPFR Library.
4//
5// Copyright © 2001-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/>.
12use crate::emulate_float_to_float_fn;
13use malachite_base::num::basic::floats::PrimitiveFloat;
14use malachite_base::num::conversion::traits::ExactFrom;
15
16use crate::Float;
17use crate::InnerFloat::Zero;
18use core::cmp::Ordering::{Equal, Greater, Less};
19use core::cmp::{Ordering, max};
20use malachite_base::num::basic::traits::Zero as ZeroTrait;
21use malachite_base::num::logic::traits::SignificantBits;
22use malachite_base::rounding_modes::RoundingMode::{self, Nearest};
23use malachite_q::Rational;
24
25// Which operand mpfr_min/mpfr_max selects: one NaN gives the other; both NaN gives the first, whose
26// rounding produces the NaN result; two zeros are picked by sign (min prefers the negative zero,
27// max the positive); otherwise the comparison decides, with ties going to the first operand. This
28// is the case analysis of mpfr_min and mpfr_max from minmax.c, MPFR 4.2.2.
29enum Choice {
30 First,
31 Second,
32}
33
34fn min_max_choice(x: &Float, y: &Float, is_max: bool) -> Choice {
35 match (x.is_nan(), y.is_nan()) {
36 (_, true) => Choice::First,
37 (true, false) => Choice::Second,
38 (false, false) => {
39 if x.is_zero() && y.is_zero() {
40 // pick by sign: min takes a negative zero, max a positive one
41 if x.is_sign_negative() == is_max {
42 Choice::Second
43 } else {
44 Choice::First
45 }
46 } else {
47 let le = x.partial_cmp(y) != Some(Ordering::Greater);
48 if le == is_max {
49 Choice::Second
50 } else {
51 Choice::First
52 }
53 }
54 }
55 }
56}
57
58impl Float {
59 /// Returns the minimum of two [`Float`]s, rounding the result to the specified precision and
60 /// with the specified rounding mode. An [`Ordering`] is also returned, indicating whether the
61 /// rounded minimum is less than, equal to, or greater than the exact minimum. Whenever this
62 /// function returns a `NaN` it also returns `Equal`.
63 ///
64 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
65 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
66 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
67 ///
68 /// The selected operand is then rounded to `prec` bits using `rm`, as by
69 /// [`Float::from_float_prec_round`]; like that function, this function may overflow if the
70 /// selected operand has the maximum exponent, and it never underflows.
71 ///
72 /// Both [`Float`]s are taken by value.
73 ///
74 /// If the output has a precision, it is `prec`.
75 ///
76 /// If you know you'll be using `Nearest`, consider using [`Float::min_prec`] instead. If you
77 /// know that your target precision is the maximum of the precisions of the two inputs, consider
78 /// using [`Float::min_round`] instead. If both of these things are true, consider using
79 /// [`Float::min`] instead.
80 ///
81 /// # Worst-case complexity
82 /// $T(n, m) = O(n + m)$
83 ///
84 /// $M(n) = O(n)$
85 ///
86 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
87 /// `max(self.significant_bits(), other.significant_bits())`.
88 ///
89 /// # Panics
90 /// Panics if `prec` is zero, or if `rm` is `Exact` but the selected operand cannot be
91 /// represented exactly at a precision of `prec` bits.
92 ///
93 /// # Examples
94 /// ```
95 /// use core::f64::consts::{E, PI};
96 /// use malachite_base::rounding_modes::RoundingMode::*;
97 /// use malachite_float::Float;
98 /// use std::cmp::Ordering::*;
99 ///
100 /// let (min, o) = Float::from(PI).min_prec_round(Float::from(E), 5, Floor);
101 /// assert_eq!(min.to_string(), "2.62");
102 /// assert_eq!(o, Less);
103 ///
104 /// let (min, o) = Float::from(PI).min_prec_round(Float::from(E), 5, Ceiling);
105 /// assert_eq!(min.to_string(), "2.75");
106 /// assert_eq!(o, Greater);
107 ///
108 /// let (min, o) = Float::from(PI).min_prec_round(Float::from(E), 20, Nearest);
109 /// assert_eq!(min.to_string(), "2.7182808");
110 /// assert_eq!(o, Less);
111 /// ```
112 #[inline]
113 pub fn min_prec_round(self, other: Self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
114 match min_max_choice(&self, &other, false) {
115 Choice::First => Self::from_float_prec_round(self, prec, rm),
116 Choice::Second => Self::from_float_prec_round(other, prec, rm),
117 }
118 }
119
120 /// Returns the minimum of two [`Float`]s, rounding the result to the specified precision and
121 /// with the specified rounding mode. An [`Ordering`] is also returned, indicating whether the
122 /// rounded minimum is less than, equal to, or greater than the exact minimum. Whenever this
123 /// function returns a `NaN` it also returns `Equal`.
124 ///
125 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
126 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
127 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
128 ///
129 /// The selected operand is then rounded to `prec` bits using `rm`, as by
130 /// [`Float::from_float_prec_round`]; like that function, this function may overflow if the
131 /// selected operand has the maximum exponent, and it never underflows.
132 ///
133 /// The first [`Float`] is taken by value and the second by reference.
134 ///
135 /// If the output has a precision, it is `prec`.
136 ///
137 /// If you know you'll be using `Nearest`, consider using [`Float::min_prec`] instead. If you
138 /// know that your target precision is the maximum of the precisions of the two inputs, consider
139 /// using [`Float::min_round`] instead. If both of these things are true, consider using
140 /// [`Float::min`] instead.
141 ///
142 /// # Worst-case complexity
143 /// $T(n, m) = O(n + m)$
144 ///
145 /// $M(n) = O(n)$
146 ///
147 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
148 /// `max(self.significant_bits(), other.significant_bits())`.
149 ///
150 /// # Panics
151 /// Panics if `prec` is zero, or if `rm` is `Exact` but the selected operand cannot be
152 /// represented exactly at a precision of `prec` bits.
153 ///
154 /// # Examples
155 /// ```
156 /// use core::f64::consts::{E, PI};
157 /// use malachite_base::rounding_modes::RoundingMode::*;
158 /// use malachite_float::Float;
159 /// use std::cmp::Ordering::*;
160 ///
161 /// let (min, o) = Float::from(PI).min_prec_round_val_ref(&Float::from(E), 5, Floor);
162 /// assert_eq!(min.to_string(), "2.62");
163 /// assert_eq!(o, Less);
164 ///
165 /// let (min, o) = Float::from(PI).min_prec_round_val_ref(&Float::from(E), 5, Ceiling);
166 /// assert_eq!(min.to_string(), "2.75");
167 /// assert_eq!(o, Greater);
168 ///
169 /// let (min, o) = Float::from(PI).min_prec_round_val_ref(&Float::from(E), 20, Nearest);
170 /// assert_eq!(min.to_string(), "2.7182808");
171 /// assert_eq!(o, Less);
172 /// ```
173 #[inline]
174 pub fn min_prec_round_val_ref(
175 self,
176 other: &Self,
177 prec: u64,
178 rm: RoundingMode,
179 ) -> (Self, Ordering) {
180 match min_max_choice(&self, other, false) {
181 Choice::First => Self::from_float_prec_round(self, prec, rm),
182 Choice::Second => Self::from_float_prec_round_ref(other, prec, rm),
183 }
184 }
185
186 /// Returns the minimum of two [`Float`]s, rounding the result to the specified precision and
187 /// with the specified rounding mode. An [`Ordering`] is also returned, indicating whether the
188 /// rounded minimum is less than, equal to, or greater than the exact minimum. Whenever this
189 /// function returns a `NaN` it also returns `Equal`.
190 ///
191 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
192 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
193 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
194 ///
195 /// The selected operand is then rounded to `prec` bits using `rm`, as by
196 /// [`Float::from_float_prec_round`]; like that function, this function may overflow if the
197 /// selected operand has the maximum exponent, and it never underflows.
198 ///
199 /// The first [`Float`] is taken by reference and the second by value.
200 ///
201 /// If the output has a precision, it is `prec`.
202 ///
203 /// If you know you'll be using `Nearest`, consider using [`Float::min_prec`] instead. If you
204 /// know that your target precision is the maximum of the precisions of the two inputs, consider
205 /// using [`Float::min_round`] instead. If both of these things are true, consider using
206 /// [`Float::min`] instead.
207 ///
208 /// # Worst-case complexity
209 /// $T(n, m) = O(n + m)$
210 ///
211 /// $M(n) = O(n)$
212 ///
213 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
214 /// `max(self.significant_bits(), other.significant_bits())`.
215 ///
216 /// # Panics
217 /// Panics if `prec` is zero, or if `rm` is `Exact` but the selected operand cannot be
218 /// represented exactly at a precision of `prec` bits.
219 ///
220 /// # Examples
221 /// ```
222 /// use core::f64::consts::{E, PI};
223 /// use malachite_base::rounding_modes::RoundingMode::*;
224 /// use malachite_float::Float;
225 /// use std::cmp::Ordering::*;
226 ///
227 /// let (min, o) = Float::from(PI).min_prec_round_ref_val(Float::from(E), 5, Floor);
228 /// assert_eq!(min.to_string(), "2.62");
229 /// assert_eq!(o, Less);
230 ///
231 /// let (min, o) = Float::from(PI).min_prec_round_ref_val(Float::from(E), 5, Ceiling);
232 /// assert_eq!(min.to_string(), "2.75");
233 /// assert_eq!(o, Greater);
234 ///
235 /// let (min, o) = Float::from(PI).min_prec_round_ref_val(Float::from(E), 20, Nearest);
236 /// assert_eq!(min.to_string(), "2.7182808");
237 /// assert_eq!(o, Less);
238 /// ```
239 #[inline]
240 pub fn min_prec_round_ref_val(
241 &self,
242 other: Self,
243 prec: u64,
244 rm: RoundingMode,
245 ) -> (Self, Ordering) {
246 match min_max_choice(self, &other, false) {
247 Choice::First => Self::from_float_prec_round_ref(self, prec, rm),
248 Choice::Second => Self::from_float_prec_round(other, prec, rm),
249 }
250 }
251
252 /// Returns the minimum of two [`Float`]s, rounding the result to the specified precision and
253 /// with the specified rounding mode. An [`Ordering`] is also returned, indicating whether the
254 /// rounded minimum is less than, equal to, or greater than the exact minimum. Whenever this
255 /// function returns a `NaN` it also returns `Equal`.
256 ///
257 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
258 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
259 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
260 ///
261 /// The selected operand is then rounded to `prec` bits using `rm`, as by
262 /// [`Float::from_float_prec_round`]; like that function, this function may overflow if the
263 /// selected operand has the maximum exponent, and it never underflows.
264 ///
265 /// Both [`Float`]s are taken by reference.
266 ///
267 /// If the output has a precision, it is `prec`.
268 ///
269 /// If you know you'll be using `Nearest`, consider using [`Float::min_prec`] instead. If you
270 /// know that your target precision is the maximum of the precisions of the two inputs, consider
271 /// using [`Float::min_round`] instead. If both of these things are true, consider using
272 /// [`Float::min`] instead.
273 ///
274 /// # Worst-case complexity
275 /// $T(n, m) = O(n + m)$
276 ///
277 /// $M(n) = O(n)$
278 ///
279 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
280 /// `max(self.significant_bits(), other.significant_bits())`.
281 ///
282 /// # Panics
283 /// Panics if `prec` is zero, or if `rm` is `Exact` but the selected operand cannot be
284 /// represented exactly at a precision of `prec` bits.
285 ///
286 /// # Examples
287 /// ```
288 /// use core::f64::consts::{E, PI};
289 /// use malachite_base::rounding_modes::RoundingMode::*;
290 /// use malachite_float::Float;
291 /// use std::cmp::Ordering::*;
292 ///
293 /// let (min, o) = Float::from(PI).min_prec_round_ref_ref(&Float::from(E), 5, Floor);
294 /// assert_eq!(min.to_string(), "2.62");
295 /// assert_eq!(o, Less);
296 ///
297 /// let (min, o) = Float::from(PI).min_prec_round_ref_ref(&Float::from(E), 5, Ceiling);
298 /// assert_eq!(min.to_string(), "2.75");
299 /// assert_eq!(o, Greater);
300 ///
301 /// let (min, o) = Float::from(PI).min_prec_round_ref_ref(&Float::from(E), 20, Nearest);
302 /// assert_eq!(min.to_string(), "2.7182808");
303 /// assert_eq!(o, Less);
304 /// ```
305 #[inline]
306 pub fn min_prec_round_ref_ref(
307 &self,
308 other: &Self,
309 prec: u64,
310 rm: RoundingMode,
311 ) -> (Self, Ordering) {
312 match min_max_choice(self, other, false) {
313 Choice::First => Self::from_float_prec_round_ref(self, prec, rm),
314 Choice::Second => Self::from_float_prec_round_ref(other, prec, rm),
315 }
316 }
317
318 /// Returns the minimum of two [`Float`]s, rounding the result to the specified precision and
319 /// with the `Nearest` rounding mode. An [`Ordering`] is also returned, indicating whether the
320 /// rounded minimum is less than, equal to, or greater than the exact minimum. Whenever this
321 /// function returns a `NaN` it also returns `Equal`.
322 ///
323 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
324 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
325 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
326 ///
327 /// The selected operand is then rounded to `prec` bits using the `Nearest` rounding mode, as by
328 /// [`Float::from_float_prec`]; like that function, this function may overflow if the selected
329 /// operand has the maximum exponent, and it never underflows.
330 ///
331 /// Both [`Float`]s are taken by value.
332 ///
333 /// If the output has a precision, it is `prec`.
334 ///
335 /// If you want to use a rounding mode other than `Nearest`, consider using
336 /// [`Float::min_prec_round`] instead. If you know that your target precision is the maximum of
337 /// the precisions of the two inputs, consider using [`Float::min`] instead.
338 ///
339 /// # Worst-case complexity
340 /// $T(n, m) = O(n + m)$
341 ///
342 /// $M(n) = O(n)$
343 ///
344 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
345 /// `max(self.significant_bits(), other.significant_bits())`.
346 ///
347 /// # Panics
348 /// Panics if `prec` is zero.
349 ///
350 /// # Examples
351 /// ```
352 /// use core::f64::consts::{E, PI};
353 /// use malachite_float::Float;
354 /// use std::cmp::Ordering::*;
355 ///
356 /// let (min, o) = Float::from(PI).min_prec(Float::from(E), 5);
357 /// assert_eq!(min.to_string(), "2.75");
358 /// assert_eq!(o, Greater);
359 ///
360 /// let (min, o) = Float::from(PI).min_prec(Float::from(E), 20);
361 /// assert_eq!(min.to_string(), "2.7182808");
362 /// assert_eq!(o, Less);
363 /// ```
364 #[inline]
365 pub fn min_prec(self, other: Self, prec: u64) -> (Self, Ordering) {
366 match min_max_choice(&self, &other, false) {
367 Choice::First => Self::from_float_prec(self, prec),
368 Choice::Second => Self::from_float_prec(other, prec),
369 }
370 }
371
372 /// Returns the minimum of two [`Float`]s, rounding the result to the specified precision and
373 /// with the `Nearest` rounding mode. An [`Ordering`] is also returned, indicating whether the
374 /// rounded minimum is less than, equal to, or greater than the exact minimum. Whenever this
375 /// function returns a `NaN` it also returns `Equal`.
376 ///
377 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
378 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
379 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
380 ///
381 /// The selected operand is then rounded to `prec` bits using the `Nearest` rounding mode, as by
382 /// [`Float::from_float_prec`]; like that function, this function may overflow if the selected
383 /// operand has the maximum exponent, and it never underflows.
384 ///
385 /// The first [`Float`] is taken by value and the second by reference.
386 ///
387 /// If the output has a precision, it is `prec`.
388 ///
389 /// If you want to use a rounding mode other than `Nearest`, consider using
390 /// [`Float::min_prec_round`] instead. If you know that your target precision is the maximum of
391 /// the precisions of the two inputs, consider using [`Float::min`] instead.
392 ///
393 /// # Worst-case complexity
394 /// $T(n, m) = O(n + m)$
395 ///
396 /// $M(n) = O(n)$
397 ///
398 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
399 /// `max(self.significant_bits(), other.significant_bits())`.
400 ///
401 /// # Panics
402 /// Panics if `prec` is zero.
403 ///
404 /// # Examples
405 /// ```
406 /// use core::f64::consts::{E, PI};
407 /// use malachite_float::Float;
408 /// use std::cmp::Ordering::*;
409 ///
410 /// let (min, o) = Float::from(PI).min_prec_val_ref(&Float::from(E), 5);
411 /// assert_eq!(min.to_string(), "2.75");
412 /// assert_eq!(o, Greater);
413 ///
414 /// let (min, o) = Float::from(PI).min_prec_val_ref(&Float::from(E), 20);
415 /// assert_eq!(min.to_string(), "2.7182808");
416 /// assert_eq!(o, Less);
417 /// ```
418 #[inline]
419 pub fn min_prec_val_ref(self, other: &Self, prec: u64) -> (Self, Ordering) {
420 match min_max_choice(&self, other, false) {
421 Choice::First => Self::from_float_prec(self, prec),
422 Choice::Second => Self::from_float_prec_ref(other, prec),
423 }
424 }
425
426 /// Returns the minimum of two [`Float`]s, rounding the result to the specified precision and
427 /// with the `Nearest` rounding mode. An [`Ordering`] is also returned, indicating whether the
428 /// rounded minimum is less than, equal to, or greater than the exact minimum. Whenever this
429 /// function returns a `NaN` it also returns `Equal`.
430 ///
431 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
432 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
433 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
434 ///
435 /// The selected operand is then rounded to `prec` bits using the `Nearest` rounding mode, as by
436 /// [`Float::from_float_prec`]; like that function, this function may overflow if the selected
437 /// operand has the maximum exponent, and it never underflows.
438 ///
439 /// The first [`Float`] is taken by reference and the second by value.
440 ///
441 /// If the output has a precision, it is `prec`.
442 ///
443 /// If you want to use a rounding mode other than `Nearest`, consider using
444 /// [`Float::min_prec_round`] instead. If you know that your target precision is the maximum of
445 /// the precisions of the two inputs, consider using [`Float::min`] instead.
446 ///
447 /// # Worst-case complexity
448 /// $T(n, m) = O(n + m)$
449 ///
450 /// $M(n) = O(n)$
451 ///
452 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
453 /// `max(self.significant_bits(), other.significant_bits())`.
454 ///
455 /// # Panics
456 /// Panics if `prec` is zero.
457 ///
458 /// # Examples
459 /// ```
460 /// use core::f64::consts::{E, PI};
461 /// use malachite_float::Float;
462 /// use std::cmp::Ordering::*;
463 ///
464 /// let (min, o) = Float::from(PI).min_prec_ref_val(Float::from(E), 5);
465 /// assert_eq!(min.to_string(), "2.75");
466 /// assert_eq!(o, Greater);
467 ///
468 /// let (min, o) = Float::from(PI).min_prec_ref_val(Float::from(E), 20);
469 /// assert_eq!(min.to_string(), "2.7182808");
470 /// assert_eq!(o, Less);
471 /// ```
472 #[inline]
473 pub fn min_prec_ref_val(&self, other: Self, prec: u64) -> (Self, Ordering) {
474 match min_max_choice(self, &other, false) {
475 Choice::First => Self::from_float_prec_ref(self, prec),
476 Choice::Second => Self::from_float_prec(other, prec),
477 }
478 }
479
480 /// Returns the minimum of two [`Float`]s, rounding the result to the specified precision and
481 /// with the `Nearest` rounding mode. An [`Ordering`] is also returned, indicating whether the
482 /// rounded minimum is less than, equal to, or greater than the exact minimum. Whenever this
483 /// function returns a `NaN` it also returns `Equal`.
484 ///
485 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
486 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
487 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
488 ///
489 /// The selected operand is then rounded to `prec` bits using the `Nearest` rounding mode, as by
490 /// [`Float::from_float_prec`]; like that function, this function may overflow if the selected
491 /// operand has the maximum exponent, and it never underflows.
492 ///
493 /// Both [`Float`]s are taken by reference.
494 ///
495 /// If the output has a precision, it is `prec`.
496 ///
497 /// If you want to use a rounding mode other than `Nearest`, consider using
498 /// [`Float::min_prec_round`] instead. If you know that your target precision is the maximum of
499 /// the precisions of the two inputs, consider using [`Float::min`] instead.
500 ///
501 /// # Worst-case complexity
502 /// $T(n, m) = O(n + m)$
503 ///
504 /// $M(n) = O(n)$
505 ///
506 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
507 /// `max(self.significant_bits(), other.significant_bits())`.
508 ///
509 /// # Panics
510 /// Panics if `prec` is zero.
511 ///
512 /// # Examples
513 /// ```
514 /// use core::f64::consts::{E, PI};
515 /// use malachite_float::Float;
516 /// use std::cmp::Ordering::*;
517 ///
518 /// let (min, o) = Float::from(PI).min_prec_ref_ref(&Float::from(E), 5);
519 /// assert_eq!(min.to_string(), "2.75");
520 /// assert_eq!(o, Greater);
521 ///
522 /// let (min, o) = Float::from(PI).min_prec_ref_ref(&Float::from(E), 20);
523 /// assert_eq!(min.to_string(), "2.7182808");
524 /// assert_eq!(o, Less);
525 /// ```
526 #[inline]
527 pub fn min_prec_ref_ref(&self, other: &Self, prec: u64) -> (Self, Ordering) {
528 match min_max_choice(self, other, false) {
529 Choice::First => Self::from_float_prec_ref(self, prec),
530 Choice::Second => Self::from_float_prec_ref(other, prec),
531 }
532 }
533
534 /// Returns the minimum of two [`Float`]s, rounding the result to the maximum of the operands'
535 /// precisions and with the specified rounding mode. An [`Ordering`] is also returned; since the
536 /// target precision is at least as high as the precision of the selected operand, the rounding
537 /// is always exact, and the result does not depend on `rm`, and the [`Ordering`] is always
538 /// `Equal`.
539 ///
540 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
541 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
542 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
543 ///
544 /// The selected operand is then padded to the target precision. This never rounds, overflows,
545 /// or underflows.
546 ///
547 /// Both [`Float`]s are taken by value.
548 ///
549 /// If the output has a precision, it is the maximum of the operands' precisions.
550 ///
551 /// If you want to specify an output precision, consider using [`Float::min_prec_round`]
552 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
553 /// [`Float::min`] instead.
554 ///
555 /// # Worst-case complexity
556 /// $T(n) = O(n)$
557 ///
558 /// $M(n) = O(n)$
559 ///
560 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
561 /// other.significant_bits())`.
562 ///
563 /// # Examples
564 /// ```
565 /// use core::f64::consts::{E, PI};
566 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
567 /// use malachite_base::rounding_modes::RoundingMode::*;
568 /// use malachite_float::Float;
569 /// use std::cmp::Ordering::*;
570 ///
571 /// let (min, o) = Float::from(PI).min_round(Float::from(E), Floor);
572 /// assert_eq!(min.to_string(), "2.7182818284590451");
573 /// assert_eq!(o, Equal);
574 ///
575 /// let (min, o) = Float::NAN.min_round(Float::from(PI), Floor);
576 /// assert_eq!(min.to_string(), "3.1415926535897931");
577 /// assert_eq!(o, Equal);
578 ///
579 /// let (min, o) = Float::ZERO.min_round(Float::NEGATIVE_ZERO, Floor);
580 /// assert_eq!(min.to_string(), "-0.0");
581 /// assert_eq!(o, Equal);
582 /// ```
583 #[inline]
584 pub fn min_round(self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
585 let target_prec = max(self.significant_bits(), other.significant_bits());
586 match min_max_choice(&self, &other, false) {
587 Choice::First => Self::from_float_prec_round(self, target_prec, rm),
588 Choice::Second => Self::from_float_prec_round(other, target_prec, rm),
589 }
590 }
591
592 /// Returns the minimum of two [`Float`]s, rounding the result to the maximum of the operands'
593 /// precisions and with the specified rounding mode. An [`Ordering`] is also returned; since the
594 /// target precision is at least as high as the precision of the selected operand, the rounding
595 /// is always exact, and the result does not depend on `rm`, and the [`Ordering`] is always
596 /// `Equal`.
597 ///
598 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
599 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
600 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
601 ///
602 /// The selected operand is then padded to the target precision. This never rounds, overflows,
603 /// or underflows.
604 ///
605 /// The first [`Float`] is taken by value and the second by reference.
606 ///
607 /// If the output has a precision, it is the maximum of the operands' precisions.
608 ///
609 /// If you want to specify an output precision, consider using [`Float::min_prec_round`]
610 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
611 /// [`Float::min`] instead.
612 ///
613 /// # Worst-case complexity
614 /// $T(n) = O(n)$
615 ///
616 /// $M(n) = O(n)$
617 ///
618 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
619 /// other.significant_bits())`.
620 ///
621 /// # Examples
622 /// ```
623 /// use core::f64::consts::{E, PI};
624 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
625 /// use malachite_base::rounding_modes::RoundingMode::*;
626 /// use malachite_float::Float;
627 /// use std::cmp::Ordering::*;
628 ///
629 /// let (min, o) = Float::from(PI).min_round_val_ref(&Float::from(E), Floor);
630 /// assert_eq!(min.to_string(), "2.7182818284590451");
631 /// assert_eq!(o, Equal);
632 ///
633 /// let (min, o) = Float::NAN.min_round_val_ref(&Float::from(PI), Floor);
634 /// assert_eq!(min.to_string(), "3.1415926535897931");
635 /// assert_eq!(o, Equal);
636 ///
637 /// let (min, o) = Float::ZERO.min_round_val_ref(&Float::NEGATIVE_ZERO, Floor);
638 /// assert_eq!(min.to_string(), "-0.0");
639 /// assert_eq!(o, Equal);
640 /// ```
641 #[inline]
642 pub fn min_round_val_ref(self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
643 let target_prec = max(self.significant_bits(), other.significant_bits());
644 match min_max_choice(&self, other, false) {
645 Choice::First => Self::from_float_prec_round(self, target_prec, rm),
646 Choice::Second => Self::from_float_prec_round_ref(other, target_prec, rm),
647 }
648 }
649
650 /// Returns the minimum of two [`Float`]s, rounding the result to the maximum of the operands'
651 /// precisions and with the specified rounding mode. An [`Ordering`] is also returned; since the
652 /// target precision is at least as high as the precision of the selected operand, the rounding
653 /// is always exact, and the result does not depend on `rm`, and the [`Ordering`] is always
654 /// `Equal`.
655 ///
656 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
657 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
658 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
659 ///
660 /// The selected operand is then padded to the target precision. This never rounds, overflows,
661 /// or underflows.
662 ///
663 /// The first [`Float`] is taken by reference and the second by value.
664 ///
665 /// If the output has a precision, it is the maximum of the operands' precisions.
666 ///
667 /// If you want to specify an output precision, consider using [`Float::min_prec_round`]
668 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
669 /// [`Float::min`] instead.
670 ///
671 /// # Worst-case complexity
672 /// $T(n) = O(n)$
673 ///
674 /// $M(n) = O(n)$
675 ///
676 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
677 /// other.significant_bits())`.
678 ///
679 /// # Examples
680 /// ```
681 /// use core::f64::consts::{E, PI};
682 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
683 /// use malachite_base::rounding_modes::RoundingMode::*;
684 /// use malachite_float::Float;
685 /// use std::cmp::Ordering::*;
686 ///
687 /// let (min, o) = Float::from(PI).min_round_ref_val(Float::from(E), Floor);
688 /// assert_eq!(min.to_string(), "2.7182818284590451");
689 /// assert_eq!(o, Equal);
690 ///
691 /// let (min, o) = Float::NAN.min_round_ref_val(Float::from(PI), Floor);
692 /// assert_eq!(min.to_string(), "3.1415926535897931");
693 /// assert_eq!(o, Equal);
694 ///
695 /// let (min, o) = Float::ZERO.min_round_ref_val(Float::NEGATIVE_ZERO, Floor);
696 /// assert_eq!(min.to_string(), "-0.0");
697 /// assert_eq!(o, Equal);
698 /// ```
699 #[inline]
700 pub fn min_round_ref_val(&self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
701 let target_prec = max(self.significant_bits(), other.significant_bits());
702 match min_max_choice(self, &other, false) {
703 Choice::First => Self::from_float_prec_round_ref(self, target_prec, rm),
704 Choice::Second => Self::from_float_prec_round(other, target_prec, rm),
705 }
706 }
707
708 /// Returns the minimum of two [`Float`]s, rounding the result to the maximum of the operands'
709 /// precisions and with the specified rounding mode. An [`Ordering`] is also returned; since the
710 /// target precision is at least as high as the precision of the selected operand, the rounding
711 /// is always exact, and the result does not depend on `rm`, and the [`Ordering`] is always
712 /// `Equal`.
713 ///
714 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
715 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
716 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
717 ///
718 /// The selected operand is then padded to the target precision. This never rounds, overflows,
719 /// or underflows.
720 ///
721 /// Both [`Float`]s are taken by reference.
722 ///
723 /// If the output has a precision, it is the maximum of the operands' precisions.
724 ///
725 /// If you want to specify an output precision, consider using [`Float::min_prec_round`]
726 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
727 /// [`Float::min`] instead.
728 ///
729 /// # Worst-case complexity
730 /// $T(n) = O(n)$
731 ///
732 /// $M(n) = O(n)$
733 ///
734 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
735 /// other.significant_bits())`.
736 ///
737 /// # Examples
738 /// ```
739 /// use core::f64::consts::{E, PI};
740 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
741 /// use malachite_base::rounding_modes::RoundingMode::*;
742 /// use malachite_float::Float;
743 /// use std::cmp::Ordering::*;
744 ///
745 /// let (min, o) = Float::from(PI).min_round_ref_ref(&Float::from(E), Floor);
746 /// assert_eq!(min.to_string(), "2.7182818284590451");
747 /// assert_eq!(o, Equal);
748 ///
749 /// let (min, o) = Float::NAN.min_round_ref_ref(&Float::from(PI), Floor);
750 /// assert_eq!(min.to_string(), "3.1415926535897931");
751 /// assert_eq!(o, Equal);
752 ///
753 /// let (min, o) = Float::ZERO.min_round_ref_ref(&Float::NEGATIVE_ZERO, Floor);
754 /// assert_eq!(min.to_string(), "-0.0");
755 /// assert_eq!(o, Equal);
756 /// ```
757 #[inline]
758 pub fn min_round_ref_ref(&self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
759 let target_prec = max(self.significant_bits(), other.significant_bits());
760 match min_max_choice(self, other, false) {
761 Choice::First => Self::from_float_prec_round_ref(self, target_prec, rm),
762 Choice::Second => Self::from_float_prec_round_ref(other, target_prec, rm),
763 }
764 }
765
766 /// Returns the minimum of two [`Float`]s, rounding the result to the maximum of the operands'
767 /// precisions. An [`Ordering`] is also returned; since the target precision is at least as high
768 /// as the precision of the selected operand, the rounding is always exact, and the [`Ordering`]
769 /// is always `Equal`.
770 ///
771 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
772 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
773 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
774 ///
775 /// The selected operand is then padded to the target precision. This never rounds, overflows,
776 /// or underflows.
777 ///
778 /// Both [`Float`]s are taken by value.
779 ///
780 /// If the output has a precision, it is the maximum of the operands' precisions.
781 ///
782 /// If you want to use a rounding mode other than `Nearest`, consider using [`Float::min_round`]
783 /// instead. If you want to specify an output precision, consider using [`Float::min_prec`]
784 /// instead.
785 ///
786 /// # Worst-case complexity
787 /// $T(n) = O(n)$
788 ///
789 /// $M(n) = O(n)$
790 ///
791 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
792 /// other.significant_bits())`.
793 ///
794 /// # Examples
795 /// ```
796 /// use core::f64::consts::{E, PI};
797 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
798 /// use malachite_float::Float;
799 /// use std::cmp::Ordering::*;
800 ///
801 /// let (min, o) = Float::from(PI).min(Float::from(E));
802 /// assert_eq!(min.to_string(), "2.7182818284590451");
803 /// assert_eq!(o, Equal);
804 ///
805 /// let (min, o) = Float::NAN.min(Float::from(PI));
806 /// assert_eq!(min.to_string(), "3.1415926535897931");
807 /// assert_eq!(o, Equal);
808 ///
809 /// let (min, o) = Float::ZERO.min(Float::NEGATIVE_ZERO);
810 /// assert_eq!(min.to_string(), "-0.0");
811 /// assert_eq!(o, Equal);
812 /// ```
813 #[inline]
814 pub fn min(self, other: Self) -> (Self, Ordering) {
815 let target_prec = max(self.significant_bits(), other.significant_bits());
816 match min_max_choice(&self, &other, false) {
817 Choice::First => Self::from_float_prec(self, target_prec),
818 Choice::Second => Self::from_float_prec(other, target_prec),
819 }
820 }
821
822 /// Returns the minimum of two [`Float`]s, rounding the result to the maximum of the operands'
823 /// precisions. An [`Ordering`] is also returned; since the target precision is at least as high
824 /// as the precision of the selected operand, the rounding is always exact, and the [`Ordering`]
825 /// is always `Equal`.
826 ///
827 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
828 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
829 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
830 ///
831 /// The selected operand is then padded to the target precision. This never rounds, overflows,
832 /// or underflows.
833 ///
834 /// The first [`Float`] is taken by value and the second by reference.
835 ///
836 /// If the output has a precision, it is the maximum of the operands' precisions.
837 ///
838 /// If you want to use a rounding mode other than `Nearest`, consider using [`Float::min_round`]
839 /// instead. If you want to specify an output precision, consider using [`Float::min_prec`]
840 /// instead.
841 ///
842 /// # Worst-case complexity
843 /// $T(n) = O(n)$
844 ///
845 /// $M(n) = O(n)$
846 ///
847 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
848 /// other.significant_bits())`.
849 ///
850 /// # Examples
851 /// ```
852 /// use core::f64::consts::{E, PI};
853 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
854 /// use malachite_float::Float;
855 /// use std::cmp::Ordering::*;
856 ///
857 /// let (min, o) = Float::from(PI).min_val_ref(&Float::from(E));
858 /// assert_eq!(min.to_string(), "2.7182818284590451");
859 /// assert_eq!(o, Equal);
860 ///
861 /// let (min, o) = Float::NAN.min_val_ref(&Float::from(PI));
862 /// assert_eq!(min.to_string(), "3.1415926535897931");
863 /// assert_eq!(o, Equal);
864 ///
865 /// let (min, o) = Float::ZERO.min_val_ref(&Float::NEGATIVE_ZERO);
866 /// assert_eq!(min.to_string(), "-0.0");
867 /// assert_eq!(o, Equal);
868 /// ```
869 #[inline]
870 pub fn min_val_ref(self, other: &Self) -> (Self, Ordering) {
871 let target_prec = max(self.significant_bits(), other.significant_bits());
872 match min_max_choice(&self, other, false) {
873 Choice::First => Self::from_float_prec(self, target_prec),
874 Choice::Second => Self::from_float_prec_ref(other, target_prec),
875 }
876 }
877
878 /// Returns the minimum of two [`Float`]s, rounding the result to the maximum of the operands'
879 /// precisions. An [`Ordering`] is also returned; since the target precision is at least as high
880 /// as the precision of the selected operand, the rounding is always exact, and the [`Ordering`]
881 /// is always `Equal`.
882 ///
883 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
884 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
885 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
886 ///
887 /// The selected operand is then padded to the target precision. This never rounds, overflows,
888 /// or underflows.
889 ///
890 /// The first [`Float`] is taken by reference and the second by value.
891 ///
892 /// If the output has a precision, it is the maximum of the operands' precisions.
893 ///
894 /// If you want to use a rounding mode other than `Nearest`, consider using [`Float::min_round`]
895 /// instead. If you want to specify an output precision, consider using [`Float::min_prec`]
896 /// instead.
897 ///
898 /// # Worst-case complexity
899 /// $T(n) = O(n)$
900 ///
901 /// $M(n) = O(n)$
902 ///
903 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
904 /// other.significant_bits())`.
905 ///
906 /// # Examples
907 /// ```
908 /// use core::f64::consts::{E, PI};
909 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
910 /// use malachite_float::Float;
911 /// use std::cmp::Ordering::*;
912 ///
913 /// let (min, o) = Float::from(PI).min_ref_val(Float::from(E));
914 /// assert_eq!(min.to_string(), "2.7182818284590451");
915 /// assert_eq!(o, Equal);
916 ///
917 /// let (min, o) = Float::NAN.min_ref_val(Float::from(PI));
918 /// assert_eq!(min.to_string(), "3.1415926535897931");
919 /// assert_eq!(o, Equal);
920 ///
921 /// let (min, o) = Float::ZERO.min_ref_val(Float::NEGATIVE_ZERO);
922 /// assert_eq!(min.to_string(), "-0.0");
923 /// assert_eq!(o, Equal);
924 /// ```
925 #[inline]
926 pub fn min_ref_val(&self, other: Self) -> (Self, Ordering) {
927 let target_prec = max(self.significant_bits(), other.significant_bits());
928 match min_max_choice(self, &other, false) {
929 Choice::First => Self::from_float_prec_ref(self, target_prec),
930 Choice::Second => Self::from_float_prec(other, target_prec),
931 }
932 }
933
934 /// Returns the minimum of two [`Float`]s, rounding the result to the maximum of the operands'
935 /// precisions. An [`Ordering`] is also returned; since the target precision is at least as high
936 /// as the precision of the selected operand, the rounding is always exact, and the [`Ordering`]
937 /// is always `Equal`.
938 ///
939 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
940 /// result is `NaN`. If both operands are zeros, a negative zero is selected if either zero is
941 /// negative, and a positive zero otherwise. Otherwise, the smaller operand is selected.
942 ///
943 /// The selected operand is then padded to the target precision. This never rounds, overflows,
944 /// or underflows.
945 ///
946 /// Both [`Float`]s are taken by reference.
947 ///
948 /// If the output has a precision, it is the maximum of the operands' precisions.
949 ///
950 /// If you want to use a rounding mode other than `Nearest`, consider using [`Float::min_round`]
951 /// instead. If you want to specify an output precision, consider using [`Float::min_prec`]
952 /// instead.
953 ///
954 /// # Worst-case complexity
955 /// $T(n) = O(n)$
956 ///
957 /// $M(n) = O(n)$
958 ///
959 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
960 /// other.significant_bits())`.
961 ///
962 /// # Examples
963 /// ```
964 /// use core::f64::consts::{E, PI};
965 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
966 /// use malachite_float::Float;
967 /// use std::cmp::Ordering::*;
968 ///
969 /// let (min, o) = Float::from(PI).min_ref_ref(&Float::from(E));
970 /// assert_eq!(min.to_string(), "2.7182818284590451");
971 /// assert_eq!(o, Equal);
972 ///
973 /// let (min, o) = Float::NAN.min_ref_ref(&Float::from(PI));
974 /// assert_eq!(min.to_string(), "3.1415926535897931");
975 /// assert_eq!(o, Equal);
976 ///
977 /// let (min, o) = Float::ZERO.min_ref_ref(&Float::NEGATIVE_ZERO);
978 /// assert_eq!(min.to_string(), "-0.0");
979 /// assert_eq!(o, Equal);
980 /// ```
981 #[inline]
982 pub fn min_ref_ref(&self, other: &Self) -> (Self, Ordering) {
983 let target_prec = max(self.significant_bits(), other.significant_bits());
984 match min_max_choice(self, other, false) {
985 Choice::First => Self::from_float_prec_ref(self, target_prec),
986 Choice::Second => Self::from_float_prec_ref(other, target_prec),
987 }
988 }
989
990 /// Returns the maximum of two [`Float`]s, rounding the result to the specified precision and
991 /// with the specified rounding mode. An [`Ordering`] is also returned, indicating whether the
992 /// rounded maximum is less than, equal to, or greater than the exact maximum. Whenever this
993 /// function returns a `NaN` it also returns `Equal`.
994 ///
995 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
996 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
997 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
998 ///
999 /// The selected operand is then rounded to `prec` bits using `rm`, as by
1000 /// [`Float::from_float_prec_round`]; like that function, this function may overflow if the
1001 /// selected operand has the maximum exponent, and it never underflows.
1002 ///
1003 /// Both [`Float`]s are taken by value.
1004 ///
1005 /// If the output has a precision, it is `prec`.
1006 ///
1007 /// If you know you'll be using `Nearest`, consider using [`Float::max_prec`] instead. If you
1008 /// know that your target precision is the maximum of the precisions of the two inputs, consider
1009 /// using [`Float::max_round`] instead. If both of these things are true, consider using
1010 /// [`Float::max`] instead.
1011 ///
1012 /// # Worst-case complexity
1013 /// $T(n, m) = O(n + m)$
1014 ///
1015 /// $M(n) = O(n)$
1016 ///
1017 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1018 /// `max(self.significant_bits(), other.significant_bits())`.
1019 ///
1020 /// # Panics
1021 /// Panics if `prec` is zero, or if `rm` is `Exact` but the selected operand cannot be
1022 /// represented exactly at a precision of `prec` bits.
1023 ///
1024 /// # Examples
1025 /// ```
1026 /// use core::f64::consts::{E, PI};
1027 /// use malachite_base::rounding_modes::RoundingMode::*;
1028 /// use malachite_float::Float;
1029 /// use std::cmp::Ordering::*;
1030 ///
1031 /// let (max, o) = Float::from(PI).max_prec_round(Float::from(E), 5, Floor);
1032 /// assert_eq!(max.to_string(), "3.12");
1033 /// assert_eq!(o, Less);
1034 ///
1035 /// let (max, o) = Float::from(PI).max_prec_round(Float::from(E), 5, Ceiling);
1036 /// assert_eq!(max.to_string(), "3.25");
1037 /// assert_eq!(o, Greater);
1038 ///
1039 /// let (max, o) = Float::from(PI).max_prec_round(Float::from(E), 20, Nearest);
1040 /// assert_eq!(max.to_string(), "3.1415939");
1041 /// assert_eq!(o, Greater);
1042 /// ```
1043 #[inline]
1044 pub fn max_prec_round(self, other: Self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
1045 match min_max_choice(&self, &other, true) {
1046 Choice::First => Self::from_float_prec_round(self, prec, rm),
1047 Choice::Second => Self::from_float_prec_round(other, prec, rm),
1048 }
1049 }
1050
1051 /// Returns the maximum of two [`Float`]s, rounding the result to the specified precision and
1052 /// with the specified rounding mode. An [`Ordering`] is also returned, indicating whether the
1053 /// rounded maximum is less than, equal to, or greater than the exact maximum. Whenever this
1054 /// function returns a `NaN` it also returns `Equal`.
1055 ///
1056 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
1057 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
1058 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
1059 ///
1060 /// The selected operand is then rounded to `prec` bits using `rm`, as by
1061 /// [`Float::from_float_prec_round`]; like that function, this function may overflow if the
1062 /// selected operand has the maximum exponent, and it never underflows.
1063 ///
1064 /// The first [`Float`] is taken by value and the second by reference.
1065 ///
1066 /// If the output has a precision, it is `prec`.
1067 ///
1068 /// If you know you'll be using `Nearest`, consider using [`Float::max_prec`] instead. If you
1069 /// know that your target precision is the maximum of the precisions of the two inputs, consider
1070 /// using [`Float::max_round`] instead. If both of these things are true, consider using
1071 /// [`Float::max`] instead.
1072 ///
1073 /// # Worst-case complexity
1074 /// $T(n, m) = O(n + m)$
1075 ///
1076 /// $M(n) = O(n)$
1077 ///
1078 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1079 /// `max(self.significant_bits(), other.significant_bits())`.
1080 ///
1081 /// # Panics
1082 /// Panics if `prec` is zero, or if `rm` is `Exact` but the selected operand cannot be
1083 /// represented exactly at a precision of `prec` bits.
1084 ///
1085 /// # Examples
1086 /// ```
1087 /// use core::f64::consts::{E, PI};
1088 /// use malachite_base::rounding_modes::RoundingMode::*;
1089 /// use malachite_float::Float;
1090 /// use std::cmp::Ordering::*;
1091 ///
1092 /// let (max, o) = Float::from(PI).max_prec_round_val_ref(&Float::from(E), 5, Floor);
1093 /// assert_eq!(max.to_string(), "3.12");
1094 /// assert_eq!(o, Less);
1095 ///
1096 /// let (max, o) = Float::from(PI).max_prec_round_val_ref(&Float::from(E), 5, Ceiling);
1097 /// assert_eq!(max.to_string(), "3.25");
1098 /// assert_eq!(o, Greater);
1099 ///
1100 /// let (max, o) = Float::from(PI).max_prec_round_val_ref(&Float::from(E), 20, Nearest);
1101 /// assert_eq!(max.to_string(), "3.1415939");
1102 /// assert_eq!(o, Greater);
1103 /// ```
1104 #[inline]
1105 pub fn max_prec_round_val_ref(
1106 self,
1107 other: &Self,
1108 prec: u64,
1109 rm: RoundingMode,
1110 ) -> (Self, Ordering) {
1111 match min_max_choice(&self, other, true) {
1112 Choice::First => Self::from_float_prec_round(self, prec, rm),
1113 Choice::Second => Self::from_float_prec_round_ref(other, prec, rm),
1114 }
1115 }
1116
1117 /// Returns the maximum of two [`Float`]s, rounding the result to the specified precision and
1118 /// with the specified rounding mode. An [`Ordering`] is also returned, indicating whether the
1119 /// rounded maximum is less than, equal to, or greater than the exact maximum. Whenever this
1120 /// function returns a `NaN` it also returns `Equal`.
1121 ///
1122 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
1123 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
1124 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
1125 ///
1126 /// The selected operand is then rounded to `prec` bits using `rm`, as by
1127 /// [`Float::from_float_prec_round`]; like that function, this function may overflow if the
1128 /// selected operand has the maximum exponent, and it never underflows.
1129 ///
1130 /// The first [`Float`] is taken by reference and the second by value.
1131 ///
1132 /// If the output has a precision, it is `prec`.
1133 ///
1134 /// If you know you'll be using `Nearest`, consider using [`Float::max_prec`] instead. If you
1135 /// know that your target precision is the maximum of the precisions of the two inputs, consider
1136 /// using [`Float::max_round`] instead. If both of these things are true, consider using
1137 /// [`Float::max`] instead.
1138 ///
1139 /// # Worst-case complexity
1140 /// $T(n, m) = O(n + m)$
1141 ///
1142 /// $M(n) = O(n)$
1143 ///
1144 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1145 /// `max(self.significant_bits(), other.significant_bits())`.
1146 ///
1147 /// # Panics
1148 /// Panics if `prec` is zero, or if `rm` is `Exact` but the selected operand cannot be
1149 /// represented exactly at a precision of `prec` bits.
1150 ///
1151 /// # Examples
1152 /// ```
1153 /// use core::f64::consts::{E, PI};
1154 /// use malachite_base::rounding_modes::RoundingMode::*;
1155 /// use malachite_float::Float;
1156 /// use std::cmp::Ordering::*;
1157 ///
1158 /// let (max, o) = Float::from(PI).max_prec_round_ref_val(Float::from(E), 5, Floor);
1159 /// assert_eq!(max.to_string(), "3.12");
1160 /// assert_eq!(o, Less);
1161 ///
1162 /// let (max, o) = Float::from(PI).max_prec_round_ref_val(Float::from(E), 5, Ceiling);
1163 /// assert_eq!(max.to_string(), "3.25");
1164 /// assert_eq!(o, Greater);
1165 ///
1166 /// let (max, o) = Float::from(PI).max_prec_round_ref_val(Float::from(E), 20, Nearest);
1167 /// assert_eq!(max.to_string(), "3.1415939");
1168 /// assert_eq!(o, Greater);
1169 /// ```
1170 #[inline]
1171 pub fn max_prec_round_ref_val(
1172 &self,
1173 other: Self,
1174 prec: u64,
1175 rm: RoundingMode,
1176 ) -> (Self, Ordering) {
1177 match min_max_choice(self, &other, true) {
1178 Choice::First => Self::from_float_prec_round_ref(self, prec, rm),
1179 Choice::Second => Self::from_float_prec_round(other, prec, rm),
1180 }
1181 }
1182
1183 /// Returns the maximum of two [`Float`]s, rounding the result to the specified precision and
1184 /// with the specified rounding mode. An [`Ordering`] is also returned, indicating whether the
1185 /// rounded maximum is less than, equal to, or greater than the exact maximum. Whenever this
1186 /// function returns a `NaN` it also returns `Equal`.
1187 ///
1188 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
1189 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
1190 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
1191 ///
1192 /// The selected operand is then rounded to `prec` bits using `rm`, as by
1193 /// [`Float::from_float_prec_round`]; like that function, this function may overflow if the
1194 /// selected operand has the maximum exponent, and it never underflows.
1195 ///
1196 /// Both [`Float`]s are taken by reference.
1197 ///
1198 /// If the output has a precision, it is `prec`.
1199 ///
1200 /// If you know you'll be using `Nearest`, consider using [`Float::max_prec`] instead. If you
1201 /// know that your target precision is the maximum of the precisions of the two inputs, consider
1202 /// using [`Float::max_round`] instead. If both of these things are true, consider using
1203 /// [`Float::max`] instead.
1204 ///
1205 /// # Worst-case complexity
1206 /// $T(n, m) = O(n + m)$
1207 ///
1208 /// $M(n) = O(n)$
1209 ///
1210 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1211 /// `max(self.significant_bits(), other.significant_bits())`.
1212 ///
1213 /// # Panics
1214 /// Panics if `prec` is zero, or if `rm` is `Exact` but the selected operand cannot be
1215 /// represented exactly at a precision of `prec` bits.
1216 ///
1217 /// # Examples
1218 /// ```
1219 /// use core::f64::consts::{E, PI};
1220 /// use malachite_base::rounding_modes::RoundingMode::*;
1221 /// use malachite_float::Float;
1222 /// use std::cmp::Ordering::*;
1223 ///
1224 /// let (max, o) = Float::from(PI).max_prec_round_ref_ref(&Float::from(E), 5, Floor);
1225 /// assert_eq!(max.to_string(), "3.12");
1226 /// assert_eq!(o, Less);
1227 ///
1228 /// let (max, o) = Float::from(PI).max_prec_round_ref_ref(&Float::from(E), 5, Ceiling);
1229 /// assert_eq!(max.to_string(), "3.25");
1230 /// assert_eq!(o, Greater);
1231 ///
1232 /// let (max, o) = Float::from(PI).max_prec_round_ref_ref(&Float::from(E), 20, Nearest);
1233 /// assert_eq!(max.to_string(), "3.1415939");
1234 /// assert_eq!(o, Greater);
1235 /// ```
1236 #[inline]
1237 pub fn max_prec_round_ref_ref(
1238 &self,
1239 other: &Self,
1240 prec: u64,
1241 rm: RoundingMode,
1242 ) -> (Self, Ordering) {
1243 match min_max_choice(self, other, true) {
1244 Choice::First => Self::from_float_prec_round_ref(self, prec, rm),
1245 Choice::Second => Self::from_float_prec_round_ref(other, prec, rm),
1246 }
1247 }
1248
1249 /// Returns the maximum of two [`Float`]s, rounding the result to the specified precision and
1250 /// with the `Nearest` rounding mode. An [`Ordering`] is also returned, indicating whether the
1251 /// rounded maximum is less than, equal to, or greater than the exact maximum. Whenever this
1252 /// function returns a `NaN` it also returns `Equal`.
1253 ///
1254 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
1255 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
1256 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
1257 ///
1258 /// The selected operand is then rounded to `prec` bits using the `Nearest` rounding mode, as by
1259 /// [`Float::from_float_prec`]; like that function, this function may overflow if the selected
1260 /// operand has the maximum exponent, and it never underflows.
1261 ///
1262 /// Both [`Float`]s are taken by value.
1263 ///
1264 /// If the output has a precision, it is `prec`.
1265 ///
1266 /// If you want to use a rounding mode other than `Nearest`, consider using
1267 /// [`Float::max_prec_round`] instead. If you know that your target precision is the maximum of
1268 /// the precisions of the two inputs, consider using [`Float::max`] instead.
1269 ///
1270 /// # Worst-case complexity
1271 /// $T(n, m) = O(n + m)$
1272 ///
1273 /// $M(n) = O(n)$
1274 ///
1275 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1276 /// `max(self.significant_bits(), other.significant_bits())`.
1277 ///
1278 /// # Panics
1279 /// Panics if `prec` is zero.
1280 ///
1281 /// # Examples
1282 /// ```
1283 /// use core::f64::consts::{E, PI};
1284 /// use malachite_float::Float;
1285 /// use std::cmp::Ordering::*;
1286 ///
1287 /// let (max, o) = Float::from(PI).max_prec(Float::from(E), 5);
1288 /// assert_eq!(max.to_string(), "3.12");
1289 /// assert_eq!(o, Less);
1290 ///
1291 /// let (max, o) = Float::from(PI).max_prec(Float::from(E), 20);
1292 /// assert_eq!(max.to_string(), "3.1415939");
1293 /// assert_eq!(o, Greater);
1294 /// ```
1295 #[inline]
1296 pub fn max_prec(self, other: Self, prec: u64) -> (Self, Ordering) {
1297 match min_max_choice(&self, &other, true) {
1298 Choice::First => Self::from_float_prec(self, prec),
1299 Choice::Second => Self::from_float_prec(other, prec),
1300 }
1301 }
1302
1303 /// Returns the maximum of two [`Float`]s, rounding the result to the specified precision and
1304 /// with the `Nearest` rounding mode. An [`Ordering`] is also returned, indicating whether the
1305 /// rounded maximum is less than, equal to, or greater than the exact maximum. Whenever this
1306 /// function returns a `NaN` it also returns `Equal`.
1307 ///
1308 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
1309 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
1310 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
1311 ///
1312 /// The selected operand is then rounded to `prec` bits using the `Nearest` rounding mode, as by
1313 /// [`Float::from_float_prec`]; like that function, this function may overflow if the selected
1314 /// operand has the maximum exponent, and it never underflows.
1315 ///
1316 /// The first [`Float`] is taken by value and the second by reference.
1317 ///
1318 /// If the output has a precision, it is `prec`.
1319 ///
1320 /// If you want to use a rounding mode other than `Nearest`, consider using
1321 /// [`Float::max_prec_round`] instead. If you know that your target precision is the maximum of
1322 /// the precisions of the two inputs, consider using [`Float::max`] instead.
1323 ///
1324 /// # Worst-case complexity
1325 /// $T(n, m) = O(n + m)$
1326 ///
1327 /// $M(n) = O(n)$
1328 ///
1329 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1330 /// `max(self.significant_bits(), other.significant_bits())`.
1331 ///
1332 /// # Panics
1333 /// Panics if `prec` is zero.
1334 ///
1335 /// # Examples
1336 /// ```
1337 /// use core::f64::consts::{E, PI};
1338 /// use malachite_float::Float;
1339 /// use std::cmp::Ordering::*;
1340 ///
1341 /// let (max, o) = Float::from(PI).max_prec_val_ref(&Float::from(E), 5);
1342 /// assert_eq!(max.to_string(), "3.12");
1343 /// assert_eq!(o, Less);
1344 ///
1345 /// let (max, o) = Float::from(PI).max_prec_val_ref(&Float::from(E), 20);
1346 /// assert_eq!(max.to_string(), "3.1415939");
1347 /// assert_eq!(o, Greater);
1348 /// ```
1349 #[inline]
1350 pub fn max_prec_val_ref(self, other: &Self, prec: u64) -> (Self, Ordering) {
1351 match min_max_choice(&self, other, true) {
1352 Choice::First => Self::from_float_prec(self, prec),
1353 Choice::Second => Self::from_float_prec_ref(other, prec),
1354 }
1355 }
1356
1357 /// Returns the maximum of two [`Float`]s, rounding the result to the specified precision and
1358 /// with the `Nearest` rounding mode. An [`Ordering`] is also returned, indicating whether the
1359 /// rounded maximum is less than, equal to, or greater than the exact maximum. Whenever this
1360 /// function returns a `NaN` it also returns `Equal`.
1361 ///
1362 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
1363 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
1364 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
1365 ///
1366 /// The selected operand is then rounded to `prec` bits using the `Nearest` rounding mode, as by
1367 /// [`Float::from_float_prec`]; like that function, this function may overflow if the selected
1368 /// operand has the maximum exponent, and it never underflows.
1369 ///
1370 /// The first [`Float`] is taken by reference and the second by value.
1371 ///
1372 /// If the output has a precision, it is `prec`.
1373 ///
1374 /// If you want to use a rounding mode other than `Nearest`, consider using
1375 /// [`Float::max_prec_round`] instead. If you know that your target precision is the maximum of
1376 /// the precisions of the two inputs, consider using [`Float::max`] instead.
1377 ///
1378 /// # Worst-case complexity
1379 /// $T(n, m) = O(n + m)$
1380 ///
1381 /// $M(n) = O(n)$
1382 ///
1383 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1384 /// `max(self.significant_bits(), other.significant_bits())`.
1385 ///
1386 /// # Panics
1387 /// Panics if `prec` is zero.
1388 ///
1389 /// # Examples
1390 /// ```
1391 /// use core::f64::consts::{E, PI};
1392 /// use malachite_float::Float;
1393 /// use std::cmp::Ordering::*;
1394 ///
1395 /// let (max, o) = Float::from(PI).max_prec_ref_val(Float::from(E), 5);
1396 /// assert_eq!(max.to_string(), "3.12");
1397 /// assert_eq!(o, Less);
1398 ///
1399 /// let (max, o) = Float::from(PI).max_prec_ref_val(Float::from(E), 20);
1400 /// assert_eq!(max.to_string(), "3.1415939");
1401 /// assert_eq!(o, Greater);
1402 /// ```
1403 #[inline]
1404 pub fn max_prec_ref_val(&self, other: Self, prec: u64) -> (Self, Ordering) {
1405 match min_max_choice(self, &other, true) {
1406 Choice::First => Self::from_float_prec_ref(self, prec),
1407 Choice::Second => Self::from_float_prec(other, prec),
1408 }
1409 }
1410
1411 /// Returns the maximum of two [`Float`]s, rounding the result to the specified precision and
1412 /// with the `Nearest` rounding mode. An [`Ordering`] is also returned, indicating whether the
1413 /// rounded maximum is less than, equal to, or greater than the exact maximum. Whenever this
1414 /// function returns a `NaN` it also returns `Equal`.
1415 ///
1416 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
1417 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
1418 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
1419 ///
1420 /// The selected operand is then rounded to `prec` bits using the `Nearest` rounding mode, as by
1421 /// [`Float::from_float_prec`]; like that function, this function may overflow if the selected
1422 /// operand has the maximum exponent, and it never underflows.
1423 ///
1424 /// Both [`Float`]s are taken by reference.
1425 ///
1426 /// If the output has a precision, it is `prec`.
1427 ///
1428 /// If you want to use a rounding mode other than `Nearest`, consider using
1429 /// [`Float::max_prec_round`] instead. If you know that your target precision is the maximum of
1430 /// the precisions of the two inputs, consider using [`Float::max`] instead.
1431 ///
1432 /// # Worst-case complexity
1433 /// $T(n, m) = O(n + m)$
1434 ///
1435 /// $M(n) = O(n)$
1436 ///
1437 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1438 /// `max(self.significant_bits(), other.significant_bits())`.
1439 ///
1440 /// # Panics
1441 /// Panics if `prec` is zero.
1442 ///
1443 /// # Examples
1444 /// ```
1445 /// use core::f64::consts::{E, PI};
1446 /// use malachite_float::Float;
1447 /// use std::cmp::Ordering::*;
1448 ///
1449 /// let (max, o) = Float::from(PI).max_prec_ref_ref(&Float::from(E), 5);
1450 /// assert_eq!(max.to_string(), "3.12");
1451 /// assert_eq!(o, Less);
1452 ///
1453 /// let (max, o) = Float::from(PI).max_prec_ref_ref(&Float::from(E), 20);
1454 /// assert_eq!(max.to_string(), "3.1415939");
1455 /// assert_eq!(o, Greater);
1456 /// ```
1457 #[inline]
1458 pub fn max_prec_ref_ref(&self, other: &Self, prec: u64) -> (Self, Ordering) {
1459 match min_max_choice(self, other, true) {
1460 Choice::First => Self::from_float_prec_ref(self, prec),
1461 Choice::Second => Self::from_float_prec_ref(other, prec),
1462 }
1463 }
1464
1465 /// Returns the maximum of two [`Float`]s, rounding the result to the maximum of the operands'
1466 /// precisions and with the specified rounding mode. An [`Ordering`] is also returned; since the
1467 /// target precision is at least as high as the precision of the selected operand, the rounding
1468 /// is always exact, and the result does not depend on `rm`, and the [`Ordering`] is always
1469 /// `Equal`.
1470 ///
1471 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
1472 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
1473 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
1474 ///
1475 /// The selected operand is then padded to the target precision. This never rounds, overflows,
1476 /// or underflows.
1477 ///
1478 /// Both [`Float`]s are taken by value.
1479 ///
1480 /// If the output has a precision, it is the maximum of the operands' precisions.
1481 ///
1482 /// If you want to specify an output precision, consider using [`Float::max_prec_round`]
1483 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
1484 /// [`Float::max`] instead.
1485 ///
1486 /// # Worst-case complexity
1487 /// $T(n) = O(n)$
1488 ///
1489 /// $M(n) = O(n)$
1490 ///
1491 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1492 /// other.significant_bits())`.
1493 ///
1494 /// # Examples
1495 /// ```
1496 /// use core::f64::consts::{E, PI};
1497 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
1498 /// use malachite_base::rounding_modes::RoundingMode::*;
1499 /// use malachite_float::Float;
1500 /// use std::cmp::Ordering::*;
1501 ///
1502 /// let (max, o) = Float::from(PI).max_round(Float::from(E), Floor);
1503 /// assert_eq!(max.to_string(), "3.1415926535897931");
1504 /// assert_eq!(o, Equal);
1505 ///
1506 /// let (max, o) = Float::NAN.max_round(Float::from(PI), Floor);
1507 /// assert_eq!(max.to_string(), "3.1415926535897931");
1508 /// assert_eq!(o, Equal);
1509 ///
1510 /// let (max, o) = Float::ZERO.max_round(Float::NEGATIVE_ZERO, Floor);
1511 /// assert_eq!(max.to_string(), "0.0");
1512 /// assert_eq!(o, Equal);
1513 /// ```
1514 #[inline]
1515 pub fn max_round(self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
1516 let target_prec = max(self.significant_bits(), other.significant_bits());
1517 match min_max_choice(&self, &other, true) {
1518 Choice::First => Self::from_float_prec_round(self, target_prec, rm),
1519 Choice::Second => Self::from_float_prec_round(other, target_prec, rm),
1520 }
1521 }
1522
1523 /// Returns the maximum of two [`Float`]s, rounding the result to the maximum of the operands'
1524 /// precisions and with the specified rounding mode. An [`Ordering`] is also returned; since the
1525 /// target precision is at least as high as the precision of the selected operand, the rounding
1526 /// is always exact, and the result does not depend on `rm`, and the [`Ordering`] is always
1527 /// `Equal`.
1528 ///
1529 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
1530 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
1531 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
1532 ///
1533 /// The selected operand is then padded to the target precision. This never rounds, overflows,
1534 /// or underflows.
1535 ///
1536 /// The first [`Float`] is taken by value and the second by reference.
1537 ///
1538 /// If the output has a precision, it is the maximum of the operands' precisions.
1539 ///
1540 /// If you want to specify an output precision, consider using [`Float::max_prec_round`]
1541 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
1542 /// [`Float::max`] instead.
1543 ///
1544 /// # Worst-case complexity
1545 /// $T(n) = O(n)$
1546 ///
1547 /// $M(n) = O(n)$
1548 ///
1549 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1550 /// other.significant_bits())`.
1551 ///
1552 /// # Examples
1553 /// ```
1554 /// use core::f64::consts::{E, PI};
1555 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
1556 /// use malachite_base::rounding_modes::RoundingMode::*;
1557 /// use malachite_float::Float;
1558 /// use std::cmp::Ordering::*;
1559 ///
1560 /// let (max, o) = Float::from(PI).max_round_val_ref(&Float::from(E), Floor);
1561 /// assert_eq!(max.to_string(), "3.1415926535897931");
1562 /// assert_eq!(o, Equal);
1563 ///
1564 /// let (max, o) = Float::NAN.max_round_val_ref(&Float::from(PI), Floor);
1565 /// assert_eq!(max.to_string(), "3.1415926535897931");
1566 /// assert_eq!(o, Equal);
1567 ///
1568 /// let (max, o) = Float::ZERO.max_round_val_ref(&Float::NEGATIVE_ZERO, Floor);
1569 /// assert_eq!(max.to_string(), "0.0");
1570 /// assert_eq!(o, Equal);
1571 /// ```
1572 #[inline]
1573 pub fn max_round_val_ref(self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
1574 let target_prec = max(self.significant_bits(), other.significant_bits());
1575 match min_max_choice(&self, other, true) {
1576 Choice::First => Self::from_float_prec_round(self, target_prec, rm),
1577 Choice::Second => Self::from_float_prec_round_ref(other, target_prec, rm),
1578 }
1579 }
1580
1581 /// Returns the maximum of two [`Float`]s, rounding the result to the maximum of the operands'
1582 /// precisions and with the specified rounding mode. An [`Ordering`] is also returned; since the
1583 /// target precision is at least as high as the precision of the selected operand, the rounding
1584 /// is always exact, and the result does not depend on `rm`, and the [`Ordering`] is always
1585 /// `Equal`.
1586 ///
1587 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
1588 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
1589 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
1590 ///
1591 /// The selected operand is then padded to the target precision. This never rounds, overflows,
1592 /// or underflows.
1593 ///
1594 /// The first [`Float`] is taken by reference and the second by value.
1595 ///
1596 /// If the output has a precision, it is the maximum of the operands' precisions.
1597 ///
1598 /// If you want to specify an output precision, consider using [`Float::max_prec_round`]
1599 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
1600 /// [`Float::max`] instead.
1601 ///
1602 /// # Worst-case complexity
1603 /// $T(n) = O(n)$
1604 ///
1605 /// $M(n) = O(n)$
1606 ///
1607 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1608 /// other.significant_bits())`.
1609 ///
1610 /// # Examples
1611 /// ```
1612 /// use core::f64::consts::{E, PI};
1613 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
1614 /// use malachite_base::rounding_modes::RoundingMode::*;
1615 /// use malachite_float::Float;
1616 /// use std::cmp::Ordering::*;
1617 ///
1618 /// let (max, o) = Float::from(PI).max_round_ref_val(Float::from(E), Floor);
1619 /// assert_eq!(max.to_string(), "3.1415926535897931");
1620 /// assert_eq!(o, Equal);
1621 ///
1622 /// let (max, o) = Float::NAN.max_round_ref_val(Float::from(PI), Floor);
1623 /// assert_eq!(max.to_string(), "3.1415926535897931");
1624 /// assert_eq!(o, Equal);
1625 ///
1626 /// let (max, o) = Float::ZERO.max_round_ref_val(Float::NEGATIVE_ZERO, Floor);
1627 /// assert_eq!(max.to_string(), "0.0");
1628 /// assert_eq!(o, Equal);
1629 /// ```
1630 #[inline]
1631 pub fn max_round_ref_val(&self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
1632 let target_prec = max(self.significant_bits(), other.significant_bits());
1633 match min_max_choice(self, &other, true) {
1634 Choice::First => Self::from_float_prec_round_ref(self, target_prec, rm),
1635 Choice::Second => Self::from_float_prec_round(other, target_prec, rm),
1636 }
1637 }
1638
1639 /// Returns the maximum of two [`Float`]s, rounding the result to the maximum of the operands'
1640 /// precisions and with the specified rounding mode. An [`Ordering`] is also returned; since the
1641 /// target precision is at least as high as the precision of the selected operand, the rounding
1642 /// is always exact, and the result does not depend on `rm`, and the [`Ordering`] is always
1643 /// `Equal`.
1644 ///
1645 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
1646 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
1647 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
1648 ///
1649 /// The selected operand is then padded to the target precision. This never rounds, overflows,
1650 /// or underflows.
1651 ///
1652 /// Both [`Float`]s are taken by reference.
1653 ///
1654 /// If the output has a precision, it is the maximum of the operands' precisions.
1655 ///
1656 /// If you want to specify an output precision, consider using [`Float::max_prec_round`]
1657 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
1658 /// [`Float::max`] instead.
1659 ///
1660 /// # Worst-case complexity
1661 /// $T(n) = O(n)$
1662 ///
1663 /// $M(n) = O(n)$
1664 ///
1665 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1666 /// other.significant_bits())`.
1667 ///
1668 /// # Examples
1669 /// ```
1670 /// use core::f64::consts::{E, PI};
1671 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
1672 /// use malachite_base::rounding_modes::RoundingMode::*;
1673 /// use malachite_float::Float;
1674 /// use std::cmp::Ordering::*;
1675 ///
1676 /// let (max, o) = Float::from(PI).max_round_ref_ref(&Float::from(E), Floor);
1677 /// assert_eq!(max.to_string(), "3.1415926535897931");
1678 /// assert_eq!(o, Equal);
1679 ///
1680 /// let (max, o) = Float::NAN.max_round_ref_ref(&Float::from(PI), Floor);
1681 /// assert_eq!(max.to_string(), "3.1415926535897931");
1682 /// assert_eq!(o, Equal);
1683 ///
1684 /// let (max, o) = Float::ZERO.max_round_ref_ref(&Float::NEGATIVE_ZERO, Floor);
1685 /// assert_eq!(max.to_string(), "0.0");
1686 /// assert_eq!(o, Equal);
1687 /// ```
1688 #[inline]
1689 pub fn max_round_ref_ref(&self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
1690 let target_prec = max(self.significant_bits(), other.significant_bits());
1691 match min_max_choice(self, other, true) {
1692 Choice::First => Self::from_float_prec_round_ref(self, target_prec, rm),
1693 Choice::Second => Self::from_float_prec_round_ref(other, target_prec, rm),
1694 }
1695 }
1696
1697 /// Returns the maximum of two [`Float`]s, rounding the result to the maximum of the operands'
1698 /// precisions. An [`Ordering`] is also returned; since the target precision is at least as high
1699 /// as the precision of the selected operand, the rounding is always exact, and the [`Ordering`]
1700 /// is always `Equal`.
1701 ///
1702 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
1703 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
1704 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
1705 ///
1706 /// The selected operand is then padded to the target precision. This never rounds, overflows,
1707 /// or underflows.
1708 ///
1709 /// Both [`Float`]s are taken by value.
1710 ///
1711 /// If the output has a precision, it is the maximum of the operands' precisions.
1712 ///
1713 /// If you want to use a rounding mode other than `Nearest`, consider using [`Float::max_round`]
1714 /// instead. If you want to specify an output precision, consider using [`Float::max_prec`]
1715 /// instead.
1716 ///
1717 /// # Worst-case complexity
1718 /// $T(n) = O(n)$
1719 ///
1720 /// $M(n) = O(n)$
1721 ///
1722 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1723 /// other.significant_bits())`.
1724 ///
1725 /// # Examples
1726 /// ```
1727 /// use core::f64::consts::{E, PI};
1728 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
1729 /// use malachite_float::Float;
1730 /// use std::cmp::Ordering::*;
1731 ///
1732 /// let (max, o) = Float::from(PI).max(Float::from(E));
1733 /// assert_eq!(max.to_string(), "3.1415926535897931");
1734 /// assert_eq!(o, Equal);
1735 ///
1736 /// let (max, o) = Float::NAN.max(Float::from(PI));
1737 /// assert_eq!(max.to_string(), "3.1415926535897931");
1738 /// assert_eq!(o, Equal);
1739 ///
1740 /// let (max, o) = Float::ZERO.max(Float::NEGATIVE_ZERO);
1741 /// assert_eq!(max.to_string(), "0.0");
1742 /// assert_eq!(o, Equal);
1743 /// ```
1744 #[inline]
1745 pub fn max(self, other: Self) -> (Self, Ordering) {
1746 let target_prec = max(self.significant_bits(), other.significant_bits());
1747 match min_max_choice(&self, &other, true) {
1748 Choice::First => Self::from_float_prec(self, target_prec),
1749 Choice::Second => Self::from_float_prec(other, target_prec),
1750 }
1751 }
1752
1753 /// Returns the maximum of two [`Float`]s, rounding the result to the maximum of the operands'
1754 /// precisions. An [`Ordering`] is also returned; since the target precision is at least as high
1755 /// as the precision of the selected operand, the rounding is always exact, and the [`Ordering`]
1756 /// is always `Equal`.
1757 ///
1758 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
1759 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
1760 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
1761 ///
1762 /// The selected operand is then padded to the target precision. This never rounds, overflows,
1763 /// or underflows.
1764 ///
1765 /// The first [`Float`] is taken by value and the second by reference.
1766 ///
1767 /// If the output has a precision, it is the maximum of the operands' precisions.
1768 ///
1769 /// If you want to use a rounding mode other than `Nearest`, consider using [`Float::max_round`]
1770 /// instead. If you want to specify an output precision, consider using [`Float::max_prec`]
1771 /// instead.
1772 ///
1773 /// # Worst-case complexity
1774 /// $T(n) = O(n)$
1775 ///
1776 /// $M(n) = O(n)$
1777 ///
1778 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1779 /// other.significant_bits())`.
1780 ///
1781 /// # Examples
1782 /// ```
1783 /// use core::f64::consts::{E, PI};
1784 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
1785 /// use malachite_float::Float;
1786 /// use std::cmp::Ordering::*;
1787 ///
1788 /// let (max, o) = Float::from(PI).max_val_ref(&Float::from(E));
1789 /// assert_eq!(max.to_string(), "3.1415926535897931");
1790 /// assert_eq!(o, Equal);
1791 ///
1792 /// let (max, o) = Float::NAN.max_val_ref(&Float::from(PI));
1793 /// assert_eq!(max.to_string(), "3.1415926535897931");
1794 /// assert_eq!(o, Equal);
1795 ///
1796 /// let (max, o) = Float::ZERO.max_val_ref(&Float::NEGATIVE_ZERO);
1797 /// assert_eq!(max.to_string(), "0.0");
1798 /// assert_eq!(o, Equal);
1799 /// ```
1800 #[inline]
1801 pub fn max_val_ref(self, other: &Self) -> (Self, Ordering) {
1802 let target_prec = max(self.significant_bits(), other.significant_bits());
1803 match min_max_choice(&self, other, true) {
1804 Choice::First => Self::from_float_prec(self, target_prec),
1805 Choice::Second => Self::from_float_prec_ref(other, target_prec),
1806 }
1807 }
1808
1809 /// Returns the maximum of two [`Float`]s, rounding the result to the maximum of the operands'
1810 /// precisions. An [`Ordering`] is also returned; since the target precision is at least as high
1811 /// as the precision of the selected operand, the rounding is always exact, and the [`Ordering`]
1812 /// is always `Equal`.
1813 ///
1814 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
1815 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
1816 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
1817 ///
1818 /// The selected operand is then padded to the target precision. This never rounds, overflows,
1819 /// or underflows.
1820 ///
1821 /// The first [`Float`] is taken by reference and the second by value.
1822 ///
1823 /// If the output has a precision, it is the maximum of the operands' precisions.
1824 ///
1825 /// If you want to use a rounding mode other than `Nearest`, consider using [`Float::max_round`]
1826 /// instead. If you want to specify an output precision, consider using [`Float::max_prec`]
1827 /// instead.
1828 ///
1829 /// # Worst-case complexity
1830 /// $T(n) = O(n)$
1831 ///
1832 /// $M(n) = O(n)$
1833 ///
1834 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1835 /// other.significant_bits())`.
1836 ///
1837 /// # Examples
1838 /// ```
1839 /// use core::f64::consts::{E, PI};
1840 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
1841 /// use malachite_float::Float;
1842 /// use std::cmp::Ordering::*;
1843 ///
1844 /// let (max, o) = Float::from(PI).max_ref_val(Float::from(E));
1845 /// assert_eq!(max.to_string(), "3.1415926535897931");
1846 /// assert_eq!(o, Equal);
1847 ///
1848 /// let (max, o) = Float::NAN.max_ref_val(Float::from(PI));
1849 /// assert_eq!(max.to_string(), "3.1415926535897931");
1850 /// assert_eq!(o, Equal);
1851 ///
1852 /// let (max, o) = Float::ZERO.max_ref_val(Float::NEGATIVE_ZERO);
1853 /// assert_eq!(max.to_string(), "0.0");
1854 /// assert_eq!(o, Equal);
1855 /// ```
1856 #[inline]
1857 pub fn max_ref_val(&self, other: Self) -> (Self, Ordering) {
1858 let target_prec = max(self.significant_bits(), other.significant_bits());
1859 match min_max_choice(self, &other, true) {
1860 Choice::First => Self::from_float_prec_ref(self, target_prec),
1861 Choice::Second => Self::from_float_prec(other, target_prec),
1862 }
1863 }
1864
1865 /// Returns the maximum of two [`Float`]s, rounding the result to the maximum of the operands'
1866 /// precisions. An [`Ordering`] is also returned; since the target precision is at least as high
1867 /// as the precision of the selected operand, the rounding is always exact, and the [`Ordering`]
1868 /// is always `Equal`.
1869 ///
1870 /// If one of the operands is a `NaN`, the other operand is selected; if both are `NaN`s, the
1871 /// result is `NaN`. If both operands are zeros, a positive zero is selected if either zero is
1872 /// positive, and a negative zero otherwise. Otherwise, the larger operand is selected.
1873 ///
1874 /// The selected operand is then padded to the target precision. This never rounds, overflows,
1875 /// or underflows.
1876 ///
1877 /// Both [`Float`]s are taken by reference.
1878 ///
1879 /// If the output has a precision, it is the maximum of the operands' precisions.
1880 ///
1881 /// If you want to use a rounding mode other than `Nearest`, consider using [`Float::max_round`]
1882 /// instead. If you want to specify an output precision, consider using [`Float::max_prec`]
1883 /// instead.
1884 ///
1885 /// # Worst-case complexity
1886 /// $T(n) = O(n)$
1887 ///
1888 /// $M(n) = O(n)$
1889 ///
1890 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1891 /// other.significant_bits())`.
1892 ///
1893 /// # Examples
1894 /// ```
1895 /// use core::f64::consts::{E, PI};
1896 /// use malachite_base::num::basic::traits::{NaN, NegativeZero, Zero};
1897 /// use malachite_float::Float;
1898 /// use std::cmp::Ordering::*;
1899 ///
1900 /// let (max, o) = Float::from(PI).max_ref_ref(&Float::from(E));
1901 /// assert_eq!(max.to_string(), "3.1415926535897931");
1902 /// assert_eq!(o, Equal);
1903 ///
1904 /// let (max, o) = Float::NAN.max_ref_ref(&Float::from(PI));
1905 /// assert_eq!(max.to_string(), "3.1415926535897931");
1906 /// assert_eq!(o, Equal);
1907 ///
1908 /// let (max, o) = Float::ZERO.max_ref_ref(&Float::NEGATIVE_ZERO);
1909 /// assert_eq!(max.to_string(), "0.0");
1910 /// assert_eq!(o, Equal);
1911 /// ```
1912 #[inline]
1913 pub fn max_ref_ref(&self, other: &Self) -> (Self, Ordering) {
1914 let target_prec = max(self.significant_bits(), other.significant_bits());
1915 match min_max_choice(self, other, true) {
1916 Choice::First => Self::from_float_prec_ref(self, target_prec),
1917 Choice::Second => Self::from_float_prec_ref(other, target_prec),
1918 }
1919 }
1920}
1921
1922// The comparison of a Float with a Rational is exact, so these mixed functions choose the true
1923// winner and round only it; converting the Rational to a Float first could select the wrong operand
1924// when the conversion crosses the other operand's value. A NaN Float yields the other operand, as
1925// in the Float-Float functions. On a tie, min returns the Float (preserving a negative zero) and
1926// max prefers the positive zero, matching the zero-sign preferences of mpfr_min and mpfr_max.
1927fn min_rational_helper(x: &Float, y: &Rational, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
1928 assert_ne!(prec, 0);
1929 match x.partial_cmp(y) {
1930 None | Some(Greater) => Float::from_rational_prec_round_ref(y, prec, rm),
1931 _ => Float::from_float_prec_round_ref(x, prec, rm),
1932 }
1933}
1934
1935fn max_rational_helper(x: &Float, y: &Rational, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
1936 assert_ne!(prec, 0);
1937 match x.partial_cmp(y) {
1938 None | Some(Less) => Float::from_rational_prec_round_ref(y, prec, rm),
1939 Some(Equal) if matches!(x, float_negative_zero!()) => (Float::ZERO, Equal),
1940 _ => Float::from_float_prec_round_ref(x, prec, rm),
1941 }
1942}
1943
1944impl Float {
1945 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the specified
1946 /// precision and with the specified rounding mode. The [`Float`] and the [`Rational`] are both
1947 /// taken by value. An [`Ordering`] is also returned, indicating whether the result is less
1948 /// than, equal to, or greater than the exact smaller value. Although `NaN`s are not comparable
1949 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1950 ///
1951 /// The comparison is exact, and only the winning operand is rounded. Converting the
1952 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
1953 /// crosses the other operand's value.
1954 ///
1955 /// Special cases:
1956 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
1957 /// [`Float`]-[`Float`] functions.
1958 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
1959 /// is preserved.
1960 ///
1961 /// # Worst-case complexity
1962 /// $T(n) = O(n \log n \log\log n)$
1963 ///
1964 /// $M(n) = O(n)$
1965 ///
1966 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
1967 /// other.significant_bits(), prec)`.
1968 ///
1969 /// # Panics
1970 /// Panics if `prec` is zero, or if `rm` is `Exact` and the winning operand is not exactly
1971 /// representable with `prec` bits.
1972 ///
1973 /// # Examples
1974 /// ```
1975 /// use core::cmp::Ordering::*;
1976 /// use malachite_base::rounding_modes::RoundingMode::*;
1977 /// use malachite_float::Float;
1978 /// use malachite_q::Rational;
1979 ///
1980 /// let (r, o) =
1981 /// Float::from(3u32).min_rational_prec_round(Rational::from_signeds(22, 7), 5, Floor);
1982 /// assert_eq!(r.to_string(), "3.00");
1983 /// assert_eq!(o, Equal);
1984 /// ```
1985 #[allow(clippy::needless_pass_by_value)]
1986 #[inline]
1987 pub fn min_rational_prec_round(
1988 self,
1989 other: Rational,
1990 prec: u64,
1991 rm: RoundingMode,
1992 ) -> (Self, Ordering) {
1993 min_rational_helper(&self, &other, prec, rm)
1994 }
1995
1996 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the specified
1997 /// precision and with the specified rounding mode. The [`Float`] is taken by value and the
1998 /// [`Rational`] by reference. An [`Ordering`] is also returned, indicating whether the result
1999 /// is less than, equal to, or greater than the exact smaller value. Although `NaN`s are not
2000 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2001 ///
2002 /// The comparison is exact, and only the winning operand is rounded. Converting the
2003 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2004 /// crosses the other operand's value.
2005 ///
2006 /// Special cases:
2007 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2008 /// [`Float`]-[`Float`] functions.
2009 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
2010 /// is preserved.
2011 ///
2012 /// # Worst-case complexity
2013 /// $T(n) = O(n \log n \log\log n)$
2014 ///
2015 /// $M(n) = O(n)$
2016 ///
2017 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2018 /// other.significant_bits(), prec)`.
2019 ///
2020 /// # Panics
2021 /// Panics if `prec` is zero, or if `rm` is `Exact` and the winning operand is not exactly
2022 /// representable with `prec` bits.
2023 ///
2024 /// # Examples
2025 /// ```
2026 /// use core::cmp::Ordering::*;
2027 /// use malachite_base::rounding_modes::RoundingMode::*;
2028 /// use malachite_float::Float;
2029 /// use malachite_q::Rational;
2030 ///
2031 /// let x = Float::from(3u32);
2032 /// let y = Rational::from_signeds(22, 7);
2033 /// let (r, o) = x.min_rational_prec_round_val_ref(&y, 5, Floor);
2034 /// assert_eq!(r.to_string(), "3.00");
2035 /// assert_eq!(o, Equal);
2036 /// ```
2037 #[inline]
2038 pub fn min_rational_prec_round_val_ref(
2039 self,
2040 other: &Rational,
2041 prec: u64,
2042 rm: RoundingMode,
2043 ) -> (Self, Ordering) {
2044 min_rational_helper(&self, other, prec, rm)
2045 }
2046
2047 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the specified
2048 /// precision and with the specified rounding mode. The [`Float`] is taken by reference and the
2049 /// [`Rational`] by value. An [`Ordering`] is also returned, indicating whether the result is
2050 /// less than, equal to, or greater than the exact smaller value. Although `NaN`s are not
2051 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2052 ///
2053 /// The comparison is exact, and only the winning operand is rounded. Converting the
2054 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2055 /// crosses the other operand's value.
2056 ///
2057 /// Special cases:
2058 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2059 /// [`Float`]-[`Float`] functions.
2060 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
2061 /// is preserved.
2062 ///
2063 /// # Worst-case complexity
2064 /// $T(n) = O(n \log n \log\log n)$
2065 ///
2066 /// $M(n) = O(n)$
2067 ///
2068 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2069 /// other.significant_bits(), prec)`.
2070 ///
2071 /// # Panics
2072 /// Panics if `prec` is zero, or if `rm` is `Exact` and the winning operand is not exactly
2073 /// representable with `prec` bits.
2074 ///
2075 /// # Examples
2076 /// ```
2077 /// use core::cmp::Ordering::*;
2078 /// use malachite_base::rounding_modes::RoundingMode::*;
2079 /// use malachite_float::Float;
2080 /// use malachite_q::Rational;
2081 ///
2082 /// let x = Float::from(3u32);
2083 /// let y = Rational::from_signeds(22, 7);
2084 /// let (r, o) = x.min_rational_prec_round_ref_val(y, 5, Floor);
2085 /// assert_eq!(r.to_string(), "3.00");
2086 /// assert_eq!(o, Equal);
2087 /// ```
2088 #[allow(clippy::needless_pass_by_value)]
2089 #[inline]
2090 pub fn min_rational_prec_round_ref_val(
2091 &self,
2092 other: Rational,
2093 prec: u64,
2094 rm: RoundingMode,
2095 ) -> (Self, Ordering) {
2096 min_rational_helper(self, &other, prec, rm)
2097 }
2098
2099 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the specified
2100 /// precision and with the specified rounding mode. The [`Float`] and the [`Rational`] are both
2101 /// taken by reference. An [`Ordering`] is also returned, indicating whether the result is less
2102 /// than, equal to, or greater than the exact smaller value. Although `NaN`s are not comparable
2103 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2104 ///
2105 /// The comparison is exact, and only the winning operand is rounded. Converting the
2106 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2107 /// crosses the other operand's value.
2108 ///
2109 /// Special cases:
2110 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2111 /// [`Float`]-[`Float`] functions.
2112 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
2113 /// is preserved.
2114 ///
2115 /// # Worst-case complexity
2116 /// $T(n) = O(n \log n \log\log n)$
2117 ///
2118 /// $M(n) = O(n)$
2119 ///
2120 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2121 /// other.significant_bits(), prec)`.
2122 ///
2123 /// # Panics
2124 /// Panics if `prec` is zero, or if `rm` is `Exact` and the winning operand is not exactly
2125 /// representable with `prec` bits.
2126 ///
2127 /// # Examples
2128 /// ```
2129 /// use core::cmp::Ordering::*;
2130 /// use malachite_base::rounding_modes::RoundingMode::*;
2131 /// use malachite_float::Float;
2132 /// use malachite_q::Rational;
2133 ///
2134 /// let x = Float::from(3u32);
2135 /// let y = Rational::from_signeds(22, 7);
2136 /// let (r, o) = x.min_rational_prec_round_ref_ref(&y, 5, Floor);
2137 /// assert_eq!(r.to_string(), "3.00");
2138 /// assert_eq!(o, Equal);
2139 /// ```
2140 #[inline]
2141 pub fn min_rational_prec_round_ref_ref(
2142 &self,
2143 other: &Rational,
2144 prec: u64,
2145 rm: RoundingMode,
2146 ) -> (Self, Ordering) {
2147 min_rational_helper(self, other, prec, rm)
2148 }
2149
2150 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the nearest
2151 /// value of the specified precision. The [`Float`] and the [`Rational`] are both taken by
2152 /// value. An [`Ordering`] is also returned, indicating whether the result is less than, equal
2153 /// to, or greater than the exact smaller value. Although `NaN`s are not comparable to any
2154 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2155 ///
2156 /// The comparison is exact, and only the winning operand is rounded. Converting the
2157 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2158 /// crosses the other operand's value.
2159 ///
2160 /// Special cases:
2161 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2162 /// [`Float`]-[`Float`] functions.
2163 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
2164 /// is preserved.
2165 ///
2166 /// # Worst-case complexity
2167 /// $T(n) = O(n \log n \log\log n)$
2168 ///
2169 /// $M(n) = O(n)$
2170 ///
2171 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2172 /// other.significant_bits(), prec)`.
2173 ///
2174 /// # Panics
2175 /// Panics if `prec` is zero.
2176 ///
2177 /// # Examples
2178 /// ```
2179 /// use core::cmp::Ordering::*;
2180 /// use malachite_float::Float;
2181 /// use malachite_q::Rational;
2182 ///
2183 /// let (r, o) = Float::from(3u32).min_rational_prec(Rational::from_signeds(22, 7), 5);
2184 /// assert_eq!(r.to_string(), "3.00");
2185 /// assert_eq!(o, Equal);
2186 /// ```
2187 #[inline]
2188 pub fn min_rational_prec(self, other: Rational, prec: u64) -> (Self, Ordering) {
2189 self.min_rational_prec_round(other, prec, Nearest)
2190 }
2191
2192 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the nearest
2193 /// value of the specified precision. The [`Float`] is taken by value and the [`Rational`] by
2194 /// reference. An [`Ordering`] is also returned, indicating whether the result is less than,
2195 /// equal to, or greater than the exact smaller value. Although `NaN`s are not comparable to any
2196 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2197 ///
2198 /// The comparison is exact, and only the winning operand is rounded. Converting the
2199 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2200 /// crosses the other operand's value.
2201 ///
2202 /// Special cases:
2203 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2204 /// [`Float`]-[`Float`] functions.
2205 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
2206 /// is preserved.
2207 ///
2208 /// # Worst-case complexity
2209 /// $T(n) = O(n \log n \log\log n)$
2210 ///
2211 /// $M(n) = O(n)$
2212 ///
2213 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2214 /// other.significant_bits(), prec)`.
2215 ///
2216 /// # Panics
2217 /// Panics if `prec` is zero.
2218 ///
2219 /// # Examples
2220 /// ```
2221 /// use core::cmp::Ordering::*;
2222 /// use malachite_float::Float;
2223 /// use malachite_q::Rational;
2224 ///
2225 /// let (r, o) = Float::from(3u32).min_rational_prec_val_ref(&Rational::from_signeds(22, 7), 5);
2226 /// assert_eq!(r.to_string(), "3.00");
2227 /// assert_eq!(o, Equal);
2228 /// ```
2229 #[inline]
2230 pub fn min_rational_prec_val_ref(self, other: &Rational, prec: u64) -> (Self, Ordering) {
2231 self.min_rational_prec_round_val_ref(other, prec, Nearest)
2232 }
2233
2234 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the nearest
2235 /// value of the specified precision. The [`Float`] is taken by reference and the [`Rational`]
2236 /// by value. An [`Ordering`] is also returned, indicating whether the result is less than,
2237 /// equal to, or greater than the exact smaller value. Although `NaN`s are not comparable to any
2238 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2239 ///
2240 /// The comparison is exact, and only the winning operand is rounded. Converting the
2241 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2242 /// crosses the other operand's value.
2243 ///
2244 /// Special cases:
2245 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2246 /// [`Float`]-[`Float`] functions.
2247 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
2248 /// is preserved.
2249 ///
2250 /// # Worst-case complexity
2251 /// $T(n) = O(n \log n \log\log n)$
2252 ///
2253 /// $M(n) = O(n)$
2254 ///
2255 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2256 /// other.significant_bits(), prec)`.
2257 ///
2258 /// # Panics
2259 /// Panics if `prec` is zero.
2260 ///
2261 /// # Examples
2262 /// ```
2263 /// use core::cmp::Ordering::*;
2264 /// use malachite_float::Float;
2265 /// use malachite_q::Rational;
2266 ///
2267 /// let (r, o) = Float::from(3u32).min_rational_prec_ref_val(Rational::from_signeds(22, 7), 5);
2268 /// assert_eq!(r.to_string(), "3.00");
2269 /// assert_eq!(o, Equal);
2270 /// ```
2271 #[inline]
2272 pub fn min_rational_prec_ref_val(&self, other: Rational, prec: u64) -> (Self, Ordering) {
2273 self.min_rational_prec_round_ref_val(other, prec, Nearest)
2274 }
2275
2276 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the nearest
2277 /// value of the specified precision. The [`Float`] and the [`Rational`] are both taken by
2278 /// reference. An [`Ordering`] is also returned, indicating whether the result is less than,
2279 /// equal to, or greater than the exact smaller value. Although `NaN`s are not comparable to any
2280 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2281 ///
2282 /// The comparison is exact, and only the winning operand is rounded. Converting the
2283 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2284 /// crosses the other operand's value.
2285 ///
2286 /// Special cases:
2287 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2288 /// [`Float`]-[`Float`] functions.
2289 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
2290 /// is preserved.
2291 ///
2292 /// # Worst-case complexity
2293 /// $T(n) = O(n \log n \log\log n)$
2294 ///
2295 /// $M(n) = O(n)$
2296 ///
2297 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2298 /// other.significant_bits(), prec)`.
2299 ///
2300 /// # Panics
2301 /// Panics if `prec` is zero.
2302 ///
2303 /// # Examples
2304 /// ```
2305 /// use core::cmp::Ordering::*;
2306 /// use malachite_float::Float;
2307 /// use malachite_q::Rational;
2308 ///
2309 /// let (r, o) = Float::from(3u32).min_rational_prec_ref_ref(&Rational::from_signeds(22, 7), 5);
2310 /// assert_eq!(r.to_string(), "3.00");
2311 /// assert_eq!(o, Equal);
2312 /// ```
2313 #[inline]
2314 pub fn min_rational_prec_ref_ref(&self, other: &Rational, prec: u64) -> (Self, Ordering) {
2315 self.min_rational_prec_round_ref_ref(other, prec, Nearest)
2316 }
2317
2318 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the
2319 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] and the [`Rational`]
2320 /// are both taken by value. An [`Ordering`] is also returned, indicating whether the result is
2321 /// less than, equal to, or greater than the exact smaller value. Although `NaN`s are not
2322 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2323 ///
2324 /// The comparison is exact, and only the winning operand is rounded. Converting the
2325 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2326 /// crosses the other operand's value.
2327 ///
2328 /// Special cases:
2329 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2330 /// [`Float`]-[`Float`] functions.
2331 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
2332 /// is preserved.
2333 ///
2334 /// # Worst-case complexity
2335 /// $T(n) = O(n \log n \log\log n)$
2336 ///
2337 /// $M(n) = O(n)$
2338 ///
2339 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2340 /// other.significant_bits())`.
2341 ///
2342 /// # Panics
2343 /// Panics if `rm` is `Exact` and the winning operand is not exactly representable with the
2344 /// output precision.
2345 ///
2346 /// # Examples
2347 /// ```
2348 /// use core::cmp::Ordering::*;
2349 /// use malachite_base::rounding_modes::RoundingMode::*;
2350 /// use malachite_float::Float;
2351 /// use malachite_q::Rational;
2352 ///
2353 /// let (r, o) = Float::from(3u32).min_rational_round(Rational::from_signeds(22, 7), Floor);
2354 /// assert_eq!(r.to_string(), "3.0");
2355 /// assert_eq!(o, Equal);
2356 /// ```
2357 #[inline]
2358 pub fn min_rational_round(self, other: Rational, rm: RoundingMode) -> (Self, Ordering) {
2359 let prec = self.significant_bits();
2360 self.min_rational_prec_round(other, prec, rm)
2361 }
2362
2363 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the
2364 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] is taken by value and
2365 /// the [`Rational`] by reference. An [`Ordering`] is also returned, indicating whether the
2366 /// result is less than, equal to, or greater than the exact smaller value. Although `NaN`s are
2367 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
2368 /// `Equal`.
2369 ///
2370 /// The comparison is exact, and only the winning operand is rounded. Converting the
2371 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2372 /// crosses the other operand's value.
2373 ///
2374 /// Special cases:
2375 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2376 /// [`Float`]-[`Float`] functions.
2377 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
2378 /// is preserved.
2379 ///
2380 /// # Worst-case complexity
2381 /// $T(n) = O(n \log n \log\log n)$
2382 ///
2383 /// $M(n) = O(n)$
2384 ///
2385 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2386 /// other.significant_bits())`.
2387 ///
2388 /// # Panics
2389 /// Panics if `rm` is `Exact` and the winning operand is not exactly representable with the
2390 /// output precision.
2391 ///
2392 /// # Examples
2393 /// ```
2394 /// use core::cmp::Ordering::*;
2395 /// use malachite_base::rounding_modes::RoundingMode::*;
2396 /// use malachite_float::Float;
2397 /// use malachite_q::Rational;
2398 ///
2399 /// let (r, o) =
2400 /// Float::from(3u32).min_rational_round_val_ref(&Rational::from_signeds(22, 7), Floor);
2401 /// assert_eq!(r.to_string(), "3.0");
2402 /// assert_eq!(o, Equal);
2403 /// ```
2404 #[inline]
2405 pub fn min_rational_round_val_ref(
2406 self,
2407 other: &Rational,
2408 rm: RoundingMode,
2409 ) -> (Self, Ordering) {
2410 let prec = self.significant_bits();
2411 self.min_rational_prec_round_val_ref(other, prec, rm)
2412 }
2413
2414 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the
2415 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] is taken by reference
2416 /// and the [`Rational`] by value. An [`Ordering`] is also returned, indicating whether the
2417 /// result is less than, equal to, or greater than the exact smaller value. Although `NaN`s are
2418 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
2419 /// `Equal`.
2420 ///
2421 /// The comparison is exact, and only the winning operand is rounded. Converting the
2422 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2423 /// crosses the other operand's value.
2424 ///
2425 /// Special cases:
2426 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2427 /// [`Float`]-[`Float`] functions.
2428 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
2429 /// is preserved.
2430 ///
2431 /// # Worst-case complexity
2432 /// $T(n) = O(n \log n \log\log n)$
2433 ///
2434 /// $M(n) = O(n)$
2435 ///
2436 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2437 /// other.significant_bits())`.
2438 ///
2439 /// # Panics
2440 /// Panics if `rm` is `Exact` and the winning operand is not exactly representable with the
2441 /// output precision.
2442 ///
2443 /// # Examples
2444 /// ```
2445 /// use core::cmp::Ordering::*;
2446 /// use malachite_base::rounding_modes::RoundingMode::*;
2447 /// use malachite_float::Float;
2448 /// use malachite_q::Rational;
2449 ///
2450 /// let (r, o) =
2451 /// Float::from(3u32).min_rational_round_ref_val(Rational::from_signeds(22, 7), Floor);
2452 /// assert_eq!(r.to_string(), "3.0");
2453 /// assert_eq!(o, Equal);
2454 /// ```
2455 #[inline]
2456 pub fn min_rational_round_ref_val(
2457 &self,
2458 other: Rational,
2459 rm: RoundingMode,
2460 ) -> (Self, Ordering) {
2461 let prec = self.significant_bits();
2462 self.min_rational_prec_round_ref_val(other, prec, rm)
2463 }
2464
2465 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the
2466 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] and the [`Rational`]
2467 /// are both taken by reference. An [`Ordering`] is also returned, indicating whether the result
2468 /// is less than, equal to, or greater than the exact smaller value. Although `NaN`s are not
2469 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2470 ///
2471 /// The comparison is exact, and only the winning operand is rounded. Converting the
2472 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2473 /// crosses the other operand's value.
2474 ///
2475 /// Special cases:
2476 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2477 /// [`Float`]-[`Float`] functions.
2478 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
2479 /// is preserved.
2480 ///
2481 /// # Worst-case complexity
2482 /// $T(n) = O(n \log n \log\log n)$
2483 ///
2484 /// $M(n) = O(n)$
2485 ///
2486 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2487 /// other.significant_bits())`.
2488 ///
2489 /// # Panics
2490 /// Panics if `rm` is `Exact` and the winning operand is not exactly representable with the
2491 /// output precision.
2492 ///
2493 /// # Examples
2494 /// ```
2495 /// use core::cmp::Ordering::*;
2496 /// use malachite_base::rounding_modes::RoundingMode::*;
2497 /// use malachite_float::Float;
2498 /// use malachite_q::Rational;
2499 ///
2500 /// let (r, o) =
2501 /// Float::from(3u32).min_rational_round_ref_ref(&Rational::from_signeds(22, 7), Floor);
2502 /// assert_eq!(r.to_string(), "3.0");
2503 /// assert_eq!(o, Equal);
2504 /// ```
2505 #[inline]
2506 pub fn min_rational_round_ref_ref(
2507 &self,
2508 other: &Rational,
2509 rm: RoundingMode,
2510 ) -> (Self, Ordering) {
2511 let prec = self.significant_bits();
2512 self.min_rational_prec_round_ref_ref(other, prec, rm)
2513 }
2514
2515 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the nearest
2516 /// value of the [`Float`]'s precision. The [`Float`] and the [`Rational`] are both taken by
2517 /// value. An [`Ordering`] is also returned, indicating whether the result is less than, equal
2518 /// to, or greater than the exact smaller value. Although `NaN`s are not comparable to any
2519 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2520 ///
2521 /// The comparison is exact, and only the winning operand is rounded. Converting the
2522 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2523 /// crosses the other operand's value.
2524 ///
2525 /// Special cases:
2526 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2527 /// [`Float`]-[`Float`] functions.
2528 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
2529 /// is preserved.
2530 ///
2531 /// # Worst-case complexity
2532 /// $T(n) = O(n \log n \log\log n)$
2533 ///
2534 /// $M(n) = O(n)$
2535 ///
2536 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2537 /// other.significant_bits())`.
2538 ///
2539 /// # Examples
2540 /// ```
2541 /// use core::cmp::Ordering::*;
2542 /// use malachite_float::Float;
2543 /// use malachite_q::Rational;
2544 ///
2545 /// let (r, o) = Float::from(3u32).min_rational(Rational::from_signeds(22, 7));
2546 /// assert_eq!(r.to_string(), "3.0");
2547 /// assert_eq!(o, Equal);
2548 /// ```
2549 #[inline]
2550 pub fn min_rational(self, other: Rational) -> (Self, Ordering) {
2551 self.min_rational_round(other, Nearest)
2552 }
2553
2554 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the nearest
2555 /// value of the [`Float`]'s precision. The [`Float`] is taken by value and the [`Rational`] by
2556 /// reference. An [`Ordering`] is also returned, indicating whether the result is less than,
2557 /// equal to, or greater than the exact smaller value. Although `NaN`s are not comparable to any
2558 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2559 ///
2560 /// The comparison is exact, and only the winning operand is rounded. Converting the
2561 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2562 /// crosses the other operand's value.
2563 ///
2564 /// Special cases:
2565 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2566 /// [`Float`]-[`Float`] functions.
2567 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
2568 /// is preserved.
2569 ///
2570 /// # Worst-case complexity
2571 /// $T(n) = O(n \log n \log\log n)$
2572 ///
2573 /// $M(n) = O(n)$
2574 ///
2575 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2576 /// other.significant_bits())`.
2577 ///
2578 /// # Examples
2579 /// ```
2580 /// use core::cmp::Ordering::*;
2581 /// use malachite_float::Float;
2582 /// use malachite_q::Rational;
2583 ///
2584 /// let (r, o) = Float::from(3u32).min_rational_val_ref(&Rational::from_signeds(22, 7));
2585 /// assert_eq!(r.to_string(), "3.0");
2586 /// assert_eq!(o, Equal);
2587 /// ```
2588 #[inline]
2589 pub fn min_rational_val_ref(self, other: &Rational) -> (Self, Ordering) {
2590 self.min_rational_round_val_ref(other, Nearest)
2591 }
2592
2593 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the nearest
2594 /// value of the [`Float`]'s precision. The [`Float`] is taken by reference and the [`Rational`]
2595 /// by value. An [`Ordering`] is also returned, indicating whether the result is less than,
2596 /// equal to, or greater than the exact smaller value. Although `NaN`s are not comparable to any
2597 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2598 ///
2599 /// The comparison is exact, and only the winning operand is rounded. Converting the
2600 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2601 /// crosses the other operand's value.
2602 ///
2603 /// Special cases:
2604 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2605 /// [`Float`]-[`Float`] functions.
2606 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
2607 /// is preserved.
2608 ///
2609 /// # Worst-case complexity
2610 /// $T(n) = O(n \log n \log\log n)$
2611 ///
2612 /// $M(n) = O(n)$
2613 ///
2614 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2615 /// other.significant_bits())`.
2616 ///
2617 /// # Examples
2618 /// ```
2619 /// use core::cmp::Ordering::*;
2620 /// use malachite_float::Float;
2621 /// use malachite_q::Rational;
2622 ///
2623 /// let (r, o) = Float::from(3u32).min_rational_ref_val(Rational::from_signeds(22, 7));
2624 /// assert_eq!(r.to_string(), "3.0");
2625 /// assert_eq!(o, Equal);
2626 /// ```
2627 #[inline]
2628 pub fn min_rational_ref_val(&self, other: Rational) -> (Self, Ordering) {
2629 self.min_rational_round_ref_val(other, Nearest)
2630 }
2631
2632 /// Computes the smaller of a [`Float`] and a [`Rational`], rounding the result to the nearest
2633 /// value of the [`Float`]'s precision. The [`Float`] and the [`Rational`] are both taken by
2634 /// reference. An [`Ordering`] is also returned, indicating whether the result is less than,
2635 /// equal to, or greater than the exact smaller value. Although `NaN`s are not comparable to any
2636 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2637 ///
2638 /// The comparison is exact, and only the winning operand is rounded. Converting the
2639 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2640 /// crosses the other operand's value.
2641 ///
2642 /// Special cases:
2643 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2644 /// [`Float`]-[`Float`] functions.
2645 /// - If the operands are equal, the [`Float`] operand is returned (rounded), so a negative zero
2646 /// is preserved.
2647 ///
2648 /// # Worst-case complexity
2649 /// $T(n) = O(n \log n \log\log n)$
2650 ///
2651 /// $M(n) = O(n)$
2652 ///
2653 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2654 /// other.significant_bits())`.
2655 ///
2656 /// # Examples
2657 /// ```
2658 /// use core::cmp::Ordering::*;
2659 /// use malachite_float::Float;
2660 /// use malachite_q::Rational;
2661 ///
2662 /// let (r, o) = Float::from(3u32).min_rational_ref_ref(&Rational::from_signeds(22, 7));
2663 /// assert_eq!(r.to_string(), "3.0");
2664 /// assert_eq!(o, Equal);
2665 /// ```
2666 #[inline]
2667 pub fn min_rational_ref_ref(&self, other: &Rational) -> (Self, Ordering) {
2668 self.min_rational_round_ref_ref(other, Nearest)
2669 }
2670
2671 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the specified
2672 /// precision and with the specified rounding mode. The [`Float`] and the [`Rational`] are both
2673 /// taken by value. An [`Ordering`] is also returned, indicating whether the result is less
2674 /// than, equal to, or greater than the exact larger value. Although `NaN`s are not comparable
2675 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2676 ///
2677 /// The comparison is exact, and only the winning operand is rounded. Converting the
2678 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2679 /// crosses the other operand's value.
2680 ///
2681 /// Special cases:
2682 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2683 /// [`Float`]-[`Float`] functions.
2684 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
2685 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
2686 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
2687 ///
2688 /// # Worst-case complexity
2689 /// $T(n) = O(n \log n \log\log n)$
2690 ///
2691 /// $M(n) = O(n)$
2692 ///
2693 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2694 /// other.significant_bits(), prec)`.
2695 ///
2696 /// # Panics
2697 /// Panics if `prec` is zero, or if `rm` is `Exact` and the winning operand is not exactly
2698 /// representable with `prec` bits.
2699 ///
2700 /// # Examples
2701 /// ```
2702 /// use core::cmp::Ordering::*;
2703 /// use malachite_base::rounding_modes::RoundingMode::*;
2704 /// use malachite_float::Float;
2705 /// use malachite_q::Rational;
2706 ///
2707 /// let (r, o) =
2708 /// Float::from(3u32).max_rational_prec_round(Rational::from_signeds(22, 7), 5, Floor);
2709 /// assert_eq!(r.to_string(), "3.12");
2710 /// assert_eq!(o, Less);
2711 ///
2712 /// let (r, o) =
2713 /// Float::from(3u32).max_rational_prec_round(Rational::from_signeds(22, 7), 5, Ceiling);
2714 /// assert_eq!(r.to_string(), "3.25");
2715 /// assert_eq!(o, Greater);
2716 /// ```
2717 #[allow(clippy::needless_pass_by_value)]
2718 #[inline]
2719 pub fn max_rational_prec_round(
2720 self,
2721 other: Rational,
2722 prec: u64,
2723 rm: RoundingMode,
2724 ) -> (Self, Ordering) {
2725 max_rational_helper(&self, &other, prec, rm)
2726 }
2727
2728 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the specified
2729 /// precision and with the specified rounding mode. The [`Float`] is taken by value and the
2730 /// [`Rational`] by reference. An [`Ordering`] is also returned, indicating whether the result
2731 /// is less than, equal to, or greater than the exact larger value. Although `NaN`s are not
2732 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2733 ///
2734 /// The comparison is exact, and only the winning operand is rounded. Converting the
2735 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2736 /// crosses the other operand's value.
2737 ///
2738 /// Special cases:
2739 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2740 /// [`Float`]-[`Float`] functions.
2741 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
2742 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
2743 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
2744 ///
2745 /// # Worst-case complexity
2746 /// $T(n) = O(n \log n \log\log n)$
2747 ///
2748 /// $M(n) = O(n)$
2749 ///
2750 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2751 /// other.significant_bits(), prec)`.
2752 ///
2753 /// # Panics
2754 /// Panics if `prec` is zero, or if `rm` is `Exact` and the winning operand is not exactly
2755 /// representable with `prec` bits.
2756 ///
2757 /// # Examples
2758 /// ```
2759 /// use core::cmp::Ordering::*;
2760 /// use malachite_base::rounding_modes::RoundingMode::*;
2761 /// use malachite_float::Float;
2762 /// use malachite_q::Rational;
2763 ///
2764 /// let x = Float::from(3u32);
2765 /// let y = Rational::from_signeds(22, 7);
2766 /// let (r, o) = x.max_rational_prec_round_val_ref(&y, 5, Floor);
2767 /// assert_eq!(r.to_string(), "3.12");
2768 /// assert_eq!(o, Less);
2769 ///
2770 /// let x = Float::from(3u32);
2771 /// let y = Rational::from_signeds(22, 7);
2772 /// let (r, o) = x.max_rational_prec_round_val_ref(&y, 5, Ceiling);
2773 /// assert_eq!(r.to_string(), "3.25");
2774 /// assert_eq!(o, Greater);
2775 /// ```
2776 #[inline]
2777 pub fn max_rational_prec_round_val_ref(
2778 self,
2779 other: &Rational,
2780 prec: u64,
2781 rm: RoundingMode,
2782 ) -> (Self, Ordering) {
2783 max_rational_helper(&self, other, prec, rm)
2784 }
2785
2786 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the specified
2787 /// precision and with the specified rounding mode. The [`Float`] is taken by reference and the
2788 /// [`Rational`] by value. An [`Ordering`] is also returned, indicating whether the result is
2789 /// less than, equal to, or greater than the exact larger value. Although `NaN`s are not
2790 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2791 ///
2792 /// The comparison is exact, and only the winning operand is rounded. Converting the
2793 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2794 /// crosses the other operand's value.
2795 ///
2796 /// Special cases:
2797 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2798 /// [`Float`]-[`Float`] functions.
2799 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
2800 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
2801 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
2802 ///
2803 /// # Worst-case complexity
2804 /// $T(n) = O(n \log n \log\log n)$
2805 ///
2806 /// $M(n) = O(n)$
2807 ///
2808 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2809 /// other.significant_bits(), prec)`.
2810 ///
2811 /// # Panics
2812 /// Panics if `prec` is zero, or if `rm` is `Exact` and the winning operand is not exactly
2813 /// representable with `prec` bits.
2814 ///
2815 /// # Examples
2816 /// ```
2817 /// use core::cmp::Ordering::*;
2818 /// use malachite_base::rounding_modes::RoundingMode::*;
2819 /// use malachite_float::Float;
2820 /// use malachite_q::Rational;
2821 ///
2822 /// let x = Float::from(3u32);
2823 /// let y = Rational::from_signeds(22, 7);
2824 /// let (r, o) = x.max_rational_prec_round_ref_val(y, 5, Floor);
2825 /// assert_eq!(r.to_string(), "3.12");
2826 /// assert_eq!(o, Less);
2827 ///
2828 /// let x = Float::from(3u32);
2829 /// let y = Rational::from_signeds(22, 7);
2830 /// let (r, o) = x.max_rational_prec_round_ref_val(y, 5, Ceiling);
2831 /// assert_eq!(r.to_string(), "3.25");
2832 /// assert_eq!(o, Greater);
2833 /// ```
2834 #[allow(clippy::needless_pass_by_value)]
2835 #[inline]
2836 pub fn max_rational_prec_round_ref_val(
2837 &self,
2838 other: Rational,
2839 prec: u64,
2840 rm: RoundingMode,
2841 ) -> (Self, Ordering) {
2842 max_rational_helper(self, &other, prec, rm)
2843 }
2844
2845 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the specified
2846 /// precision and with the specified rounding mode. The [`Float`] and the [`Rational`] are both
2847 /// taken by reference. An [`Ordering`] is also returned, indicating whether the result is less
2848 /// than, equal to, or greater than the exact larger value. Although `NaN`s are not comparable
2849 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2850 ///
2851 /// The comparison is exact, and only the winning operand is rounded. Converting the
2852 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2853 /// crosses the other operand's value.
2854 ///
2855 /// Special cases:
2856 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2857 /// [`Float`]-[`Float`] functions.
2858 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
2859 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
2860 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
2861 ///
2862 /// # Worst-case complexity
2863 /// $T(n) = O(n \log n \log\log n)$
2864 ///
2865 /// $M(n) = O(n)$
2866 ///
2867 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2868 /// other.significant_bits(), prec)`.
2869 ///
2870 /// # Panics
2871 /// Panics if `prec` is zero, or if `rm` is `Exact` and the winning operand is not exactly
2872 /// representable with `prec` bits.
2873 ///
2874 /// # Examples
2875 /// ```
2876 /// use core::cmp::Ordering::*;
2877 /// use malachite_base::rounding_modes::RoundingMode::*;
2878 /// use malachite_float::Float;
2879 /// use malachite_q::Rational;
2880 ///
2881 /// let x = Float::from(3u32);
2882 /// let y = Rational::from_signeds(22, 7);
2883 /// let (r, o) = x.max_rational_prec_round_ref_ref(&y, 5, Floor);
2884 /// assert_eq!(r.to_string(), "3.12");
2885 /// assert_eq!(o, Less);
2886 ///
2887 /// let x = Float::from(3u32);
2888 /// let y = Rational::from_signeds(22, 7);
2889 /// let (r, o) = x.max_rational_prec_round_ref_ref(&y, 5, Ceiling);
2890 /// assert_eq!(r.to_string(), "3.25");
2891 /// assert_eq!(o, Greater);
2892 /// ```
2893 #[inline]
2894 pub fn max_rational_prec_round_ref_ref(
2895 &self,
2896 other: &Rational,
2897 prec: u64,
2898 rm: RoundingMode,
2899 ) -> (Self, Ordering) {
2900 max_rational_helper(self, other, prec, rm)
2901 }
2902
2903 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the nearest
2904 /// value of the specified precision. The [`Float`] and the [`Rational`] are both taken by
2905 /// value. An [`Ordering`] is also returned, indicating whether the result is less than, equal
2906 /// to, or greater than the exact larger value. Although `NaN`s are not comparable to any
2907 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2908 ///
2909 /// The comparison is exact, and only the winning operand is rounded. Converting the
2910 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2911 /// crosses the other operand's value.
2912 ///
2913 /// Special cases:
2914 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2915 /// [`Float`]-[`Float`] functions.
2916 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
2917 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
2918 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
2919 ///
2920 /// # Worst-case complexity
2921 /// $T(n) = O(n \log n \log\log n)$
2922 ///
2923 /// $M(n) = O(n)$
2924 ///
2925 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2926 /// other.significant_bits(), prec)`.
2927 ///
2928 /// # Panics
2929 /// Panics if `prec` is zero.
2930 ///
2931 /// # Examples
2932 /// ```
2933 /// use core::cmp::Ordering::*;
2934 /// use malachite_float::Float;
2935 /// use malachite_q::Rational;
2936 ///
2937 /// let (r, o) = Float::from(3u32).max_rational_prec(Rational::from_signeds(22, 7), 5);
2938 /// assert_eq!(r.to_string(), "3.12");
2939 /// assert_eq!(o, Less);
2940 /// ```
2941 #[inline]
2942 pub fn max_rational_prec(self, other: Rational, prec: u64) -> (Self, Ordering) {
2943 self.max_rational_prec_round(other, prec, Nearest)
2944 }
2945
2946 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the nearest
2947 /// value of the specified precision. The [`Float`] is taken by value and the [`Rational`] by
2948 /// reference. An [`Ordering`] is also returned, indicating whether the result is less than,
2949 /// equal to, or greater than the exact larger value. Although `NaN`s are not comparable to any
2950 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2951 ///
2952 /// The comparison is exact, and only the winning operand is rounded. Converting the
2953 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2954 /// crosses the other operand's value.
2955 ///
2956 /// Special cases:
2957 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
2958 /// [`Float`]-[`Float`] functions.
2959 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
2960 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
2961 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
2962 ///
2963 /// # Worst-case complexity
2964 /// $T(n) = O(n \log n \log\log n)$
2965 ///
2966 /// $M(n) = O(n)$
2967 ///
2968 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
2969 /// other.significant_bits(), prec)`.
2970 ///
2971 /// # Panics
2972 /// Panics if `prec` is zero.
2973 ///
2974 /// # Examples
2975 /// ```
2976 /// use core::cmp::Ordering::*;
2977 /// use malachite_float::Float;
2978 /// use malachite_q::Rational;
2979 ///
2980 /// let (r, o) = Float::from(3u32).max_rational_prec_val_ref(&Rational::from_signeds(22, 7), 5);
2981 /// assert_eq!(r.to_string(), "3.12");
2982 /// assert_eq!(o, Less);
2983 /// ```
2984 #[inline]
2985 pub fn max_rational_prec_val_ref(self, other: &Rational, prec: u64) -> (Self, Ordering) {
2986 self.max_rational_prec_round_val_ref(other, prec, Nearest)
2987 }
2988
2989 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the nearest
2990 /// value of the specified precision. The [`Float`] is taken by reference and the [`Rational`]
2991 /// by value. An [`Ordering`] is also returned, indicating whether the result is less than,
2992 /// equal to, or greater than the exact larger value. Although `NaN`s are not comparable to any
2993 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2994 ///
2995 /// The comparison is exact, and only the winning operand is rounded. Converting the
2996 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
2997 /// crosses the other operand's value.
2998 ///
2999 /// Special cases:
3000 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
3001 /// [`Float`]-[`Float`] functions.
3002 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
3003 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
3004 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
3005 ///
3006 /// # Worst-case complexity
3007 /// $T(n) = O(n \log n \log\log n)$
3008 ///
3009 /// $M(n) = O(n)$
3010 ///
3011 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3012 /// other.significant_bits(), prec)`.
3013 ///
3014 /// # Panics
3015 /// Panics if `prec` is zero.
3016 ///
3017 /// # Examples
3018 /// ```
3019 /// use core::cmp::Ordering::*;
3020 /// use malachite_float::Float;
3021 /// use malachite_q::Rational;
3022 ///
3023 /// let (r, o) = Float::from(3u32).max_rational_prec_ref_val(Rational::from_signeds(22, 7), 5);
3024 /// assert_eq!(r.to_string(), "3.12");
3025 /// assert_eq!(o, Less);
3026 /// ```
3027 #[inline]
3028 pub fn max_rational_prec_ref_val(&self, other: Rational, prec: u64) -> (Self, Ordering) {
3029 self.max_rational_prec_round_ref_val(other, prec, Nearest)
3030 }
3031
3032 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the nearest
3033 /// value of the specified precision. The [`Float`] and the [`Rational`] are both taken by
3034 /// reference. An [`Ordering`] is also returned, indicating whether the result is less than,
3035 /// equal to, or greater than the exact larger value. Although `NaN`s are not comparable to any
3036 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3037 ///
3038 /// The comparison is exact, and only the winning operand is rounded. Converting the
3039 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
3040 /// crosses the other operand's value.
3041 ///
3042 /// Special cases:
3043 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
3044 /// [`Float`]-[`Float`] functions.
3045 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
3046 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
3047 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
3048 ///
3049 /// # Worst-case complexity
3050 /// $T(n) = O(n \log n \log\log n)$
3051 ///
3052 /// $M(n) = O(n)$
3053 ///
3054 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3055 /// other.significant_bits(), prec)`.
3056 ///
3057 /// # Panics
3058 /// Panics if `prec` is zero.
3059 ///
3060 /// # Examples
3061 /// ```
3062 /// use core::cmp::Ordering::*;
3063 /// use malachite_float::Float;
3064 /// use malachite_q::Rational;
3065 ///
3066 /// let (r, o) = Float::from(3u32).max_rational_prec_ref_ref(&Rational::from_signeds(22, 7), 5);
3067 /// assert_eq!(r.to_string(), "3.12");
3068 /// assert_eq!(o, Less);
3069 /// ```
3070 #[inline]
3071 pub fn max_rational_prec_ref_ref(&self, other: &Rational, prec: u64) -> (Self, Ordering) {
3072 self.max_rational_prec_round_ref_ref(other, prec, Nearest)
3073 }
3074
3075 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the
3076 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] and the [`Rational`]
3077 /// are both taken by value. An [`Ordering`] is also returned, indicating whether the result is
3078 /// less than, equal to, or greater than the exact larger value. Although `NaN`s are not
3079 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3080 ///
3081 /// The comparison is exact, and only the winning operand is rounded. Converting the
3082 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
3083 /// crosses the other operand's value.
3084 ///
3085 /// Special cases:
3086 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
3087 /// [`Float`]-[`Float`] functions.
3088 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
3089 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
3090 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
3091 ///
3092 /// # Worst-case complexity
3093 /// $T(n) = O(n \log n \log\log n)$
3094 ///
3095 /// $M(n) = O(n)$
3096 ///
3097 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3098 /// other.significant_bits())`.
3099 ///
3100 /// # Panics
3101 /// Panics if `rm` is `Exact` and the winning operand is not exactly representable with the
3102 /// output precision.
3103 ///
3104 /// # Examples
3105 /// ```
3106 /// use core::cmp::Ordering::*;
3107 /// use malachite_base::rounding_modes::RoundingMode::*;
3108 /// use malachite_float::Float;
3109 /// use malachite_q::Rational;
3110 ///
3111 /// let (r, o) = Float::from(3u32).max_rational_round(Rational::from_signeds(22, 7), Floor);
3112 /// assert_eq!(r.to_string(), "3.0");
3113 /// assert_eq!(o, Less);
3114 /// ```
3115 #[inline]
3116 pub fn max_rational_round(self, other: Rational, rm: RoundingMode) -> (Self, Ordering) {
3117 let prec = self.significant_bits();
3118 self.max_rational_prec_round(other, prec, rm)
3119 }
3120
3121 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the
3122 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] is taken by value and
3123 /// the [`Rational`] by reference. An [`Ordering`] is also returned, indicating whether the
3124 /// result is less than, equal to, or greater than the exact larger value. Although `NaN`s are
3125 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
3126 /// `Equal`.
3127 ///
3128 /// The comparison is exact, and only the winning operand is rounded. Converting the
3129 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
3130 /// crosses the other operand's value.
3131 ///
3132 /// Special cases:
3133 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
3134 /// [`Float`]-[`Float`] functions.
3135 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
3136 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
3137 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
3138 ///
3139 /// # Worst-case complexity
3140 /// $T(n) = O(n \log n \log\log n)$
3141 ///
3142 /// $M(n) = O(n)$
3143 ///
3144 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3145 /// other.significant_bits())`.
3146 ///
3147 /// # Panics
3148 /// Panics if `rm` is `Exact` and the winning operand is not exactly representable with the
3149 /// output precision.
3150 ///
3151 /// # Examples
3152 /// ```
3153 /// use core::cmp::Ordering::*;
3154 /// use malachite_base::rounding_modes::RoundingMode::*;
3155 /// use malachite_float::Float;
3156 /// use malachite_q::Rational;
3157 ///
3158 /// let (r, o) =
3159 /// Float::from(3u32).max_rational_round_val_ref(&Rational::from_signeds(22, 7), Floor);
3160 /// assert_eq!(r.to_string(), "3.0");
3161 /// assert_eq!(o, Less);
3162 /// ```
3163 #[inline]
3164 pub fn max_rational_round_val_ref(
3165 self,
3166 other: &Rational,
3167 rm: RoundingMode,
3168 ) -> (Self, Ordering) {
3169 let prec = self.significant_bits();
3170 self.max_rational_prec_round_val_ref(other, prec, rm)
3171 }
3172
3173 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the
3174 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] is taken by reference
3175 /// and the [`Rational`] by value. An [`Ordering`] is also returned, indicating whether the
3176 /// result is less than, equal to, or greater than the exact larger value. Although `NaN`s are
3177 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
3178 /// `Equal`.
3179 ///
3180 /// The comparison is exact, and only the winning operand is rounded. Converting the
3181 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
3182 /// crosses the other operand's value.
3183 ///
3184 /// Special cases:
3185 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
3186 /// [`Float`]-[`Float`] functions.
3187 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
3188 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
3189 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
3190 ///
3191 /// # Worst-case complexity
3192 /// $T(n) = O(n \log n \log\log n)$
3193 ///
3194 /// $M(n) = O(n)$
3195 ///
3196 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3197 /// other.significant_bits())`.
3198 ///
3199 /// # Panics
3200 /// Panics if `rm` is `Exact` and the winning operand is not exactly representable with the
3201 /// output precision.
3202 ///
3203 /// # Examples
3204 /// ```
3205 /// use core::cmp::Ordering::*;
3206 /// use malachite_base::rounding_modes::RoundingMode::*;
3207 /// use malachite_float::Float;
3208 /// use malachite_q::Rational;
3209 ///
3210 /// let (r, o) =
3211 /// Float::from(3u32).max_rational_round_ref_val(Rational::from_signeds(22, 7), Floor);
3212 /// assert_eq!(r.to_string(), "3.0");
3213 /// assert_eq!(o, Less);
3214 /// ```
3215 #[inline]
3216 pub fn max_rational_round_ref_val(
3217 &self,
3218 other: Rational,
3219 rm: RoundingMode,
3220 ) -> (Self, Ordering) {
3221 let prec = self.significant_bits();
3222 self.max_rational_prec_round_ref_val(other, prec, rm)
3223 }
3224
3225 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the
3226 /// [`Float`]'s precision, with the specified rounding mode. The [`Float`] and the [`Rational`]
3227 /// are both taken by reference. An [`Ordering`] is also returned, indicating whether the result
3228 /// is less than, equal to, or greater than the exact larger value. Although `NaN`s are not
3229 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3230 ///
3231 /// The comparison is exact, and only the winning operand is rounded. Converting the
3232 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
3233 /// crosses the other operand's value.
3234 ///
3235 /// Special cases:
3236 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
3237 /// [`Float`]-[`Float`] functions.
3238 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
3239 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
3240 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
3241 ///
3242 /// # Worst-case complexity
3243 /// $T(n) = O(n \log n \log\log n)$
3244 ///
3245 /// $M(n) = O(n)$
3246 ///
3247 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3248 /// other.significant_bits())`.
3249 ///
3250 /// # Panics
3251 /// Panics if `rm` is `Exact` and the winning operand is not exactly representable with the
3252 /// output precision.
3253 ///
3254 /// # Examples
3255 /// ```
3256 /// use core::cmp::Ordering::*;
3257 /// use malachite_base::rounding_modes::RoundingMode::*;
3258 /// use malachite_float::Float;
3259 /// use malachite_q::Rational;
3260 ///
3261 /// let (r, o) =
3262 /// Float::from(3u32).max_rational_round_ref_ref(&Rational::from_signeds(22, 7), Floor);
3263 /// assert_eq!(r.to_string(), "3.0");
3264 /// assert_eq!(o, Less);
3265 /// ```
3266 #[inline]
3267 pub fn max_rational_round_ref_ref(
3268 &self,
3269 other: &Rational,
3270 rm: RoundingMode,
3271 ) -> (Self, Ordering) {
3272 let prec = self.significant_bits();
3273 self.max_rational_prec_round_ref_ref(other, prec, rm)
3274 }
3275
3276 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the nearest
3277 /// value of the [`Float`]'s precision. The [`Float`] and the [`Rational`] are both taken by
3278 /// value. An [`Ordering`] is also returned, indicating whether the result is less than, equal
3279 /// to, or greater than the exact larger value. Although `NaN`s are not comparable to any
3280 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3281 ///
3282 /// The comparison is exact, and only the winning operand is rounded. Converting the
3283 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
3284 /// crosses the other operand's value.
3285 ///
3286 /// Special cases:
3287 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
3288 /// [`Float`]-[`Float`] functions.
3289 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
3290 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
3291 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
3292 ///
3293 /// # Worst-case complexity
3294 /// $T(n) = O(n \log n \log\log n)$
3295 ///
3296 /// $M(n) = O(n)$
3297 ///
3298 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3299 /// other.significant_bits())`.
3300 ///
3301 /// # Examples
3302 /// ```
3303 /// use core::cmp::Ordering::*;
3304 /// use malachite_float::Float;
3305 /// use malachite_q::Rational;
3306 ///
3307 /// let (r, o) = Float::from(3u32).max_rational(Rational::from_signeds(22, 7));
3308 /// assert_eq!(r.to_string(), "3.0");
3309 /// assert_eq!(o, Less);
3310 /// ```
3311 #[inline]
3312 pub fn max_rational(self, other: Rational) -> (Self, Ordering) {
3313 self.max_rational_round(other, Nearest)
3314 }
3315
3316 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the nearest
3317 /// value of the [`Float`]'s precision. The [`Float`] is taken by value and the [`Rational`] by
3318 /// reference. An [`Ordering`] is also returned, indicating whether the result is less than,
3319 /// equal to, or greater than the exact larger value. Although `NaN`s are not comparable to any
3320 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3321 ///
3322 /// The comparison is exact, and only the winning operand is rounded. Converting the
3323 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
3324 /// crosses the other operand's value.
3325 ///
3326 /// Special cases:
3327 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
3328 /// [`Float`]-[`Float`] functions.
3329 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
3330 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
3331 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
3332 ///
3333 /// # Worst-case complexity
3334 /// $T(n) = O(n \log n \log\log n)$
3335 ///
3336 /// $M(n) = O(n)$
3337 ///
3338 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3339 /// other.significant_bits())`.
3340 ///
3341 /// # Examples
3342 /// ```
3343 /// use core::cmp::Ordering::*;
3344 /// use malachite_float::Float;
3345 /// use malachite_q::Rational;
3346 ///
3347 /// let (r, o) = Float::from(3u32).max_rational_val_ref(&Rational::from_signeds(22, 7));
3348 /// assert_eq!(r.to_string(), "3.0");
3349 /// assert_eq!(o, Less);
3350 /// ```
3351 #[inline]
3352 pub fn max_rational_val_ref(self, other: &Rational) -> (Self, Ordering) {
3353 self.max_rational_round_val_ref(other, Nearest)
3354 }
3355
3356 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the nearest
3357 /// value of the [`Float`]'s precision. The [`Float`] is taken by reference and the [`Rational`]
3358 /// by value. An [`Ordering`] is also returned, indicating whether the result is less than,
3359 /// equal to, or greater than the exact larger value. Although `NaN`s are not comparable to any
3360 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3361 ///
3362 /// The comparison is exact, and only the winning operand is rounded. Converting the
3363 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
3364 /// crosses the other operand's value.
3365 ///
3366 /// Special cases:
3367 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
3368 /// [`Float`]-[`Float`] functions.
3369 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
3370 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
3371 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
3372 ///
3373 /// # Worst-case complexity
3374 /// $T(n) = O(n \log n \log\log n)$
3375 ///
3376 /// $M(n) = O(n)$
3377 ///
3378 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3379 /// other.significant_bits())`.
3380 ///
3381 /// # Examples
3382 /// ```
3383 /// use core::cmp::Ordering::*;
3384 /// use malachite_float::Float;
3385 /// use malachite_q::Rational;
3386 ///
3387 /// let (r, o) = Float::from(3u32).max_rational_ref_val(Rational::from_signeds(22, 7));
3388 /// assert_eq!(r.to_string(), "3.0");
3389 /// assert_eq!(o, Less);
3390 /// ```
3391 #[inline]
3392 pub fn max_rational_ref_val(&self, other: Rational) -> (Self, Ordering) {
3393 self.max_rational_round_ref_val(other, Nearest)
3394 }
3395
3396 /// Computes the larger of a [`Float`] and a [`Rational`], rounding the result to the nearest
3397 /// value of the [`Float`]'s precision. The [`Float`] and the [`Rational`] are both taken by
3398 /// reference. An [`Ordering`] is also returned, indicating whether the result is less than,
3399 /// equal to, or greater than the exact larger value. Although `NaN`s are not comparable to any
3400 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3401 ///
3402 /// The comparison is exact, and only the winning operand is rounded. Converting the
3403 /// [`Rational`] to a [`Float`] first could select the wrong operand, when the conversion
3404 /// crosses the other operand's value.
3405 ///
3406 /// Special cases:
3407 /// - If the [`Float`] is `NaN`, the [`Rational`] operand is returned (rounded), as with the
3408 /// [`Float`]-[`Float`] functions.
3409 /// - If the operands are equal, the [`Float`] operand is returned (rounded), except that a
3410 /// negative zero loses the tie against the (unsigned, treated as positive) zero [`Rational`]:
3411 /// the result is then a positive zero, matching the positive-zero preference of `mpfr_max`.
3412 ///
3413 /// # Worst-case complexity
3414 /// $T(n) = O(n \log n \log\log n)$
3415 ///
3416 /// $M(n) = O(n)$
3417 ///
3418 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.complexity(),
3419 /// other.significant_bits())`.
3420 ///
3421 /// # Examples
3422 /// ```
3423 /// use core::cmp::Ordering::*;
3424 /// use malachite_float::Float;
3425 /// use malachite_q::Rational;
3426 ///
3427 /// let (r, o) = Float::from(3u32).max_rational_ref_ref(&Rational::from_signeds(22, 7));
3428 /// assert_eq!(r.to_string(), "3.0");
3429 /// assert_eq!(o, Less);
3430 /// ```
3431 #[inline]
3432 pub fn max_rational_ref_ref(&self, other: &Rational) -> (Self, Ordering) {
3433 self.max_rational_round_ref_ref(other, Nearest)
3434 }
3435}
3436
3437/// Computes the smaller of a primitive float and a [`Rational`], correctly rounding the result to
3438/// the nearest value.
3439///
3440/// The comparison is exact, and only the winning operand is rounded, so the right operand is
3441/// selected even when converting the [`Rational`] to a primitive float first would land on the
3442/// other side of the comparison. A NaN input yields the [`Rational`], rounded.
3443///
3444/// # Worst-case complexity
3445/// $T(n) = O(n \log n \log\log n)$
3446///
3447/// $M(n) = O(n)$
3448///
3449/// where $T$ is time, $M$ is additional memory, and $n$ is `y.significant_bits()`.
3450///
3451/// # Examples
3452/// ```
3453/// use malachite_base::num::float::NiceFloat;
3454/// use malachite_float::float::comparison::min_max::primitive_float_min_rational;
3455/// use malachite_q::Rational;
3456///
3457/// assert_eq!(
3458/// NiceFloat(primitive_float_min_rational(
3459/// 3.0,
3460/// &Rational::from_signeds(22, 7)
3461/// )),
3462/// NiceFloat(3.0)
3463/// );
3464/// ```
3465#[allow(clippy::type_repetition_in_bounds)]
3466#[inline]
3467pub fn primitive_float_min_rational<T: PrimitiveFloat>(x: T, y: &Rational) -> T
3468where
3469 Float: From<T> + PartialOrd<T>,
3470 for<'a> T: ExactFrom<&'a Float>,
3471{
3472 emulate_float_to_float_fn(|x, prec| x.min_rational_prec_val_ref(y, prec), x)
3473}
3474
3475/// Computes the larger of a primitive float and a [`Rational`], correctly rounding the result to
3476/// the nearest value.
3477///
3478/// The comparison is exact, and only the winning operand is rounded. A NaN input yields the
3479/// [`Rational`], rounded.
3480///
3481/// # Worst-case complexity
3482/// $T(n) = O(n \log n \log\log n)$
3483///
3484/// $M(n) = O(n)$
3485///
3486/// where $T$ is time, $M$ is additional memory, and $n$ is `y.significant_bits()`.
3487///
3488/// # Examples
3489/// ```
3490/// use malachite_base::num::float::NiceFloat;
3491/// use malachite_float::float::comparison::min_max::primitive_float_max_rational;
3492/// use malachite_q::Rational;
3493///
3494/// assert_eq!(
3495/// NiceFloat(primitive_float_max_rational(
3496/// 3.0,
3497/// &Rational::from_signeds(22, 7)
3498/// )),
3499/// NiceFloat(3.142857142857143)
3500/// );
3501/// ```
3502#[allow(clippy::type_repetition_in_bounds)]
3503#[inline]
3504pub fn primitive_float_max_rational<T: PrimitiveFloat>(x: T, y: &Rational) -> T
3505where
3506 Float: From<T> + PartialOrd<T>,
3507 for<'a> T: ExactFrom<&'a Float>,
3508{
3509 emulate_float_to_float_fn(|x, prec| x.max_rational_prec_val_ref(y, prec), x)
3510}