malachite_float/arithmetic/div.rs
1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::InnerFloat::{Finite, Infinity, NaN, Zero};
10use crate::arithmetic::is_power_of_2::abs_is_power_of_2;
11use crate::{
12 Float, float_either_infinity, float_either_zero, float_infinity, float_nan,
13 float_negative_infinity, float_negative_zero, float_zero,
14};
15use core::cmp::Ordering::{self, *};
16use core::cmp::max;
17use core::mem::swap;
18use core::ops::{Div, DivAssign};
19use malachite_base::num::arithmetic::traits::{CheckedLogBase2, IsPowerOf2, NegAssign, Sign};
20use malachite_base::num::basic::traits::Zero as ZeroTrait;
21use malachite_base::num::conversion::traits::ExactFrom;
22use malachite_base::num::logic::traits::{NotAssign, SignificantBits};
23use malachite_base::rounding_modes::RoundingMode::{self, *};
24use malachite_nz::natural::arithmetic::float_div::{
25 div_float_significands_in_place, div_float_significands_in_place_ref,
26 div_float_significands_ref_ref, div_float_significands_ref_val,
27};
28use malachite_q::Rational;
29
30const DIV_RATIONAL_THRESHOLD: u64 = 50;
31const RATIONAL_DIV_THRESHOLD: u64 = 50;
32
33fn div_rational_prec_round_assign_naive(
34 x: &mut Float,
35 y: Rational,
36 prec: u64,
37 rm: RoundingMode,
38) -> Ordering {
39 assert_ne!(prec, 0);
40 match (&mut *x, y) {
41 (float_nan!(), _) => Equal,
42 (Float(Infinity { sign }), y) => {
43 if y < 0 {
44 sign.not_assign();
45 };
46 Equal
47 }
48 (Float(Zero { sign }), y) => {
49 match y.sign() {
50 Equal => *x = float_nan!(),
51 Greater => {}
52 Less => sign.not_assign(),
53 }
54 Equal
55 }
56 (x, y) => {
57 if y == 0 {
58 *x = Float(Infinity { sign: *x > 0u32 });
59 Equal
60 } else {
61 let not_sign = *x < 0;
62 let mut z = Float::ZERO;
63 swap(x, &mut z);
64 let (mut quotient, o) =
65 Float::from_rational_prec_round(Rational::exact_from(z) / y, prec, rm);
66 if quotient == 0u32 && not_sign {
67 quotient.neg_assign();
68 }
69 *x = quotient;
70 o
71 }
72 }
73 }
74}
75
76fn div_rational_prec_round_assign_naive_ref(
77 x: &mut Float,
78 y: &Rational,
79 prec: u64,
80 rm: RoundingMode,
81) -> Ordering {
82 assert_ne!(prec, 0);
83 match (&mut *x, y) {
84 (float_nan!(), _) => Equal,
85 (Float(Infinity { sign }), y) => {
86 if *y < 0 {
87 sign.not_assign();
88 };
89 Equal
90 }
91 (Float(Zero { sign }), y) => {
92 match y.sign() {
93 Equal => *x = float_nan!(),
94 Greater => {}
95 Less => sign.not_assign(),
96 }
97 Equal
98 }
99 (x, y) => {
100 if *y == 0 {
101 *x = Float(Infinity { sign: *x > 0u32 });
102 Equal
103 } else {
104 let not_sign = *x < 0;
105 let mut z = Float::ZERO;
106 swap(x, &mut z);
107 let (mut quotient, o) =
108 Float::from_rational_prec_round(Rational::exact_from(z) / y, prec, rm);
109 if quotient == 0u32 && not_sign {
110 quotient.neg_assign();
111 }
112 *x = quotient;
113 o
114 }
115 }
116 }
117}
118
119pub_test! {div_rational_prec_round_naive(
120 mut x: Float,
121 y: Rational,
122 prec: u64,
123 rm: RoundingMode,
124) -> (Float, Ordering) {
125 let o = div_rational_prec_round_assign_naive(&mut x, y, prec, rm);
126 (x, o)
127}}
128
129pub_test! {div_rational_prec_round_naive_val_ref(
130 mut x: Float,
131 y: &Rational,
132 prec: u64,
133 rm: RoundingMode,
134) -> (Float, Ordering) {
135 let o = div_rational_prec_round_assign_naive_ref(&mut x, y, prec, rm);
136 (x, o)
137}}
138
139pub_test! {div_rational_prec_round_naive_ref_val(
140 x: &Float,
141 y: Rational,
142 prec: u64,
143 rm: RoundingMode,
144) -> (Float, Ordering) {
145 assert_ne!(prec, 0);
146 match (x, y) {
147 (float_nan!(), _) => (float_nan!(), Equal),
148 (Float(Infinity { sign }), y) => (
149 if y >= 0u32 {
150 Float(Infinity { sign: *sign })
151 } else {
152 Float(Infinity { sign: !*sign })
153 },
154 Equal,
155 ),
156 (Float(Zero { sign }), y) => (
157 match y.sign() {
158 Equal => float_nan!(),
159 Greater => Float(Zero { sign: *sign }),
160 Less => Float(Zero { sign: !*sign }),
161 },
162 Equal,
163 ),
164 (x, y) => {
165 if y == 0 {
166 (Float(Infinity { sign: *x > 0u32 }), Equal)
167 } else {
168 let (mut quotient, o) =
169 Float::from_rational_prec_round(Rational::exact_from(x) / y, prec, rm);
170 if quotient == 0u32 && *x < 0 {
171 quotient.neg_assign();
172 }
173 (quotient, o)
174 }
175 }
176 }
177}}
178
179pub_test! {div_rational_prec_round_naive_ref_ref(
180 x: &Float,
181 y: &Rational,
182 prec: u64,
183 rm: RoundingMode,
184) -> (Float, Ordering) {
185 assert_ne!(prec, 0);
186 match (x, y) {
187 (float_nan!(), _) => (float_nan!(), Equal),
188 (Float(Infinity { sign }), y) => (
189 if *y >= 0u32 {
190 Float(Infinity { sign: *sign })
191 } else {
192 Float(Infinity { sign: !*sign })
193 },
194 Equal,
195 ),
196 (Float(Zero { sign }), y) => (
197 match y.sign() {
198 Equal => float_nan!(),
199 Greater => Float(Zero { sign: *sign }),
200 Less => Float(Zero { sign: !*sign }),
201 },
202 Equal,
203 ),
204 (x, y) => {
205 if *y == 0 {
206 (Float(Infinity { sign: *x > 0u32 }), Equal)
207 } else {
208 let (mut quotient, o) =
209 Float::from_rational_prec_round(Rational::exact_from(x) / y, prec, rm);
210 if quotient == 0u32 && *x < 0 {
211 quotient.neg_assign();
212 }
213 (quotient, o)
214 }
215 }
216 }
217}}
218
219fn div_rational_prec_round_assign_direct(
220 x: &mut Float,
221 y: Rational,
222 prec: u64,
223 mut rm: RoundingMode,
224) -> Ordering {
225 assert_ne!(prec, 0);
226 let sign = y >= 0;
227 let (n, d) = y.into_numerator_and_denominator();
228 if !sign {
229 rm.neg_assign();
230 }
231 let o = match (
232 if n == 0 { None } else { n.checked_log_base_2() },
233 d.checked_log_base_2(),
234 ) {
235 (Some(log_n), Some(log_d)) => {
236 let o = x.set_prec_round(prec, rm);
237 *x <<= log_d;
238 *x >>= log_n;
239 o
240 }
241 (None, Some(log_d)) => {
242 let o = x.div_prec_round_assign(Float::exact_from(n), prec, rm);
243 *x <<= log_d;
244 o
245 }
246 (Some(log_n), None) => {
247 let o = x.mul_prec_round_assign(Float::exact_from(d), prec, rm);
248 *x >>= log_n;
249 o
250 }
251 (None, None) => {
252 let n = Float::exact_from(n);
253 let d = Float::exact_from(d);
254 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
255 x.mul_prec_round_assign(d, mul_prec, Floor);
256 x.div_prec_round_assign(n, prec, rm)
257 }
258 };
259 if sign {
260 o
261 } else {
262 x.neg_assign();
263 o.reverse()
264 }
265}
266
267fn div_rational_prec_round_assign_direct_ref(
268 x: &mut Float,
269 y: &Rational,
270 prec: u64,
271 mut rm: RoundingMode,
272) -> Ordering {
273 assert_ne!(prec, 0);
274 let sign = *y >= 0;
275 let (n, d) = y.numerator_and_denominator_ref();
276 if !sign {
277 rm.neg_assign();
278 }
279 let o = match (
280 if *n == 0 {
281 None
282 } else {
283 n.checked_log_base_2()
284 },
285 d.checked_log_base_2(),
286 ) {
287 (Some(log_n), Some(log_d)) => {
288 let o = x.set_prec_round(prec, rm);
289 *x <<= log_d;
290 *x >>= log_n;
291 o
292 }
293 (None, Some(log_d)) => {
294 let o = x.div_prec_round_assign(Float::exact_from(n), prec, rm);
295 *x <<= log_d;
296 o
297 }
298 (Some(log_n), None) => {
299 let o = x.mul_prec_round_assign(Float::exact_from(d), prec, rm);
300 *x >>= log_n;
301 o
302 }
303 (None, None) => {
304 let n = Float::exact_from(n);
305 let d = Float::exact_from(d);
306 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
307 x.mul_prec_round_assign(d, mul_prec, Floor);
308 x.div_prec_round_assign(n, prec, rm)
309 }
310 };
311 if sign {
312 o
313 } else {
314 x.neg_assign();
315 o.reverse()
316 }
317}
318
319pub_test! {div_rational_prec_round_direct(
320 mut x: Float,
321 y: Rational,
322 prec: u64,
323 rm: RoundingMode,
324) -> (Float, Ordering) {
325 let o = div_rational_prec_round_assign_direct(&mut x, y, prec, rm);
326 (x, o)
327}}
328
329pub_test! {div_rational_prec_round_direct_val_ref(
330 mut x: Float,
331 y: &Rational,
332 prec: u64,
333 rm: RoundingMode,
334) -> (Float, Ordering) {
335 let o = div_rational_prec_round_assign_direct_ref(&mut x, y, prec, rm);
336 (x, o)
337}}
338
339pub_test! {div_rational_prec_round_direct_ref_val(
340 x: &Float,
341 y: Rational,
342 prec: u64,
343 mut rm: RoundingMode,
344) -> (Float, Ordering) {
345 assert_ne!(prec, 0);
346 let sign = y >= 0;
347 let (n, d) = y.into_numerator_and_denominator();
348 if !sign {
349 rm.neg_assign();
350 }
351 let (quotient, o) = match (
352 if n == 0 { None } else { n.checked_log_base_2() },
353 d.checked_log_base_2(),
354 ) {
355 (Some(log_n), Some(log_d)) => {
356 let (quotient, o) = Float::from_float_prec_round_ref(x, prec, rm);
357 (quotient << log_d >> log_n, o)
358 }
359 (None, Some(log_d)) => {
360 let (quotient, o) = x.div_prec_round_ref_val(Float::exact_from(n), prec, rm);
361 (quotient << log_d, o)
362 }
363 (Some(log_n), None) => {
364 let (quotient, o) = x.mul_prec_round_ref_val(Float::exact_from(d), prec, rm);
365 (quotient >> log_n, o)
366 }
367 (None, None) => {
368 let n = Float::exact_from(n);
369 let d = Float::exact_from(d);
370 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
371 x.mul_prec_round_ref_val(d, mul_prec, Floor)
372 .0
373 .div_prec_round(n, prec, rm)
374 }
375 };
376 if sign {
377 (quotient, o)
378 } else {
379 (-quotient, o.reverse())
380 }
381}}
382
383pub_test! {div_rational_prec_round_direct_ref_ref(
384 x: &Float,
385 y: &Rational,
386 prec: u64,
387 mut rm: RoundingMode,
388) -> (Float, Ordering) {
389 assert_ne!(prec, 0);
390 let sign = *y >= 0;
391 let (n, d) = y.numerator_and_denominator_ref();
392 if !sign {
393 rm.neg_assign();
394 }
395 let (quotient, o) = match (
396 if *n == 0 {
397 None
398 } else {
399 n.checked_log_base_2()
400 },
401 d.checked_log_base_2(),
402 ) {
403 (Some(log_n), Some(log_d)) => {
404 let (quotient, o) = Float::from_float_prec_round_ref(x, prec, rm);
405 (quotient << log_d >> log_n, o)
406 }
407 (None, Some(log_d)) => {
408 let (quotient, o) =
409 x.div_prec_round_ref_val(Float::exact_from(n), prec, rm);
410 (quotient << log_d, o)
411 }
412 (Some(log_n), None) => {
413 let (quotient, o) =
414 x.mul_prec_round_ref_val(Float::exact_from(d), prec, rm);
415 (quotient >> log_n, o)
416 }
417 (None, None) => {
418 let n = Float::exact_from(n);
419 let d = Float::exact_from(d);
420 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
421 x.mul_prec_round_ref_val(d, mul_prec, Floor)
422 .0
423 .div_prec_round(n, prec, rm)
424 }
425 };
426 if sign {
427 (quotient, o)
428 } else {
429 (-quotient, o.reverse())
430 }
431}}
432
433pub_test! {rational_div_float_prec_round_naive(
434 x: Rational,
435 y: Float,
436 prec: u64,
437 rm: RoundingMode,
438) -> (Float, Ordering) {
439 assert_ne!(prec, 0);
440 match (x, y) {
441 (_, float_nan!()) => (float_nan!(), Equal),
442 (x, Float(Infinity { sign })) => (
443 if x >= 0u32 {
444 Float(Zero { sign })
445 } else {
446 Float(Zero { sign: !sign })
447 },
448 Equal,
449 ),
450 (x, Float(Zero { sign })) => (
451 match x.sign() {
452 Equal => float_nan!(),
453 Greater => Float(Infinity { sign }),
454 Less => Float(Infinity { sign: !sign }),
455 },
456 Equal,
457 ),
458 (x, y) => {
459 let not_sign = y < 0;
460 let (mut quotient, o) =
461 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
462 if quotient == 0u32 && not_sign {
463 quotient.neg_assign();
464 }
465 (quotient, o)
466 }
467 }
468}}
469
470pub_test! {rational_div_float_prec_round_naive_val_ref(
471 x: Rational,
472 y: &Float,
473 prec: u64,
474 rm: RoundingMode,
475) -> (Float, Ordering) {
476 assert_ne!(prec, 0);
477 match (x, y) {
478 (_, float_nan!()) => (float_nan!(), Equal),
479 (x, Float(Infinity { sign })) => (
480 if x >= 0u32 {
481 Float(Zero { sign: *sign })
482 } else {
483 Float(Zero { sign: !*sign })
484 },
485 Equal,
486 ),
487 (x, Float(Zero { sign })) => (
488 match x.sign() {
489 Equal => float_nan!(),
490 Greater => Float(Infinity { sign: *sign }),
491 Less => Float(Infinity { sign: !*sign }),
492 },
493 Equal,
494 ),
495 (x, y) => {
496 let (mut quotient, o) =
497 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
498 if quotient == 0u32 && *y < 0 {
499 quotient.neg_assign();
500 }
501 (quotient, o)
502 }
503 }
504}}
505
506pub_test! {rational_div_float_prec_round_naive_ref_val(
507 x: &Rational,
508 y: Float,
509 prec: u64,
510 rm: RoundingMode,
511) -> (Float, Ordering) {
512 assert_ne!(prec, 0);
513 match (x, y) {
514 (_, float_nan!()) => (float_nan!(), Equal),
515 (x, Float(Infinity { sign })) => (
516 if *x >= 0u32 {
517 Float(Zero { sign })
518 } else {
519 Float(Zero { sign: !sign })
520 },
521 Equal,
522 ),
523 (x, Float(Zero { sign })) => (
524 match x.sign() {
525 Equal => float_nan!(),
526 Greater => Float(Infinity { sign }),
527 Less => Float(Infinity { sign: !sign }),
528 },
529 Equal,
530 ),
531 (x, y) => {
532 let not_sign = y < 0;
533 let (mut quotient, o) =
534 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
535 if quotient == 0u32 && not_sign {
536 quotient.neg_assign();
537 }
538 (quotient, o)
539 }
540 }
541}}
542
543pub_test! {rational_div_float_prec_round_naive_ref_ref(
544 x: &Rational,
545 y: &Float,
546 prec: u64,
547 rm: RoundingMode,
548) -> (Float, Ordering) {
549 assert_ne!(prec, 0);
550 match (x, y) {
551 (_, float_nan!()) => (float_nan!(), Equal),
552 (x, Float(Infinity { sign })) => (
553 if *x >= 0u32 {
554 Float(Zero { sign: *sign })
555 } else {
556 Float(Zero { sign: !*sign })
557 },
558 Equal,
559 ),
560 (x, Float(Zero { sign })) => (
561 match x.sign() {
562 Equal => float_nan!(),
563 Greater => Float(Infinity { sign: *sign }),
564 Less => Float(Infinity { sign: !*sign }),
565 },
566 Equal,
567 ),
568 (x, y) => {
569 let (mut quotient, o) =
570 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
571 if quotient == 0u32 && *y < 0 {
572 quotient.neg_assign();
573 }
574 (quotient, o)
575 }
576 }
577}}
578
579pub_test! {rational_div_float_prec_round_direct(
580 x: Rational,
581 y: Float,
582 prec: u64,
583 mut rm: RoundingMode,
584) -> (Float, Ordering) {
585 assert_ne!(prec, 0);
586 let sign = x >= 0;
587 let (n, d) = x.into_numerator_and_denominator();
588 if !sign {
589 rm.neg_assign();
590 }
591 let (quotient, o) = match (
592 if n == 0 { None } else { n.checked_log_base_2() },
593 d.checked_log_base_2(),
594 ) {
595 (Some(log_n), Some(log_d)) => {
596 let (quotient, o) = Float::reciprocal_prec_round(y, prec, rm);
597 (quotient << log_n >> log_d, o)
598 }
599 (None, Some(log_d)) => {
600 let (quotient, o) = Float::exact_from(n).div_prec_round(y, prec, rm);
601 (quotient >> log_d, o)
602 }
603 (Some(log_n), None) => {
604 let d = Float::exact_from(d);
605 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
606 let (quotient, o) = y
607 .mul_prec_round(d, mul_prec, Floor)
608 .0
609 .reciprocal_prec_round(prec, rm);
610 (quotient << log_n, o)
611 }
612 (None, None) => {
613 let n = Float::exact_from(n);
614 let d = Float::exact_from(d);
615 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
616 n.div_prec_round(y.mul_prec_round(d, mul_prec, Floor).0, prec, rm)
617 }
618 };
619 if sign {
620 (quotient, o)
621 } else {
622 (-quotient, o.reverse())
623 }
624}}
625
626pub_test! {rational_div_float_prec_round_direct_val_ref(
627 x: Rational,
628 y: &Float,
629 prec: u64,
630 mut rm: RoundingMode,
631) -> (Float, Ordering) {
632 assert_ne!(prec, 0);
633 let sign = x >= 0;
634 let (n, d) = x.into_numerator_and_denominator();
635 if !sign {
636 rm.neg_assign();
637 }
638 let (quotient, o) = match (
639 if n == 0 { None } else { n.checked_log_base_2() },
640 d.checked_log_base_2(),
641 ) {
642 (Some(log_n), Some(log_d)) => {
643 let (quotient, o) = Float::reciprocal_prec_round_ref(y, prec, rm);
644 (quotient << log_n >> log_d, o)
645 }
646 (None, Some(log_d)) => {
647 let (quotient, o) = Float::exact_from(n).div_prec_round_val_ref(y, prec, rm);
648 (quotient >> log_d, o)
649 }
650 (Some(log_n), None) => {
651 let d = Float::exact_from(d);
652 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
653 let (quotient, o) = y
654 .mul_prec_round_ref_val(d, mul_prec, Floor)
655 .0
656 .reciprocal_prec_round(prec, rm);
657 (quotient << log_n, o)
658 }
659 (None, None) => {
660 let n = Float::exact_from(n);
661 let d = Float::exact_from(d);
662 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
663 n.div_prec_round(y.mul_prec_round_ref_val(d, mul_prec, Floor).0, prec, rm)
664 }
665 };
666 if sign {
667 (quotient, o)
668 } else {
669 (-quotient, o.reverse())
670 }
671}}
672
673pub_test! {rational_div_float_prec_round_direct_ref_val(
674 x: &Rational,
675 y: Float,
676 prec: u64,
677 mut rm: RoundingMode,
678) -> (Float, Ordering) {
679 assert_ne!(prec, 0);
680 let sign = *x >= 0;
681 let (n, d) = x.numerator_and_denominator_ref();
682 if !sign {
683 rm.neg_assign();
684 }
685 let (quotient, o) = match (
686 if *n == 0 {
687 None
688 } else {
689 n.checked_log_base_2()
690 },
691 d.checked_log_base_2(),
692 ) {
693 (Some(log_n), Some(log_d)) => {
694 let (quotient, o) = Float::reciprocal_prec_round(y, prec, rm);
695 (quotient << log_n >> log_d, o)
696 }
697 (None, Some(log_d)) => {
698 let (quotient, o) = Float::exact_from(n).div_prec_round(y, prec, rm);
699 (quotient >> log_d, o)
700 }
701 (Some(log_n), None) => {
702 let d = Float::exact_from(d);
703 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
704 let (quotient, o) = y
705 .mul_prec_round(d, mul_prec, Floor)
706 .0
707 .reciprocal_prec_round(prec, rm);
708 (quotient << log_n, o)
709 }
710 (None, None) => {
711 let n = Float::exact_from(n);
712 let d = Float::exact_from(d);
713 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
714 n.div_prec_round(y.mul_prec_round(d, mul_prec, Floor).0, prec, rm)
715 }
716 };
717 if sign {
718 (quotient, o)
719 } else {
720 (-quotient, o.reverse())
721 }
722}}
723
724pub_test! {rational_div_float_prec_round_direct_ref_ref(
725 x: &Rational,
726 y: &Float,
727 prec: u64,
728 mut rm: RoundingMode,
729) -> (Float, Ordering) {
730 assert_ne!(prec, 0);
731 let sign = *x >= 0;
732 let (n, d) = x.numerator_and_denominator_ref();
733 if !sign {
734 rm.neg_assign();
735 }
736 let (quotient, o) = match (
737 if *n == 0 {
738 None
739 } else {
740 n.checked_log_base_2()
741 },
742 d.checked_log_base_2(),
743 ) {
744 (Some(log_n), Some(log_d)) => {
745 let (quotient, o) = Float::reciprocal_prec_round_ref(y, prec, rm);
746 (quotient << log_n >> log_d, o)
747 }
748 (None, Some(log_d)) => {
749 let (quotient, o) =
750 Float::exact_from(n).div_prec_round_val_ref(y, prec, rm);
751 (quotient >> log_d, o)
752 }
753 (Some(log_n), None) => {
754 let d = Float::exact_from(d);
755 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
756 let (quotient, o) = y
757 .mul_prec_round_ref_val(d, mul_prec, Floor)
758 .0
759 .reciprocal_prec_round(prec, rm);
760 (quotient << log_n, o)
761 }
762 (None, None) => {
763 let n = Float::exact_from(n);
764 let d = Float::exact_from(d);
765 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
766 n.div_prec_round(y.mul_prec_round_ref_val(d, mul_prec, Floor).0, prec, rm)
767 }
768 };
769 if sign {
770 (quotient, o)
771 } else {
772 (-quotient, o.reverse())
773 }
774}}
775
776impl Float {
777 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
778 /// specified rounding mode. Both [`Float`]s are taken by value. An [`Ordering`] is also
779 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
780 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
781 /// function returns a `NaN` it also returns `Equal`.
782 ///
783 /// See [`RoundingMode`] for a description of the possible rounding modes.
784 ///
785 /// $$
786 /// f(x,y,p,m) = x/y+\varepsilon.
787 /// $$
788 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
789 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
790 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
791 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
792 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
793 ///
794 /// If the output has a precision, it is `prec`.
795 ///
796 /// Special cases:
797 /// - $f(\text{NaN},x,p,m)=f(x,\text{NaN},p,m)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
798 /// \text{NaN}$
799 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
800 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
801 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
802 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
803 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
804 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
805 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
806 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
807 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
808 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
809 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
810 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
811 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
812 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $x<0.0$
813 /// - $f(x,-\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
814 /// - $f(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
815 ///
816 /// Overflow and underflow:
817 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
818 /// returned instead.
819 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
820 /// is returned instead, where `p` is the precision of the input.
821 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
822 /// returned instead.
823 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
824 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
825 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
826 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
827 /// instead.
828 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
829 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
830 /// instead.
831 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
832 /// instead.
833 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
834 /// instead.
835 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
836 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
837 /// returned instead.
838 ///
839 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec`] instead. If you
840 /// know that your target precision is the maximum of the precisions of the two inputs, consider
841 /// using [`Float::div_round`] instead. If both of these things are true, consider using `/`
842 /// instead.
843 ///
844 /// # Worst-case complexity
845 /// $T(n) = O(n \log n \log\log n)$
846 ///
847 /// $M(n) = O(n \log n)$
848 ///
849 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
850 /// other.significant_bits(), `prec`)`.
851 ///
852 /// # Panics
853 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
854 ///
855 /// # Examples
856 /// ```
857 /// use core::f64::consts::{E, PI};
858 /// use malachite_base::rounding_modes::RoundingMode::*;
859 /// use malachite_float::Float;
860 /// use std::cmp::Ordering::*;
861 ///
862 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 5, Floor);
863 /// assert_eq!(quotient.to_string(), "1.12");
864 /// assert_eq!(o, Less);
865 ///
866 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 5, Ceiling);
867 /// assert_eq!(quotient.to_string(), "1.19");
868 /// assert_eq!(o, Greater);
869 ///
870 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 5, Nearest);
871 /// assert_eq!(quotient.to_string(), "1.12");
872 /// assert_eq!(o, Less);
873 ///
874 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 20, Floor);
875 /// assert_eq!(quotient.to_string(), "1.155725");
876 /// assert_eq!(o, Less);
877 ///
878 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 20, Ceiling);
879 /// assert_eq!(quotient.to_string(), "1.155727");
880 /// assert_eq!(o, Greater);
881 ///
882 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 20, Nearest);
883 /// assert_eq!(quotient.to_string(), "1.155727");
884 /// assert_eq!(o, Greater);
885 /// ```
886 #[inline]
887 pub fn div_prec_round(mut self, other: Self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
888 let o = self.div_prec_round_assign(other, prec, rm);
889 (self, o)
890 }
891
892 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
893 /// specified rounding mode. The first [`Float`] is are taken by value and the second by
894 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
895 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
896 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
897 ///
898 /// See [`RoundingMode`] for a description of the possible rounding modes.
899 ///
900 /// $$
901 /// f(x,y,p,m) = x/y+\varepsilon.
902 /// $$
903 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
904 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
905 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
906 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
907 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
908 ///
909 /// If the output has a precision, it is `prec`.
910 ///
911 /// Special cases:
912 /// - $f(\text{NaN},x,p,m)=f(x,\text{NaN},p,m)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
913 /// \text{NaN}$
914 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
915 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
916 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
917 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
918 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
919 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
920 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
921 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
922 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
923 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
924 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
925 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
926 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
927 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $x<0.0$
928 /// - $f(x,-\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
929 /// - $f(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
930 ///
931 /// Overflow and underflow:
932 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
933 /// returned instead.
934 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
935 /// is returned instead, where `p` is the precision of the input.
936 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
937 /// returned instead.
938 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
939 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
940 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
941 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
942 /// instead.
943 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
944 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
945 /// instead.
946 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
947 /// instead.
948 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
949 /// instead.
950 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
951 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
952 /// returned instead.
953 ///
954 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_val_ref`] instead.
955 /// If you know that your target precision is the maximum of the precisions of the two inputs,
956 /// consider using [`Float::div_round_val_ref`] instead. If both of these things are true,
957 /// consider using `/` instead.
958 ///
959 /// # Worst-case complexity
960 /// $T(n) = O(n \log n \log\log n)$
961 ///
962 /// $M(n) = O(n \log n)$
963 ///
964 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
965 /// other.significant_bits(), `prec`)`.
966 ///
967 /// # Panics
968 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
969 ///
970 /// # Examples
971 /// ```
972 /// use core::f64::consts::{E, PI};
973 /// use malachite_base::rounding_modes::RoundingMode::*;
974 /// use malachite_float::Float;
975 /// use std::cmp::Ordering::*;
976 ///
977 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 5, Floor);
978 /// assert_eq!(quotient.to_string(), "1.12");
979 /// assert_eq!(o, Less);
980 ///
981 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 5, Ceiling);
982 /// assert_eq!(quotient.to_string(), "1.19");
983 /// assert_eq!(o, Greater);
984 ///
985 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 5, Nearest);
986 /// assert_eq!(quotient.to_string(), "1.12");
987 /// assert_eq!(o, Less);
988 ///
989 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 20, Floor);
990 /// assert_eq!(quotient.to_string(), "1.155725");
991 /// assert_eq!(o, Less);
992 ///
993 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 20, Ceiling);
994 /// assert_eq!(quotient.to_string(), "1.155727");
995 /// assert_eq!(o, Greater);
996 ///
997 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 20, Nearest);
998 /// assert_eq!(quotient.to_string(), "1.155727");
999 /// assert_eq!(o, Greater);
1000 /// ```
1001 #[inline]
1002 pub fn div_prec_round_val_ref(
1003 mut self,
1004 other: &Self,
1005 prec: u64,
1006 rm: RoundingMode,
1007 ) -> (Self, Ordering) {
1008 let o = self.div_prec_round_assign_ref(other, prec, rm);
1009 (self, o)
1010 }
1011
1012 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
1013 /// specified rounding mode. The first [`Float`] is are taken by reference and the second by
1014 /// value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
1015 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
1016 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1017 ///
1018 /// See [`RoundingMode`] for a description of the possible rounding modes.
1019 ///
1020 /// $$
1021 /// f(x,y,p,m) = x/y+\varepsilon.
1022 /// $$
1023 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1024 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1025 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
1026 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
1027 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1028 ///
1029 /// If the output has a precision, it is `prec`.
1030 ///
1031 /// Special cases:
1032 /// - $f(\text{NaN},x,p,m)=f(x,\text{NaN},p,m)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1033 /// \text{NaN}$
1034 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
1035 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
1036 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
1037 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
1038 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
1039 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
1040 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
1041 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
1042 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
1043 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1044 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1045 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1046 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1047 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $x<0.0$
1048 /// - $f(x,-\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1049 /// - $f(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1050 ///
1051 /// Overflow and underflow:
1052 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1053 /// returned instead.
1054 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
1055 /// is returned instead, where `p` is the precision of the input.
1056 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
1057 /// returned instead.
1058 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
1059 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
1060 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1061 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1062 /// instead.
1063 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1064 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1065 /// instead.
1066 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
1067 /// instead.
1068 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
1069 /// instead.
1070 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1071 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
1072 /// returned instead.
1073 ///
1074 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_ref_val`] instead.
1075 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1076 /// consider using [`Float::div_round_ref_val`] instead. If both of these things are true,
1077 /// consider using `/` instead.
1078 ///
1079 /// # Worst-case complexity
1080 /// $T(n) = O(n \log n \log\log n)$
1081 ///
1082 /// $M(n) = O(n \log n)$
1083 ///
1084 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1085 /// other.significant_bits(), `prec`)`.
1086 ///
1087 /// # Panics
1088 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
1089 ///
1090 /// # Examples
1091 /// ```
1092 /// use core::f64::consts::{E, PI};
1093 /// use malachite_base::rounding_modes::RoundingMode::*;
1094 /// use malachite_float::Float;
1095 /// use std::cmp::Ordering::*;
1096 ///
1097 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 5, Floor);
1098 /// assert_eq!(quotient.to_string(), "1.12");
1099 /// assert_eq!(o, Less);
1100 ///
1101 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 5, Ceiling);
1102 /// assert_eq!(quotient.to_string(), "1.19");
1103 /// assert_eq!(o, Greater);
1104 ///
1105 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 5, Nearest);
1106 /// assert_eq!(quotient.to_string(), "1.12");
1107 /// assert_eq!(o, Less);
1108 ///
1109 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 20, Floor);
1110 /// assert_eq!(quotient.to_string(), "1.155725");
1111 /// assert_eq!(o, Less);
1112 ///
1113 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 20, Ceiling);
1114 /// assert_eq!(quotient.to_string(), "1.155727");
1115 /// assert_eq!(o, Greater);
1116 ///
1117 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 20, Nearest);
1118 /// assert_eq!(quotient.to_string(), "1.155727");
1119 /// assert_eq!(o, Greater);
1120 /// ```
1121 #[inline]
1122 pub fn div_prec_round_ref_val(
1123 &self,
1124 other: Self,
1125 prec: u64,
1126 rm: RoundingMode,
1127 ) -> (Self, Ordering) {
1128 assert_ne!(prec, 0);
1129 match (self, other) {
1130 (float_nan!(), _)
1131 | (_, float_nan!())
1132 | (float_either_infinity!(), float_either_infinity!())
1133 | (float_either_zero!(), float_either_zero!()) => (float_nan!(), Equal),
1134 (
1135 Self(Infinity { sign: x_sign }),
1136 Self(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
1137 )
1138 | (Self(Finite { sign: x_sign, .. }), Self(Zero { sign: y_sign })) => (
1139 Self(Infinity {
1140 sign: *x_sign == y_sign,
1141 }),
1142 Equal,
1143 ),
1144 (
1145 Self(Zero { sign: x_sign }),
1146 Self(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
1147 )
1148 | (Self(Finite { sign: x_sign, .. }), Self(Infinity { sign: y_sign })) => (
1149 Self(Zero {
1150 sign: *x_sign == y_sign,
1151 }),
1152 Equal,
1153 ),
1154 (
1155 Self(Finite {
1156 sign: x_sign,
1157 exponent: x_exp,
1158 precision: x_prec,
1159 significand: x,
1160 }),
1161 Self(Finite {
1162 sign: y_sign,
1163 exponent: y_exp,
1164 precision: y_prec,
1165 significand: mut y,
1166 }),
1167 ) => {
1168 if y.is_power_of_2() {
1169 let (mut quotient, mut o) =
1170 self.shr_prec_round_ref(y_exp - 1, prec, if y_sign { rm } else { -rm });
1171 if !y_sign {
1172 quotient.neg_assign();
1173 o = o.reverse();
1174 }
1175 return (quotient, o);
1176 }
1177 let sign = *x_sign == y_sign;
1178 let exp_diff = *x_exp - y_exp;
1179 if exp_diff > Self::MAX_EXPONENT {
1180 return match (sign, rm) {
1181 (_, Exact) => panic!("Inexact Float division"),
1182 (true, Ceiling | Up | Nearest) => (float_infinity!(), Greater),
1183 (true, _) => (Self::max_finite_value_with_prec(prec), Less),
1184 (false, Floor | Up | Nearest) => (float_negative_infinity!(), Less),
1185 (false, _) => (-Self::max_finite_value_with_prec(prec), Greater),
1186 };
1187 } else if exp_diff + 2 < Self::MIN_EXPONENT {
1188 return match (sign, rm) {
1189 (_, Exact) => panic!("Inexact Float division"),
1190 (true, Ceiling | Up) => (Self::min_positive_value_prec(prec), Greater),
1191 (true, _) => (float_zero!(), Less),
1192 (false, Floor | Up) => (-Self::min_positive_value_prec(prec), Less),
1193 (false, _) => (float_negative_zero!(), Greater),
1194 };
1195 }
1196 let (quotient, exp_offset, o) = div_float_significands_ref_val(
1197 x,
1198 *x_prec,
1199 &mut y,
1200 y_prec,
1201 prec,
1202 if sign { rm } else { -rm },
1203 );
1204 let exp = exp_diff.checked_add(i32::exact_from(exp_offset)).unwrap();
1205 if exp > Self::MAX_EXPONENT {
1206 return match (sign, rm) {
1207 (_, Exact) => panic!("Inexact Float division"),
1208 (true, Ceiling | Up | Nearest) => (float_infinity!(), Greater),
1209 (true, _) => (Self::max_finite_value_with_prec(prec), Less),
1210 (false, Floor | Up | Nearest) => (float_negative_infinity!(), Less),
1211 (false, _) => (-Self::max_finite_value_with_prec(prec), Greater),
1212 };
1213 } else if exp < Self::MIN_EXPONENT {
1214 return if rm == Nearest
1215 && exp == Self::MIN_EXPONENT - 1
1216 && (o == Less || !quotient.is_power_of_2())
1217 {
1218 if sign {
1219 (Self::min_positive_value_prec(prec), Greater)
1220 } else {
1221 (-Self::min_positive_value_prec(prec), Less)
1222 }
1223 } else {
1224 match (sign, rm) {
1225 (_, Exact) => panic!("Inexact Float division"),
1226 (true, Ceiling | Up) => (Self::min_positive_value_prec(prec), Greater),
1227 (true, _) => (float_zero!(), Less),
1228 (false, Floor | Up) => (-Self::min_positive_value_prec(prec), Less),
1229 (false, _) => (float_negative_zero!(), Greater),
1230 }
1231 };
1232 }
1233 (
1234 Self(Finite {
1235 sign,
1236 exponent: exp,
1237 precision: prec,
1238 significand: quotient,
1239 }),
1240 if sign { o } else { o.reverse() },
1241 )
1242 }
1243 }
1244 }
1245
1246 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
1247 /// specified rounding mode. Both [`Float`]s are taken by reference. An [`Ordering`] is also
1248 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
1249 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
1250 /// function returns a `NaN` it also returns `Equal`.
1251 ///
1252 /// See [`RoundingMode`] for a description of the possible rounding modes.
1253 ///
1254 /// $$
1255 /// f(x,y,p,m) = x/y+\varepsilon.
1256 /// $$
1257 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1258 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1259 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
1260 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
1261 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1262 ///
1263 /// If the output has a precision, it is `prec`.
1264 ///
1265 /// Special cases:
1266 /// - $f(\text{NaN},x,p,m)=f(x,\text{NaN},p,m)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1267 /// \text{NaN}$
1268 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
1269 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
1270 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
1271 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
1272 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
1273 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
1274 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
1275 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
1276 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
1277 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1278 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1279 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1280 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1281 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $x<0.0$
1282 /// - $f(x,-\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1283 /// - $f(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1284 ///
1285 /// Overflow and underflow:
1286 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1287 /// returned instead.
1288 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
1289 /// is returned instead, where `p` is the precision of the input.
1290 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
1291 /// returned instead.
1292 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
1293 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
1294 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1295 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1296 /// instead.
1297 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1298 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1299 /// instead.
1300 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
1301 /// instead.
1302 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
1303 /// instead.
1304 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1305 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
1306 /// returned instead.
1307 ///
1308 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_ref_ref`] instead.
1309 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1310 /// consider using [`Float::div_round_ref_ref`] instead. If both of these things are true,
1311 /// consider using `/` instead.
1312 ///
1313 /// # Worst-case complexity
1314 /// $T(n) = O(n \log n \log\log n)$
1315 ///
1316 /// $M(n) = O(n \log n)$
1317 ///
1318 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1319 /// other.significant_bits(), `prec`)`.
1320 ///
1321 /// # Panics
1322 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
1323 ///
1324 /// # Examples
1325 /// ```
1326 /// use core::f64::consts::{E, PI};
1327 /// use malachite_base::rounding_modes::RoundingMode::*;
1328 /// use malachite_float::Float;
1329 /// use std::cmp::Ordering::*;
1330 ///
1331 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 5, Floor);
1332 /// assert_eq!(quotient.to_string(), "1.12");
1333 /// assert_eq!(o, Less);
1334 ///
1335 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 5, Ceiling);
1336 /// assert_eq!(quotient.to_string(), "1.19");
1337 /// assert_eq!(o, Greater);
1338 ///
1339 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 5, Nearest);
1340 /// assert_eq!(quotient.to_string(), "1.12");
1341 /// assert_eq!(o, Less);
1342 ///
1343 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 20, Floor);
1344 /// assert_eq!(quotient.to_string(), "1.155725");
1345 /// assert_eq!(o, Less);
1346 ///
1347 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 20, Ceiling);
1348 /// assert_eq!(quotient.to_string(), "1.155727");
1349 /// assert_eq!(o, Greater);
1350 ///
1351 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 20, Nearest);
1352 /// assert_eq!(quotient.to_string(), "1.155727");
1353 /// assert_eq!(o, Greater);
1354 /// ```
1355 #[inline]
1356 pub fn div_prec_round_ref_ref(
1357 &self,
1358 other: &Self,
1359 prec: u64,
1360 rm: RoundingMode,
1361 ) -> (Self, Ordering) {
1362 assert_ne!(prec, 0);
1363 match (self, other) {
1364 (float_nan!(), _)
1365 | (_, float_nan!())
1366 | (float_either_infinity!(), float_either_infinity!())
1367 | (float_either_zero!(), float_either_zero!()) => (float_nan!(), Equal),
1368 (
1369 Self(Infinity { sign: x_sign }),
1370 Self(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
1371 )
1372 | (Self(Finite { sign: x_sign, .. }), Self(Zero { sign: y_sign })) => (
1373 Self(Infinity {
1374 sign: x_sign == y_sign,
1375 }),
1376 Equal,
1377 ),
1378 (
1379 Self(Zero { sign: x_sign }),
1380 Self(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
1381 )
1382 | (Self(Finite { sign: x_sign, .. }), Self(Infinity { sign: y_sign })) => (
1383 Self(Zero {
1384 sign: x_sign == y_sign,
1385 }),
1386 Equal,
1387 ),
1388 (
1389 Self(Finite {
1390 sign: x_sign,
1391 exponent: x_exp,
1392 precision: x_prec,
1393 significand: x,
1394 }),
1395 Self(Finite {
1396 sign: y_sign,
1397 exponent: y_exp,
1398 precision: y_prec,
1399 significand: y,
1400 }),
1401 ) => {
1402 if y.is_power_of_2() {
1403 let (mut quotient, mut o) =
1404 self.shr_prec_round_ref(y_exp - 1, prec, if *y_sign { rm } else { -rm });
1405 if !*y_sign {
1406 quotient.neg_assign();
1407 o = o.reverse();
1408 }
1409 return (quotient, o);
1410 }
1411 let sign = x_sign == y_sign;
1412 let exp_diff = *x_exp - y_exp;
1413 if exp_diff > Self::MAX_EXPONENT {
1414 return match (sign, rm) {
1415 (_, Exact) => panic!("Inexact Float division"),
1416 (true, Ceiling | Up | Nearest) => (float_infinity!(), Greater),
1417 (true, _) => (Self::max_finite_value_with_prec(prec), Less),
1418 (false, Floor | Up | Nearest) => (float_negative_infinity!(), Less),
1419 (false, _) => (-Self::max_finite_value_with_prec(prec), Greater),
1420 };
1421 } else if exp_diff + 2 < Self::MIN_EXPONENT {
1422 return match (sign, rm) {
1423 (_, Exact) => panic!("Inexact Float division"),
1424 (true, Ceiling | Up) => (Self::min_positive_value_prec(prec), Greater),
1425 (true, _) => (float_zero!(), Less),
1426 (false, Floor | Up) => (-Self::min_positive_value_prec(prec), Less),
1427 (false, _) => (float_negative_zero!(), Greater),
1428 };
1429 }
1430 let (quotient, exp_offset, o) = div_float_significands_ref_ref(
1431 x,
1432 *x_prec,
1433 y,
1434 *y_prec,
1435 prec,
1436 if sign { rm } else { -rm },
1437 );
1438 let exp = exp_diff.checked_add(i32::exact_from(exp_offset)).unwrap();
1439 if exp > Self::MAX_EXPONENT {
1440 return match (sign, rm) {
1441 (_, Exact) => panic!("Inexact Float division"),
1442 (true, Ceiling | Up | Nearest) => (float_infinity!(), Greater),
1443 (true, _) => (Self::max_finite_value_with_prec(prec), Less),
1444 (false, Floor | Up | Nearest) => (float_negative_infinity!(), Less),
1445 (false, _) => (-Self::max_finite_value_with_prec(prec), Greater),
1446 };
1447 } else if exp < Self::MIN_EXPONENT {
1448 return if rm == Nearest
1449 && exp == Self::MIN_EXPONENT - 1
1450 && (o == Less || !quotient.is_power_of_2())
1451 {
1452 if sign {
1453 (Self::min_positive_value_prec(prec), Greater)
1454 } else {
1455 (-Self::min_positive_value_prec(prec), Less)
1456 }
1457 } else {
1458 match (sign, rm) {
1459 (_, Exact) => panic!("Inexact Float division"),
1460 (true, Ceiling | Up) => (Self::min_positive_value_prec(prec), Greater),
1461 (true, _) => (float_zero!(), Less),
1462 (false, Floor | Up) => (-Self::min_positive_value_prec(prec), Less),
1463 (false, _) => (float_negative_zero!(), Greater),
1464 }
1465 };
1466 }
1467 (
1468 Self(Finite {
1469 sign,
1470 exponent: exp,
1471 precision: prec,
1472 significand: quotient,
1473 }),
1474 if sign { o } else { o.reverse() },
1475 )
1476 }
1477 }
1478 }
1479
1480 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1481 /// Both [`Float`]s are taken by value. An [`Ordering`] is also returned, indicating whether the
1482 /// rounded quotient is less than, equal to, or greater than the exact quotient. Although `NaN`s
1483 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
1484 /// `Equal`.
1485 ///
1486 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1487 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1488 /// description of the `Nearest` rounding mode.
1489 ///
1490 /// $$
1491 /// f(x,y,p) = x/y+\varepsilon.
1492 /// $$
1493 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1494 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1495 ///
1496 /// If the output has a precision, it is `prec`.
1497 ///
1498 /// Special cases:
1499 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1500 /// \text{NaN}$
1501 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
1502 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
1503 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
1504 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
1505 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
1506 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
1507 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
1508 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
1509 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
1510 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
1511 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1512 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1513 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
1514 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
1515 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1516 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1517 ///
1518 /// Overflow and underflow:
1519 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
1520 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
1521 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1522 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1523 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1524 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1525 ///
1526 /// If you want to use a rounding mode other than `Nearest`, consider using
1527 /// [`Float::div_prec_round`] instead. If you know that your target precision is the maximum of
1528 /// the precisions of the two inputs, consider using `/` instead.
1529 ///
1530 /// # Worst-case complexity
1531 /// $T(n) = O(n \log n \log\log n)$
1532 ///
1533 /// $M(n) = O(n \log n)$
1534 ///
1535 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1536 /// other.significant_bits(), `prec`)`.
1537 ///
1538 /// # Examples
1539 /// ```
1540 /// use core::f64::consts::{E, PI};
1541 /// use malachite_float::Float;
1542 /// use std::cmp::Ordering::*;
1543 ///
1544 /// let (quotient, o) = Float::from(PI).div_prec(Float::from(E), 5);
1545 /// assert_eq!(quotient.to_string(), "1.12");
1546 /// assert_eq!(o, Less);
1547 ///
1548 /// let (quotient, o) = Float::from(PI).div_prec(Float::from(E), 20);
1549 /// assert_eq!(quotient.to_string(), "1.155727");
1550 /// assert_eq!(o, Greater);
1551 /// ```
1552 #[inline]
1553 pub fn div_prec(self, other: Self, prec: u64) -> (Self, Ordering) {
1554 self.div_prec_round(other, prec, Nearest)
1555 }
1556
1557 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1558 /// The first [`Float`] is taken by value and the second by reference. An [`Ordering`] is also
1559 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
1560 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
1561 /// function returns a `NaN` it also returns `Equal`.
1562 ///
1563 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1564 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1565 /// description of the `Nearest` rounding mode.
1566 ///
1567 /// $$
1568 /// f(x,y,p) = x/y+\varepsilon.
1569 /// $$
1570 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1571 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1572 ///
1573 /// If the output has a precision, it is `prec`.
1574 ///
1575 /// Special cases:
1576 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1577 /// \text{NaN}$
1578 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
1579 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
1580 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
1581 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
1582 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
1583 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
1584 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
1585 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
1586 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
1587 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
1588 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1589 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1590 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
1591 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
1592 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1593 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1594 ///
1595 /// Overflow and underflow:
1596 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
1597 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
1598 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1599 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1600 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1601 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1602 ///
1603 /// If you want to use a rounding mode other than `Nearest`, consider using
1604 /// [`Float::div_prec_round_val_ref`] instead. If you know that your target precision is the
1605 /// maximum of the precisions of the two inputs, consider using `/` instead.
1606 ///
1607 /// # Worst-case complexity
1608 /// $T(n) = O(n \log n \log\log n)$
1609 ///
1610 /// $M(n) = O(n \log n)$
1611 ///
1612 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1613 /// other.significant_bits(), `prec`)`.
1614 ///
1615 /// # Examples
1616 /// ```
1617 /// use core::f64::consts::{E, PI};
1618 /// use malachite_float::Float;
1619 /// use std::cmp::Ordering::*;
1620 ///
1621 /// let (quotient, o) = Float::from(PI).div_prec_val_ref(&Float::from(E), 5);
1622 /// assert_eq!(quotient.to_string(), "1.12");
1623 /// assert_eq!(o, Less);
1624 ///
1625 /// let (quotient, o) = Float::from(PI).div_prec_val_ref(&Float::from(E), 20);
1626 /// assert_eq!(quotient.to_string(), "1.155727");
1627 /// assert_eq!(o, Greater);
1628 /// ```
1629 #[inline]
1630 pub fn div_prec_val_ref(self, other: &Self, prec: u64) -> (Self, Ordering) {
1631 self.div_prec_round_val_ref(other, prec, Nearest)
1632 }
1633
1634 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1635 /// The first [`Float`] is taken by reference and the second by value. An [`Ordering`] is also
1636 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
1637 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
1638 /// function returns a `NaN` it also returns `Equal`.
1639 ///
1640 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1641 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1642 /// description of the `Nearest` rounding mode.
1643 ///
1644 /// $$
1645 /// f(x,y,p) = x/y+\varepsilon.
1646 /// $$
1647 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1648 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1649 ///
1650 /// If the output has a precision, it is `prec`.
1651 ///
1652 /// Special cases:
1653 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1654 /// \text{NaN}$
1655 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
1656 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
1657 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
1658 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
1659 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
1660 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
1661 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
1662 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
1663 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
1664 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
1665 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1666 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1667 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
1668 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
1669 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1670 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1671 ///
1672 /// Overflow and underflow:
1673 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
1674 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
1675 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1676 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1677 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1678 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1679 ///
1680 /// If you want to use a rounding mode other than `Nearest`, consider using
1681 /// [`Float::div_prec_round_ref_val`] instead. If you know that your target precision is the
1682 /// maximum of the precisions of the two inputs, consider using `/` instead.
1683 ///
1684 /// # Worst-case complexity
1685 /// $T(n) = O(n \log n \log\log n)$
1686 ///
1687 /// $M(n) = O(n \log n)$
1688 ///
1689 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1690 /// other.significant_bits(), `prec`)`.
1691 ///
1692 /// # Examples
1693 /// ```
1694 /// use core::f64::consts::{E, PI};
1695 /// use malachite_float::Float;
1696 /// use std::cmp::Ordering::*;
1697 ///
1698 /// let (quotient, o) = Float::from(PI).div_prec_ref_val(Float::from(E), 5);
1699 /// assert_eq!(quotient.to_string(), "1.12");
1700 /// assert_eq!(o, Less);
1701 ///
1702 /// let (quotient, o) = Float::from(PI).div_prec_ref_val(Float::from(E), 20);
1703 /// assert_eq!(quotient.to_string(), "1.155727");
1704 /// assert_eq!(o, Greater);
1705 /// ```
1706 #[inline]
1707 pub fn div_prec_ref_val(&self, other: Self, prec: u64) -> (Self, Ordering) {
1708 self.div_prec_round_ref_val(other, prec, Nearest)
1709 }
1710
1711 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1712 /// Both [`Float`]s are taken by reference. An [`Ordering`] is also returned, indicating whether
1713 /// the rounded quotient is less than, equal to, or greater than the exact quotient. Although
1714 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
1715 /// returns `Equal`.
1716 ///
1717 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1718 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1719 /// description of the `Nearest` rounding mode.
1720 ///
1721 /// $$
1722 /// f(x,y,p) = x/y+\varepsilon.
1723 /// $$
1724 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1725 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1726 ///
1727 /// If the output has a precision, it is `prec`.
1728 ///
1729 /// Special cases:
1730 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1731 /// \text{NaN}$
1732 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
1733 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
1734 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
1735 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
1736 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
1737 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
1738 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
1739 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
1740 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
1741 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
1742 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1743 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1744 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
1745 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
1746 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1747 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1748 ///
1749 /// Overflow and underflow:
1750 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
1751 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
1752 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1753 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1754 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1755 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1756 ///
1757 /// If you want to use a rounding mode other than `Nearest`, consider using
1758 /// [`Float::div_prec_round_ref_ref`] instead. If you know that your target precision is the
1759 /// maximum of the precisions of the two inputs, consider using `/` instead.
1760 ///
1761 /// # Worst-case complexity
1762 /// $T(n) = O(n \log n \log\log n)$
1763 ///
1764 /// $M(n) = O(n \log n)$
1765 ///
1766 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1767 /// other.significant_bits(), `prec`)`.
1768 ///
1769 /// # Examples
1770 /// ```
1771 /// use core::f64::consts::{E, PI};
1772 /// use malachite_float::Float;
1773 /// use std::cmp::Ordering::*;
1774 ///
1775 /// let (quotient, o) = Float::from(PI).div_prec_ref_ref(&Float::from(E), 5);
1776 /// assert_eq!(quotient.to_string(), "1.12");
1777 /// assert_eq!(o, Less);
1778 ///
1779 /// let (quotient, o) = Float::from(PI).div_prec_ref_ref(&Float::from(E), 20);
1780 /// assert_eq!(quotient.to_string(), "1.155727");
1781 /// assert_eq!(o, Greater);
1782 /// ```
1783 #[inline]
1784 pub fn div_prec_ref_ref(&self, other: &Self, prec: u64) -> (Self, Ordering) {
1785 self.div_prec_round_ref_ref(other, prec, Nearest)
1786 }
1787
1788 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. Both
1789 /// [`Float`]s are taken by value. An [`Ordering`] is also returned, indicating whether the
1790 /// rounded quotient is less than, equal to, or greater than the exact quotient. Although `NaN`s
1791 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
1792 /// `Equal`.
1793 ///
1794 /// The precision of the output is the maximum of the precision of the inputs. See
1795 /// [`RoundingMode`] for a description of the possible rounding modes.
1796 ///
1797 /// $$
1798 /// f(x,y,m) = x/y+\varepsilon.
1799 /// $$
1800 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1801 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1802 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
1803 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
1804 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
1805 ///
1806 /// If the output has a precision, it is the maximum of the precisions of the inputs.
1807 ///
1808 /// Special cases:
1809 /// - $f(\text{NaN},x,m)=f(x,\text{NaN},p,m)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1810 /// \text{NaN}$
1811 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
1812 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
1813 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
1814 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
1815 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
1816 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
1817 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
1818 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
1819 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
1820 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1821 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1822 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1823 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1824 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
1825 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1826 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1827 ///
1828 /// Overflow and underflow:
1829 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1830 /// returned instead.
1831 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
1832 /// returned instead, where `p` is the precision of the input.
1833 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
1834 /// returned instead.
1835 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
1836 /// is returned instead, where `p` is the precision of the input.
1837 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1838 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1839 /// instead.
1840 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1841 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1842 /// instead.
1843 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
1844 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
1845 /// instead.
1846 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1847 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
1848 /// returned instead.
1849 ///
1850 /// If you want to specify an output precision, consider using [`Float::div_prec_round`]
1851 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
1852 /// instead.
1853 ///
1854 /// # Worst-case complexity
1855 /// $T(n) = O(n \log n \log\log n)$
1856 ///
1857 /// $M(n) = O(n \log n)$
1858 ///
1859 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1860 /// other.significant_bits())`.
1861 ///
1862 /// # Panics
1863 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
1864 /// represent the output.
1865 ///
1866 /// # Examples
1867 /// ```
1868 /// use core::f64::consts::{E, PI};
1869 /// use malachite_base::rounding_modes::RoundingMode::*;
1870 /// use malachite_float::Float;
1871 /// use std::cmp::Ordering::*;
1872 ///
1873 /// let (quotient, o) = Float::from(PI).div_round(Float::from(E), Floor);
1874 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
1875 /// assert_eq!(o, Less);
1876 ///
1877 /// let (quotient, o) = Float::from(PI).div_round(Float::from(E), Ceiling);
1878 /// assert_eq!(quotient.to_string(), "1.155727349790922");
1879 /// assert_eq!(o, Greater);
1880 ///
1881 /// let (quotient, o) = Float::from(PI).div_round(Float::from(E), Nearest);
1882 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
1883 /// assert_eq!(o, Less);
1884 /// ```
1885 #[inline]
1886 pub fn div_round(self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
1887 let prec = max(self.significant_bits(), other.significant_bits());
1888 self.div_prec_round(other, prec, rm)
1889 }
1890
1891 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. The first
1892 /// [`Float`] is taken by value and the second by reference. An [`Ordering`] is also returned,
1893 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
1894 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function
1895 /// returns a `NaN` it also returns `Equal`.
1896 ///
1897 /// The precision of the output is the maximum of the precision of the inputs. See
1898 /// [`RoundingMode`] for a description of the possible rounding modes.
1899 ///
1900 /// $$
1901 /// f(x,y,m) = x/y+\varepsilon.
1902 /// $$
1903 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1904 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1905 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
1906 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
1907 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
1908 ///
1909 /// If the output has a precision, it is the maximum of the precisions of the inputs.
1910 ///
1911 /// Special cases:
1912 /// - $f(\text{NaN},x,m)=f(x,\text{NaN},p,m)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1913 /// \text{NaN}$
1914 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
1915 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
1916 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
1917 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
1918 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
1919 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
1920 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
1921 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
1922 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
1923 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1924 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1925 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1926 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1927 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
1928 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1929 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1930 ///
1931 /// Overflow and underflow:
1932 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1933 /// returned instead.
1934 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
1935 /// returned instead, where `p` is the precision of the input.
1936 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
1937 /// returned instead.
1938 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
1939 /// is returned instead, where `p` is the precision of the input.
1940 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1941 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1942 /// instead.
1943 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1944 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1945 /// instead.
1946 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
1947 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
1948 /// instead.
1949 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1950 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
1951 /// returned instead.
1952 ///
1953 /// If you want to specify an output precision, consider using [`Float::div_prec_round_val_ref`]
1954 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
1955 /// instead.
1956 ///
1957 /// # Worst-case complexity
1958 /// $T(n) = O(n \log n \log\log n)$
1959 ///
1960 /// $M(n) = O(n \log n)$
1961 ///
1962 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1963 /// other.significant_bits())`.
1964 ///
1965 /// # Panics
1966 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
1967 /// represent the output.
1968 ///
1969 /// # Examples
1970 /// ```
1971 /// use core::f64::consts::{E, PI};
1972 /// use malachite_base::rounding_modes::RoundingMode::*;
1973 /// use malachite_float::Float;
1974 /// use std::cmp::Ordering::*;
1975 ///
1976 /// let (quotient, o) = Float::from(PI).div_round_val_ref(&Float::from(E), Floor);
1977 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
1978 /// assert_eq!(o, Less);
1979 ///
1980 /// let (quotient, o) = Float::from(PI).div_round_val_ref(&Float::from(E), Ceiling);
1981 /// assert_eq!(quotient.to_string(), "1.155727349790922");
1982 /// assert_eq!(o, Greater);
1983 ///
1984 /// let (quotient, o) = Float::from(PI).div_round_val_ref(&Float::from(E), Nearest);
1985 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
1986 /// assert_eq!(o, Less);
1987 /// ```
1988 #[inline]
1989 pub fn div_round_val_ref(self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
1990 let prec = max(self.significant_bits(), other.significant_bits());
1991 self.div_prec_round_val_ref(other, prec, rm)
1992 }
1993
1994 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. The first
1995 /// [`Float`] is taken by reference and the second by value. An [`Ordering`] is also returned,
1996 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
1997 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function
1998 /// returns a `NaN` it also returns `Equal`.
1999 ///
2000 /// The precision of the output is the maximum of the precision of the inputs. See
2001 /// [`RoundingMode`] for a description of the possible rounding modes.
2002 ///
2003 /// $$
2004 /// f(x,y,m) = x/y+\varepsilon.
2005 /// $$
2006 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2007 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2008 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2009 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2010 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2011 ///
2012 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2013 ///
2014 /// Special cases:
2015 /// - $f(\text{NaN},x,m)=f(x,\text{NaN},p,m)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
2016 /// \text{NaN}$
2017 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
2018 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
2019 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
2020 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
2021 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
2022 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
2023 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
2024 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
2025 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
2026 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
2027 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2028 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2029 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
2030 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
2031 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2032 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2033 ///
2034 /// Overflow and underflow:
2035 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
2036 /// returned instead.
2037 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
2038 /// returned instead, where `p` is the precision of the input.
2039 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
2040 /// returned instead.
2041 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
2042 /// is returned instead, where `p` is the precision of the input.
2043 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
2044 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
2045 /// instead.
2046 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
2047 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
2048 /// instead.
2049 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
2050 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
2051 /// instead.
2052 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
2053 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
2054 /// returned instead.
2055 ///
2056 /// If you want to specify an output precision, consider using [`Float::div_prec_round_ref_val`]
2057 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
2058 /// instead.
2059 ///
2060 /// # Worst-case complexity
2061 /// $T(n) = O(n \log n \log\log n)$
2062 ///
2063 /// $M(n) = O(n \log n)$
2064 ///
2065 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2066 /// other.significant_bits())`.
2067 ///
2068 /// # Panics
2069 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
2070 /// represent the output.
2071 ///
2072 /// # Examples
2073 /// ```
2074 /// use core::f64::consts::{E, PI};
2075 /// use malachite_base::rounding_modes::RoundingMode::*;
2076 /// use malachite_float::Float;
2077 /// use std::cmp::Ordering::*;
2078 ///
2079 /// let (quotient, o) = Float::from(PI).div_round_ref_val(Float::from(E), Floor);
2080 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2081 /// assert_eq!(o, Less);
2082 ///
2083 /// let (quotient, o) = Float::from(PI).div_round_ref_val(Float::from(E), Ceiling);
2084 /// assert_eq!(quotient.to_string(), "1.155727349790922");
2085 /// assert_eq!(o, Greater);
2086 ///
2087 /// let (quotient, o) = Float::from(PI).div_round_ref_val(Float::from(E), Nearest);
2088 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2089 /// assert_eq!(o, Less);
2090 /// ```
2091 #[inline]
2092 pub fn div_round_ref_val(&self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
2093 let prec = max(self.significant_bits(), other.significant_bits());
2094 self.div_prec_round_ref_val(other, prec, rm)
2095 }
2096
2097 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. Both
2098 /// [`Float`]s are taken by reference. An [`Ordering`] is also returned, indicating whether the
2099 /// rounded quotient is less than, equal to, or greater than the exact quotient. Although `NaN`s
2100 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
2101 /// `Equal`.
2102 ///
2103 /// The precision of the output is the maximum of the precision of the inputs. See
2104 /// [`RoundingMode`] for a description of the possible rounding modes.
2105 ///
2106 /// $$
2107 /// f(x,y,m) = x/y+\varepsilon.
2108 /// $$
2109 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2110 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2111 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2112 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2113 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2114 ///
2115 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2116 ///
2117 /// Special cases:
2118 /// - $f(\text{NaN},x,m)=f(x,\text{NaN},p,m)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
2119 /// \text{NaN}$
2120 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
2121 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
2122 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
2123 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
2124 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
2125 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
2126 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
2127 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
2128 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
2129 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
2130 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2131 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2132 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
2133 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
2134 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2135 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2136 ///
2137 /// Overflow and underflow:
2138 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
2139 /// returned instead.
2140 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
2141 /// returned instead, where `p` is the precision of the input.
2142 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
2143 /// returned instead.
2144 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
2145 /// is returned instead, where `p` is the precision of the input.
2146 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
2147 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
2148 /// instead.
2149 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
2150 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
2151 /// instead.
2152 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
2153 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
2154 /// instead.
2155 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
2156 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
2157 /// returned instead.
2158 ///
2159 /// If you want to specify an output precision, consider using [`Float::div_prec_round_ref_ref`]
2160 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
2161 /// instead.
2162 ///
2163 /// # Worst-case complexity
2164 /// $T(n) = O(n \log n \log\log n)$
2165 ///
2166 /// $M(n) = O(n \log n)$
2167 ///
2168 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2169 /// other.significant_bits())`.
2170 ///
2171 /// # Panics
2172 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
2173 /// represent the output.
2174 ///
2175 /// # Examples
2176 /// ```
2177 /// use core::f64::consts::{E, PI};
2178 /// use malachite_base::rounding_modes::RoundingMode::*;
2179 /// use malachite_float::Float;
2180 /// use std::cmp::Ordering::*;
2181 ///
2182 /// let (quotient, o) = Float::from(PI).div_round_ref_ref(&Float::from(E), Floor);
2183 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2184 /// assert_eq!(o, Less);
2185 ///
2186 /// let (quotient, o) = Float::from(PI).div_round_ref_ref(&Float::from(E), Ceiling);
2187 /// assert_eq!(quotient.to_string(), "1.155727349790922");
2188 /// assert_eq!(o, Greater);
2189 ///
2190 /// let (quotient, o) = Float::from(PI).div_round_ref_ref(&Float::from(E), Nearest);
2191 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2192 /// assert_eq!(o, Less);
2193 /// ```
2194 #[inline]
2195 pub fn div_round_ref_ref(&self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
2196 let prec = max(self.significant_bits(), other.significant_bits());
2197 self.div_prec_round_ref_ref(other, prec, rm)
2198 }
2199
2200 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the specified precision
2201 /// and with the specified rounding mode. The [`Float`] on the right-hand side is taken by
2202 /// value. An [`Ordering`] is returned, indicating whether the rounded quotient is less than,
2203 /// equal to, or greater than the exact quotient. Although `NaN`s are not comparable to any
2204 /// [`Float`], whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
2205 ///
2206 /// See [`RoundingMode`] for a description of the possible rounding modes.
2207 ///
2208 /// $$
2209 /// x \gets x/y+\varepsilon.
2210 /// $$
2211 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2212 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2213 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2214 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2215 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2216 ///
2217 /// If the output has a precision, it is `prec`.
2218 ///
2219 /// See the [`Float::div_prec_round`] documentation for information on special cases, overflow,
2220 /// and underflow.
2221 ///
2222 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_assign`] instead. If
2223 /// you know that your target precision is the maximum of the precisions of the two inputs,
2224 /// consider using [`Float::div_round_assign`] instead. If both of these things are true,
2225 /// consider using `/=` instead.
2226 ///
2227 /// # Worst-case complexity
2228 /// $T(n) = O(n \log n \log\log n)$
2229 ///
2230 /// $M(n) = O(n \log n)$
2231 ///
2232 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2233 /// other.significant_bits(), `prec`)`.
2234 ///
2235 /// # Panics
2236 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2237 ///
2238 /// # Examples
2239 /// ```
2240 /// use core::f64::consts::{E, PI};
2241 /// use malachite_base::rounding_modes::RoundingMode::*;
2242 /// use malachite_float::Float;
2243 /// use std::cmp::Ordering::*;
2244 ///
2245 /// let mut quotient = Float::from(PI);
2246 /// assert_eq!(
2247 /// quotient.div_prec_round_assign(Float::from(E), 5, Floor),
2248 /// Less
2249 /// );
2250 /// assert_eq!(quotient.to_string(), "1.12");
2251 ///
2252 /// let mut quotient = Float::from(PI);
2253 /// assert_eq!(
2254 /// quotient.div_prec_round_assign(Float::from(E), 5, Ceiling),
2255 /// Greater
2256 /// );
2257 /// assert_eq!(quotient.to_string(), "1.19");
2258 ///
2259 /// let mut quotient = Float::from(PI);
2260 /// assert_eq!(
2261 /// quotient.div_prec_round_assign(Float::from(E), 5, Nearest),
2262 /// Less
2263 /// );
2264 /// assert_eq!(quotient.to_string(), "1.12");
2265 ///
2266 /// let mut quotient = Float::from(PI);
2267 /// assert_eq!(
2268 /// quotient.div_prec_round_assign(Float::from(E), 20, Floor),
2269 /// Less
2270 /// );
2271 /// assert_eq!(quotient.to_string(), "1.155725");
2272 ///
2273 /// let mut quotient = Float::from(PI);
2274 /// assert_eq!(
2275 /// quotient.div_prec_round_assign(Float::from(E), 20, Ceiling),
2276 /// Greater
2277 /// );
2278 /// assert_eq!(quotient.to_string(), "1.155727");
2279 ///
2280 /// let mut quotient = Float::from(PI);
2281 /// assert_eq!(
2282 /// quotient.div_prec_round_assign(Float::from(E), 20, Nearest),
2283 /// Greater
2284 /// );
2285 /// assert_eq!(quotient.to_string(), "1.155727");
2286 /// ```
2287 #[inline]
2288 pub fn div_prec_round_assign(&mut self, other: Self, prec: u64, rm: RoundingMode) -> Ordering {
2289 assert_ne!(prec, 0);
2290 match (&mut *self, other) {
2291 (float_nan!(), _)
2292 | (_, float_nan!())
2293 | (float_either_infinity!(), float_either_infinity!())
2294 | (float_either_zero!(), float_either_zero!()) => {
2295 *self = float_nan!();
2296 Equal
2297 }
2298 (
2299 Self(Infinity { sign: x_sign }),
2300 Self(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
2301 )
2302 | (Self(Finite { sign: x_sign, .. }), Self(Zero { sign: y_sign })) => {
2303 *self = Self(Infinity {
2304 sign: *x_sign == y_sign,
2305 });
2306 Equal
2307 }
2308 (
2309 Self(Zero { sign: x_sign }),
2310 Self(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
2311 )
2312 | (Self(Finite { sign: x_sign, .. }), Self(Infinity { sign: y_sign })) => {
2313 *self = Self(Zero {
2314 sign: *x_sign == y_sign,
2315 });
2316 Equal
2317 }
2318 (_, y) if abs_is_power_of_2(&y) => {
2319 let sign = y >= 0;
2320 let mut o = self.shr_prec_round_assign(
2321 y.get_exponent().unwrap() - 1,
2322 prec,
2323 if sign { rm } else { -rm },
2324 );
2325 if !sign {
2326 self.neg_assign();
2327 o = o.reverse();
2328 }
2329 o
2330 }
2331 (
2332 Self(Finite {
2333 sign: x_sign,
2334 exponent: x_exp,
2335 precision: x_prec,
2336 significand: x,
2337 }),
2338 Self(Finite {
2339 sign: y_sign,
2340 exponent: y_exp,
2341 precision: y_prec,
2342 significand: mut y,
2343 }),
2344 ) => {
2345 let sign = *x_sign == y_sign;
2346 let exp_diff = *x_exp - y_exp;
2347 if exp_diff > Self::MAX_EXPONENT {
2348 return match (sign, rm) {
2349 (_, Exact) => panic!("Inexact Float division"),
2350 (true, Ceiling | Up | Nearest) => {
2351 *self = float_infinity!();
2352 Greater
2353 }
2354 (true, _) => {
2355 *self = Self::max_finite_value_with_prec(prec);
2356 Less
2357 }
2358 (false, Floor | Up | Nearest) => {
2359 *self = float_negative_infinity!();
2360 Less
2361 }
2362 (false, _) => {
2363 *self = -Self::max_finite_value_with_prec(prec);
2364 Greater
2365 }
2366 };
2367 } else if exp_diff + 2 < Self::MIN_EXPONENT {
2368 return match (sign, rm) {
2369 (_, Exact) => panic!("Inexact Float division"),
2370 (true, Ceiling | Up) => {
2371 *self = Self::min_positive_value_prec(prec);
2372 Greater
2373 }
2374 (true, _) => {
2375 *self = float_zero!();
2376 Less
2377 }
2378 (false, Floor | Up) => {
2379 *self = -Self::min_positive_value_prec(prec);
2380 Less
2381 }
2382 (false, _) => {
2383 *self = float_negative_zero!();
2384 Greater
2385 }
2386 };
2387 }
2388 let (exp_offset, o) = div_float_significands_in_place(
2389 x,
2390 *x_prec,
2391 &mut y,
2392 y_prec,
2393 prec,
2394 if sign { rm } else { -rm },
2395 );
2396 *x_exp = exp_diff.checked_add(i32::exact_from(exp_offset)).unwrap();
2397 if *x_exp > Self::MAX_EXPONENT {
2398 return match (sign, rm) {
2399 (_, Exact) => panic!("Inexact Float division"),
2400 (true, Ceiling | Up | Nearest) => {
2401 *self = float_infinity!();
2402 Greater
2403 }
2404 (true, _) => {
2405 *self = Self::max_finite_value_with_prec(prec);
2406 Less
2407 }
2408 (false, Floor | Up | Nearest) => {
2409 *self = float_negative_infinity!();
2410 Less
2411 }
2412 (false, _) => {
2413 *self = -Self::max_finite_value_with_prec(prec);
2414 Greater
2415 }
2416 };
2417 } else if *x_exp < Self::MIN_EXPONENT {
2418 return if rm == Nearest
2419 && *x_exp == Self::MIN_EXPONENT - 1
2420 && (o == Less || !x.is_power_of_2())
2421 {
2422 if sign {
2423 *self = Self::min_positive_value_prec(prec);
2424 Greater
2425 } else {
2426 *self = -Self::min_positive_value_prec(prec);
2427 Less
2428 }
2429 } else {
2430 match (sign, rm) {
2431 (_, Exact) => panic!("Inexact Float division"),
2432 (true, Ceiling | Up) => {
2433 *self = Self::min_positive_value_prec(prec);
2434 Greater
2435 }
2436 (true, _) => {
2437 *self = float_zero!();
2438 Less
2439 }
2440 (false, Floor | Up) => {
2441 *self = -Self::min_positive_value_prec(prec);
2442 Less
2443 }
2444 (false, _) => {
2445 *self = float_negative_zero!();
2446 Greater
2447 }
2448 }
2449 };
2450 }
2451 *x_sign = sign;
2452 *x_prec = prec;
2453 if sign { o } else { o.reverse() }
2454 }
2455 }
2456 }
2457
2458 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the specified precision
2459 /// and with the specified rounding mode. The [`Float`] on the right-hand side is taken by
2460 /// reference. An [`Ordering`] is returned, indicating whether the rounded quotient is less
2461 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
2462 /// any [`Float`], whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
2463 ///
2464 /// See [`RoundingMode`] for a description of the possible rounding modes.
2465 ///
2466 /// $$
2467 /// x \gets x/y+\varepsilon.
2468 /// $$
2469 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2470 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2471 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2472 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2473 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2474 ///
2475 /// If the output has a precision, it is `prec`.
2476 ///
2477 /// See the [`Float::div_prec_round`] documentation for information on special cases, overflow,
2478 /// and underflow.
2479 ///
2480 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_assign_ref`]
2481 /// instead. If you know that your target precision is the maximum of the precisions of the two
2482 /// inputs, consider using [`Float::div_round_assign_ref`] instead. If both of these things are
2483 /// true, consider using `/=` instead.
2484 ///
2485 /// # Worst-case complexity
2486 /// $T(n) = O(n \log n \log\log n)$
2487 ///
2488 /// $M(n) = O(n \log n)$
2489 ///
2490 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2491 /// other.significant_bits(), `prec`)`.
2492 ///
2493 /// # Panics
2494 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2495 ///
2496 /// # Examples
2497 /// ```
2498 /// use core::f64::consts::{E, PI};
2499 /// use malachite_base::rounding_modes::RoundingMode::*;
2500 /// use malachite_float::Float;
2501 /// use std::cmp::Ordering::*;
2502 ///
2503 /// let mut quotient = Float::from(PI);
2504 /// assert_eq!(
2505 /// quotient.div_prec_round_assign_ref(&Float::from(E), 5, Floor),
2506 /// Less
2507 /// );
2508 /// assert_eq!(quotient.to_string(), "1.12");
2509 ///
2510 /// let mut quotient = Float::from(PI);
2511 /// assert_eq!(
2512 /// quotient.div_prec_round_assign_ref(&Float::from(E), 5, Ceiling),
2513 /// Greater
2514 /// );
2515 /// assert_eq!(quotient.to_string(), "1.19");
2516 ///
2517 /// let mut quotient = Float::from(PI);
2518 /// assert_eq!(
2519 /// quotient.div_prec_round_assign_ref(&Float::from(E), 5, Nearest),
2520 /// Less
2521 /// );
2522 /// assert_eq!(quotient.to_string(), "1.12");
2523 ///
2524 /// let mut quotient = Float::from(PI);
2525 /// assert_eq!(
2526 /// quotient.div_prec_round_assign_ref(&Float::from(E), 20, Floor),
2527 /// Less
2528 /// );
2529 /// assert_eq!(quotient.to_string(), "1.155725");
2530 ///
2531 /// let mut quotient = Float::from(PI);
2532 /// assert_eq!(
2533 /// quotient.div_prec_round_assign_ref(&Float::from(E), 20, Ceiling),
2534 /// Greater
2535 /// );
2536 /// assert_eq!(quotient.to_string(), "1.155727");
2537 ///
2538 /// let mut quotient = Float::from(PI);
2539 /// assert_eq!(
2540 /// quotient.div_prec_round_assign_ref(&Float::from(E), 20, Nearest),
2541 /// Greater
2542 /// );
2543 /// assert_eq!(quotient.to_string(), "1.155727");
2544 /// ```
2545 #[inline]
2546 pub fn div_prec_round_assign_ref(
2547 &mut self,
2548 other: &Self,
2549 prec: u64,
2550 rm: RoundingMode,
2551 ) -> Ordering {
2552 assert_ne!(prec, 0);
2553 match (&mut *self, other) {
2554 (float_nan!(), _)
2555 | (_, float_nan!())
2556 | (float_either_infinity!(), float_either_infinity!())
2557 | (float_either_zero!(), float_either_zero!()) => {
2558 *self = float_nan!();
2559 Equal
2560 }
2561 (
2562 Self(Infinity { sign: x_sign }),
2563 Self(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
2564 )
2565 | (Self(Finite { sign: x_sign, .. }), Self(Zero { sign: y_sign })) => {
2566 *self = Self(Infinity {
2567 sign: x_sign == y_sign,
2568 });
2569 Equal
2570 }
2571 (
2572 Self(Zero { sign: x_sign }),
2573 Self(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
2574 )
2575 | (Self(Finite { sign: x_sign, .. }), Self(Infinity { sign: y_sign })) => {
2576 *self = Self(Zero {
2577 sign: x_sign == y_sign,
2578 });
2579 Equal
2580 }
2581 (_, y) if abs_is_power_of_2(y) => {
2582 let sign = *y >= 0;
2583 let mut o = self.shr_prec_round_assign(
2584 y.get_exponent().unwrap() - 1,
2585 prec,
2586 if sign { rm } else { -rm },
2587 );
2588 if !sign {
2589 self.neg_assign();
2590 o = o.reverse();
2591 }
2592 o
2593 }
2594 (
2595 Self(Finite {
2596 sign: x_sign,
2597 exponent: x_exp,
2598 precision: x_prec,
2599 significand: x,
2600 }),
2601 Self(Finite {
2602 sign: y_sign,
2603 exponent: y_exp,
2604 precision: y_prec,
2605 significand: y,
2606 }),
2607 ) => {
2608 let sign = x_sign == y_sign;
2609 let exp_diff = *x_exp - y_exp;
2610 if exp_diff > Self::MAX_EXPONENT {
2611 return match (sign, rm) {
2612 (_, Exact) => panic!("Inexact Float division"),
2613 (true, Ceiling | Up | Nearest) => {
2614 *self = float_infinity!();
2615 Greater
2616 }
2617 (true, _) => {
2618 *self = Self::max_finite_value_with_prec(prec);
2619 Less
2620 }
2621 (false, Floor | Up | Nearest) => {
2622 *self = float_negative_infinity!();
2623 Less
2624 }
2625 (false, _) => {
2626 *self = -Self::max_finite_value_with_prec(prec);
2627 Greater
2628 }
2629 };
2630 } else if exp_diff + 2 < Self::MIN_EXPONENT {
2631 return match (sign, rm) {
2632 (_, Exact) => panic!("Inexact Float division"),
2633 (true, Ceiling | Up) => {
2634 *self = Self::min_positive_value_prec(prec);
2635 Greater
2636 }
2637 (true, _) => {
2638 *self = float_zero!();
2639 Less
2640 }
2641 (false, Floor | Up) => {
2642 *self = -Self::min_positive_value_prec(prec);
2643 Less
2644 }
2645 (false, _) => {
2646 *self = float_negative_zero!();
2647 Greater
2648 }
2649 };
2650 }
2651 let (exp_offset, o) = div_float_significands_in_place_ref(
2652 x,
2653 *x_prec,
2654 y,
2655 *y_prec,
2656 prec,
2657 if sign { rm } else { -rm },
2658 );
2659 *x_exp = exp_diff.checked_add(i32::exact_from(exp_offset)).unwrap();
2660 if *x_exp > Self::MAX_EXPONENT {
2661 return match (sign, rm) {
2662 (_, Exact) => panic!("Inexact Float division"),
2663 (true, Ceiling | Up | Nearest) => {
2664 *self = float_infinity!();
2665 Greater
2666 }
2667 (true, _) => {
2668 *self = Self::max_finite_value_with_prec(prec);
2669 Less
2670 }
2671 (false, Floor | Up | Nearest) => {
2672 *self = float_negative_infinity!();
2673 Less
2674 }
2675 (false, _) => {
2676 *self = -Self::max_finite_value_with_prec(prec);
2677 Greater
2678 }
2679 };
2680 } else if *x_exp < Self::MIN_EXPONENT {
2681 return if rm == Nearest
2682 && *x_exp == Self::MIN_EXPONENT - 1
2683 && (o == Less || !x.is_power_of_2())
2684 {
2685 if sign {
2686 *self = Self::min_positive_value_prec(prec);
2687 Greater
2688 } else {
2689 *self = -Self::min_positive_value_prec(prec);
2690 Less
2691 }
2692 } else {
2693 match (sign, rm) {
2694 (_, Exact) => panic!("Inexact Float division"),
2695 (true, Ceiling | Up) => {
2696 *self = Self::min_positive_value_prec(prec);
2697 Greater
2698 }
2699 (true, _) => {
2700 *self = float_zero!();
2701 Less
2702 }
2703 (false, Floor | Up) => {
2704 *self = -Self::min_positive_value_prec(prec);
2705 Less
2706 }
2707 (false, _) => {
2708 *self = float_negative_zero!();
2709 Greater
2710 }
2711 }
2712 };
2713 }
2714 *x_sign = sign;
2715 *x_prec = prec;
2716 if sign { o } else { o.reverse() }
2717 }
2718 }
2719 }
2720
2721 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the nearest value of the
2722 /// specified precision. The [`Float`] on the right-hand side is taken by value. An [`Ordering`]
2723 /// is returned, indicating whether the rounded quotient is less than, equal to, or greater than
2724 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
2725 /// function sets the [`Float`] to `NaN` it also returns `Equal`.
2726 ///
2727 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
2728 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2729 /// description of the `Nearest` rounding mode.
2730 ///
2731 /// $$
2732 /// x \gets x/y+\varepsilon.
2733 /// $$
2734 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2735 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2736 ///
2737 /// If the output has a precision, it is `prec`.
2738 ///
2739 /// See the [`Float::div_prec`] documentation for information on special cases, overflow, and
2740 /// underflow.
2741 ///
2742 /// If you want to use a rounding mode other than `Nearest`, consider using
2743 /// [`Float::div_prec_round_assign`] instead. If you know that your target precision is the
2744 /// maximum of the precisions of the two inputs, consider using `/=` instead.
2745 ///
2746 /// # Worst-case complexity
2747 /// $T(n) = O(n \log n \log\log n)$
2748 ///
2749 /// $M(n) = O(n \log n)$
2750 ///
2751 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2752 /// other.significant_bits(), `prec`)`.
2753 ///
2754 /// # Examples
2755 /// ```
2756 /// use core::f64::consts::{E, PI};
2757 /// use malachite_float::Float;
2758 /// use std::cmp::Ordering::*;
2759 ///
2760 /// let mut x = Float::from(PI);
2761 /// assert_eq!(x.div_prec_assign(Float::from(E), 5), Less);
2762 /// assert_eq!(x.to_string(), "1.12");
2763 ///
2764 /// let mut x = Float::from(PI);
2765 /// assert_eq!(x.div_prec_assign(Float::from(E), 20), Greater);
2766 /// assert_eq!(x.to_string(), "1.155727");
2767 /// ```
2768 #[inline]
2769 pub fn div_prec_assign(&mut self, other: Self, prec: u64) -> Ordering {
2770 self.div_prec_round_assign(other, prec, Nearest)
2771 }
2772
2773 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the nearest value of the
2774 /// specified precision. The [`Float`] on the right-hand side is taken by reference. An
2775 /// [`Ordering`] is returned, indicating whether the rounded quotient is less than, equal to, or
2776 /// greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
2777 /// whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
2778 ///
2779 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
2780 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2781 /// description of the `Nearest` rounding mode.
2782 ///
2783 /// $$
2784 /// x \gets x/y+\varepsilon.
2785 /// $$
2786 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2787 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2788 ///
2789 /// If the output has a precision, it is `prec`.
2790 ///
2791 /// See the [`Float::div_prec`] documentation for information on special cases, overflow, and
2792 /// underflow.
2793 ///
2794 /// If you want to use a rounding mode other than `Nearest`, consider using
2795 /// [`Float::div_prec_round_assign_ref`] instead. If you know that your target precision is the
2796 /// maximum of the precisions of the two inputs, consider using `/=` instead.
2797 ///
2798 /// # Worst-case complexity
2799 /// $T(n) = O(n \log n \log\log n)$
2800 ///
2801 /// $M(n) = O(n \log n)$
2802 ///
2803 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2804 /// other.significant_bits(), `prec`)`.
2805 ///
2806 /// # Examples
2807 /// ```
2808 /// use core::f64::consts::{E, PI};
2809 /// use malachite_float::Float;
2810 /// use std::cmp::Ordering::*;
2811 ///
2812 /// let mut x = Float::from(PI);
2813 /// assert_eq!(x.div_prec_assign_ref(&Float::from(E), 5), Less);
2814 /// assert_eq!(x.to_string(), "1.12");
2815 ///
2816 /// let mut x = Float::from(PI);
2817 /// assert_eq!(x.div_prec_assign_ref(&Float::from(E), 20), Greater);
2818 /// assert_eq!(x.to_string(), "1.155727");
2819 /// ```
2820 #[inline]
2821 pub fn div_prec_assign_ref(&mut self, other: &Self, prec: u64) -> Ordering {
2822 self.div_prec_round_assign_ref(other, prec, Nearest)
2823 }
2824
2825 /// Divides a [`Float`] by a [`Float`] in place, rounding the result with the specified rounding
2826 /// mode. The [`Float`] on the right-hand side is taken by value. An [`Ordering`] is returned,
2827 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
2828 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
2829 /// the [`Float`] to `NaN` it also returns `Equal`.
2830 ///
2831 /// The precision of the output is the maximum of the precision of the inputs. See
2832 /// [`RoundingMode`] for a description of the possible rounding modes.
2833 ///
2834 /// $$
2835 /// x \gets x/y+\varepsilon.
2836 /// $$
2837 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2838 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2839 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2840 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2841 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2842 ///
2843 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2844 ///
2845 /// See the [`Float::div_round`] documentation for information on special cases, overflow, and
2846 /// underflow.
2847 ///
2848 /// If you want to specify an output precision, consider using [`Float::div_prec_round_assign`]
2849 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/=`
2850 /// instead.
2851 ///
2852 /// # Worst-case complexity
2853 /// $T(n) = O(n \log n \log\log n)$
2854 ///
2855 /// $M(n) = O(n \log n)$
2856 ///
2857 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2858 /// other.significant_bits())`.
2859 ///
2860 /// # Panics
2861 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
2862 /// represent the output.
2863 ///
2864 /// # Examples
2865 /// ```
2866 /// use core::f64::consts::{E, PI};
2867 /// use malachite_base::rounding_modes::RoundingMode::*;
2868 /// use malachite_float::Float;
2869 /// use std::cmp::Ordering::*;
2870 ///
2871 /// let mut x = Float::from(PI);
2872 /// assert_eq!(x.div_round_assign(Float::from(E), Floor), Less);
2873 /// assert_eq!(x.to_string(), "1.1557273497909217");
2874 ///
2875 /// let mut x = Float::from(PI);
2876 /// assert_eq!(x.div_round_assign(Float::from(E), Ceiling), Greater);
2877 /// assert_eq!(x.to_string(), "1.155727349790922");
2878 ///
2879 /// let mut x = Float::from(PI);
2880 /// assert_eq!(x.div_round_assign(Float::from(E), Nearest), Less);
2881 /// assert_eq!(x.to_string(), "1.1557273497909217");
2882 /// ```
2883 #[inline]
2884 pub fn div_round_assign(&mut self, other: Self, rm: RoundingMode) -> Ordering {
2885 let prec = max(self.significant_bits(), other.significant_bits());
2886 self.div_prec_round_assign(other, prec, rm)
2887 }
2888
2889 /// Divides a [`Float`] by a [`Float`] in place, rounding the result with the specified rounding
2890 /// mode. The [`Float`] on the right-hand side is taken by reference. An [`Ordering`] is
2891 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
2892 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
2893 /// function sets the [`Float`] to `NaN` it also returns `Equal`.
2894 ///
2895 /// The precision of the output is the maximum of the precision of the inputs. See
2896 /// [`RoundingMode`] for a description of the possible rounding modes.
2897 ///
2898 /// $$
2899 /// x \gets x/y+\varepsilon.
2900 /// $$
2901 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2902 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2903 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2904 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2905 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2906 ///
2907 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2908 ///
2909 /// See the [`Float::div_round`] documentation for information on special cases, overflow, and
2910 /// underflow.
2911 ///
2912 /// If you want to specify an output precision, consider using
2913 /// [`Float::div_prec_round_assign_ref`] instead. If you know you'll be using the `Nearest`
2914 /// rounding mode, consider using `/=` instead.
2915 ///
2916 /// # Worst-case complexity
2917 /// $T(n) = O(n \log n \log\log n)$
2918 ///
2919 /// $M(n) = O(n \log n)$
2920 ///
2921 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2922 /// other.significant_bits())`.
2923 ///
2924 /// # Panics
2925 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
2926 /// represent the output.
2927 ///
2928 /// # Examples
2929 /// ```
2930 /// use core::f64::consts::{E, PI};
2931 /// use malachite_base::rounding_modes::RoundingMode::*;
2932 /// use malachite_float::Float;
2933 /// use std::cmp::Ordering::*;
2934 ///
2935 /// let mut x = Float::from(PI);
2936 /// assert_eq!(x.div_round_assign_ref(&Float::from(E), Floor), Less);
2937 /// assert_eq!(x.to_string(), "1.1557273497909217");
2938 ///
2939 /// let mut x = Float::from(PI);
2940 /// assert_eq!(x.div_round_assign_ref(&Float::from(E), Ceiling), Greater);
2941 /// assert_eq!(x.to_string(), "1.155727349790922");
2942 ///
2943 /// let mut x = Float::from(PI);
2944 /// assert_eq!(x.div_round_assign_ref(&Float::from(E), Nearest), Less);
2945 /// assert_eq!(x.to_string(), "1.1557273497909217");
2946 /// ```
2947 #[inline]
2948 pub fn div_round_assign_ref(&mut self, other: &Self, rm: RoundingMode) -> Ordering {
2949 let prec = max(self.significant_bits(), other.significant_bits());
2950 self.div_prec_round_assign_ref(other, prec, rm)
2951 }
2952
2953 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
2954 /// with the specified rounding mode. The [`Float`] and the [`Rational`] are both taken by
2955 /// value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
2956 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
2957 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2958 ///
2959 /// See [`RoundingMode`] for a description of the possible rounding modes.
2960 ///
2961 /// $$
2962 /// f(x,y,p,m) = x/y+\varepsilon.
2963 /// $$
2964 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2965 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2966 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2967 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2968 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2969 ///
2970 /// If the output has a precision, it is `prec`.
2971 ///
2972 /// Special cases:
2973 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
2974 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
2975 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
2976 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
2977 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
2978 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
2979 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
2980 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
2981 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
2982 ///
2983 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec`] instead.
2984 /// If you know that your target precision is the precision of the [`Float`] input, consider
2985 /// using [`Float::div_rational_round`] instead. If both of these things are true, consider
2986 /// using `/` instead.
2987 ///
2988 /// # Worst-case complexity
2989 /// $T(n) = O(n \log n \log\log n)$
2990 ///
2991 /// $M(n) = O(n \log n)$
2992 ///
2993 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2994 /// other.significant_bits(), prec)`.
2995 ///
2996 /// # Panics
2997 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2998 ///
2999 /// # Examples
3000 /// ```
3001 /// use core::f64::consts::PI;
3002 /// use malachite_base::rounding_modes::RoundingMode::*;
3003 /// use malachite_float::Float;
3004 /// use malachite_q::Rational;
3005 /// use std::cmp::Ordering::*;
3006 ///
3007 /// let (quotient, o) =
3008 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 5, Floor);
3009 /// assert_eq!(quotient.to_string(), "9.0");
3010 /// assert_eq!(o, Less);
3011 ///
3012 /// let (quotient, o) =
3013 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 5, Ceiling);
3014 /// assert_eq!(quotient.to_string(), "9.5");
3015 /// assert_eq!(o, Greater);
3016 ///
3017 /// let (quotient, o) =
3018 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 5, Nearest);
3019 /// assert_eq!(quotient.to_string(), "9.5");
3020 /// assert_eq!(o, Greater);
3021 ///
3022 /// let (quotient, o) =
3023 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 20, Floor);
3024 /// assert_eq!(quotient.to_string(), "9.42477");
3025 /// assert_eq!(o, Less);
3026 ///
3027 /// let (quotient, o) =
3028 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 20, Ceiling);
3029 /// assert_eq!(quotient.to_string(), "9.42479");
3030 /// assert_eq!(o, Greater);
3031 ///
3032 /// let (quotient, o) =
3033 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 20, Nearest);
3034 /// assert_eq!(quotient.to_string(), "9.42477");
3035 /// assert_eq!(o, Less);
3036 /// ```
3037 #[inline]
3038 pub fn div_rational_prec_round(
3039 mut self,
3040 other: Rational,
3041 prec: u64,
3042 rm: RoundingMode,
3043 ) -> (Self, Ordering) {
3044 let o = self.div_rational_prec_round_assign(other, prec, rm);
3045 (self, o)
3046 }
3047
3048 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
3049 /// with the specified rounding mode. The [`Float`] is taken by value and the [`Rational`] by
3050 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
3051 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
3052 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3053 ///
3054 /// See [`RoundingMode`] for a description of the possible rounding modes.
3055 ///
3056 /// $$
3057 /// f(x,y,p,m) = x/y+\varepsilon.
3058 /// $$
3059 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3060 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3061 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3062 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3063 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3064 ///
3065 /// If the output has a precision, it is `prec`.
3066 ///
3067 /// Special cases:
3068 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
3069 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
3070 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
3071 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
3072 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
3073 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
3074 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
3075 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
3076 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
3077 ///
3078 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_val_ref`]
3079 /// instead. If you know that your target precision is the precision of the [`Float`] input,
3080 /// consider using [`Float::div_rational_round_val_ref`] instead. If both of these things are
3081 /// true, consider using `/` instead.
3082 ///
3083 /// # Worst-case complexity
3084 /// $T(n) = O(n \log n \log\log n)$
3085 ///
3086 /// $M(n) = O(n \log n)$
3087 ///
3088 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3089 /// other.significant_bits(), prec)`.
3090 ///
3091 /// # Panics
3092 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3093 ///
3094 /// # Examples
3095 /// ```
3096 /// use core::f64::consts::PI;
3097 /// use malachite_base::rounding_modes::RoundingMode::*;
3098 /// use malachite_float::Float;
3099 /// use malachite_q::Rational;
3100 /// use std::cmp::Ordering::*;
3101 ///
3102 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3103 /// &Rational::from_unsigneds(1u8, 3),
3104 /// 5,
3105 /// Floor,
3106 /// );
3107 /// assert_eq!(quotient.to_string(), "9.0");
3108 /// assert_eq!(o, Less);
3109 ///
3110 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3111 /// &Rational::from_unsigneds(1u8, 3),
3112 /// 5,
3113 /// Ceiling,
3114 /// );
3115 /// assert_eq!(quotient.to_string(), "9.5");
3116 /// assert_eq!(o, Greater);
3117 ///
3118 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3119 /// &Rational::from_unsigneds(1u8, 3),
3120 /// 5,
3121 /// Nearest,
3122 /// );
3123 /// assert_eq!(quotient.to_string(), "9.5");
3124 /// assert_eq!(o, Greater);
3125 ///
3126 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3127 /// &Rational::from_unsigneds(1u8, 3),
3128 /// 20,
3129 /// Floor,
3130 /// );
3131 /// assert_eq!(quotient.to_string(), "9.42477");
3132 /// assert_eq!(o, Less);
3133 ///
3134 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3135 /// &Rational::from_unsigneds(1u8, 3),
3136 /// 20,
3137 /// Ceiling,
3138 /// );
3139 /// assert_eq!(quotient.to_string(), "9.42479");
3140 /// assert_eq!(o, Greater);
3141 ///
3142 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3143 /// &Rational::from_unsigneds(1u8, 3),
3144 /// 20,
3145 /// Nearest,
3146 /// );
3147 /// assert_eq!(quotient.to_string(), "9.42477");
3148 /// assert_eq!(o, Less);
3149 /// ```
3150 #[inline]
3151 pub fn div_rational_prec_round_val_ref(
3152 mut self,
3153 other: &Rational,
3154 prec: u64,
3155 rm: RoundingMode,
3156 ) -> (Self, Ordering) {
3157 let o = self.div_rational_prec_round_assign_ref(other, prec, rm);
3158 (self, o)
3159 }
3160
3161 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
3162 /// with the specified rounding mode. The [`Float`] is taken by reference and the [`Rational`]
3163 /// by value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
3164 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
3165 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3166 ///
3167 /// See [`RoundingMode`] for a description of the possible rounding modes.
3168 ///
3169 /// $$
3170 /// f(x,y,p,m) = x/y+\varepsilon.
3171 /// $$
3172 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3173 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3174 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3175 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3176 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3177 ///
3178 /// If the output has a precision, it is `prec`.
3179 ///
3180 /// Special cases:
3181 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
3182 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
3183 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
3184 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
3185 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
3186 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
3187 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
3188 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
3189 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
3190 ///
3191 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_ref_val`]
3192 /// instead. If you know that your target precision is the precision of the [`Float`] input,
3193 /// consider using [`Float::div_rational_round_ref_val`] instead. If both of these things are
3194 /// true, consider using `/` instead.
3195 ///
3196 /// # Worst-case complexity
3197 /// $T(n) = O(n \log n \log\log n)$
3198 ///
3199 /// $M(n) = O(n \log n)$
3200 ///
3201 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3202 /// other.significant_bits(), prec)`.
3203 ///
3204 /// # Panics
3205 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3206 ///
3207 /// # Examples
3208 /// ```
3209 /// use core::f64::consts::PI;
3210 /// use malachite_base::rounding_modes::RoundingMode::*;
3211 /// use malachite_float::Float;
3212 /// use malachite_q::Rational;
3213 /// use std::cmp::Ordering::*;
3214 ///
3215 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3216 /// Rational::from_unsigneds(1u8, 3),
3217 /// 5,
3218 /// Floor,
3219 /// );
3220 /// assert_eq!(quotient.to_string(), "9.0");
3221 /// assert_eq!(o, Less);
3222 ///
3223 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3224 /// Rational::from_unsigneds(1u8, 3),
3225 /// 5,
3226 /// Ceiling,
3227 /// );
3228 /// assert_eq!(quotient.to_string(), "9.5");
3229 /// assert_eq!(o, Greater);
3230 ///
3231 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3232 /// Rational::from_unsigneds(1u8, 3),
3233 /// 5,
3234 /// Nearest,
3235 /// );
3236 /// assert_eq!(quotient.to_string(), "9.5");
3237 /// assert_eq!(o, Greater);
3238 ///
3239 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3240 /// Rational::from_unsigneds(1u8, 3),
3241 /// 20,
3242 /// Floor,
3243 /// );
3244 /// assert_eq!(quotient.to_string(), "9.42477");
3245 /// assert_eq!(o, Less);
3246 ///
3247 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3248 /// Rational::from_unsigneds(1u8, 3),
3249 /// 20,
3250 /// Ceiling,
3251 /// );
3252 /// assert_eq!(quotient.to_string(), "9.42479");
3253 /// assert_eq!(o, Greater);
3254 ///
3255 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3256 /// Rational::from_unsigneds(1u8, 3),
3257 /// 20,
3258 /// Nearest,
3259 /// );
3260 /// assert_eq!(quotient.to_string(), "9.42477");
3261 /// assert_eq!(o, Less);
3262 /// ```
3263 #[inline]
3264 pub fn div_rational_prec_round_ref_val(
3265 &self,
3266 other: Rational,
3267 prec: u64,
3268 rm: RoundingMode,
3269 ) -> (Self, Ordering) {
3270 if max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD {
3271 div_rational_prec_round_naive_ref_val(self, other, prec, rm)
3272 } else {
3273 div_rational_prec_round_direct_ref_val(self, other, prec, rm)
3274 }
3275 }
3276
3277 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
3278 /// with the specified rounding mode. The [`Float`] and the [`Rational`] are both taken by
3279 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
3280 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
3281 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3282 ///
3283 /// See [`RoundingMode`] for a description of the possible rounding modes.
3284 ///
3285 /// $$
3286 /// f(x,y,p,m) = x/y+\varepsilon.
3287 /// $$
3288 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3289 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3290 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3291 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3292 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3293 ///
3294 /// If the output has a precision, it is `prec`.
3295 ///
3296 /// Special cases:
3297 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
3298 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
3299 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
3300 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
3301 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
3302 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
3303 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
3304 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
3305 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
3306 ///
3307 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_ref_ref`]
3308 /// instead. If you know that your target precision is the precision of the [`Float`] input,
3309 /// consider using [`Float::div_rational_round_ref_ref`] instead. If both of these things are
3310 /// true, consider using `/` instead.
3311 ///
3312 /// # Worst-case complexity
3313 /// $T(n) = O(n \log n \log\log n)$
3314 ///
3315 /// $M(n) = O(n \log n)$
3316 ///
3317 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3318 /// other.significant_bits(), prec)`.
3319 ///
3320 /// # Panics
3321 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3322 ///
3323 /// # Examples
3324 /// ```
3325 /// use core::f64::consts::PI;
3326 /// use malachite_base::rounding_modes::RoundingMode::*;
3327 /// use malachite_float::Float;
3328 /// use malachite_q::Rational;
3329 /// use std::cmp::Ordering::*;
3330 ///
3331 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3332 /// &Rational::from_unsigneds(1u8, 3),
3333 /// 5,
3334 /// Floor,
3335 /// );
3336 /// assert_eq!(quotient.to_string(), "9.0");
3337 /// assert_eq!(o, Less);
3338 ///
3339 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3340 /// &Rational::from_unsigneds(1u8, 3),
3341 /// 5,
3342 /// Ceiling,
3343 /// );
3344 /// assert_eq!(quotient.to_string(), "9.5");
3345 /// assert_eq!(o, Greater);
3346 ///
3347 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3348 /// &Rational::from_unsigneds(1u8, 3),
3349 /// 5,
3350 /// Nearest,
3351 /// );
3352 /// assert_eq!(quotient.to_string(), "9.5");
3353 /// assert_eq!(o, Greater);
3354 ///
3355 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3356 /// &Rational::from_unsigneds(1u8, 3),
3357 /// 20,
3358 /// Floor,
3359 /// );
3360 /// assert_eq!(quotient.to_string(), "9.42477");
3361 /// assert_eq!(o, Less);
3362 ///
3363 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3364 /// &Rational::from_unsigneds(1u8, 3),
3365 /// 20,
3366 /// Ceiling,
3367 /// );
3368 /// assert_eq!(quotient.to_string(), "9.42479");
3369 /// assert_eq!(o, Greater);
3370 ///
3371 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3372 /// &Rational::from_unsigneds(1u8, 3),
3373 /// 20,
3374 /// Nearest,
3375 /// );
3376 /// assert_eq!(quotient.to_string(), "9.42477");
3377 /// assert_eq!(o, Less);
3378 /// ```
3379 #[inline]
3380 pub fn div_rational_prec_round_ref_ref(
3381 &self,
3382 other: &Rational,
3383 prec: u64,
3384 rm: RoundingMode,
3385 ) -> (Self, Ordering) {
3386 if max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD {
3387 div_rational_prec_round_naive_ref_ref(self, other, prec, rm)
3388 } else {
3389 div_rational_prec_round_direct_ref_ref(self, other, prec, rm)
3390 }
3391 }
3392
3393 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3394 /// specified precision. The [`Float`] and the [`Rational`] are both are taken by value. An
3395 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3396 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3397 /// whenever this function returns a `NaN` it also returns `Equal`.
3398 ///
3399 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3400 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3401 /// description of the `Nearest` rounding mode.
3402 ///
3403 /// $$
3404 /// f(x,y,p) = x/y+\varepsilon.
3405 /// $$
3406 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3407 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3408 ///
3409 /// If the output has a precision, it is `prec`.
3410 ///
3411 /// Special cases:
3412 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
3413 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
3414 /// - $f(\infty,x,p)=-\infty$ if $x<0$
3415 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
3416 /// - $f(-\infty,x,p)=\infty$ if $x<0$
3417 /// - $f(0.0,x,p)=0.0$ if $x>0$
3418 /// - $f(0.0,x,p)=-0.0$ if $x<0$
3419 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
3420 /// - $f(-0.0,x,p)=0.0$ if $x<0$
3421 ///
3422 /// If you want to use a rounding mode other than `Nearest`, consider using
3423 /// [`Float::div_rational_prec_round`] instead. If you know that your target precision is the
3424 /// precision of the [`Float`] input, consider using `/` instead.
3425 ///
3426 /// # Worst-case complexity
3427 /// $T(n) = O(n \log n \log\log n)$
3428 ///
3429 /// $M(n) = O(n \log n)$
3430 ///
3431 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3432 /// other.significant_bits(), prec)`.
3433 ///
3434 /// # Examples
3435 /// ```
3436 /// use core::f64::consts::PI;
3437 /// use malachite_base::num::conversion::traits::ExactFrom;
3438 /// use malachite_float::Float;
3439 /// use malachite_q::Rational;
3440 /// use std::cmp::Ordering::*;
3441 ///
3442 /// let (quotient, o) = Float::from(PI).div_rational_prec(Rational::exact_from(1.5), 5);
3443 /// assert_eq!(quotient.to_string(), "2.1");
3444 /// assert_eq!(o, Greater);
3445 ///
3446 /// let (quotient, o) = Float::from(PI).div_rational_prec(Rational::exact_from(1.5), 20);
3447 /// assert_eq!(quotient.to_string(), "2.094395");
3448 /// assert_eq!(o, Less);
3449 /// ```
3450 #[inline]
3451 pub fn div_rational_prec(self, other: Rational, prec: u64) -> (Self, Ordering) {
3452 self.div_rational_prec_round(other, prec, Nearest)
3453 }
3454
3455 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3456 /// specified precision. The [`Float`] is taken by value and the [`Rational`] by reference. An
3457 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3458 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3459 /// whenever this function returns a `NaN` it also returns `Equal`.
3460 ///
3461 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3462 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3463 /// description of the `Nearest` rounding mode.
3464 ///
3465 /// $$
3466 /// f(x,y,p) = x/y+\varepsilon.
3467 /// $$
3468 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3469 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3470 ///
3471 /// If the output has a precision, it is `prec`.
3472 ///
3473 /// Special cases:
3474 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
3475 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
3476 /// - $f(\infty,x,p)=-\infty$ if $x<0$
3477 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
3478 /// - $f(-\infty,x,p)=\infty$ if $x<0$
3479 /// - $f(0.0,x,p)=0.0$ if $x>0$
3480 /// - $f(0.0,x,p)=-0.0$ if $x<0$
3481 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
3482 /// - $f(-0.0,x,p)=0.0$ if $x<0$
3483 ///
3484 /// If you want to use a rounding mode other than `Nearest`, consider using
3485 /// [`Float::div_rational_prec_round_val_ref`] instead. If you know that your target precision
3486 /// is the precision of the [`Float`] input, consider using `/` instead.
3487 ///
3488 /// # Worst-case complexity
3489 /// $T(n) = O(n \log n \log\log n)$
3490 ///
3491 /// $M(n) = O(n \log n)$
3492 ///
3493 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3494 /// other.significant_bits(), prec)`.
3495 ///
3496 /// # Examples
3497 /// ```
3498 /// use core::f64::consts::PI;
3499 /// use malachite_base::num::conversion::traits::ExactFrom;
3500 /// use malachite_float::Float;
3501 /// use malachite_q::Rational;
3502 /// use std::cmp::Ordering::*;
3503 ///
3504 /// let (quotient, o) =
3505 /// Float::from(PI).div_rational_prec_val_ref(&Rational::exact_from(1.5), 5);
3506 /// assert_eq!(quotient.to_string(), "2.1");
3507 /// assert_eq!(o, Greater);
3508 ///
3509 /// let (quotient, o) =
3510 /// Float::from(PI).div_rational_prec_val_ref(&Rational::exact_from(1.5), 20);
3511 /// assert_eq!(quotient.to_string(), "2.094395");
3512 /// assert_eq!(o, Less);
3513 /// ```
3514 #[inline]
3515 pub fn div_rational_prec_val_ref(self, other: &Rational, prec: u64) -> (Self, Ordering) {
3516 self.div_rational_prec_round_val_ref(other, prec, Nearest)
3517 }
3518
3519 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3520 /// specified precision. The [`Float`] is taken by reference and the [`Rational`] by value. An
3521 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3522 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3523 /// whenever this function returns a `NaN` it also returns `Equal`.
3524 ///
3525 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3526 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3527 /// description of the `Nearest` rounding mode.
3528 ///
3529 /// $$
3530 /// f(x,y,p) = x/y+\varepsilon.
3531 /// $$
3532 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3533 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3534 ///
3535 /// If the output has a precision, it is `prec`.
3536 ///
3537 /// Special cases:
3538 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
3539 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
3540 /// - $f(\infty,x,p)=-\infty$ if $x<0$
3541 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
3542 /// - $f(-\infty,x,p)=\infty$ if $x<0$
3543 /// - $f(0.0,x,p)=0.0$ if $x>0$
3544 /// - $f(0.0,x,p)=-0.0$ if $x<0$
3545 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
3546 /// - $f(-0.0,x,p)=0.0$ if $x<0$
3547 ///
3548 /// If you want to use a rounding mode other than `Nearest`, consider using
3549 /// [`Float::div_rational_prec_round_ref_val`] instead. If you know that your target precision
3550 /// is the precision of the [`Float`] input, consider using `/` instead.
3551 ///
3552 /// # Worst-case complexity
3553 /// $T(n) = O(n \log n \log\log n)$
3554 ///
3555 /// $M(n) = O(n \log n)$
3556 ///
3557 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3558 /// other.significant_bits(), prec)`.
3559 ///
3560 /// # Examples
3561 /// ```
3562 /// use core::f64::consts::PI;
3563 /// use malachite_base::num::conversion::traits::ExactFrom;
3564 /// use malachite_float::Float;
3565 /// use malachite_q::Rational;
3566 /// use std::cmp::Ordering::*;
3567 ///
3568 /// let (quotient, o) = Float::from(PI).div_rational_prec_ref_val(Rational::exact_from(1.5), 5);
3569 /// assert_eq!(quotient.to_string(), "2.1");
3570 /// assert_eq!(o, Greater);
3571 ///
3572 /// let (quotient, o) =
3573 /// Float::from(PI).div_rational_prec_ref_val(Rational::exact_from(1.5), 20);
3574 /// assert_eq!(quotient.to_string(), "2.094395");
3575 /// assert_eq!(o, Less);
3576 /// ```
3577 #[inline]
3578 pub fn div_rational_prec_ref_val(&self, other: Rational, prec: u64) -> (Self, Ordering) {
3579 self.div_rational_prec_round_ref_val(other, prec, Nearest)
3580 }
3581
3582 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3583 /// specified precision. The [`Float`] and the [`Rational`] are both are taken by reference. An
3584 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3585 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3586 /// whenever this function returns a `NaN` it also returns `Equal`.
3587 ///
3588 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3589 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3590 /// description of the `Nearest` rounding mode.
3591 ///
3592 /// $$
3593 /// f(x,y,p) = x/y+\varepsilon.
3594 /// $$
3595 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3596 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3597 ///
3598 /// If the output has a precision, it is `prec`.
3599 ///
3600 /// Special cases:
3601 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
3602 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
3603 /// - $f(\infty,x,p)=-\infty$ if $x<0$
3604 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
3605 /// - $f(-\infty,x,p)=\infty$ if $x<0$
3606 /// - $f(0.0,x,p)=0.0$ if $x>0$
3607 /// - $f(0.0,x,p)=-0.0$ if $x<0$
3608 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
3609 /// - $f(-0.0,x,p)=0.0$ if $x<0$
3610 ///
3611 /// If you want to use a rounding mode other than `Nearest`, consider using
3612 /// [`Float::div_rational_prec_round_ref_ref`] instead. If you know that your target precision
3613 /// is the precision of the [`Float`] input, consider using `/` instead.
3614 ///
3615 /// # Worst-case complexity
3616 /// $T(n) = O(n \log n \log\log n)$
3617 ///
3618 /// $M(n) = O(n \log n)$
3619 ///
3620 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3621 /// other.significant_bits(), prec)`.
3622 ///
3623 /// # Examples
3624 /// ```
3625 /// use core::f64::consts::PI;
3626 /// use malachite_base::num::conversion::traits::ExactFrom;
3627 /// use malachite_float::Float;
3628 /// use malachite_q::Rational;
3629 /// use std::cmp::Ordering::*;
3630 ///
3631 /// let (quotient, o) =
3632 /// Float::from(PI).div_rational_prec_ref_ref(&Rational::exact_from(1.5), 5);
3633 /// assert_eq!(quotient.to_string(), "2.1");
3634 /// assert_eq!(o, Greater);
3635 ///
3636 /// let (quotient, o) =
3637 /// Float::from(PI).div_rational_prec_ref_ref(&Rational::exact_from(1.5), 20);
3638 /// assert_eq!(quotient.to_string(), "2.094395");
3639 /// assert_eq!(o, Less);
3640 /// ```
3641 #[inline]
3642 pub fn div_rational_prec_ref_ref(&self, other: &Rational, prec: u64) -> (Self, Ordering) {
3643 self.div_rational_prec_round_ref_ref(other, prec, Nearest)
3644 }
3645
3646 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
3647 /// The [`Float`] and the [`Rational`] are both are taken by value. An [`Ordering`] is also
3648 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3649 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3650 /// function returns a `NaN` it also returns `Equal`.
3651 ///
3652 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
3653 /// for a description of the possible rounding modes.
3654 ///
3655 /// $$
3656 /// f(x,y,m) = x/y+\varepsilon.
3657 /// $$
3658 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3659 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3660 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3661 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3662 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3663 ///
3664 /// If the output has a precision, it is the precision of the [`Float`] input.
3665 ///
3666 /// Special cases:
3667 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
3668 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
3669 /// - $f(\infty,x,m)=-\infty$ if $x<0$
3670 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
3671 /// - $f(-\infty,x,m)=\infty$ if $x<0$
3672 /// - $f(0.0,x,m)=0.0$ if $x>0$
3673 /// - $f(0.0,x,m)=-0.0$ if $x<0$
3674 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
3675 /// - $f(-0.0,x,m)=0.0$ if $x<0$
3676 ///
3677 /// If you want to specify an output precision, consider using
3678 /// [`Float::div_rational_prec_round`] instead. If you know you'll be using the `Nearest`
3679 /// rounding mode, consider using `/` instead.
3680 ///
3681 /// # Worst-case complexity
3682 /// $T(n) = O(n \log n \log\log n)$
3683 ///
3684 /// $M(n) = O(n \log n)$
3685 ///
3686 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3687 /// other.significant_bits())`.
3688 ///
3689 /// # Panics
3690 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
3691 /// represent the output.
3692 ///
3693 /// # Examples
3694 /// ```
3695 /// use core::f64::consts::PI;
3696 /// use malachite_base::rounding_modes::RoundingMode::*;
3697 /// use malachite_float::Float;
3698 /// use malachite_q::Rational;
3699 /// use std::cmp::Ordering::*;
3700 ///
3701 /// let (quotient, o) =
3702 /// Float::from(PI).div_rational_round(Rational::from_unsigneds(1u8, 3), Floor);
3703 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3704 /// assert_eq!(o, Less);
3705 ///
3706 /// let (quotient, o) =
3707 /// Float::from(PI).div_rational_round(Rational::from_unsigneds(1u8, 3), Ceiling);
3708 /// assert_eq!(quotient.to_string(), "9.42477796076939");
3709 /// assert_eq!(o, Greater);
3710 ///
3711 /// let (quotient, o) =
3712 /// Float::from(PI).div_rational_round(Rational::from_unsigneds(1u8, 3), Nearest);
3713 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3714 /// assert_eq!(o, Less);
3715 /// ```
3716 #[inline]
3717 pub fn div_rational_round(self, other: Rational, rm: RoundingMode) -> (Self, Ordering) {
3718 let prec = self.significant_bits();
3719 self.div_rational_prec_round(other, prec, rm)
3720 }
3721
3722 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
3723 /// The [`Float`] is taken by value and the [`Rational`] by reference. An [`Ordering`] is also
3724 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3725 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3726 /// function returns a `NaN` it also returns `Equal`.
3727 ///
3728 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
3729 /// for a description of the possible rounding modes.
3730 ///
3731 /// $$
3732 /// f(x,y,m) = x/y+\varepsilon.
3733 /// $$
3734 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3735 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3736 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3737 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3738 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3739 ///
3740 /// If the output has a precision, it is the precision of the [`Float`] input.
3741 ///
3742 /// Special cases:
3743 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
3744 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
3745 /// - $f(\infty,x,m)=-\infty$ if $x<0$
3746 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
3747 /// - $f(-\infty,x,m)=\infty$ if $x<0$
3748 /// - $f(0.0,x,m)=0.0$ if $x>0$
3749 /// - $f(0.0,x,m)=-0.0$ if $x<0$
3750 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
3751 /// - $f(-0.0,x,m)=0.0$ if $x<0$
3752 ///
3753 /// If you want to specify an output precision, consider using
3754 /// [`Float::div_rational_prec_round_val_ref`] instead. If you know you'll be using the
3755 /// `Nearest` rounding mode, consider using `/` instead.
3756 ///
3757 /// # Worst-case complexity
3758 /// $T(n) = O(n \log n \log\log n)$
3759 ///
3760 /// $M(n) = O(n \log n)$
3761 ///
3762 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3763 /// other.significant_bits())`.
3764 ///
3765 /// # Panics
3766 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
3767 /// represent the output.
3768 ///
3769 /// # Examples
3770 /// ```
3771 /// use core::f64::consts::PI;
3772 /// use malachite_base::rounding_modes::RoundingMode::*;
3773 /// use malachite_float::Float;
3774 /// use malachite_q::Rational;
3775 /// use std::cmp::Ordering::*;
3776 ///
3777 /// let (quotient, o) =
3778 /// Float::from(PI).div_rational_round_val_ref(&Rational::from_unsigneds(1u8, 3), Floor);
3779 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3780 /// assert_eq!(o, Less);
3781 ///
3782 /// let (quotient, o) =
3783 /// Float::from(PI).div_rational_round_val_ref(&Rational::from_unsigneds(1u8, 3), Ceiling);
3784 /// assert_eq!(quotient.to_string(), "9.42477796076939");
3785 /// assert_eq!(o, Greater);
3786 ///
3787 /// let (quotient, o) =
3788 /// Float::from(PI).div_rational_round_val_ref(&Rational::from_unsigneds(1u8, 3), Nearest);
3789 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3790 /// assert_eq!(o, Less);
3791 /// ```
3792 #[inline]
3793 pub fn div_rational_round_val_ref(
3794 self,
3795 other: &Rational,
3796 rm: RoundingMode,
3797 ) -> (Self, Ordering) {
3798 let prec = self.significant_bits();
3799 self.div_rational_prec_round_val_ref(other, prec, rm)
3800 }
3801
3802 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
3803 /// The [`Float`] is taken by reference and the [`Rational`] by value. An [`Ordering`] is also
3804 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3805 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3806 /// function returns a `NaN` it also returns `Equal`.
3807 ///
3808 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
3809 /// for a description of the possible rounding modes.
3810 ///
3811 /// $$
3812 /// f(x,y,m) = x/y+\varepsilon.
3813 /// $$
3814 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3815 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3816 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3817 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3818 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3819 ///
3820 /// If the output has a precision, it is the precision of the [`Float`] input.
3821 ///
3822 /// Special cases:
3823 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
3824 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
3825 /// - $f(\infty,x,m)=-\infty$ if $x<0$
3826 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
3827 /// - $f(-\infty,x,m)=\infty$ if $x<0$
3828 /// - $f(0.0,x,m)=0.0$ if $x>0$
3829 /// - $f(0.0,x,m)=-0.0$ if $x<0$
3830 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
3831 /// - $f(-0.0,x,m)=0.0$ if $x<0$
3832 ///
3833 /// If you want to specify an output precision, consider using
3834 /// [`Float::div_rational_prec_round_ref_val`] instead. If you know you'll be using the
3835 /// `Nearest` rounding mode, consider using `/` instead.
3836 ///
3837 /// # Worst-case complexity
3838 /// $T(n) = O(n \log n \log\log n)$
3839 ///
3840 /// $M(n) = O(n \log n)$
3841 ///
3842 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3843 /// other.significant_bits())`.
3844 ///
3845 /// # Panics
3846 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
3847 /// represent the output.
3848 ///
3849 /// # Examples
3850 /// ```
3851 /// use core::f64::consts::PI;
3852 /// use malachite_base::rounding_modes::RoundingMode::*;
3853 /// use malachite_float::Float;
3854 /// use malachite_q::Rational;
3855 /// use std::cmp::Ordering::*;
3856 ///
3857 /// let (quotient, o) =
3858 /// Float::from(PI).div_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Floor);
3859 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3860 /// assert_eq!(o, Less);
3861 ///
3862 /// let (quotient, o) =
3863 /// Float::from(PI).div_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Ceiling);
3864 /// assert_eq!(quotient.to_string(), "9.42477796076939");
3865 /// assert_eq!(o, Greater);
3866 ///
3867 /// let (quotient, o) =
3868 /// Float::from(PI).div_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Nearest);
3869 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3870 /// assert_eq!(o, Less);
3871 /// ```
3872 #[inline]
3873 pub fn div_rational_round_ref_val(
3874 &self,
3875 other: Rational,
3876 rm: RoundingMode,
3877 ) -> (Self, Ordering) {
3878 let prec = self.significant_bits();
3879 self.div_rational_prec_round_ref_val(other, prec, rm)
3880 }
3881
3882 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
3883 /// The [`Float`] and the [`Rational`] are both are taken by reference. An [`Ordering`] is also
3884 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3885 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3886 /// function returns a `NaN` it also returns `Equal`.
3887 ///
3888 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
3889 /// for a description of the possible rounding modes.
3890 ///
3891 /// $$
3892 /// f(x,y,m) = x/y+\varepsilon.
3893 /// $$
3894 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3895 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3896 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3897 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3898 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3899 ///
3900 /// If the output has a precision, it is the precision of the [`Float`] input.
3901 ///
3902 /// Special cases:
3903 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
3904 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
3905 /// - $f(\infty,x,m)=-\infty$ if $x<0$
3906 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
3907 /// - $f(-\infty,x,m)=\infty$ if $x<0$
3908 /// - $f(0.0,x,m)=0.0$ if $x>0$
3909 /// - $f(0.0,x,m)=-0.0$ if $x<0$
3910 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
3911 /// - $f(-0.0,x,m)=0.0$ if $x<0$
3912 ///
3913 /// If you want to specify an output precision, consider using
3914 /// [`Float::div_rational_prec_round_ref_ref`] instead. If you know you'll be using the
3915 /// `Nearest` rounding mode, consider using `/` instead.
3916 ///
3917 /// # Worst-case complexity
3918 /// $T(n) = O(n \log n \log\log n)$
3919 ///
3920 /// $M(n) = O(n \log n)$
3921 ///
3922 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3923 /// other.significant_bits())`.
3924 ///
3925 /// # Panics
3926 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
3927 /// represent the output.
3928 ///
3929 /// # Examples
3930 /// ```
3931 /// use core::f64::consts::PI;
3932 /// use malachite_base::rounding_modes::RoundingMode::*;
3933 /// use malachite_float::Float;
3934 /// use malachite_q::Rational;
3935 /// use std::cmp::Ordering::*;
3936 ///
3937 /// let (quotient, o) =
3938 /// Float::from(PI).div_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Floor);
3939 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3940 /// assert_eq!(o, Less);
3941 ///
3942 /// let (quotient, o) =
3943 /// Float::from(PI).div_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Ceiling);
3944 /// assert_eq!(quotient.to_string(), "9.42477796076939");
3945 /// assert_eq!(o, Greater);
3946 ///
3947 /// let (quotient, o) =
3948 /// Float::from(PI).div_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Nearest);
3949 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3950 /// assert_eq!(o, Less);
3951 /// ```
3952 #[inline]
3953 pub fn div_rational_round_ref_ref(
3954 &self,
3955 other: &Rational,
3956 rm: RoundingMode,
3957 ) -> (Self, Ordering) {
3958 let prec = self.significant_bits();
3959 self.div_rational_prec_round_ref_ref(other, prec, rm)
3960 }
3961
3962 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the specified
3963 /// precision and with the specified rounding mode. The [`Rational`] is taken by value. An
3964 /// [`Ordering`] is returned, indicating whether the rounded quotient is less than, equal to, or
3965 /// greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3966 /// whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
3967 ///
3968 /// See [`RoundingMode`] for a description of the possible rounding modes.
3969 ///
3970 /// $$
3971 /// x \gets x/y+\varepsilon.
3972 /// $$
3973 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3974 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3975 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3976 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3977 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3978 ///
3979 /// If the output has a precision, it is `prec`.
3980 ///
3981 /// See the [`Float::div_rational_prec_round`] documentation for information on special cases.
3982 ///
3983 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_assign`]
3984 /// instead. If you know that your target precision is the precision of the [`Float`] input,
3985 /// consider using [`Float::div_rational_round_assign`] instead. If both of these things are
3986 /// true, consider using `/=` instead.
3987 ///
3988 /// # Worst-case complexity
3989 /// $T(n) = O(n \log n \log\log n)$
3990 ///
3991 /// $M(n) = O(n \log n)$
3992 ///
3993 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3994 /// other.significant_bits(), prec)`.
3995 ///
3996 /// # Panics
3997 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3998 ///
3999 /// # Examples
4000 /// ```
4001 /// use core::f64::consts::PI;
4002 /// use malachite_base::rounding_modes::RoundingMode::*;
4003 /// use malachite_float::Float;
4004 /// use malachite_q::Rational;
4005 /// use std::cmp::Ordering::*;
4006 ///
4007 /// let mut x = Float::from(PI);
4008 /// assert_eq!(
4009 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 5, Floor),
4010 /// Less
4011 /// );
4012 /// assert_eq!(x.to_string(), "9.0");
4013 ///
4014 /// let mut x = Float::from(PI);
4015 /// assert_eq!(
4016 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 5, Ceiling),
4017 /// Greater
4018 /// );
4019 /// assert_eq!(x.to_string(), "9.5");
4020 ///
4021 /// let mut x = Float::from(PI);
4022 /// assert_eq!(
4023 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 5, Nearest),
4024 /// Greater
4025 /// );
4026 /// assert_eq!(x.to_string(), "9.5");
4027 ///
4028 /// let mut x = Float::from(PI);
4029 /// assert_eq!(
4030 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 20, Floor),
4031 /// Less
4032 /// );
4033 /// assert_eq!(x.to_string(), "9.42477");
4034 ///
4035 /// let mut x = Float::from(PI);
4036 /// assert_eq!(
4037 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 20, Ceiling),
4038 /// Greater
4039 /// );
4040 /// assert_eq!(x.to_string(), "9.42479");
4041 ///
4042 /// let mut x = Float::from(PI);
4043 /// assert_eq!(
4044 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 20, Nearest),
4045 /// Less
4046 /// );
4047 /// assert_eq!(x.to_string(), "9.42477");
4048 /// ```
4049 #[inline]
4050 pub fn div_rational_prec_round_assign(
4051 &mut self,
4052 other: Rational,
4053 prec: u64,
4054 rm: RoundingMode,
4055 ) -> Ordering {
4056 if max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD {
4057 div_rational_prec_round_assign_naive(self, other, prec, rm)
4058 } else {
4059 div_rational_prec_round_assign_direct(self, other, prec, rm)
4060 }
4061 }
4062
4063 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the specified
4064 /// precision and with the specified rounding mode. The [`Rational`] is taken by reference. An
4065 /// [`Ordering`] is returned, indicating whether the rounded quotient is less than, equal to, or
4066 /// greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
4067 /// whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
4068 ///
4069 /// See [`RoundingMode`] for a description of the possible rounding modes.
4070 ///
4071 /// $$
4072 /// x \gets x/y+\varepsilon.
4073 /// $$
4074 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4075 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4076 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4077 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4078 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4079 ///
4080 /// If the output has a precision, it is `prec`.
4081 ///
4082 /// See the [`Float::div_rational_prec_round`] documentation for information on special cases.
4083 ///
4084 /// If you know you'll be using `Nearest`, consider using
4085 /// [`Float::div_rational_prec_assign_ref`] instead. If you know that your target precision is
4086 /// the precision of the [`Float`] input, consider using
4087 /// [`Float::div_rational_round_assign_ref`] instead. If both of these things are true, consider
4088 /// using `/=` instead.
4089 ///
4090 /// # Worst-case complexity
4091 /// $T(n) = O(n \log n \log\log n)$
4092 ///
4093 /// $M(n) = O(n \log n)$
4094 ///
4095 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4096 /// other.significant_bits(), prec)`.
4097 ///
4098 /// # Panics
4099 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4100 ///
4101 /// # Examples
4102 /// ```
4103 /// use core::f64::consts::PI;
4104 /// use malachite_base::rounding_modes::RoundingMode::*;
4105 /// use malachite_float::Float;
4106 /// use malachite_q::Rational;
4107 /// use std::cmp::Ordering::*;
4108 ///
4109 /// let mut x = Float::from(PI);
4110 /// assert_eq!(
4111 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 5, Floor),
4112 /// Less
4113 /// );
4114 /// assert_eq!(x.to_string(), "9.0");
4115 ///
4116 /// let mut x = Float::from(PI);
4117 /// assert_eq!(
4118 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 5, Ceiling),
4119 /// Greater
4120 /// );
4121 /// assert_eq!(x.to_string(), "9.5");
4122 ///
4123 /// let mut x = Float::from(PI);
4124 /// assert_eq!(
4125 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 5, Nearest),
4126 /// Greater
4127 /// );
4128 /// assert_eq!(x.to_string(), "9.5");
4129 ///
4130 /// let mut x = Float::from(PI);
4131 /// assert_eq!(
4132 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 20, Floor),
4133 /// Less
4134 /// );
4135 /// assert_eq!(x.to_string(), "9.42477");
4136 ///
4137 /// let mut x = Float::from(PI);
4138 /// assert_eq!(
4139 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 20, Ceiling),
4140 /// Greater
4141 /// );
4142 /// assert_eq!(x.to_string(), "9.42479");
4143 ///
4144 /// let mut x = Float::from(PI);
4145 /// assert_eq!(
4146 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 20, Nearest),
4147 /// Less
4148 /// );
4149 /// assert_eq!(x.to_string(), "9.42477");
4150 /// ```
4151 #[inline]
4152 pub fn div_rational_prec_round_assign_ref(
4153 &mut self,
4154 other: &Rational,
4155 prec: u64,
4156 rm: RoundingMode,
4157 ) -> Ordering {
4158 if max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD {
4159 div_rational_prec_round_assign_naive_ref(self, other, prec, rm)
4160 } else {
4161 div_rational_prec_round_assign_direct_ref(self, other, prec, rm)
4162 }
4163 }
4164
4165 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the nearest value of
4166 /// the specified precision. The [`Rational`] is taken by value. An [`Ordering`] is returned,
4167 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
4168 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
4169 /// the [`Float`] to `NaN` it also returns `Equal`.
4170 ///
4171 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4172 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4173 /// description of the `Nearest` rounding mode.
4174 ///
4175 /// $$
4176 /// x \gets x/y+\varepsilon.
4177 /// $$
4178 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4179 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4180 ///
4181 /// If the output has a precision, it is `prec`.
4182 ///
4183 /// See the [`Float::div_rational_prec`] documentation for information on special cases.
4184 ///
4185 /// If you want to use a rounding mode other than `Nearest`, consider using
4186 /// [`Float::div_rational_prec_round_assign`] instead. If you know that your target precision is
4187 /// the maximum of the precisions of the two inputs, consider using `/=` instead.
4188 ///
4189 /// # Worst-case complexity
4190 /// $T(n) = O(n \log n \log\log n)$
4191 ///
4192 /// $M(n) = O(n \log n)$
4193 ///
4194 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4195 /// other.significant_bits(), prec)`.
4196 ///
4197 /// # Examples
4198 /// ```
4199 /// use core::f64::consts::PI;
4200 /// use malachite_base::num::conversion::traits::ExactFrom;
4201 /// use malachite_float::Float;
4202 /// use malachite_q::Rational;
4203 /// use std::cmp::Ordering::*;
4204 ///
4205 /// let mut x = Float::from(PI);
4206 /// assert_eq!(
4207 /// x.div_rational_prec_assign(Rational::exact_from(1.5), 5),
4208 /// Greater
4209 /// );
4210 /// assert_eq!(x.to_string(), "2.1");
4211 ///
4212 /// let mut x = Float::from(PI);
4213 /// assert_eq!(
4214 /// x.div_rational_prec_assign(Rational::exact_from(1.5), 20),
4215 /// Less
4216 /// );
4217 /// assert_eq!(x.to_string(), "2.094395");
4218 /// ```
4219 #[inline]
4220 pub fn div_rational_prec_assign(&mut self, other: Rational, prec: u64) -> Ordering {
4221 self.div_rational_prec_round_assign(other, prec, Nearest)
4222 }
4223
4224 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the nearest value of
4225 /// the specified precision. The [`Rational`] is taken by reference. An [`Ordering`] is
4226 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4227 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4228 /// function sets the [`Float`] to `NaN` it also returns `Equal`.
4229 ///
4230 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4231 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4232 /// description of the `Nearest` rounding mode.
4233 ///
4234 /// $$
4235 /// x \gets x/y+\varepsilon.
4236 /// $$
4237 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4238 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4239 ///
4240 /// If the output has a precision, it is `prec`.
4241 ///
4242 /// See the [`Float::div_rational_prec`] documentation for information on special cases.
4243 ///
4244 /// If you want to use a rounding mode other than `Nearest`, consider using
4245 /// [`Float::div_rational_prec_round_assign`] instead. If you know that your target precision is
4246 /// the maximum of the precisions of the two inputs, consider using `/=` instead.
4247 ///
4248 /// # Worst-case complexity
4249 /// $T(n) = O(n \log n \log\log n)$
4250 ///
4251 /// $M(n) = O(n \log n)$
4252 ///
4253 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4254 /// other.significant_bits(), prec)`.
4255 ///
4256 /// # Examples
4257 /// ```
4258 /// use core::f64::consts::PI;
4259 /// use malachite_base::num::conversion::traits::ExactFrom;
4260 /// use malachite_float::Float;
4261 /// use malachite_q::Rational;
4262 /// use std::cmp::Ordering::*;
4263 ///
4264 /// let mut x = Float::from(PI);
4265 /// assert_eq!(
4266 /// x.div_rational_prec_assign_ref(&Rational::exact_from(1.5), 5),
4267 /// Greater
4268 /// );
4269 /// assert_eq!(x.to_string(), "2.1");
4270 ///
4271 /// let mut x = Float::from(PI);
4272 /// assert_eq!(
4273 /// x.div_rational_prec_assign_ref(&Rational::exact_from(1.5), 20),
4274 /// Less
4275 /// );
4276 /// assert_eq!(x.to_string(), "2.094395");
4277 /// ```
4278 #[inline]
4279 pub fn div_rational_prec_assign_ref(&mut self, other: &Rational, prec: u64) -> Ordering {
4280 self.div_rational_prec_round_assign_ref(other, prec, Nearest)
4281 }
4282
4283 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result with the specified
4284 /// rounding mode. The [`Rational`] is taken by value. An [`Ordering`] is returned, indicating
4285 /// whether the rounded quotient is less than, equal to, or greater than the exact quotient.
4286 /// Although `NaN`s are not comparable to any [`Float`], whenever this function sets the
4287 /// [`Float`] to `NaN` it also returns `Equal`.
4288 ///
4289 /// The precision of the output is the precision of the input [`Float`]. See [`RoundingMode`]
4290 /// for a description of the possible rounding modes.
4291 ///
4292 /// $$
4293 /// x \gets x/y+\varepsilon.
4294 /// $$
4295 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4296 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4297 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4298 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4299 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4300 ///
4301 /// If the output has a precision, it is the precision of the input [`Float`].
4302 ///
4303 /// See the [`Float::div_rational_round`] documentation for information on special cases.
4304 ///
4305 /// If you want to specify an output precision, consider using
4306 /// [`Float::div_rational_prec_round_assign`] instead. If you know you'll be using the `Nearest`
4307 /// rounding mode, consider using `/=` instead.
4308 ///
4309 /// # Worst-case complexity
4310 /// $T(n) = O(n \log n \log\log n)$
4311 ///
4312 /// $M(n) = O(n \log n)$
4313 ///
4314 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4315 /// other.significant_bits())`.
4316 ///
4317 /// # Panics
4318 /// Panics if `rm` is `Exact` but the precision of the input [`Float`] is not high enough to
4319 /// represent the output.
4320 ///
4321 /// # Examples
4322 /// ```
4323 /// use core::f64::consts::PI;
4324 /// use malachite_base::rounding_modes::RoundingMode::*;
4325 /// use malachite_float::Float;
4326 /// use malachite_q::Rational;
4327 /// use std::cmp::Ordering::*;
4328 ///
4329 /// let mut x = Float::from(PI);
4330 /// assert_eq!(
4331 /// x.div_rational_round_assign(Rational::from_unsigneds(1u8, 3), Floor),
4332 /// Less
4333 /// );
4334 /// assert_eq!(x.to_string(), "9.42477796076938");
4335 ///
4336 /// let mut x = Float::from(PI);
4337 /// assert_eq!(
4338 /// x.div_rational_round_assign(Rational::from_unsigneds(1u8, 3), Ceiling),
4339 /// Greater
4340 /// );
4341 /// assert_eq!(x.to_string(), "9.42477796076939");
4342 ///
4343 /// let mut x = Float::from(PI);
4344 /// assert_eq!(
4345 /// x.div_rational_round_assign(Rational::from_unsigneds(1u8, 3), Nearest),
4346 /// Less
4347 /// );
4348 /// assert_eq!(x.to_string(), "9.42477796076938");
4349 /// ```
4350 #[inline]
4351 pub fn div_rational_round_assign(&mut self, other: Rational, rm: RoundingMode) -> Ordering {
4352 let prec = self.significant_bits();
4353 self.div_rational_prec_round_assign(other, prec, rm)
4354 }
4355
4356 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result with the specified
4357 /// rounding mode. The [`Rational`] is taken by reference. An [`Ordering`] is returned,
4358 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
4359 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
4360 /// the [`Float`] to `NaN` it also returns `Equal`.
4361 ///
4362 /// The precision of the output is the precision of the input [`Float`]. See [`RoundingMode`]
4363 /// for a description of the possible rounding modes.
4364 ///
4365 /// $$
4366 /// x \gets x/y+\varepsilon.
4367 /// $$
4368 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4369 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4370 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4371 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4372 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4373 ///
4374 /// If the output has a precision, it is the precision of the input [`Float`].
4375 ///
4376 /// See the [`Float::div_rational_round`] documentation for information on special cases.
4377 ///
4378 /// If you want to specify an output precision, consider using
4379 /// [`Float::div_rational_prec_round_assign`] instead. If you know you'll be using the `Nearest`
4380 /// rounding mode, consider using `/=` instead.
4381 ///
4382 /// # Worst-case complexity
4383 /// $T(n) = O(n \log n \log\log n)$
4384 ///
4385 /// $M(n) = O(n \log n)$
4386 ///
4387 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4388 /// other.significant_bits())`.
4389 ///
4390 /// # Panics
4391 /// Panics if `rm` is `Exact` but the precision of the input [`Float`] is not high enough to
4392 /// represent the output.
4393 ///
4394 /// # Examples
4395 /// ```
4396 /// use core::f64::consts::PI;
4397 /// use malachite_base::rounding_modes::RoundingMode::*;
4398 /// use malachite_float::Float;
4399 /// use malachite_q::Rational;
4400 /// use std::cmp::Ordering::*;
4401 ///
4402 /// let mut x = Float::from(PI);
4403 /// assert_eq!(
4404 /// x.div_rational_round_assign_ref(&Rational::from_unsigneds(1u8, 3), Floor),
4405 /// Less
4406 /// );
4407 /// assert_eq!(x.to_string(), "9.42477796076938");
4408 ///
4409 /// let mut x = Float::from(PI);
4410 /// assert_eq!(
4411 /// x.div_rational_round_assign_ref(&Rational::from_unsigneds(1u8, 3), Ceiling),
4412 /// Greater
4413 /// );
4414 /// assert_eq!(x.to_string(), "9.42477796076939");
4415 ///
4416 /// let mut x = Float::from(PI);
4417 /// assert_eq!(
4418 /// x.div_rational_round_assign_ref(&Rational::from_unsigneds(1u8, 3), Nearest),
4419 /// Less
4420 /// );
4421 /// assert_eq!(x.to_string(), "9.42477796076938");
4422 /// ```
4423 #[inline]
4424 pub fn div_rational_round_assign_ref(
4425 &mut self,
4426 other: &Rational,
4427 rm: RoundingMode,
4428 ) -> Ordering {
4429 let prec = self.significant_bits();
4430 self.div_rational_prec_round_assign_ref(other, prec, rm)
4431 }
4432
4433 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
4434 /// with the specified rounding mode. The [`Rational`] and the [`Float`] are both taken by
4435 /// value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
4436 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
4437 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4438 ///
4439 /// See [`RoundingMode`] for a description of the possible rounding modes.
4440 ///
4441 /// $$
4442 /// f(x,y,p,m) = x/y+\varepsilon.
4443 /// $$
4444 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4445 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4446 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4447 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4448 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4449 ///
4450 /// If the output has a precision, it is `prec`.
4451 ///
4452 /// Special cases:
4453 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
4454 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
4455 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4456 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
4457 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
4458 /// - $f(0,x,p,m)=0.0$ if $x>0$
4459 /// - $f(0,x,p,m)=-0.0$ if $x<0$
4460 ///
4461 /// If you know you'll be using `Nearest`, consider using [`Float::rational_div_float_prec`]
4462 /// instead. If you know that your target precision is the precision of the [`Float`] input,
4463 /// consider using [`Float::rational_div_float_round`] instead. If both of these things are
4464 /// true, consider using `/` instead.
4465 ///
4466 /// # Worst-case complexity
4467 /// $T(n) = O(n \log n \log\log n)$
4468 ///
4469 /// $M(n) = O(n \log n)$
4470 ///
4471 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4472 /// y.significant_bits(), prec)`.
4473 ///
4474 /// # Panics
4475 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4476 ///
4477 /// # Examples
4478 /// ```
4479 /// use core::f64::consts::PI;
4480 /// use malachite_base::rounding_modes::RoundingMode::*;
4481 /// use malachite_float::Float;
4482 /// use malachite_q::Rational;
4483 /// use std::cmp::Ordering::*;
4484 ///
4485 /// let (quotient, o) =
4486 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 5, Floor);
4487 /// assert_eq!(quotient.to_string(), "0.94");
4488 /// assert_eq!(o, Less);
4489 ///
4490 /// let (quotient, o) =
4491 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 5, Ceiling);
4492 /// assert_eq!(quotient.to_string(), "0.97");
4493 /// assert_eq!(o, Greater);
4494 ///
4495 /// let (quotient, o) =
4496 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 5, Nearest);
4497 /// assert_eq!(quotient.to_string(), "0.97");
4498 /// assert_eq!(o, Greater);
4499 ///
4500 /// let (quotient, o) =
4501 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 20, Floor);
4502 /// assert_eq!(quotient.to_string(), "0.954929");
4503 /// assert_eq!(o, Less);
4504 ///
4505 /// let (quotient, o) =
4506 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 20, Ceiling);
4507 /// assert_eq!(quotient.to_string(), "0.95493");
4508 /// assert_eq!(o, Greater);
4509 ///
4510 /// let (quotient, o) =
4511 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 20, Nearest);
4512 /// assert_eq!(quotient.to_string(), "0.954929");
4513 /// assert_eq!(o, Less);
4514 /// ```
4515 #[inline]
4516 pub fn rational_div_float_prec_round(
4517 x: Rational,
4518 y: Self,
4519 prec: u64,
4520 rm: RoundingMode,
4521 ) -> (Self, Ordering) {
4522 if max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
4523 rational_div_float_prec_round_naive(x, y, prec, rm)
4524 } else {
4525 rational_div_float_prec_round_direct(x, y, prec, rm)
4526 }
4527 }
4528
4529 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
4530 /// with the specified rounding mode. The [`Rational`] is taken by value and the [`Float`] by
4531 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
4532 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
4533 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4534 ///
4535 /// See [`RoundingMode`] for a description of the possible rounding modes.
4536 ///
4537 /// $$
4538 /// f(x,y,p,m) = x/y+\varepsilon.
4539 /// $$
4540 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4541 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4542 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4543 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4544 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4545 ///
4546 /// If the output has a precision, it is `prec`.
4547 ///
4548 /// Special cases:
4549 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
4550 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
4551 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4552 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
4553 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
4554 /// - $f(0,x,p,m)=0.0$ if $x>0$
4555 /// - $f(0,x,p,m)=-0.0$ if $x<0$
4556 ///
4557 /// If you know you'll be using `Nearest`, consider using
4558 /// [`Float::rational_div_float_prec_val_ref`] instead. If you know that your target precision
4559 /// is the precision of the [`Float`] input, consider using
4560 /// [`Float::rational_div_float_round_val_ref`] instead. If both of these things are true,
4561 /// consider using `/` instead.
4562 ///
4563 /// # Worst-case complexity
4564 /// $T(n) = O(n \log n \log\log n)$
4565 ///
4566 /// $M(n) = O(n \log n)$
4567 ///
4568 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4569 /// y.significant_bits(), prec)`.
4570 ///
4571 /// # Panics
4572 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4573 ///
4574 /// # Examples
4575 /// ```
4576 /// use core::f64::consts::PI;
4577 /// use malachite_base::rounding_modes::RoundingMode::*;
4578 /// use malachite_float::Float;
4579 /// use malachite_q::Rational;
4580 /// use std::cmp::Ordering::*;
4581 ///
4582 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4583 /// Rational::from(3),
4584 /// &Float::from(PI),
4585 /// 5,
4586 /// Floor,
4587 /// );
4588 /// assert_eq!(quotient.to_string(), "0.94");
4589 /// assert_eq!(o, Less);
4590 ///
4591 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4592 /// Rational::from(3),
4593 /// &Float::from(PI),
4594 /// 5,
4595 /// Ceiling,
4596 /// );
4597 /// assert_eq!(quotient.to_string(), "0.97");
4598 /// assert_eq!(o, Greater);
4599 ///
4600 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4601 /// Rational::from(3),
4602 /// &Float::from(PI),
4603 /// 5,
4604 /// Nearest,
4605 /// );
4606 /// assert_eq!(quotient.to_string(), "0.97");
4607 /// assert_eq!(o, Greater);
4608 ///
4609 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4610 /// Rational::from(3),
4611 /// &Float::from(PI),
4612 /// 20,
4613 /// Floor,
4614 /// );
4615 /// assert_eq!(quotient.to_string(), "0.954929");
4616 /// assert_eq!(o, Less);
4617 ///
4618 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4619 /// Rational::from(3),
4620 /// &Float::from(PI),
4621 /// 20,
4622 /// Ceiling,
4623 /// );
4624 /// assert_eq!(quotient.to_string(), "0.95493");
4625 /// assert_eq!(o, Greater);
4626 ///
4627 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4628 /// Rational::from(3),
4629 /// &Float::from(PI),
4630 /// 20,
4631 /// Nearest,
4632 /// );
4633 /// assert_eq!(quotient.to_string(), "0.954929");
4634 /// assert_eq!(o, Less);
4635 /// ```
4636 #[inline]
4637 pub fn rational_div_float_prec_round_val_ref(
4638 x: Rational,
4639 y: &Self,
4640 prec: u64,
4641 rm: RoundingMode,
4642 ) -> (Self, Ordering) {
4643 if max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
4644 rational_div_float_prec_round_naive_val_ref(x, y, prec, rm)
4645 } else {
4646 rational_div_float_prec_round_direct_val_ref(x, y, prec, rm)
4647 }
4648 }
4649
4650 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
4651 /// with the specified rounding mode. The [`Rational`] is taken by reference and the [`Float`]
4652 /// by value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
4653 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
4654 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4655 ///
4656 /// See [`RoundingMode`] for a description of the possible rounding modes.
4657 ///
4658 /// $$
4659 /// f(x,y,p,m) = x/y+\varepsilon.
4660 /// $$
4661 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4662 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4663 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4664 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4665 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4666 ///
4667 /// If the output has a precision, it is `prec`.
4668 ///
4669 /// Special cases:
4670 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
4671 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
4672 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4673 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
4674 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
4675 /// - $f(0,x,p,m)=0.0$ if $x>0$
4676 /// - $f(0,x,p,m)=-0.0$ if $x<0$
4677 ///
4678 /// If you know you'll be using `Nearest`, consider using
4679 /// [`Float::rational_div_float_prec_ref_val`] instead. If you know that your target precision
4680 /// is the precision of the [`Float`] input, consider using
4681 /// [`Float::rational_div_float_round_ref_val`] instead. If both of these things are true,
4682 /// consider using `/` instead.
4683 ///
4684 /// # Worst-case complexity
4685 /// $T(n) = O(n \log n \log\log n)$
4686 ///
4687 /// $M(n) = O(n \log n)$
4688 ///
4689 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4690 /// y.significant_bits(), prec)`.
4691 ///
4692 /// # Panics
4693 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4694 ///
4695 /// # Examples
4696 /// ```
4697 /// use core::f64::consts::PI;
4698 /// use malachite_base::rounding_modes::RoundingMode::*;
4699 /// use malachite_float::Float;
4700 /// use malachite_q::Rational;
4701 /// use std::cmp::Ordering::*;
4702 ///
4703 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4704 /// &Rational::from(3),
4705 /// Float::from(PI),
4706 /// 5,
4707 /// Floor,
4708 /// );
4709 /// assert_eq!(quotient.to_string(), "0.94");
4710 /// assert_eq!(o, Less);
4711 ///
4712 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4713 /// &Rational::from(3),
4714 /// Float::from(PI),
4715 /// 5,
4716 /// Ceiling,
4717 /// );
4718 /// assert_eq!(quotient.to_string(), "0.97");
4719 /// assert_eq!(o, Greater);
4720 ///
4721 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4722 /// &Rational::from(3),
4723 /// Float::from(PI),
4724 /// 5,
4725 /// Nearest,
4726 /// );
4727 /// assert_eq!(quotient.to_string(), "0.97");
4728 /// assert_eq!(o, Greater);
4729 ///
4730 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4731 /// &Rational::from(3),
4732 /// Float::from(PI),
4733 /// 20,
4734 /// Floor,
4735 /// );
4736 /// assert_eq!(quotient.to_string(), "0.954929");
4737 /// assert_eq!(o, Less);
4738 ///
4739 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4740 /// &Rational::from(3),
4741 /// Float::from(PI),
4742 /// 20,
4743 /// Ceiling,
4744 /// );
4745 /// assert_eq!(quotient.to_string(), "0.95493");
4746 /// assert_eq!(o, Greater);
4747 ///
4748 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4749 /// &Rational::from(3),
4750 /// Float::from(PI),
4751 /// 20,
4752 /// Nearest,
4753 /// );
4754 /// assert_eq!(quotient.to_string(), "0.954929");
4755 /// assert_eq!(o, Less);
4756 /// ```
4757 #[inline]
4758 pub fn rational_div_float_prec_round_ref_val(
4759 x: &Rational,
4760 y: Self,
4761 prec: u64,
4762 rm: RoundingMode,
4763 ) -> (Self, Ordering) {
4764 if max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
4765 rational_div_float_prec_round_naive_ref_val(x, y, prec, rm)
4766 } else {
4767 rational_div_float_prec_round_direct_ref_val(x, y, prec, rm)
4768 }
4769 }
4770
4771 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
4772 /// with the specified rounding mode. The [`Rational`] and the [`Float`] are both taken by
4773 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
4774 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
4775 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4776 ///
4777 /// See [`RoundingMode`] for a description of the possible rounding modes.
4778 ///
4779 /// $$
4780 /// f(x,y,p,m) = x/y+\varepsilon.
4781 /// $$
4782 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4783 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4784 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4785 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4786 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4787 ///
4788 /// If the output has a precision, it is `prec`.
4789 ///
4790 /// Special cases:
4791 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
4792 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
4793 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4794 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
4795 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
4796 /// - $f(0,x,p,m)=0.0$ if $x>0$
4797 /// - $f(0,x,p,m)=-0.0$ if $x<0$
4798 ///
4799 /// If you know you'll be using `Nearest`, consider using
4800 /// [`Float::rational_div_float_prec_ref_ref`] instead. If you know that your target precision
4801 /// is the precision of the [`Float`] input, consider using
4802 /// [`Float::rational_div_float_round_ref_ref`] instead. If both of these things are true,
4803 /// consider using `/` instead.
4804 ///
4805 /// # Worst-case complexity
4806 /// $T(n) = O(n \log n \log\log n)$
4807 ///
4808 /// $M(n) = O(n \log n)$
4809 ///
4810 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4811 /// y.significant_bits(), prec)`.
4812 ///
4813 /// # Panics
4814 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4815 ///
4816 /// # Examples
4817 /// ```
4818 /// use core::f64::consts::PI;
4819 /// use malachite_base::rounding_modes::RoundingMode::*;
4820 /// use malachite_float::Float;
4821 /// use malachite_q::Rational;
4822 /// use std::cmp::Ordering::*;
4823 ///
4824 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4825 /// &Rational::from(3),
4826 /// &Float::from(PI),
4827 /// 5,
4828 /// Floor,
4829 /// );
4830 /// assert_eq!(quotient.to_string(), "0.94");
4831 /// assert_eq!(o, Less);
4832 ///
4833 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4834 /// &Rational::from(3),
4835 /// &Float::from(PI),
4836 /// 5,
4837 /// Ceiling,
4838 /// );
4839 /// assert_eq!(quotient.to_string(), "0.97");
4840 /// assert_eq!(o, Greater);
4841 ///
4842 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4843 /// &Rational::from(3),
4844 /// &Float::from(PI),
4845 /// 5,
4846 /// Nearest,
4847 /// );
4848 /// assert_eq!(quotient.to_string(), "0.97");
4849 /// assert_eq!(o, Greater);
4850 ///
4851 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4852 /// &Rational::from(3),
4853 /// &Float::from(PI),
4854 /// 20,
4855 /// Floor,
4856 /// );
4857 /// assert_eq!(quotient.to_string(), "0.954929");
4858 /// assert_eq!(o, Less);
4859 ///
4860 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4861 /// &Rational::from(3),
4862 /// &Float::from(PI),
4863 /// 20,
4864 /// Ceiling,
4865 /// );
4866 /// assert_eq!(quotient.to_string(), "0.95493");
4867 /// assert_eq!(o, Greater);
4868 ///
4869 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4870 /// &Rational::from(3),
4871 /// &Float::from(PI),
4872 /// 20,
4873 /// Nearest,
4874 /// );
4875 /// assert_eq!(quotient.to_string(), "0.954929");
4876 /// assert_eq!(o, Less);
4877 /// ```
4878 #[inline]
4879 pub fn rational_div_float_prec_round_ref_ref(
4880 x: &Rational,
4881 y: &Self,
4882 prec: u64,
4883 rm: RoundingMode,
4884 ) -> (Self, Ordering) {
4885 if max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
4886 rational_div_float_prec_round_naive_ref_ref(x, y, prec, rm)
4887 } else {
4888 rational_div_float_prec_round_direct_ref_ref(x, y, prec, rm)
4889 }
4890 }
4891
4892 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
4893 /// specified precision. The [`Rational`] and the [`Float`] are both are taken by value. An
4894 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
4895 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
4896 /// whenever this function returns a `NaN` it also returns `Equal`.
4897 ///
4898 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4899 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4900 /// description of the `Nearest` rounding mode.
4901 ///
4902 /// $$
4903 /// f(x,y,p) = x/y+\varepsilon.
4904 /// $$
4905 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4906 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4907 ///
4908 /// If the output has a precision, it is `prec`.
4909 ///
4910 /// Special cases:
4911 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
4912 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
4913 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
4914 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
4915 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
4916 /// - $f(0,x,p)=0.0$ if $x>0$
4917 /// - $f(0,x,p)=-0.0$ if $x<0$
4918 ///
4919 /// If you want to use a rounding mode other than `Nearest`, consider using
4920 /// [`Float::rational_div_float_prec_round`] instead. If you know that your target precision is
4921 /// the precision of the [`Float`] input, consider using `/` instead.
4922 ///
4923 /// # Worst-case complexity
4924 /// $T(n) = O(n \log n \log\log n)$
4925 ///
4926 /// $M(n) = O(n \log n)$
4927 ///
4928 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4929 /// y.significant_bits(), prec)`.
4930 ///
4931 /// # Examples
4932 /// ```
4933 /// use core::f64::consts::PI;
4934 /// use malachite_float::Float;
4935 /// use malachite_q::Rational;
4936 /// use std::cmp::Ordering::*;
4937 ///
4938 /// let (quotient, o) = Float::rational_div_float_prec(Rational::from(3), Float::from(PI), 5);
4939 /// assert_eq!(quotient.to_string(), "0.97");
4940 /// assert_eq!(o, Greater);
4941 ///
4942 /// let (quotient, o) = Float::rational_div_float_prec(Rational::from(3), Float::from(PI), 20);
4943 /// assert_eq!(quotient.to_string(), "0.954929");
4944 /// assert_eq!(o, Less);
4945 /// ```
4946 #[inline]
4947 pub fn rational_div_float_prec(x: Rational, y: Self, prec: u64) -> (Self, Ordering) {
4948 Self::rational_div_float_prec_round(x, y, prec, Nearest)
4949 }
4950
4951 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
4952 /// specified precision. The [`Rational`] is taken by value and the [`Float`] by reference. An
4953 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
4954 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
4955 /// whenever this function returns a `NaN` it also returns `Equal`.
4956 ///
4957 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4958 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4959 /// description of the `Nearest` rounding mode.
4960 ///
4961 /// $$
4962 /// f(x,y,p) = x/y+\varepsilon.
4963 /// $$
4964 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4965 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4966 ///
4967 /// If the output has a precision, it is `prec`.
4968 ///
4969 /// Special cases:
4970 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
4971 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
4972 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
4973 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
4974 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
4975 /// - $f(0,x,p)=0.0$ if $x>0$
4976 /// - $f(0,x,p)=-0.0$ if $x<0$
4977 ///
4978 /// If you want to use a rounding mode other than `Nearest`, consider using
4979 /// [`Float::rational_div_float_prec_round_val_ref`] instead. If you know that your target
4980 /// precision is the precision of the [`Float`] input, consider using `/` instead.
4981 ///
4982 /// # Worst-case complexity
4983 /// $T(n) = O(n \log n \log\log n)$
4984 ///
4985 /// $M(n) = O(n \log n)$
4986 ///
4987 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4988 /// y.significant_bits(), prec)`.
4989 ///
4990 /// # Examples
4991 /// ```
4992 /// use core::f64::consts::PI;
4993 /// use malachite_float::Float;
4994 /// use malachite_q::Rational;
4995 /// use std::cmp::Ordering::*;
4996 ///
4997 /// let (quotient, o) =
4998 /// Float::rational_div_float_prec_val_ref(Rational::from(3), &Float::from(PI), 5);
4999 /// assert_eq!(quotient.to_string(), "0.97");
5000 /// assert_eq!(o, Greater);
5001 ///
5002 /// let (quotient, o) =
5003 /// Float::rational_div_float_prec_val_ref(Rational::from(3), &Float::from(PI), 20);
5004 /// assert_eq!(quotient.to_string(), "0.954929");
5005 /// assert_eq!(o, Less);
5006 /// ```
5007 #[inline]
5008 pub fn rational_div_float_prec_val_ref(x: Rational, y: &Self, prec: u64) -> (Self, Ordering) {
5009 Self::rational_div_float_prec_round_val_ref(x, y, prec, Nearest)
5010 }
5011
5012 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
5013 /// specified precision. The [`Rational`] is taken by reference and the [`Float`] by value. An
5014 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
5015 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
5016 /// whenever this function returns a `NaN` it also returns `Equal`.
5017 ///
5018 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
5019 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
5020 /// description of the `Nearest` rounding mode.
5021 ///
5022 /// $$
5023 /// f(x,y,p) = x/y+\varepsilon.
5024 /// $$
5025 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5026 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5027 ///
5028 /// If the output has a precision, it is `prec`.
5029 ///
5030 /// Special cases:
5031 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
5032 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
5033 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
5034 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
5035 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
5036 /// - $f(0,x,p)=0.0$ if $x>0$
5037 /// - $f(0,x,p)=-0.0$ if $x<0$
5038 ///
5039 /// If you want to use a rounding mode other than `Nearest`, consider using
5040 /// [`Float::rational_div_float_prec_round_ref_val`] instead. If you know that your target
5041 /// precision is the precision of the [`Float`] input, consider using `/` instead.
5042 ///
5043 /// # Worst-case complexity
5044 /// $T(n) = O(n \log n \log\log n)$
5045 ///
5046 /// $M(n) = O(n \log n)$
5047 ///
5048 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5049 /// y.significant_bits(), prec)`.
5050 ///
5051 /// # Examples
5052 /// ```
5053 /// use core::f64::consts::PI;
5054 /// use malachite_float::Float;
5055 /// use malachite_q::Rational;
5056 /// use std::cmp::Ordering::*;
5057 ///
5058 /// let (quotient, o) =
5059 /// Float::rational_div_float_prec_ref_val(&Rational::from(3), Float::from(PI), 5);
5060 /// assert_eq!(quotient.to_string(), "0.97");
5061 /// assert_eq!(o, Greater);
5062 ///
5063 /// let (quotient, o) =
5064 /// Float::rational_div_float_prec_ref_val(&Rational::from(3), Float::from(PI), 20);
5065 /// assert_eq!(quotient.to_string(), "0.954929");
5066 /// assert_eq!(o, Less);
5067 /// ```
5068 #[inline]
5069 pub fn rational_div_float_prec_ref_val(x: &Rational, y: Self, prec: u64) -> (Self, Ordering) {
5070 Self::rational_div_float_prec_round_ref_val(x, y, prec, Nearest)
5071 }
5072
5073 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
5074 /// specified precision. The [`Rational`] and the [`Float`] are both are taken by reference. An
5075 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
5076 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
5077 /// whenever this function returns a `NaN` it also returns `Equal`.
5078 ///
5079 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
5080 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
5081 /// description of the `Nearest` rounding mode.
5082 ///
5083 /// $$
5084 /// f(x,y,p) = x/y+\varepsilon.
5085 /// $$
5086 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5087 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5088 ///
5089 /// If the output has a precision, it is `prec`.
5090 ///
5091 /// Special cases:
5092 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
5093 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
5094 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
5095 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
5096 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
5097 /// - $f(0,x,p)=0.0$ if $x>0$
5098 /// - $f(0,x,p)=-0.0$ if $x<0$
5099 ///
5100 /// If you want to use a rounding mode other than `Nearest`, consider using
5101 /// [`Float::rational_div_float_prec_round_ref_ref`] instead. If you know that your target
5102 /// precision is the precision of the [`Float`] input, consider using `/` instead.
5103 ///
5104 /// # Worst-case complexity
5105 /// $T(n) = O(n \log n \log\log n)$
5106 ///
5107 /// $M(n) = O(n \log n)$
5108 ///
5109 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5110 /// y.significant_bits(), prec)`.
5111 ///
5112 /// # Examples
5113 /// ```
5114 /// use core::f64::consts::PI;
5115 /// use malachite_float::Float;
5116 /// use malachite_q::Rational;
5117 /// use std::cmp::Ordering::*;
5118 ///
5119 /// let (quotient, o) =
5120 /// Float::rational_div_float_prec_ref_ref(&Rational::from(3), &Float::from(PI), 5);
5121 /// assert_eq!(quotient.to_string(), "0.97");
5122 /// assert_eq!(o, Greater);
5123 ///
5124 /// let (quotient, o) =
5125 /// Float::rational_div_float_prec_ref_ref(&Rational::from(3), &Float::from(PI), 20);
5126 /// assert_eq!(quotient.to_string(), "0.954929");
5127 /// assert_eq!(o, Less);
5128 /// ```
5129 #[inline]
5130 pub fn rational_div_float_prec_ref_ref(x: &Rational, y: &Self, prec: u64) -> (Self, Ordering) {
5131 Self::rational_div_float_prec_round_ref_ref(x, y, prec, Nearest)
5132 }
5133
5134 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
5135 /// The [`Rational`] and the [`Float`] are both are taken by value. An [`Ordering`] is also
5136 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
5137 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
5138 /// function returns a `NaN` it also returns `Equal`.
5139 ///
5140 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
5141 /// for a description of the possible rounding modes.
5142 ///
5143 /// $$
5144 /// f(x,y,m) = x/y+\varepsilon.
5145 /// $$
5146 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5147 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5148 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
5149 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
5150 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
5151 ///
5152 /// If the output has a precision, it is the precision of the [`Float`] input.
5153 ///
5154 /// Special cases:
5155 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
5156 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
5157 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
5158 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
5159 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
5160 /// - $f(0,x,m)=0.0$ if $x>0$
5161 /// - $f(0,x,m)=-0.0$ if $x<0$
5162 ///
5163 /// If you want to specify an output precision, consider using
5164 /// [`Float::rational_div_float_prec_round`] instead. If you know you'll be using the `Nearest`
5165 /// rounding mode, consider using `/` instead.
5166 ///
5167 /// # Worst-case complexity
5168 /// $T(n) = O(n \log n \log\log n)$
5169 ///
5170 /// $M(n) = O(n \log n)$
5171 ///
5172 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5173 /// y.significant_bits())`.
5174 ///
5175 /// # Panics
5176 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
5177 /// represent the output.
5178 ///
5179 /// # Examples
5180 /// ```
5181 /// use core::f64::consts::PI;
5182 /// use malachite_base::rounding_modes::RoundingMode::*;
5183 /// use malachite_float::Float;
5184 /// use malachite_q::Rational;
5185 /// use std::cmp::Ordering::*;
5186 ///
5187 /// let (quotient, o) =
5188 /// Float::rational_div_float_round(Rational::from(3), Float::from(PI), Floor);
5189 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
5190 /// assert_eq!(o, Less);
5191 ///
5192 /// let (quotient, o) =
5193 /// Float::rational_div_float_round(Rational::from(3), Float::from(PI), Ceiling);
5194 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
5195 /// assert_eq!(o, Greater);
5196 ///
5197 /// let (quotient, o) =
5198 /// Float::rational_div_float_round(Rational::from(3), Float::from(PI), Nearest);
5199 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
5200 /// assert_eq!(o, Greater);
5201 /// ```
5202 #[inline]
5203 pub fn rational_div_float_round(x: Rational, y: Self, rm: RoundingMode) -> (Self, Ordering) {
5204 let prec = y.significant_bits();
5205 Self::rational_div_float_prec_round(x, y, prec, rm)
5206 }
5207
5208 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
5209 /// The [`Rational`] is taken by value and the [`Float`] by reference. An [`Ordering`] is also
5210 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
5211 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
5212 /// function returns a `NaN` it also returns `Equal`.
5213 ///
5214 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
5215 /// for a description of the possible rounding modes.
5216 ///
5217 /// $$
5218 /// f(x,y,m) = x/y+\varepsilon.
5219 /// $$
5220 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5221 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5222 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
5223 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
5224 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
5225 ///
5226 /// If the output has a precision, it is the precision of the [`Float`] input.
5227 ///
5228 /// Special cases:
5229 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
5230 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
5231 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
5232 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
5233 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
5234 /// - $f(0,x,m)=0.0$ if $x>0$
5235 /// - $f(0,x,m)=-0.0$ if $x<0$
5236 ///
5237 /// If you want to specify an output precision, consider using
5238 /// [`Float::rational_div_float_prec_round_val_ref`] instead. If you know you'll be using the
5239 /// `Nearest` rounding mode, consider using `/` instead.
5240 ///
5241 /// # Worst-case complexity
5242 /// $T(n) = O(n \log n \log\log n)$
5243 ///
5244 /// $M(n) = O(n \log n)$
5245 ///
5246 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5247 /// y.significant_bits())`.
5248 ///
5249 /// # Panics
5250 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
5251 /// represent the output.
5252 ///
5253 /// # Examples
5254 /// ```
5255 /// use core::f64::consts::PI;
5256 /// use malachite_base::rounding_modes::RoundingMode::*;
5257 /// use malachite_float::Float;
5258 /// use malachite_q::Rational;
5259 /// use std::cmp::Ordering::*;
5260 ///
5261 /// let (quotient, o) =
5262 /// Float::rational_div_float_round_val_ref(Rational::from(3), &Float::from(PI), Floor);
5263 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
5264 /// assert_eq!(o, Less);
5265 ///
5266 /// let (quotient, o) =
5267 /// Float::rational_div_float_round_val_ref(Rational::from(3), &Float::from(PI), Ceiling);
5268 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
5269 /// assert_eq!(o, Greater);
5270 ///
5271 /// let (quotient, o) =
5272 /// Float::rational_div_float_round_val_ref(Rational::from(3), &Float::from(PI), Nearest);
5273 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
5274 /// assert_eq!(o, Greater);
5275 /// ```
5276 #[inline]
5277 pub fn rational_div_float_round_val_ref(
5278 x: Rational,
5279 y: &Self,
5280 rm: RoundingMode,
5281 ) -> (Self, Ordering) {
5282 let prec = y.significant_bits();
5283 Self::rational_div_float_prec_round_val_ref(x, y, prec, rm)
5284 }
5285
5286 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
5287 /// The [`Rational`] is taken by reference and the [`Float`] by value. An [`Ordering`] is also
5288 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
5289 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
5290 /// function returns a `NaN` it also returns `Equal`.
5291 ///
5292 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
5293 /// for a description of the possible rounding modes.
5294 ///
5295 /// $$
5296 /// f(x,y,m) = x/y+\varepsilon.
5297 /// $$
5298 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5299 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5300 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
5301 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
5302 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
5303 ///
5304 /// If the output has a precision, it is the precision of the [`Float`] input.
5305 ///
5306 /// Special cases:
5307 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
5308 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
5309 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
5310 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
5311 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
5312 /// - $f(0,x,m)=0.0$ if $x>0$
5313 /// - $f(0,x,m)=-0.0$ if $x<0$
5314 ///
5315 /// If you want to specify an output precision, consider using
5316 /// [`Float::rational_div_float_prec_round_ref_val`] instead. If you know you'll be using the
5317 /// `Nearest` rounding mode, consider using `/` instead.
5318 ///
5319 /// # Worst-case complexity
5320 /// $T(n) = O(n \log n \log\log n)$
5321 ///
5322 /// $M(n) = O(n \log n)$
5323 ///
5324 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5325 /// y.significant_bits())`.
5326 ///
5327 /// # Panics
5328 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
5329 /// represent the output.
5330 ///
5331 /// # Examples
5332 /// ```
5333 /// use core::f64::consts::PI;
5334 /// use malachite_base::rounding_modes::RoundingMode::*;
5335 /// use malachite_float::Float;
5336 /// use malachite_q::Rational;
5337 /// use std::cmp::Ordering::*;
5338 ///
5339 /// let (quotient, o) =
5340 /// Float::rational_div_float_round_ref_val(&Rational::from(3), Float::from(PI), Floor);
5341 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
5342 /// assert_eq!(o, Less);
5343 ///
5344 /// let (quotient, o) =
5345 /// Float::rational_div_float_round_ref_val(&Rational::from(3), Float::from(PI), Ceiling);
5346 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
5347 /// assert_eq!(o, Greater);
5348 ///
5349 /// let (quotient, o) =
5350 /// Float::rational_div_float_round_ref_val(&Rational::from(3), Float::from(PI), Nearest);
5351 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
5352 /// assert_eq!(o, Greater);
5353 /// ```
5354 #[inline]
5355 pub fn rational_div_float_round_ref_val(
5356 x: &Rational,
5357 y: Self,
5358 rm: RoundingMode,
5359 ) -> (Self, Ordering) {
5360 let prec = y.significant_bits();
5361 Self::rational_div_float_prec_round_ref_val(x, y, prec, rm)
5362 }
5363
5364 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
5365 /// The [`Rational`] and the [`Float`] are both are taken by reference. An [`Ordering`] is also
5366 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
5367 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
5368 /// function returns a `NaN` it also returns `Equal`.
5369 ///
5370 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
5371 /// for a description of the possible rounding modes.
5372 ///
5373 /// $$
5374 /// f(x,y,m) = x/y+\varepsilon.
5375 /// $$
5376 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5377 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5378 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
5379 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
5380 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
5381 ///
5382 /// If the output has a precision, it is the precision of the [`Float`] input.
5383 ///
5384 /// Special cases:
5385 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
5386 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
5387 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
5388 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
5389 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
5390 /// - $f(0,x,m)=0.0$ if $x>0$
5391 /// - $f(0,x,m)=-0.0$ if $x<0$
5392 ///
5393 /// If you want to specify an output precision, consider using
5394 /// [`Float::rational_div_float_prec_round_ref_ref`] instead. If you know you'll be using the
5395 /// `Nearest` rounding mode, consider using `/` instead.
5396 ///
5397 /// # Worst-case complexity
5398 /// $T(n) = O(n \log n \log\log n)$
5399 ///
5400 /// $M(n) = O(n \log n)$
5401 ///
5402 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5403 /// y.significant_bits())`.
5404 ///
5405 /// # Panics
5406 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
5407 /// represent the output.
5408 ///
5409 /// # Examples
5410 /// ```
5411 /// use core::f64::consts::PI;
5412 /// use malachite_base::rounding_modes::RoundingMode::*;
5413 /// use malachite_float::Float;
5414 /// use malachite_q::Rational;
5415 /// use std::cmp::Ordering::*;
5416 ///
5417 /// let (quotient, o) =
5418 /// Float::rational_div_float_round_ref_ref(&Rational::from(3), &Float::from(PI), Floor);
5419 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
5420 /// assert_eq!(o, Less);
5421 ///
5422 /// let (quotient, o) =
5423 /// Float::rational_div_float_round_ref_ref(&Rational::from(3), &Float::from(PI), Ceiling);
5424 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
5425 /// assert_eq!(o, Greater);
5426 ///
5427 /// let (quotient, o) =
5428 /// Float::rational_div_float_round_ref_ref(&Rational::from(3), &Float::from(PI), Nearest);
5429 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
5430 /// assert_eq!(o, Greater);
5431 /// ```
5432 #[inline]
5433 pub fn rational_div_float_round_ref_ref(
5434 x: &Rational,
5435 y: &Self,
5436 rm: RoundingMode,
5437 ) -> (Self, Ordering) {
5438 let prec = y.significant_bits();
5439 Self::rational_div_float_prec_round_ref_ref(x, y, prec, rm)
5440 }
5441}
5442
5443impl Div<Self> for Float {
5444 type Output = Self;
5445
5446 /// Divides two [`Float`]s, taking both by value.
5447 ///
5448 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5449 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5450 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5451 /// `Nearest` rounding mode.
5452 ///
5453 /// $$
5454 /// f(x,y) = x/y+\varepsilon.
5455 /// $$
5456 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5457 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5458 /// where $p$ is the maximum precision of the inputs.
5459 ///
5460 /// Special cases:
5461 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
5462 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
5463 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
5464 /// - $f(x,0.0)=\infty$ if $x>0.0$
5465 /// - $f(x,0.0)=-\infty$ if $x<0.0$
5466 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
5467 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
5468 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
5469 /// - $f(x,-0.0)=\infty$ if $x<0.0$
5470 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
5471 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
5472 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5473 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5474 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
5475 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
5476 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5477 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5478 ///
5479 /// Overflow and underflow:
5480 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
5481 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
5482 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5483 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5484 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
5485 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5486 ///
5487 /// If you want to use a rounding mode other than `Nearest`, consider using [`Float::div_prec`]
5488 /// instead. If you want to specify the output precision, consider using [`Float::div_round`].
5489 /// If you want both of these things, consider using [`Float::div_prec_round`].
5490 ///
5491 /// # Worst-case complexity
5492 /// $T(n) = O(n \log n \log\log n)$
5493 ///
5494 /// $M(n) = O(n \log n)$
5495 ///
5496 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5497 /// other.significant_bits())`.
5498 ///
5499 /// # Examples
5500 /// ```
5501 /// use malachite_base::num::basic::traits::{
5502 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5503 /// };
5504 /// use malachite_float::Float;
5505 ///
5506 /// assert!((Float::from(1.5) / Float::NAN).is_nan());
5507 /// assert_eq!(Float::from(1.5) / Float::ZERO, Float::INFINITY);
5508 /// assert_eq!(
5509 /// Float::from(1.5) / Float::NEGATIVE_ZERO,
5510 /// Float::NEGATIVE_INFINITY
5511 /// );
5512 /// assert_eq!(Float::from(-1.5) / Float::ZERO, Float::NEGATIVE_INFINITY);
5513 /// assert_eq!(Float::from(-1.5) / Float::NEGATIVE_ZERO, Float::INFINITY);
5514 /// assert!((Float::ZERO / Float::ZERO).is_nan());
5515 ///
5516 /// assert_eq!((Float::from(1.5) / Float::from(2.5)).to_string(), "0.6");
5517 /// assert_eq!((Float::from(1.5) / Float::from(-2.5)).to_string(), "-0.6");
5518 /// assert_eq!((Float::from(-1.5) / Float::from(2.5)).to_string(), "-0.6");
5519 /// assert_eq!((Float::from(-1.5) / Float::from(-2.5)).to_string(), "0.6");
5520 /// ```
5521 #[inline]
5522 fn div(self, other: Self) -> Self {
5523 let prec = max(self.significant_bits(), other.significant_bits());
5524 self.div_prec_round(other, prec, Nearest).0
5525 }
5526}
5527
5528impl Div<&Self> for Float {
5529 type Output = Self;
5530
5531 /// Divides two [`Float`]s, taking the first by value and the second by reference.
5532 ///
5533 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5534 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5535 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5536 /// `Nearest` rounding mode.
5537 ///
5538 /// $$
5539 /// f(x,y) = x/y+\varepsilon.
5540 /// $$
5541 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5542 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5543 /// where $p$ is the maximum precision of the inputs.
5544 ///
5545 /// Special cases:
5546 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
5547 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
5548 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
5549 /// - $f(x,0.0)=\infty$ if $x>0.0$
5550 /// - $f(x,0.0)=-\infty$ if $x<0.0$
5551 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
5552 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
5553 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
5554 /// - $f(x,-0.0)=\infty$ if $x<0.0$
5555 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
5556 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
5557 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5558 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5559 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
5560 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
5561 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5562 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5563 ///
5564 /// Overflow and underflow:
5565 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
5566 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
5567 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5568 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5569 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
5570 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5571 ///
5572 /// If you want to use a rounding mode other than `Nearest`, consider using
5573 /// [`Float::div_prec_val_ref`] instead. If you want to specify the output precision, consider
5574 /// using [`Float::div_round_val_ref`]. If you want both of these things, consider using
5575 /// [`Float::div_prec_round_val_ref`].
5576 ///
5577 /// # Worst-case complexity
5578 /// $T(n) = O(n \log n \log\log n)$
5579 ///
5580 /// $M(n) = O(n \log n)$
5581 ///
5582 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5583 /// other.significant_bits())`.
5584 ///
5585 /// # Examples
5586 /// ```
5587 /// use malachite_base::num::basic::traits::{
5588 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5589 /// };
5590 /// use malachite_float::Float;
5591 ///
5592 /// assert!((Float::from(1.5) / &Float::NAN).is_nan());
5593 /// assert_eq!(Float::from(1.5) / &Float::ZERO, Float::INFINITY);
5594 /// assert_eq!(
5595 /// Float::from(1.5) / &Float::NEGATIVE_ZERO,
5596 /// Float::NEGATIVE_INFINITY
5597 /// );
5598 /// assert_eq!(Float::from(-1.5) / &Float::ZERO, Float::NEGATIVE_INFINITY);
5599 /// assert_eq!(Float::from(-1.5) / &Float::NEGATIVE_ZERO, Float::INFINITY);
5600 /// assert!((Float::ZERO / &Float::ZERO).is_nan());
5601 ///
5602 /// assert_eq!((Float::from(1.5) / &Float::from(2.5)).to_string(), "0.6");
5603 /// assert_eq!((Float::from(1.5) / &Float::from(-2.5)).to_string(), "-0.6");
5604 /// assert_eq!((Float::from(-1.5) / &Float::from(2.5)).to_string(), "-0.6");
5605 /// assert_eq!((Float::from(-1.5) / &Float::from(-2.5)).to_string(), "0.6");
5606 /// ```
5607 #[inline]
5608 fn div(self, other: &Self) -> Self {
5609 let prec = max(self.significant_bits(), other.significant_bits());
5610 self.div_prec_round_val_ref(other, prec, Nearest).0
5611 }
5612}
5613
5614impl Div<Float> for &Float {
5615 type Output = Float;
5616
5617 /// Divides two [`Float`]s, taking the first by reference and the second by value.
5618 ///
5619 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5620 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5621 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5622 /// `Nearest` rounding mode.
5623 ///
5624 /// $$
5625 /// f(x,y) = x/y+\varepsilon.
5626 /// $$
5627 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5628 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5629 /// where $p$ is the maximum precision of the inputs.
5630 ///
5631 /// Special cases:
5632 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
5633 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
5634 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
5635 /// - $f(x,0.0)=\infty$ if $x>0.0$
5636 /// - $f(x,0.0)=-\infty$ if $x<0.0$
5637 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
5638 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
5639 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
5640 /// - $f(x,-0.0)=\infty$ if $x<0.0$
5641 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
5642 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
5643 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5644 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5645 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
5646 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
5647 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5648 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5649 ///
5650 /// Overflow and underflow:
5651 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
5652 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
5653 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5654 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5655 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
5656 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5657 ///
5658 /// If you want to use a rounding mode other than `Nearest`, consider using
5659 /// [`Float::div_prec_ref_val`] instead. If you want to specify the output precision, consider
5660 /// using [`Float::div_round_ref_val`]. If you want both of these things, consider using
5661 /// [`Float::div_prec_round_ref_val`].
5662 ///
5663 /// # Worst-case complexity
5664 /// $T(n) = O(n \log n \log\log n)$
5665 ///
5666 /// $M(n) = O(n \log n)$
5667 ///
5668 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5669 /// other.significant_bits())`.
5670 ///
5671 /// # Examples
5672 /// ```
5673 /// use malachite_base::num::basic::traits::{
5674 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5675 /// };
5676 /// use malachite_float::Float;
5677 ///
5678 /// assert!((&Float::from(1.5) / Float::NAN).is_nan());
5679 /// assert_eq!(&Float::from(1.5) / Float::ZERO, Float::INFINITY);
5680 /// assert_eq!(
5681 /// &Float::from(1.5) / Float::NEGATIVE_ZERO,
5682 /// Float::NEGATIVE_INFINITY
5683 /// );
5684 /// assert_eq!(&Float::from(-1.5) / Float::ZERO, Float::NEGATIVE_INFINITY);
5685 /// assert_eq!(&Float::from(-1.5) / Float::NEGATIVE_ZERO, Float::INFINITY);
5686 /// assert!((&Float::ZERO / Float::ZERO).is_nan());
5687 ///
5688 /// assert_eq!((&Float::from(1.5) / Float::from(2.5)).to_string(), "0.6");
5689 /// assert_eq!((&Float::from(1.5) / Float::from(-2.5)).to_string(), "-0.6");
5690 /// assert_eq!((&Float::from(-1.5) / Float::from(2.5)).to_string(), "-0.6");
5691 /// assert_eq!((&Float::from(-1.5) / Float::from(-2.5)).to_string(), "0.6");
5692 /// ```
5693 #[inline]
5694 fn div(self, other: Float) -> Float {
5695 let prec = max(self.significant_bits(), other.significant_bits());
5696 self.div_prec_round_ref_val(other, prec, Nearest).0
5697 }
5698}
5699
5700impl Div<&Float> for &Float {
5701 type Output = Float;
5702
5703 /// Divides two [`Float`]s, taking both by reference.
5704 ///
5705 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5706 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5707 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5708 /// `Nearest` rounding mode.
5709 ///
5710 /// $$
5711 /// f(x,y) = x/y+\varepsilon.
5712 /// $$
5713 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5714 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5715 /// where $p$ is the maximum precision of the inputs.
5716 ///
5717 /// Special cases:
5718 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
5719 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
5720 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
5721 /// - $f(x,0.0)=\infty$ if $x>0.0$
5722 /// - $f(x,0.0)=-\infty$ if $x<0.0$
5723 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
5724 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
5725 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
5726 /// - $f(x,-0.0)=\infty$ if $x<0.0$
5727 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
5728 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
5729 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5730 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5731 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
5732 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
5733 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5734 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5735 ///
5736 /// Overflow and underflow:
5737 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
5738 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
5739 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5740 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5741 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
5742 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5743 ///
5744 /// If you want to use a rounding mode other than `Nearest`, consider using
5745 /// [`Float::div_prec_ref_ref`] instead. If you want to specify the output precision, consider
5746 /// using [`Float::div_round_ref_ref`]. If you want both of these things, consider using
5747 /// [`Float::div_prec_round_ref_ref`].
5748 ///
5749 /// # Worst-case complexity
5750 /// $T(n) = O(n \log n \log\log n)$
5751 ///
5752 /// $M(n) = O(n \log n)$
5753 ///
5754 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5755 /// other.significant_bits())`.
5756 ///
5757 /// # Examples
5758 /// ```
5759 /// use malachite_base::num::basic::traits::{
5760 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5761 /// };
5762 /// use malachite_float::Float;
5763 ///
5764 /// assert!((&Float::from(1.5) / &Float::NAN).is_nan());
5765 /// assert_eq!(&Float::from(1.5) / &Float::ZERO, Float::INFINITY);
5766 /// assert_eq!(
5767 /// &Float::from(1.5) / &Float::NEGATIVE_ZERO,
5768 /// Float::NEGATIVE_INFINITY
5769 /// );
5770 /// assert_eq!(&Float::from(-1.5) / &Float::ZERO, Float::NEGATIVE_INFINITY);
5771 /// assert_eq!(&Float::from(-1.5) / &Float::NEGATIVE_ZERO, Float::INFINITY);
5772 /// assert!((&Float::ZERO / &Float::ZERO).is_nan());
5773 ///
5774 /// assert_eq!((&Float::from(1.5) / &Float::from(2.5)).to_string(), "0.6");
5775 /// assert_eq!((&Float::from(1.5) / &Float::from(-2.5)).to_string(), "-0.6");
5776 /// assert_eq!((&Float::from(-1.5) / &Float::from(2.5)).to_string(), "-0.6");
5777 /// assert_eq!((&Float::from(-1.5) / &Float::from(-2.5)).to_string(), "0.6");
5778 /// ```
5779 #[inline]
5780 fn div(self, other: &Float) -> Float {
5781 let prec = max(self.significant_bits(), other.significant_bits());
5782 self.div_prec_round_ref_ref(other, prec, Nearest).0
5783 }
5784}
5785
5786impl DivAssign<Self> for Float {
5787 /// Divides a [`Float`] by a [`Float`] in place, taking the [`Float`] on the right-hand side by
5788 /// value.
5789 ///
5790 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5791 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5792 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5793 /// `Nearest` rounding mode.
5794 ///
5795 /// $$
5796 /// x\gets = x/y+\varepsilon.
5797 /// $$
5798 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5799 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5800 /// where $p$ is the maximum precision of the inputs.
5801 ///
5802 /// See the `/` documentation for information on special cases, overflow, and underflow.
5803 ///
5804 /// If you want to use a rounding mode other than `Nearest`, consider using
5805 /// [`Float::div_prec_assign`] instead. If you want to specify the output precision, consider
5806 /// using [`Float::div_round_assign`]. If you want both of these things, consider using
5807 /// [`Float::div_prec_round_assign`].
5808 ///
5809 /// # Worst-case complexity
5810 /// $T(n) = O(n \log n \log\log n)$
5811 ///
5812 /// $M(n) = O(n \log n)$
5813 ///
5814 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5815 /// other.significant_bits())`.
5816 ///
5817 /// # Examples
5818 /// ```
5819 /// use malachite_base::num::basic::traits::{
5820 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5821 /// };
5822 /// use malachite_float::Float;
5823 ///
5824 /// let mut x = Float::from(1.5);
5825 /// x /= Float::NAN;
5826 /// assert!(x.is_nan());
5827 ///
5828 /// let mut x = Float::from(1.5);
5829 /// x /= Float::ZERO;
5830 /// assert_eq!(x, Float::INFINITY);
5831 ///
5832 /// let mut x = Float::from(1.5);
5833 /// x /= Float::NEGATIVE_ZERO;
5834 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5835 ///
5836 /// let mut x = Float::from(-1.5);
5837 /// x /= Float::ZERO;
5838 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5839 ///
5840 /// let mut x = Float::from(-1.5);
5841 /// x /= Float::NEGATIVE_ZERO;
5842 /// assert_eq!(x, Float::INFINITY);
5843 ///
5844 /// let mut x = Float::INFINITY;
5845 /// x /= Float::INFINITY;
5846 /// assert!(x.is_nan());
5847 ///
5848 /// let mut x = Float::from(1.5);
5849 /// x /= Float::from(2.5);
5850 /// assert_eq!(x.to_string(), "0.6");
5851 ///
5852 /// let mut x = Float::from(1.5);
5853 /// x /= Float::from(-2.5);
5854 /// assert_eq!(x.to_string(), "-0.6");
5855 ///
5856 /// let mut x = Float::from(-1.5);
5857 /// x /= Float::from(2.5);
5858 /// assert_eq!(x.to_string(), "-0.6");
5859 ///
5860 /// let mut x = Float::from(-1.5);
5861 /// x /= Float::from(-2.5);
5862 /// assert_eq!(x.to_string(), "0.6");
5863 /// ```
5864 #[inline]
5865 fn div_assign(&mut self, other: Self) {
5866 let prec = max(self.significant_bits(), other.significant_bits());
5867 self.div_prec_round_assign(other, prec, Nearest);
5868 }
5869}
5870
5871impl DivAssign<&Self> for Float {
5872 /// Divides a [`Float`] by a [`Float`] in place, taking the [`Float`] on the right-hand side by
5873 /// reference.
5874 ///
5875 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5876 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5877 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5878 /// `Nearest` rounding mode.
5879 ///
5880 /// $$
5881 /// x\gets = x/y+\varepsilon.
5882 /// $$
5883 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5884 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5885 /// where $p$ is the maximum precision of the inputs.
5886 ///
5887 /// See the `/` documentation for information on special cases, overflow, and underflow.
5888 ///
5889 /// If you want to use a rounding mode other than `Nearest`, consider using
5890 /// [`Float::div_prec_assign`] instead. If you want to specify the output precision, consider
5891 /// using [`Float::div_round_assign`]. If you want both of these things, consider using
5892 /// [`Float::div_prec_round_assign`].
5893 ///
5894 /// # Worst-case complexity
5895 /// $T(n) = O(n \log n \log\log n)$
5896 ///
5897 /// $M(n) = O(n \log n)$
5898 ///
5899 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5900 /// other.significant_bits())`.
5901 ///
5902 /// # Examples
5903 /// ```
5904 /// use malachite_base::num::basic::traits::{
5905 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5906 /// };
5907 /// use malachite_float::Float;
5908 ///
5909 /// let mut x = Float::from(1.5);
5910 /// x /= &Float::NAN;
5911 /// assert!(x.is_nan());
5912 ///
5913 /// let mut x = Float::from(1.5);
5914 /// x /= &Float::ZERO;
5915 /// assert_eq!(x, Float::INFINITY);
5916 ///
5917 /// let mut x = Float::from(1.5);
5918 /// x /= &Float::NEGATIVE_ZERO;
5919 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5920 ///
5921 /// let mut x = Float::from(-1.5);
5922 /// x /= &Float::ZERO;
5923 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5924 ///
5925 /// let mut x = Float::from(-1.5);
5926 /// x /= &Float::NEGATIVE_ZERO;
5927 /// assert_eq!(x, Float::INFINITY);
5928 ///
5929 /// let mut x = Float::INFINITY;
5930 /// x /= &Float::INFINITY;
5931 /// assert!(x.is_nan());
5932 ///
5933 /// let mut x = Float::from(1.5);
5934 /// x /= &Float::from(2.5);
5935 /// assert_eq!(x.to_string(), "0.6");
5936 ///
5937 /// let mut x = Float::from(1.5);
5938 /// x /= &Float::from(-2.5);
5939 /// assert_eq!(x.to_string(), "-0.6");
5940 ///
5941 /// let mut x = Float::from(-1.5);
5942 /// x /= &Float::from(2.5);
5943 /// assert_eq!(x.to_string(), "-0.6");
5944 ///
5945 /// let mut x = Float::from(-1.5);
5946 /// x /= &Float::from(-2.5);
5947 /// assert_eq!(x.to_string(), "0.6");
5948 /// ```
5949 #[inline]
5950 fn div_assign(&mut self, other: &Self) {
5951 let prec = max(self.significant_bits(), other.significant_bits());
5952 self.div_prec_round_assign_ref(other, prec, Nearest);
5953 }
5954}
5955
5956impl Div<Rational> for Float {
5957 type Output = Self;
5958
5959 /// Divides a [`Float`] by a [`Rational`], taking both by value.
5960 ///
5961 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
5962 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
5963 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
5964 /// rounding mode.
5965 ///
5966 /// $$
5967 /// f(x,y) = x/y+\varepsilon.
5968 /// $$
5969 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5970 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5971 /// where $p$ is the precision of the input [`Float`].
5972 ///
5973 /// Special cases:
5974 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
5975 /// - $f(\infty,x)=\infty$ if $x\geq 0$
5976 /// - $f(\infty,x)=-\infty$ if $x<0$
5977 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
5978 /// - $f(-\infty,x)=\infty$ if $x<0$
5979 /// - $f(0.0,x)=0.0$ if $x>0$
5980 /// - $f(0.0,x)=-0.0$ if $x<0$
5981 /// - $f(-0.0,x)=-0.0$ if $x>0$
5982 /// - $f(-0.0,x)=0.0$ if $x<0$
5983 ///
5984 /// If you want to use a rounding mode other than `Nearest`, consider using
5985 /// [`Float::div_rational_prec`] instead. If you want to specify the output precision, consider
5986 /// using [`Float::div_rational_round`]. If you want both of these things, consider using
5987 /// [`Float::div_rational_prec_round`].
5988 ///
5989 /// # Worst-case complexity
5990 /// $T(n) = O(n \log n \log\log n)$
5991 ///
5992 /// $M(n) = O(n \log n)$
5993 ///
5994 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5995 /// other.significant_bits())`.
5996 ///
5997 /// # Examples
5998 /// ```
5999 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
6000 /// use malachite_base::num::conversion::traits::ExactFrom;
6001 /// use malachite_float::Float;
6002 /// use malachite_q::Rational;
6003 ///
6004 /// assert!((Float::NAN / Rational::exact_from(1.5)).is_nan());
6005 /// assert_eq!(Float::INFINITY / Rational::exact_from(1.5), Float::INFINITY);
6006 /// assert_eq!(
6007 /// Float::NEGATIVE_INFINITY / Rational::exact_from(1.5),
6008 /// Float::NEGATIVE_INFINITY
6009 /// );
6010 /// assert_eq!(
6011 /// Float::INFINITY / Rational::exact_from(-1.5),
6012 /// Float::NEGATIVE_INFINITY
6013 /// );
6014 /// assert_eq!(
6015 /// Float::NEGATIVE_INFINITY / Rational::exact_from(-1.5),
6016 /// Float::INFINITY
6017 /// );
6018 ///
6019 /// assert_eq!(
6020 /// (Float::from(2.5) / Rational::exact_from(1.5)).to_string(),
6021 /// "1.8"
6022 /// );
6023 /// assert_eq!(
6024 /// (Float::from(2.5) / Rational::exact_from(-1.5)).to_string(),
6025 /// "-1.8"
6026 /// );
6027 /// assert_eq!(
6028 /// (Float::from(-2.5) / Rational::exact_from(1.5)).to_string(),
6029 /// "-1.8"
6030 /// );
6031 /// assert_eq!(
6032 /// (Float::from(-2.5) / Rational::exact_from(-1.5)).to_string(),
6033 /// "1.8"
6034 /// );
6035 /// ```
6036 #[inline]
6037 fn div(self, other: Rational) -> Self {
6038 let prec = self.significant_bits();
6039 self.div_rational_prec_round(other, prec, Nearest).0
6040 }
6041}
6042
6043impl Div<&Rational> for Float {
6044 type Output = Self;
6045
6046 /// Divides a [`Float`] by a [`Rational`], taking the first by value and the second by
6047 /// reference.
6048 ///
6049 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6050 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6051 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6052 /// rounding mode.
6053 ///
6054 /// $$
6055 /// f(x,y) = x/y+\varepsilon.
6056 /// $$
6057 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6058 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6059 /// where $p$ is the precision of the input [`Float`].
6060 ///
6061 /// Special cases:
6062 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
6063 /// - $f(\infty,x)=\infty$ if $x\geq 0$
6064 /// - $f(\infty,x)=-\infty$ if $x<0$
6065 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
6066 /// - $f(-\infty,x)=\infty$ if $x<0$
6067 /// - $f(0.0,x)=0.0$ if $x>0$
6068 /// - $f(0.0,x)=-0.0$ if $x<0$
6069 /// - $f(-0.0,x)=-0.0$ if $x>0$
6070 /// - $f(-0.0,x)=0.0$ if $x<0$
6071 ///
6072 /// If you want to use a rounding mode other than `Nearest`, consider using
6073 /// [`Float::div_rational_prec_val_ref`] instead. If you want to specify the output precision,
6074 /// consider using [`Float::div_rational_round_val_ref`]. If you want both of these things,
6075 /// consider using [`Float::div_rational_prec_round_val_ref`].
6076 ///
6077 /// # Worst-case complexity
6078 /// $T(n) = O(n \log n \log\log n)$
6079 ///
6080 /// $M(n) = O(n \log n)$
6081 ///
6082 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6083 /// other.significant_bits())`.
6084 ///
6085 /// # Examples
6086 /// ```
6087 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
6088 /// use malachite_base::num::conversion::traits::ExactFrom;
6089 /// use malachite_float::Float;
6090 /// use malachite_q::Rational;
6091 ///
6092 /// assert!((Float::NAN / &Rational::exact_from(1.5)).is_nan());
6093 /// assert_eq!(
6094 /// Float::INFINITY / &Rational::exact_from(1.5),
6095 /// Float::INFINITY
6096 /// );
6097 /// assert_eq!(
6098 /// Float::NEGATIVE_INFINITY / &Rational::exact_from(1.5),
6099 /// Float::NEGATIVE_INFINITY
6100 /// );
6101 /// assert_eq!(
6102 /// Float::INFINITY / &Rational::exact_from(-1.5),
6103 /// Float::NEGATIVE_INFINITY
6104 /// );
6105 /// assert_eq!(
6106 /// Float::NEGATIVE_INFINITY / &Rational::exact_from(-1.5),
6107 /// Float::INFINITY
6108 /// );
6109 ///
6110 /// assert_eq!(
6111 /// (Float::from(2.5) / &Rational::exact_from(1.5)).to_string(),
6112 /// "1.8"
6113 /// );
6114 /// assert_eq!(
6115 /// (Float::from(2.5) / &Rational::exact_from(-1.5)).to_string(),
6116 /// "-1.8"
6117 /// );
6118 /// assert_eq!(
6119 /// (Float::from(-2.5) / &Rational::exact_from(1.5)).to_string(),
6120 /// "-1.8"
6121 /// );
6122 /// assert_eq!(
6123 /// (Float::from(-2.5) / &Rational::exact_from(-1.5)).to_string(),
6124 /// "1.8"
6125 /// );
6126 /// ```
6127 #[inline]
6128 fn div(self, other: &Rational) -> Self {
6129 let prec = self.significant_bits();
6130 self.div_rational_prec_round_val_ref(other, prec, Nearest).0
6131 }
6132}
6133
6134impl Div<Rational> for &Float {
6135 type Output = Float;
6136
6137 /// Divides a [`Float`] by a [`Rational`], taking the first by reference and the second by
6138 /// value.
6139 ///
6140 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6141 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6142 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6143 /// rounding mode.
6144 ///
6145 /// $$
6146 /// f(x,y) = x/y+\varepsilon.
6147 /// $$
6148 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6149 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6150 /// where $p$ is the precision of the input [`Float`].
6151 ///
6152 /// Special cases:
6153 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
6154 /// - $f(\infty,x)=\infty$ if $x\geq 0$
6155 /// - $f(\infty,x)=-\infty$ if $x<0$
6156 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
6157 /// - $f(-\infty,x)=\infty$ if $x<0$
6158 /// - $f(0.0,x)=0.0$ if $x>0$
6159 /// - $f(0.0,x)=-0.0$ if $x<0$
6160 /// - $f(-0.0,x)=-0.0$ if $x>0$
6161 /// - $f(-0.0,x)=0.0$ if $x<0$
6162 ///
6163 /// If you want to use a rounding mode other than `Nearest`, consider using
6164 /// [`Float::div_rational_prec_ref_val`] instead. If you want to specify the output precision,
6165 /// consider using [`Float::div_rational_round_ref_val`]. If you want both of these things,
6166 /// consider using [`Float::div_rational_prec_round_ref_val`].
6167 ///
6168 /// # Worst-case complexity
6169 /// $T(n) = O(n \log n \log\log n)$
6170 ///
6171 /// $M(n) = O(n \log n)$
6172 ///
6173 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6174 /// other.significant_bits())`.
6175 ///
6176 /// # Examples
6177 /// ```
6178 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
6179 /// use malachite_base::num::conversion::traits::ExactFrom;
6180 /// use malachite_float::Float;
6181 /// use malachite_q::Rational;
6182 ///
6183 /// assert!((&Float::NAN / Rational::exact_from(1.5)).is_nan());
6184 /// assert_eq!(
6185 /// &Float::INFINITY / Rational::exact_from(1.5),
6186 /// Float::INFINITY
6187 /// );
6188 /// assert_eq!(
6189 /// &Float::NEGATIVE_INFINITY / Rational::exact_from(1.5),
6190 /// Float::NEGATIVE_INFINITY
6191 /// );
6192 /// assert_eq!(
6193 /// &Float::INFINITY / Rational::exact_from(-1.5),
6194 /// Float::NEGATIVE_INFINITY
6195 /// );
6196 /// assert_eq!(
6197 /// &Float::NEGATIVE_INFINITY / Rational::exact_from(-1.5),
6198 /// Float::INFINITY
6199 /// );
6200 ///
6201 /// assert_eq!(
6202 /// (&Float::from(2.5) / Rational::exact_from(1.5)).to_string(),
6203 /// "1.8"
6204 /// );
6205 /// assert_eq!(
6206 /// (&Float::from(2.5) / Rational::exact_from(-1.5)).to_string(),
6207 /// "-1.8"
6208 /// );
6209 /// assert_eq!(
6210 /// (&Float::from(-2.5) / Rational::exact_from(1.5)).to_string(),
6211 /// "-1.8"
6212 /// );
6213 /// assert_eq!(
6214 /// (&Float::from(-2.5) / Rational::exact_from(-1.5)).to_string(),
6215 /// "1.8"
6216 /// );
6217 /// ```
6218 #[inline]
6219 fn div(self, other: Rational) -> Float {
6220 let prec = self.significant_bits();
6221 self.div_rational_prec_round_ref_val(other, prec, Nearest).0
6222 }
6223}
6224
6225impl Div<&Rational> for &Float {
6226 type Output = Float;
6227
6228 /// Divides a [`Float`] by a [`Rational`], taking both by reference.
6229 ///
6230 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6231 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6232 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6233 /// rounding mode.
6234 ///
6235 /// $$
6236 /// f(x,y) = x/y+\varepsilon.
6237 /// $$
6238 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6239 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6240 /// where $p$ is the precision of the input [`Float`].
6241 ///
6242 /// Special cases:
6243 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
6244 /// - $f(\infty,x)=\infty$ if $x\geq 0$
6245 /// - $f(\infty,x)=-\infty$ if $x<0$
6246 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
6247 /// - $f(-\infty,x)=\infty$ if $x<0$
6248 /// - $f(0.0,x)=0.0$ if $x>0$
6249 /// - $f(0.0,x)=-0.0$ if $x<0$
6250 /// - $f(-0.0,x)=-0.0$ if $x>0$
6251 /// - $f(-0.0,x)=0.0$ if $x<0$
6252 ///
6253 /// If you want to use a rounding mode other than `Nearest`, consider using
6254 /// [`Float::div_rational_prec_ref_ref`] instead. If you want to specify the output precision,
6255 /// consider using [`Float::div_rational_round_ref_ref`]. If you want both of these things,
6256 /// consider using [`Float::div_rational_prec_round_ref_ref`].
6257 ///
6258 /// # Worst-case complexity
6259 /// $T(n) = O(n \log n \log\log n)$
6260 ///
6261 /// $M(n) = O(n \log n)$
6262 ///
6263 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6264 /// other.significant_bits())`.
6265 ///
6266 /// # Examples
6267 /// ```
6268 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
6269 /// use malachite_base::num::conversion::traits::ExactFrom;
6270 /// use malachite_float::Float;
6271 /// use malachite_q::Rational;
6272 ///
6273 /// assert!((&Float::NAN / &Rational::exact_from(1.5)).is_nan());
6274 /// assert_eq!(
6275 /// &Float::INFINITY / &Rational::exact_from(1.5),
6276 /// Float::INFINITY
6277 /// );
6278 /// assert_eq!(
6279 /// &Float::NEGATIVE_INFINITY / &Rational::exact_from(1.5),
6280 /// Float::NEGATIVE_INFINITY
6281 /// );
6282 /// assert_eq!(
6283 /// &Float::INFINITY / &Rational::exact_from(-1.5),
6284 /// Float::NEGATIVE_INFINITY
6285 /// );
6286 /// assert_eq!(
6287 /// &Float::NEGATIVE_INFINITY / &Rational::exact_from(-1.5),
6288 /// Float::INFINITY
6289 /// );
6290 ///
6291 /// assert_eq!(
6292 /// (&Float::from(2.5) / &Rational::exact_from(1.5)).to_string(),
6293 /// "1.8"
6294 /// );
6295 /// assert_eq!(
6296 /// (&Float::from(2.5) / &Rational::exact_from(-1.5)).to_string(),
6297 /// "-1.8"
6298 /// );
6299 /// assert_eq!(
6300 /// (&Float::from(-2.5) / &Rational::exact_from(1.5)).to_string(),
6301 /// "-1.8"
6302 /// );
6303 /// assert_eq!(
6304 /// (&Float::from(-2.5) / &Rational::exact_from(-1.5)).to_string(),
6305 /// "1.8"
6306 /// );
6307 /// ```
6308 #[inline]
6309 fn div(self, other: &Rational) -> Float {
6310 let prec = self.significant_bits();
6311 self.div_rational_prec_round_ref_ref(other, prec, Nearest).0
6312 }
6313}
6314
6315impl DivAssign<Rational> for Float {
6316 /// Divides a [`Float`] by a [`Rational`] in place, taking the [`Rational`] by value.
6317 ///
6318 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6319 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6320 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6321 /// rounding mode.
6322 ///
6323 /// $$
6324 /// x\gets = x/y+\varepsilon.
6325 /// $$
6326 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6327 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6328 /// where $p$ is the precision of the input [`Float`].
6329 ///
6330 /// See the `/` documentation for information on special cases.
6331 ///
6332 /// If you want to use a rounding mode other than `Nearest`, consider using
6333 /// [`Float::div_rational_prec_assign`] instead. If you want to specify the output precision,
6334 /// consider using [`Float::div_rational_round_assign`]. If you want both of these things,
6335 /// consider using [`Float::div_rational_prec_round_assign`].
6336 ///
6337 /// # Worst-case complexity
6338 /// $T(n) = O(n \log n \log\log n)$
6339 ///
6340 /// $M(n) = O(n \log n)$
6341 ///
6342 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6343 /// other.significant_bits())`.
6344 ///
6345 /// # Examples
6346 /// ```
6347 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
6348 /// use malachite_base::num::conversion::traits::ExactFrom;
6349 /// use malachite_float::Float;
6350 /// use malachite_q::Rational;
6351 ///
6352 /// let mut x = Float::NAN;
6353 /// x /= Rational::exact_from(1.5);
6354 /// assert!(x.is_nan());
6355 ///
6356 /// let mut x = Float::INFINITY;
6357 /// x /= Rational::exact_from(1.5);
6358 /// assert_eq!(x, Float::INFINITY);
6359 ///
6360 /// let mut x = Float::NEGATIVE_INFINITY;
6361 /// x /= Rational::exact_from(1.5);
6362 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
6363 ///
6364 /// let mut x = Float::INFINITY;
6365 /// x /= Rational::exact_from(-1.5);
6366 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
6367 ///
6368 /// let mut x = Float::NEGATIVE_INFINITY;
6369 /// x /= Rational::exact_from(-1.5);
6370 /// assert_eq!(x, Float::INFINITY);
6371 ///
6372 /// let mut x = Float::from(2.5);
6373 /// x /= Rational::exact_from(1.5);
6374 /// assert_eq!(x.to_string(), "1.8");
6375 /// ```
6376 #[inline]
6377 fn div_assign(&mut self, other: Rational) {
6378 let prec = self.significant_bits();
6379 self.div_rational_prec_round_assign(other, prec, Nearest);
6380 }
6381}
6382
6383impl DivAssign<&Rational> for Float {
6384 /// Divides a [`Float`] by a [`Rational`] in place, taking the [`Rational`] by reference.
6385 ///
6386 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6387 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6388 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6389 /// rounding mode.
6390 ///
6391 /// $$
6392 /// x\gets = x/y+\varepsilon.
6393 /// $$
6394 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6395 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6396 /// where $p$ is the precision of the input [`Float`].
6397 ///
6398 /// See the `/` documentation for information on special cases.
6399 ///
6400 /// If you want to use a rounding mode other than `Nearest`, consider using
6401 /// [`Float::div_rational_prec_assign_ref`] instead. If you want to specify the output
6402 /// precision, consider using [`Float::div_rational_round_assign_ref`]. If you want both of
6403 /// these things, consider using [`Float::div_rational_prec_round_assign_ref`].
6404 ///
6405 /// # Worst-case complexity
6406 /// $T(n) = O(n \log n \log\log n)$
6407 ///
6408 /// $M(n) = O(n \log n)$
6409 ///
6410 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6411 /// other.significant_bits())`.
6412 ///
6413 /// # Examples
6414 /// ```
6415 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
6416 /// use malachite_base::num::conversion::traits::ExactFrom;
6417 /// use malachite_float::Float;
6418 /// use malachite_q::Rational;
6419 ///
6420 /// let mut x = Float::NAN;
6421 /// x /= &Rational::exact_from(1.5);
6422 /// assert!(x.is_nan());
6423 ///
6424 /// let mut x = Float::INFINITY;
6425 /// x /= &Rational::exact_from(1.5);
6426 /// assert_eq!(x, Float::INFINITY);
6427 ///
6428 /// let mut x = Float::NEGATIVE_INFINITY;
6429 /// x /= &Rational::exact_from(1.5);
6430 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
6431 ///
6432 /// let mut x = Float::INFINITY;
6433 /// x /= &Rational::exact_from(-1.5);
6434 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
6435 ///
6436 /// let mut x = Float::NEGATIVE_INFINITY;
6437 /// x /= &Rational::exact_from(-1.5);
6438 /// assert_eq!(x, Float::INFINITY);
6439 ///
6440 /// let mut x = Float::from(2.5);
6441 /// x /= &Rational::exact_from(1.5);
6442 /// assert_eq!(x.to_string(), "1.8");
6443 /// ```
6444 #[inline]
6445 fn div_assign(&mut self, other: &Rational) {
6446 let prec = self.significant_bits();
6447 self.div_rational_prec_round_assign_ref(other, prec, Nearest);
6448 }
6449}
6450
6451impl Div<Float> for Rational {
6452 type Output = Float;
6453
6454 /// Divides a [`Rational`] by a [`Float`], taking both by value.
6455 ///
6456 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6457 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6458 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6459 /// rounding mode.
6460 ///
6461 /// $$
6462 /// f(x,y) = x/y+\varepsilon.
6463 /// $$
6464 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6465 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6466 /// where $p$ is the precision of the input [`Float`].
6467 ///
6468 /// Special cases:
6469 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
6470 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
6471 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
6472 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
6473 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
6474 /// - $f(0,x,p,m)=0.0$ if $x>0$
6475 /// - $f(0,x,p,m)=-0.0$ if $x<0$
6476 ///
6477 /// # Worst-case complexity
6478 /// $T(n) = O(n \log n \log\log n)$
6479 ///
6480 /// $M(n) = O(n \log n)$
6481 ///
6482 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6483 /// other.significant_bits())`.
6484 ///
6485 /// # Examples
6486 /// ```
6487 /// use malachite_base::num::basic::traits::{
6488 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6489 /// };
6490 /// use malachite_base::num::conversion::traits::ExactFrom;
6491 /// use malachite_float::Float;
6492 /// use malachite_q::Rational;
6493 ///
6494 /// assert!((Rational::exact_from(1.5) / Float::NAN).is_nan());
6495 /// assert_eq!(Rational::exact_from(1.5) / Float::ZERO, Float::INFINITY);
6496 /// assert_eq!(
6497 /// Rational::exact_from(1.5) / Float::NEGATIVE_ZERO,
6498 /// Float::NEGATIVE_INFINITY
6499 /// );
6500 /// assert_eq!(
6501 /// Rational::exact_from(-1.5) / Float::ZERO,
6502 /// Float::NEGATIVE_INFINITY
6503 /// );
6504 /// assert_eq!(
6505 /// Rational::exact_from(-1.5) / Float::NEGATIVE_ZERO,
6506 /// Float::INFINITY
6507 /// );
6508 ///
6509 /// assert_eq!(
6510 /// (Rational::exact_from(1.5) / Float::from(2.5)).to_string(),
6511 /// "0.6"
6512 /// );
6513 /// assert_eq!(
6514 /// (Rational::exact_from(-1.5) / Float::from(2.5)).to_string(),
6515 /// "-0.6"
6516 /// );
6517 /// assert_eq!(
6518 /// (Rational::exact_from(1.5) / Float::from(-2.5)).to_string(),
6519 /// "-0.6"
6520 /// );
6521 /// assert_eq!(
6522 /// (Rational::exact_from(-1.5) / Float::from(-2.5)).to_string(),
6523 /// "0.6"
6524 /// );
6525 /// ```
6526 #[inline]
6527 fn div(self, other: Float) -> Float {
6528 let prec = other.significant_bits();
6529 Float::rational_div_float_prec_round(self, other, prec, Nearest).0
6530 }
6531}
6532
6533impl Div<&Float> for Rational {
6534 type Output = Float;
6535
6536 /// Divides a [`Rational`] by a [`Float`], taking the [`Rational`] by value and the [`Float`] by
6537 /// reference.
6538 ///
6539 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6540 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6541 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6542 /// rounding mode.
6543 ///
6544 /// $$
6545 /// f(x,y) = x/y+\varepsilon.
6546 /// $$
6547 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6548 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6549 /// where $p$ is the precision of the input [`Float`].
6550 ///
6551 /// Special cases:
6552 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
6553 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
6554 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
6555 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
6556 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
6557 /// - $f(0,x,p,m)=0.0$ if $x>0$
6558 /// - $f(0,x,p,m)=-0.0$ if $x<0$
6559 ///
6560 /// # Worst-case complexity
6561 /// $T(n) = O(n \log n \log\log n)$
6562 ///
6563 /// $M(n) = O(n \log n)$
6564 ///
6565 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6566 /// other.significant_bits())`.
6567 ///
6568 /// # Examples
6569 /// ```
6570 /// use malachite_base::num::basic::traits::{
6571 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6572 /// };
6573 /// use malachite_base::num::conversion::traits::ExactFrom;
6574 /// use malachite_float::Float;
6575 /// use malachite_q::Rational;
6576 ///
6577 /// assert!((Rational::exact_from(1.5) / &Float::NAN).is_nan());
6578 /// assert_eq!(Rational::exact_from(1.5) / &Float::ZERO, Float::INFINITY);
6579 /// assert_eq!(
6580 /// Rational::exact_from(1.5) / &Float::NEGATIVE_ZERO,
6581 /// Float::NEGATIVE_INFINITY
6582 /// );
6583 /// assert_eq!(
6584 /// Rational::exact_from(-1.5) / &Float::ZERO,
6585 /// Float::NEGATIVE_INFINITY
6586 /// );
6587 /// assert_eq!(
6588 /// Rational::exact_from(-1.5) / &Float::NEGATIVE_ZERO,
6589 /// Float::INFINITY
6590 /// );
6591 ///
6592 /// assert_eq!(
6593 /// (Rational::exact_from(1.5) / &Float::from(2.5)).to_string(),
6594 /// "0.6"
6595 /// );
6596 /// assert_eq!(
6597 /// (Rational::exact_from(-1.5) / &Float::from(2.5)).to_string(),
6598 /// "-0.6"
6599 /// );
6600 /// assert_eq!(
6601 /// (Rational::exact_from(1.5) / &Float::from(-2.5)).to_string(),
6602 /// "-0.6"
6603 /// );
6604 /// assert_eq!(
6605 /// (Rational::exact_from(-1.5) / &Float::from(-2.5)).to_string(),
6606 /// "0.6"
6607 /// );
6608 /// ```
6609 #[inline]
6610 fn div(self, other: &Float) -> Float {
6611 let prec = other.significant_bits();
6612 Float::rational_div_float_prec_round_val_ref(self, other, prec, Nearest).0
6613 }
6614}
6615
6616impl Div<Float> for &Rational {
6617 type Output = Float;
6618
6619 /// Divides a [`Rational`] by a [`Float`], taking the [`Rational`] by reference and the
6620 /// [`Float`] by value.
6621 ///
6622 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6623 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6624 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6625 /// rounding mode.
6626 ///
6627 /// $$
6628 /// f(x,y) = x/y+\varepsilon.
6629 /// $$
6630 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6631 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6632 /// where $p$ is the precision of the input [`Float`].
6633 ///
6634 /// Special cases:
6635 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
6636 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
6637 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
6638 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
6639 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
6640 /// - $f(0,x,p,m)=0.0$ if $x>0$
6641 /// - $f(0,x,p,m)=-0.0$ if $x<0$
6642 ///
6643 /// # Worst-case complexity
6644 /// $T(n) = O(n \log n \log\log n)$
6645 ///
6646 /// $M(n) = O(n \log n)$
6647 ///
6648 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6649 /// other.significant_bits())`.
6650 ///
6651 /// # Examples
6652 /// ```
6653 /// use malachite_base::num::basic::traits::{
6654 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6655 /// };
6656 /// use malachite_base::num::conversion::traits::ExactFrom;
6657 /// use malachite_float::Float;
6658 /// use malachite_q::Rational;
6659 ///
6660 /// assert!((&Rational::exact_from(1.5) / Float::NAN).is_nan());
6661 /// assert_eq!(&Rational::exact_from(1.5) / Float::ZERO, Float::INFINITY);
6662 /// assert_eq!(
6663 /// &Rational::exact_from(1.5) / Float::NEGATIVE_ZERO,
6664 /// Float::NEGATIVE_INFINITY
6665 /// );
6666 /// assert_eq!(
6667 /// &Rational::exact_from(-1.5) / Float::ZERO,
6668 /// Float::NEGATIVE_INFINITY
6669 /// );
6670 /// assert_eq!(
6671 /// &Rational::exact_from(-1.5) / Float::NEGATIVE_ZERO,
6672 /// Float::INFINITY
6673 /// );
6674 ///
6675 /// assert_eq!(
6676 /// (&Rational::exact_from(1.5) / Float::from(2.5)).to_string(),
6677 /// "0.6"
6678 /// );
6679 /// assert_eq!(
6680 /// (&Rational::exact_from(-1.5) / Float::from(2.5)).to_string(),
6681 /// "-0.6"
6682 /// );
6683 /// assert_eq!(
6684 /// (&Rational::exact_from(1.5) / Float::from(-2.5)).to_string(),
6685 /// "-0.6"
6686 /// );
6687 /// assert_eq!(
6688 /// (&Rational::exact_from(-1.5) / Float::from(-2.5)).to_string(),
6689 /// "0.6"
6690 /// );
6691 /// ```
6692 #[inline]
6693 fn div(self, other: Float) -> Float {
6694 let prec = other.significant_bits();
6695 Float::rational_div_float_prec_round_ref_val(self, other, prec, Nearest).0
6696 }
6697}
6698
6699impl Div<&Float> for &Rational {
6700 type Output = Float;
6701
6702 /// Divides a [`Rational`] by a [`Float`], taking both by reference.
6703 ///
6704 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6705 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6706 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6707 /// rounding mode.
6708 ///
6709 /// $$
6710 /// f(x,y) = x/y+\varepsilon.
6711 /// $$
6712 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6713 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6714 /// where $p$ is the precision of the input [`Float`].
6715 ///
6716 /// Special cases:
6717 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
6718 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
6719 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
6720 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
6721 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
6722 /// - $f(0,x,p,m)=0.0$ if $x>0$
6723 /// - $f(0,x,p,m)=-0.0$ if $x<0$
6724 ///
6725 /// # Worst-case complexity
6726 /// $T(n) = O(n \log n \log\log n)$
6727 ///
6728 /// $M(n) = O(n \log n)$
6729 ///
6730 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6731 /// other.significant_bits())`.
6732 ///
6733 /// # Examples
6734 /// ```
6735 /// use malachite_base::num::basic::traits::{
6736 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6737 /// };
6738 /// use malachite_base::num::conversion::traits::ExactFrom;
6739 /// use malachite_float::Float;
6740 /// use malachite_q::Rational;
6741 ///
6742 /// assert!((&Rational::exact_from(1.5) / &Float::NAN).is_nan());
6743 /// assert_eq!(&Rational::exact_from(1.5) / &Float::ZERO, Float::INFINITY);
6744 /// assert_eq!(
6745 /// &Rational::exact_from(1.5) / &Float::NEGATIVE_ZERO,
6746 /// Float::NEGATIVE_INFINITY
6747 /// );
6748 /// assert_eq!(
6749 /// &Rational::exact_from(-1.5) / &Float::ZERO,
6750 /// Float::NEGATIVE_INFINITY
6751 /// );
6752 /// assert_eq!(
6753 /// &Rational::exact_from(-1.5) / &Float::NEGATIVE_ZERO,
6754 /// Float::INFINITY
6755 /// );
6756 ///
6757 /// assert_eq!(
6758 /// (&Rational::exact_from(1.5) / &Float::from(2.5)).to_string(),
6759 /// "0.6"
6760 /// );
6761 /// assert_eq!(
6762 /// (&Rational::exact_from(-1.5) / &Float::from(2.5)).to_string(),
6763 /// "-0.6"
6764 /// );
6765 /// assert_eq!(
6766 /// (&Rational::exact_from(1.5) / &Float::from(-2.5)).to_string(),
6767 /// "-0.6"
6768 /// );
6769 /// assert_eq!(
6770 /// (&Rational::exact_from(-1.5) / &Float::from(-2.5)).to_string(),
6771 /// "0.6"
6772 /// );
6773 /// ```
6774 #[inline]
6775 fn div(self, other: &Float) -> Float {
6776 let prec = other.significant_bits();
6777 Float::rational_div_float_prec_round_ref_ref(self, other, prec, Nearest).0
6778 }
6779}