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::{Float, float_either_infinity, float_either_zero, float_nan};
12use core::cmp::Ordering::{self, *};
13use core::cmp::max;
14use core::mem::swap;
15use core::ops::{Div, DivAssign};
16use malachite_base::num::arithmetic::traits::{CheckedLogBase2, IsPowerOf2, NegAssign, Sign};
17use malachite_base::num::basic::traits::Zero as ZeroTrait;
18use malachite_base::num::conversion::traits::ExactFrom;
19use malachite_base::num::logic::traits::{NotAssign, SignificantBits};
20use malachite_base::rounding_modes::RoundingMode::{self, *};
21use malachite_nz::natural::arithmetic::float_div::{
22 div_float_significands_in_place, div_float_significands_in_place_ref,
23 div_float_significands_ref_ref, div_float_significands_ref_val,
24};
25use malachite_q::Rational;
26
27const DIV_RATIONAL_THRESHOLD: u64 = 50;
28const RATIONAL_DIV_THRESHOLD: u64 = 50;
29
30fn div_rational_prec_round_assign_naive(
31 x: &mut Float,
32 y: Rational,
33 prec: u64,
34 rm: RoundingMode,
35) -> Ordering {
36 assert_ne!(prec, 0);
37 match (&mut *x, y) {
38 (float_nan!(), _) => Equal,
39 (Float(Infinity { sign }), y) => {
40 if y < 0 {
41 sign.not_assign();
42 };
43 Equal
44 }
45 (Float(Zero { sign }), y) => {
46 match y.sign() {
47 Equal => *x = float_nan!(),
48 Greater => {}
49 Less => sign.not_assign(),
50 }
51 Equal
52 }
53 (x, y) => {
54 if y == 0 {
55 *x = Float(Infinity { sign: *x > 0u32 });
56 Equal
57 } else {
58 let not_sign = *x < 0;
59 let mut z = Float::ZERO;
60 swap(x, &mut z);
61 let (mut quotient, o) =
62 Float::from_rational_prec_round(Rational::exact_from(z) / y, prec, rm);
63 if quotient == 0u32 && not_sign {
64 quotient.neg_assign();
65 }
66 *x = quotient;
67 o
68 }
69 }
70 }
71}
72
73fn div_rational_prec_round_assign_naive_ref(
74 x: &mut Float,
75 y: &Rational,
76 prec: u64,
77 rm: RoundingMode,
78) -> Ordering {
79 assert_ne!(prec, 0);
80 match (&mut *x, y) {
81 (float_nan!(), _) => Equal,
82 (Float(Infinity { sign }), y) => {
83 if *y < 0 {
84 sign.not_assign();
85 };
86 Equal
87 }
88 (Float(Zero { sign }), y) => {
89 match y.sign() {
90 Equal => *x = float_nan!(),
91 Greater => {}
92 Less => sign.not_assign(),
93 }
94 Equal
95 }
96 (x, y) => {
97 if *y == 0 {
98 *x = Float(Infinity { sign: *x > 0u32 });
99 Equal
100 } else {
101 let not_sign = *x < 0;
102 let mut z = Float::ZERO;
103 swap(x, &mut z);
104 let (mut quotient, o) =
105 Float::from_rational_prec_round(Rational::exact_from(z) / y, prec, rm);
106 if quotient == 0u32 && not_sign {
107 quotient.neg_assign();
108 }
109 *x = quotient;
110 o
111 }
112 }
113 }
114}
115
116pub_test! {div_rational_prec_round_naive(
117 mut x: Float,
118 y: Rational,
119 prec: u64,
120 rm: RoundingMode,
121) -> (Float, Ordering) {
122 let o = div_rational_prec_round_assign_naive(&mut x, y, prec, rm);
123 (x, o)
124}}
125
126pub_test! {div_rational_prec_round_naive_val_ref(
127 mut x: Float,
128 y: &Rational,
129 prec: u64,
130 rm: RoundingMode,
131) -> (Float, Ordering) {
132 let o = div_rational_prec_round_assign_naive_ref(&mut x, y, prec, rm);
133 (x, o)
134}}
135
136pub_test! {div_rational_prec_round_naive_ref_val(
137 x: &Float,
138 y: Rational,
139 prec: u64,
140 rm: RoundingMode,
141) -> (Float, Ordering) {
142 assert_ne!(prec, 0);
143 match (x, y) {
144 (float_nan!(), _) => (float_nan!(), Equal),
145 (Float(Infinity { sign }), y) => (
146 if y >= 0u32 {
147 Float(Infinity { sign: *sign })
148 } else {
149 Float(Infinity { sign: !*sign })
150 },
151 Equal,
152 ),
153 (Float(Zero { sign }), y) => (
154 match y.sign() {
155 Equal => float_nan!(),
156 Greater => Float(Zero { sign: *sign }),
157 Less => Float(Zero { sign: !*sign }),
158 },
159 Equal,
160 ),
161 (x, y) => {
162 if y == 0 {
163 (Float(Infinity { sign: *x > 0u32 }), Equal)
164 } else {
165 let (mut quotient, o) =
166 Float::from_rational_prec_round(Rational::exact_from(x) / y, prec, rm);
167 if quotient == 0u32 && *x < 0 {
168 quotient.neg_assign();
169 }
170 (quotient, o)
171 }
172 }
173 }
174}}
175
176pub_test! {div_rational_prec_round_naive_ref_ref(
177 x: &Float,
178 y: &Rational,
179 prec: u64,
180 rm: RoundingMode,
181) -> (Float, Ordering) {
182 assert_ne!(prec, 0);
183 match (x, y) {
184 (float_nan!(), _) => (float_nan!(), Equal),
185 (Float(Infinity { sign }), y) => (
186 if *y >= 0u32 {
187 Float(Infinity { sign: *sign })
188 } else {
189 Float(Infinity { sign: !*sign })
190 },
191 Equal,
192 ),
193 (Float(Zero { sign }), y) => (
194 match y.sign() {
195 Equal => float_nan!(),
196 Greater => Float(Zero { sign: *sign }),
197 Less => Float(Zero { sign: !*sign }),
198 },
199 Equal,
200 ),
201 (x, y) => {
202 if *y == 0 {
203 (Float(Infinity { sign: *x > 0u32 }), Equal)
204 } else {
205 let (mut quotient, o) =
206 Float::from_rational_prec_round(Rational::exact_from(x) / y, prec, rm);
207 if quotient == 0u32 && *x < 0 {
208 quotient.neg_assign();
209 }
210 (quotient, o)
211 }
212 }
213 }
214}}
215
216fn div_rational_prec_round_assign_direct(
217 x: &mut Float,
218 y: Rational,
219 prec: u64,
220 mut rm: RoundingMode,
221) -> Ordering {
222 assert_ne!(prec, 0);
223 let sign = y >= 0;
224 let (n, d) = y.into_numerator_and_denominator();
225 if !sign {
226 rm.neg_assign();
227 }
228 let o = match (
229 if n == 0 { None } else { n.checked_log_base_2() },
230 d.checked_log_base_2(),
231 ) {
232 (Some(log_n), Some(log_d)) => {
233 let o = x.set_prec_round(prec, rm);
234 *x <<= log_d;
235 *x >>= log_n;
236 o
237 }
238 (None, Some(log_d)) => {
239 let o = x.div_prec_round_assign(Float::exact_from(n), prec, rm);
240 *x <<= log_d;
241 o
242 }
243 (Some(log_n), None) => {
244 let o = x.mul_prec_round_assign(Float::exact_from(d), prec, rm);
245 *x >>= log_n;
246 o
247 }
248 (None, None) => {
249 let n = Float::exact_from(n);
250 let d = Float::exact_from(d);
251 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
252 x.mul_prec_round_assign(d, mul_prec, Floor);
253 x.div_prec_round_assign(n, prec, rm)
254 }
255 };
256 if sign {
257 o
258 } else {
259 x.neg_assign();
260 o.reverse()
261 }
262}
263
264fn div_rational_prec_round_assign_direct_ref(
265 x: &mut Float,
266 y: &Rational,
267 prec: u64,
268 mut rm: RoundingMode,
269) -> Ordering {
270 assert_ne!(prec, 0);
271 let sign = *y >= 0;
272 let (n, d) = y.numerator_and_denominator_ref();
273 if !sign {
274 rm.neg_assign();
275 }
276 let o = match (
277 if *n == 0 {
278 None
279 } else {
280 n.checked_log_base_2()
281 },
282 d.checked_log_base_2(),
283 ) {
284 (Some(log_n), Some(log_d)) => {
285 let o = x.set_prec_round(prec, rm);
286 *x <<= log_d;
287 *x >>= log_n;
288 o
289 }
290 (None, Some(log_d)) => {
291 let o = x.div_prec_round_assign(Float::exact_from(n), prec, rm);
292 *x <<= log_d;
293 o
294 }
295 (Some(log_n), None) => {
296 let o = x.mul_prec_round_assign(Float::exact_from(d), prec, rm);
297 *x >>= log_n;
298 o
299 }
300 (None, None) => {
301 let n = Float::exact_from(n);
302 let d = Float::exact_from(d);
303 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
304 x.mul_prec_round_assign(d, mul_prec, Floor);
305 x.div_prec_round_assign(n, prec, rm)
306 }
307 };
308 if sign {
309 o
310 } else {
311 x.neg_assign();
312 o.reverse()
313 }
314}
315
316pub_test! {div_rational_prec_round_direct(
317 mut x: Float,
318 y: Rational,
319 prec: u64,
320 rm: RoundingMode,
321) -> (Float, Ordering) {
322 let o = div_rational_prec_round_assign_direct(&mut x, y, prec, rm);
323 (x, o)
324}}
325
326pub_test! {div_rational_prec_round_direct_val_ref(
327 mut x: Float,
328 y: &Rational,
329 prec: u64,
330 rm: RoundingMode,
331) -> (Float, Ordering) {
332 let o = div_rational_prec_round_assign_direct_ref(&mut x, y, prec, rm);
333 (x, o)
334}}
335
336pub_test! {div_rational_prec_round_direct_ref_val(
337 x: &Float,
338 y: Rational,
339 prec: u64,
340 mut rm: RoundingMode,
341) -> (Float, Ordering) {
342 assert_ne!(prec, 0);
343 let sign = y >= 0;
344 let (n, d) = y.into_numerator_and_denominator();
345 if !sign {
346 rm.neg_assign();
347 }
348 let (quotient, o) = match (
349 if n == 0 { None } else { n.checked_log_base_2() },
350 d.checked_log_base_2(),
351 ) {
352 (Some(log_n), Some(log_d)) => {
353 let (quotient, o) = Float::from_float_prec_round_ref(x, prec, rm);
354 (quotient << log_d >> log_n, o)
355 }
356 (None, Some(log_d)) => {
357 let (quotient, o) = x.div_prec_round_ref_val(Float::exact_from(n), prec, rm);
358 (quotient << log_d, o)
359 }
360 (Some(log_n), None) => {
361 let (quotient, o) = x.mul_prec_round_ref_val(Float::exact_from(d), prec, rm);
362 (quotient >> log_n, o)
363 }
364 (None, None) => {
365 let n = Float::exact_from(n);
366 let d = Float::exact_from(d);
367 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
368 x.mul_prec_round_ref_val(d, mul_prec, Floor)
369 .0
370 .div_prec_round(n, prec, rm)
371 }
372 };
373 if sign {
374 (quotient, o)
375 } else {
376 (-quotient, o.reverse())
377 }
378}}
379
380pub_test! {div_rational_prec_round_direct_ref_ref(
381 x: &Float,
382 y: &Rational,
383 prec: u64,
384 mut rm: RoundingMode,
385) -> (Float, Ordering) {
386 assert_ne!(prec, 0);
387 let sign = *y >= 0;
388 let (n, d) = y.numerator_and_denominator_ref();
389 if !sign {
390 rm.neg_assign();
391 }
392 let (quotient, o) = match (
393 if *n == 0 {
394 None
395 } else {
396 n.checked_log_base_2()
397 },
398 d.checked_log_base_2(),
399 ) {
400 (Some(log_n), Some(log_d)) => {
401 let (quotient, o) = Float::from_float_prec_round_ref(x, prec, rm);
402 (quotient << log_d >> log_n, o)
403 }
404 (None, Some(log_d)) => {
405 let (quotient, o) =
406 x.div_prec_round_ref_val(Float::exact_from(n), prec, rm);
407 (quotient << log_d, o)
408 }
409 (Some(log_n), None) => {
410 let (quotient, o) =
411 x.mul_prec_round_ref_val(Float::exact_from(d), prec, rm);
412 (quotient >> log_n, o)
413 }
414 (None, None) => {
415 let n = Float::exact_from(n);
416 let d = Float::exact_from(d);
417 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
418 x.mul_prec_round_ref_val(d, mul_prec, Floor)
419 .0
420 .div_prec_round(n, prec, rm)
421 }
422 };
423 if sign {
424 (quotient, o)
425 } else {
426 (-quotient, o.reverse())
427 }
428}}
429
430pub_test! {rational_div_float_prec_round_naive(
431 x: Rational,
432 y: Float,
433 prec: u64,
434 rm: RoundingMode,
435) -> (Float, Ordering) {
436 assert_ne!(prec, 0);
437 match (x, y) {
438 (_, float_nan!()) => (float_nan!(), Equal),
439 (x, Float(Infinity { sign })) => (
440 if x >= 0u32 {
441 Float(Zero { sign })
442 } else {
443 Float(Zero { sign: !sign })
444 },
445 Equal,
446 ),
447 (x, Float(Zero { sign })) => (
448 match x.sign() {
449 Equal => float_nan!(),
450 Greater => Float(Infinity { sign }),
451 Less => Float(Infinity { sign: !sign }),
452 },
453 Equal,
454 ),
455 (x, y) => {
456 let not_sign = y < 0;
457 let (mut quotient, o) =
458 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
459 if quotient == 0u32 && not_sign {
460 quotient.neg_assign();
461 }
462 (quotient, o)
463 }
464 }
465}}
466
467pub_test! {rational_div_float_prec_round_naive_val_ref(
468 x: Rational,
469 y: &Float,
470 prec: u64,
471 rm: RoundingMode,
472) -> (Float, Ordering) {
473 assert_ne!(prec, 0);
474 match (x, y) {
475 (_, float_nan!()) => (float_nan!(), Equal),
476 (x, Float(Infinity { sign })) => (
477 if x >= 0u32 {
478 Float(Zero { sign: *sign })
479 } else {
480 Float(Zero { sign: !*sign })
481 },
482 Equal,
483 ),
484 (x, Float(Zero { sign })) => (
485 match x.sign() {
486 Equal => float_nan!(),
487 Greater => Float(Infinity { sign: *sign }),
488 Less => Float(Infinity { sign: !*sign }),
489 },
490 Equal,
491 ),
492 (x, y) => {
493 let (mut quotient, o) =
494 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
495 if quotient == 0u32 && *y < 0 {
496 quotient.neg_assign();
497 }
498 (quotient, o)
499 }
500 }
501}}
502
503pub_test! {rational_div_float_prec_round_naive_ref_val(
504 x: &Rational,
505 y: Float,
506 prec: u64,
507 rm: RoundingMode,
508) -> (Float, Ordering) {
509 assert_ne!(prec, 0);
510 match (x, y) {
511 (_, float_nan!()) => (float_nan!(), Equal),
512 (x, Float(Infinity { sign })) => (
513 if *x >= 0u32 {
514 Float(Zero { sign })
515 } else {
516 Float(Zero { sign: !sign })
517 },
518 Equal,
519 ),
520 (x, Float(Zero { sign })) => (
521 match x.sign() {
522 Equal => float_nan!(),
523 Greater => Float(Infinity { sign }),
524 Less => Float(Infinity { sign: !sign }),
525 },
526 Equal,
527 ),
528 (x, y) => {
529 let not_sign = y < 0;
530 let (mut quotient, o) =
531 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
532 if quotient == 0u32 && not_sign {
533 quotient.neg_assign();
534 }
535 (quotient, o)
536 }
537 }
538}}
539
540pub_test! {rational_div_float_prec_round_naive_ref_ref(
541 x: &Rational,
542 y: &Float,
543 prec: u64,
544 rm: RoundingMode,
545) -> (Float, Ordering) {
546 assert_ne!(prec, 0);
547 match (x, y) {
548 (_, float_nan!()) => (float_nan!(), Equal),
549 (x, Float(Infinity { sign })) => (
550 if *x >= 0u32 {
551 Float(Zero { sign: *sign })
552 } else {
553 Float(Zero { sign: !*sign })
554 },
555 Equal,
556 ),
557 (x, Float(Zero { sign })) => (
558 match x.sign() {
559 Equal => float_nan!(),
560 Greater => Float(Infinity { sign: *sign }),
561 Less => Float(Infinity { sign: !*sign }),
562 },
563 Equal,
564 ),
565 (x, y) => {
566 let (mut quotient, o) =
567 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
568 if quotient == 0u32 && *y < 0 {
569 quotient.neg_assign();
570 }
571 (quotient, o)
572 }
573 }
574}}
575
576pub_test! {rational_div_float_prec_round_direct(
577 x: Rational,
578 y: Float,
579 prec: u64,
580 mut rm: RoundingMode,
581) -> (Float, Ordering) {
582 assert_ne!(prec, 0);
583 let sign = x >= 0;
584 let (n, d) = x.into_numerator_and_denominator();
585 if !sign {
586 rm.neg_assign();
587 }
588 let (quotient, o) = match (
589 if n == 0 { None } else { n.checked_log_base_2() },
590 d.checked_log_base_2(),
591 ) {
592 (Some(log_n), Some(log_d)) => {
593 let (quotient, o) = Float::reciprocal_prec_round(y, prec, rm);
594 (quotient << log_n >> log_d, o)
595 }
596 (None, Some(log_d)) => {
597 let (quotient, o) = Float::exact_from(n).div_prec_round(y, prec, rm);
598 (quotient >> log_d, o)
599 }
600 (Some(log_n), None) => {
601 let d = Float::exact_from(d);
602 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
603 let (quotient, o) = y
604 .mul_prec_round(d, mul_prec, Floor)
605 .0
606 .reciprocal_prec_round(prec, rm);
607 (quotient << log_n, o)
608 }
609 (None, None) => {
610 let n = Float::exact_from(n);
611 let d = Float::exact_from(d);
612 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
613 n.div_prec_round(y.mul_prec_round(d, mul_prec, Floor).0, prec, rm)
614 }
615 };
616 if sign {
617 (quotient, o)
618 } else {
619 (-quotient, o.reverse())
620 }
621}}
622
623pub_test! {rational_div_float_prec_round_direct_val_ref(
624 x: Rational,
625 y: &Float,
626 prec: u64,
627 mut rm: RoundingMode,
628) -> (Float, Ordering) {
629 assert_ne!(prec, 0);
630 let sign = x >= 0;
631 let (n, d) = x.into_numerator_and_denominator();
632 if !sign {
633 rm.neg_assign();
634 }
635 let (quotient, o) = match (
636 if n == 0 { None } else { n.checked_log_base_2() },
637 d.checked_log_base_2(),
638 ) {
639 (Some(log_n), Some(log_d)) => {
640 let (quotient, o) = Float::reciprocal_prec_round_ref(y, prec, rm);
641 (quotient << log_n >> log_d, o)
642 }
643 (None, Some(log_d)) => {
644 let (quotient, o) = Float::exact_from(n).div_prec_round_val_ref(y, prec, rm);
645 (quotient >> log_d, o)
646 }
647 (Some(log_n), None) => {
648 let d = Float::exact_from(d);
649 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
650 let (quotient, o) = y
651 .mul_prec_round_ref_val(d, mul_prec, Floor)
652 .0
653 .reciprocal_prec_round(prec, rm);
654 (quotient << log_n, o)
655 }
656 (None, None) => {
657 let n = Float::exact_from(n);
658 let d = Float::exact_from(d);
659 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
660 n.div_prec_round(y.mul_prec_round_ref_val(d, mul_prec, Floor).0, prec, rm)
661 }
662 };
663 if sign {
664 (quotient, o)
665 } else {
666 (-quotient, o.reverse())
667 }
668}}
669
670pub_test! {rational_div_float_prec_round_direct_ref_val(
671 x: &Rational,
672 y: Float,
673 prec: u64,
674 mut rm: RoundingMode,
675) -> (Float, Ordering) {
676 assert_ne!(prec, 0);
677 let sign = *x >= 0;
678 let (n, d) = x.numerator_and_denominator_ref();
679 if !sign {
680 rm.neg_assign();
681 }
682 let (quotient, o) = match (
683 if *n == 0 {
684 None
685 } else {
686 n.checked_log_base_2()
687 },
688 d.checked_log_base_2(),
689 ) {
690 (Some(log_n), Some(log_d)) => {
691 let (quotient, o) = Float::reciprocal_prec_round(y, prec, rm);
692 (quotient << log_n >> log_d, o)
693 }
694 (None, Some(log_d)) => {
695 let (quotient, o) = Float::exact_from(n).div_prec_round(y, prec, rm);
696 (quotient >> log_d, o)
697 }
698 (Some(log_n), None) => {
699 let d = Float::exact_from(d);
700 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
701 let (quotient, o) = y
702 .mul_prec_round(d, mul_prec, Floor)
703 .0
704 .reciprocal_prec_round(prec, rm);
705 (quotient << log_n, o)
706 }
707 (None, None) => {
708 let n = Float::exact_from(n);
709 let d = Float::exact_from(d);
710 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
711 n.div_prec_round(y.mul_prec_round(d, mul_prec, Floor).0, prec, rm)
712 }
713 };
714 if sign {
715 (quotient, o)
716 } else {
717 (-quotient, o.reverse())
718 }
719}}
720
721pub_test! {rational_div_float_prec_round_direct_ref_ref(
722 x: &Rational,
723 y: &Float,
724 prec: u64,
725 mut rm: RoundingMode,
726) -> (Float, Ordering) {
727 assert_ne!(prec, 0);
728 let sign = *x >= 0;
729 let (n, d) = x.numerator_and_denominator_ref();
730 if !sign {
731 rm.neg_assign();
732 }
733 let (quotient, o) = match (
734 if *n == 0 {
735 None
736 } else {
737 n.checked_log_base_2()
738 },
739 d.checked_log_base_2(),
740 ) {
741 (Some(log_n), Some(log_d)) => {
742 let (quotient, o) = Float::reciprocal_prec_round_ref(y, prec, rm);
743 (quotient << log_n >> log_d, o)
744 }
745 (None, Some(log_d)) => {
746 let (quotient, o) =
747 Float::exact_from(n).div_prec_round_val_ref(y, prec, rm);
748 (quotient >> log_d, o)
749 }
750 (Some(log_n), None) => {
751 let d = Float::exact_from(d);
752 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
753 let (quotient, o) = y
754 .mul_prec_round_ref_val(d, mul_prec, Floor)
755 .0
756 .reciprocal_prec_round(prec, rm);
757 (quotient << log_n, o)
758 }
759 (None, None) => {
760 let n = Float::exact_from(n);
761 let d = Float::exact_from(d);
762 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
763 n.div_prec_round(y.mul_prec_round_ref_val(d, mul_prec, Floor).0, prec, rm)
764 }
765 };
766 if sign {
767 (quotient, o)
768 } else {
769 (-quotient, o.reverse())
770 }
771}}
772
773impl Float {
774 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
775 /// specified rounding mode. Both [`Float`]s are taken by value. An [`Ordering`] is also
776 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
777 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
778 /// function returns a `NaN` it also returns `Equal`.
779 ///
780 /// See [`RoundingMode`] for a description of the possible rounding modes.
781 ///
782 /// $$
783 /// f(x,y,p,m) = x/y+\varepsilon.
784 /// $$
785 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
786 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
787 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
788 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
789 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
790 ///
791 /// If the output has a precision, it is `prec`.
792 ///
793 /// Special cases:
794 /// - $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) =
795 /// \text{NaN}$
796 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
797 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
798 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
799 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
800 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
801 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
802 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
803 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
804 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
805 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
806 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
807 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
808 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
809 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $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(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
812 ///
813 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec`] instead. If you
814 /// know that your target precision is the maximum of the precisions of the two inputs, consider
815 /// using [`Float::div_round`] instead. If both of these things are true, consider using `/`
816 /// instead.
817 ///
818 /// # Worst-case complexity
819 /// $T(n) = O(n \log n \log\log n)$
820 ///
821 /// $M(n) = O(n \log n)$
822 ///
823 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
824 /// other.significant_bits(), `prec`)`.
825 ///
826 /// # Panics
827 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
828 ///
829 /// # Examples
830 /// ```
831 /// use core::f64::consts::{E, PI};
832 /// use malachite_base::rounding_modes::RoundingMode::*;
833 /// use malachite_float::Float;
834 /// use std::cmp::Ordering::*;
835 ///
836 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 5, Floor);
837 /// assert_eq!(quotient.to_string(), "1.12");
838 /// assert_eq!(o, Less);
839 ///
840 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 5, Ceiling);
841 /// assert_eq!(quotient.to_string(), "1.19");
842 /// assert_eq!(o, Greater);
843 ///
844 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 5, Nearest);
845 /// assert_eq!(quotient.to_string(), "1.12");
846 /// assert_eq!(o, Less);
847 ///
848 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 20, Floor);
849 /// assert_eq!(quotient.to_string(), "1.155725");
850 /// assert_eq!(o, Less);
851 ///
852 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 20, Ceiling);
853 /// assert_eq!(quotient.to_string(), "1.155727");
854 /// assert_eq!(o, Greater);
855 ///
856 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 20, Nearest);
857 /// assert_eq!(quotient.to_string(), "1.155727");
858 /// assert_eq!(o, Greater);
859 /// ```
860 #[inline]
861 pub fn div_prec_round(
862 mut self,
863 other: Float,
864 prec: u64,
865 rm: RoundingMode,
866 ) -> (Float, Ordering) {
867 let o = self.div_prec_round_assign(other, prec, rm);
868 (self, o)
869 }
870
871 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
872 /// specified rounding mode. The first [`Float`] is are taken by value and the second by
873 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
874 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
875 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
876 ///
877 /// See [`RoundingMode`] for a description of the possible rounding modes.
878 ///
879 /// $$
880 /// f(x,y,p,m) = x/y+\varepsilon.
881 /// $$
882 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
883 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
884 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
885 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
886 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
887 ///
888 /// If the output has a precision, it is `prec`.
889 ///
890 /// Special cases:
891 /// - $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) =
892 /// \text{NaN}$
893 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
894 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
895 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
896 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
897 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
898 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
899 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
900 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
901 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
902 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
903 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
904 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
905 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
906 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $x<0.0$
907 /// - $f(x,-\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
908 /// - $f(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
909 ///
910 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_val_ref`] instead.
911 /// If you know that your target precision is the maximum of the precisions of the two inputs,
912 /// consider using [`Float::div_round_val_ref`] instead. If both of these things are true,
913 /// consider using `/` instead.
914 ///
915 /// # Worst-case complexity
916 /// $T(n) = O(n \log n \log\log n)$
917 ///
918 /// $M(n) = O(n \log n)$
919 ///
920 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
921 /// other.significant_bits(), `prec`)`.
922 ///
923 /// # Panics
924 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
925 ///
926 /// # Examples
927 /// ```
928 /// use core::f64::consts::{E, PI};
929 /// use malachite_base::rounding_modes::RoundingMode::*;
930 /// use malachite_float::Float;
931 /// use std::cmp::Ordering::*;
932 ///
933 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 5, Floor);
934 /// assert_eq!(quotient.to_string(), "1.12");
935 /// assert_eq!(o, Less);
936 ///
937 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 5, Ceiling);
938 /// assert_eq!(quotient.to_string(), "1.19");
939 /// assert_eq!(o, Greater);
940 ///
941 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 5, Nearest);
942 /// assert_eq!(quotient.to_string(), "1.12");
943 /// assert_eq!(o, Less);
944 ///
945 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 20, Floor);
946 /// assert_eq!(quotient.to_string(), "1.155725");
947 /// assert_eq!(o, Less);
948 ///
949 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 20, Ceiling);
950 /// assert_eq!(quotient.to_string(), "1.155727");
951 /// assert_eq!(o, Greater);
952 ///
953 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 20, Nearest);
954 /// assert_eq!(quotient.to_string(), "1.155727");
955 /// assert_eq!(o, Greater);
956 /// ```
957 #[inline]
958 pub fn div_prec_round_val_ref(
959 mut self,
960 other: &Float,
961 prec: u64,
962 rm: RoundingMode,
963 ) -> (Float, Ordering) {
964 let o = self.div_prec_round_assign_ref(other, prec, rm);
965 (self, o)
966 }
967
968 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
969 /// specified rounding mode. The first [`Float`] is are taken by reference and the second by
970 /// value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
971 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
972 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
973 ///
974 /// See [`RoundingMode`] for a description of the possible rounding modes.
975 ///
976 /// $$
977 /// f(x,y,p,m) = x/y+\varepsilon.
978 /// $$
979 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
980 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
981 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
982 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
983 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
984 ///
985 /// If the output has a precision, it is `prec`.
986 ///
987 /// Special cases:
988 /// - $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) =
989 /// \text{NaN}$
990 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
991 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
992 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
993 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
994 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
995 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
996 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
997 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
998 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
999 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1000 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1001 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1002 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1003 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $x<0.0$
1004 /// - $f(x,-\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1005 /// - $f(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1006 ///
1007 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_ref_val`] instead.
1008 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1009 /// consider using [`Float::div_round_ref_val`] instead. If both of these things are true,
1010 /// consider using `/` instead.
1011 ///
1012 /// # Worst-case complexity
1013 /// $T(n) = O(n \log n \log\log n)$
1014 ///
1015 /// $M(n) = O(n \log n)$
1016 ///
1017 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1018 /// other.significant_bits(), `prec`)`.
1019 ///
1020 /// # Panics
1021 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
1022 ///
1023 /// # Examples
1024 /// ```
1025 /// use core::f64::consts::{E, PI};
1026 /// use malachite_base::rounding_modes::RoundingMode::*;
1027 /// use malachite_float::Float;
1028 /// use std::cmp::Ordering::*;
1029 ///
1030 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 5, Floor);
1031 /// assert_eq!(quotient.to_string(), "1.12");
1032 /// assert_eq!(o, Less);
1033 ///
1034 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 5, Ceiling);
1035 /// assert_eq!(quotient.to_string(), "1.19");
1036 /// assert_eq!(o, Greater);
1037 ///
1038 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 5, Nearest);
1039 /// assert_eq!(quotient.to_string(), "1.12");
1040 /// assert_eq!(o, Less);
1041 ///
1042 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 20, Floor);
1043 /// assert_eq!(quotient.to_string(), "1.155725");
1044 /// assert_eq!(o, Less);
1045 ///
1046 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 20, Ceiling);
1047 /// assert_eq!(quotient.to_string(), "1.155727");
1048 /// assert_eq!(o, Greater);
1049 ///
1050 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 20, Nearest);
1051 /// assert_eq!(quotient.to_string(), "1.155727");
1052 /// assert_eq!(o, Greater);
1053 /// ```
1054 #[inline]
1055 pub fn div_prec_round_ref_val(
1056 &self,
1057 other: Float,
1058 prec: u64,
1059 rm: RoundingMode,
1060 ) -> (Float, Ordering) {
1061 assert_ne!(prec, 0);
1062 match (self, other) {
1063 (float_nan!(), _)
1064 | (_, float_nan!())
1065 | (float_either_infinity!(), float_either_infinity!())
1066 | (float_either_zero!(), float_either_zero!()) => (float_nan!(), Equal),
1067 (
1068 Float(Infinity { sign: x_sign }),
1069 Float(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
1070 )
1071 | (Float(Finite { sign: x_sign, .. }), Float(Zero { sign: y_sign })) => (
1072 Float(Infinity {
1073 sign: *x_sign == y_sign,
1074 }),
1075 Equal,
1076 ),
1077 (
1078 Float(Zero { sign: x_sign }),
1079 Float(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
1080 )
1081 | (Float(Finite { sign: x_sign, .. }), Float(Infinity { sign: y_sign })) => (
1082 Float(Zero {
1083 sign: *x_sign == y_sign,
1084 }),
1085 Equal,
1086 ),
1087 (
1088 Float(Finite {
1089 sign: x_sign,
1090 exponent: x_exp,
1091 precision: x_prec,
1092 significand: x,
1093 }),
1094 Float(Finite {
1095 sign: y_sign,
1096 exponent: y_exp,
1097 precision: y_prec,
1098 significand: mut y,
1099 }),
1100 ) => {
1101 if y.is_power_of_2() {
1102 let (mut quotient, mut o) =
1103 Float::from_float_prec_round_ref(self, prec, if y_sign { rm } else { -rm });
1104 if !y_sign {
1105 quotient.neg_assign();
1106 o = o.reverse();
1107 }
1108 return (quotient >> y_exp.checked_sub(1).unwrap(), o);
1109 }
1110 let sign = *x_sign == y_sign;
1111 let (quotient, exp_offset, o) = div_float_significands_ref_val(
1112 x,
1113 *x_prec,
1114 &mut y,
1115 y_prec,
1116 prec,
1117 if sign { rm } else { -rm },
1118 );
1119 let exp = x_exp
1120 .checked_sub(y_exp)
1121 .unwrap()
1122 .checked_add(i32::exact_from(exp_offset))
1123 .unwrap();
1124 (
1125 Float(Finite {
1126 sign,
1127 exponent: exp,
1128 precision: prec,
1129 significand: quotient,
1130 }),
1131 if sign { o } else { o.reverse() },
1132 )
1133 }
1134 }
1135 }
1136
1137 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
1138 /// specified rounding mode. Both [`Float`]s are taken by reference. An [`Ordering`] is also
1139 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
1140 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
1141 /// function returns a `NaN` it also returns `Equal`.
1142 ///
1143 /// See [`RoundingMode`] for a description of the possible rounding modes.
1144 ///
1145 /// $$
1146 /// f(x,y,p,m) = x/y+\varepsilon.
1147 /// $$
1148 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1149 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1150 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
1151 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
1152 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1153 ///
1154 /// If the output has a precision, it is `prec`.
1155 ///
1156 /// Special cases:
1157 /// - $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) =
1158 /// \text{NaN}$
1159 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
1160 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
1161 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
1162 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
1163 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
1164 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
1165 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
1166 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
1167 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
1168 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1169 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1170 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1171 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1172 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $x<0.0$
1173 /// - $f(x,-\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1174 /// - $f(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1175 ///
1176 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_ref_ref`] instead.
1177 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1178 /// consider using [`Float::div_round_ref_ref`] instead. If both of these things are true,
1179 /// consider using `/` instead.
1180 ///
1181 /// # Worst-case complexity
1182 /// $T(n) = O(n \log n \log\log n)$
1183 ///
1184 /// $M(n) = O(n \log n)$
1185 ///
1186 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1187 /// other.significant_bits(), `prec`)`.
1188 ///
1189 /// # Panics
1190 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
1191 ///
1192 /// # Examples
1193 /// ```
1194 /// use core::f64::consts::{E, PI};
1195 /// use malachite_base::rounding_modes::RoundingMode::*;
1196 /// use malachite_float::Float;
1197 /// use std::cmp::Ordering::*;
1198 ///
1199 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 5, Floor);
1200 /// assert_eq!(quotient.to_string(), "1.12");
1201 /// assert_eq!(o, Less);
1202 ///
1203 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 5, Ceiling);
1204 /// assert_eq!(quotient.to_string(), "1.19");
1205 /// assert_eq!(o, Greater);
1206 ///
1207 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 5, Nearest);
1208 /// assert_eq!(quotient.to_string(), "1.12");
1209 /// assert_eq!(o, Less);
1210 ///
1211 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 20, Floor);
1212 /// assert_eq!(quotient.to_string(), "1.155725");
1213 /// assert_eq!(o, Less);
1214 ///
1215 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 20, Ceiling);
1216 /// assert_eq!(quotient.to_string(), "1.155727");
1217 /// assert_eq!(o, Greater);
1218 ///
1219 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 20, Nearest);
1220 /// assert_eq!(quotient.to_string(), "1.155727");
1221 /// assert_eq!(o, Greater);
1222 /// ```
1223 #[inline]
1224 pub fn div_prec_round_ref_ref(
1225 &self,
1226 other: &Float,
1227 prec: u64,
1228 rm: RoundingMode,
1229 ) -> (Float, Ordering) {
1230 assert_ne!(prec, 0);
1231 match (self, other) {
1232 (float_nan!(), _)
1233 | (_, float_nan!())
1234 | (float_either_infinity!(), float_either_infinity!())
1235 | (float_either_zero!(), float_either_zero!()) => (float_nan!(), Equal),
1236 (
1237 Float(Infinity { sign: x_sign }),
1238 Float(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
1239 )
1240 | (Float(Finite { sign: x_sign, .. }), Float(Zero { sign: y_sign })) => (
1241 Float(Infinity {
1242 sign: x_sign == y_sign,
1243 }),
1244 Equal,
1245 ),
1246 (
1247 Float(Zero { sign: x_sign }),
1248 Float(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
1249 )
1250 | (Float(Finite { sign: x_sign, .. }), Float(Infinity { sign: y_sign })) => (
1251 Float(Zero {
1252 sign: x_sign == y_sign,
1253 }),
1254 Equal,
1255 ),
1256 (
1257 Float(Finite {
1258 sign: x_sign,
1259 exponent: x_exp,
1260 precision: x_prec,
1261 significand: x,
1262 }),
1263 Float(Finite {
1264 sign: y_sign,
1265 exponent: y_exp,
1266 precision: y_prec,
1267 significand: y,
1268 }),
1269 ) => {
1270 if y.is_power_of_2() {
1271 let (mut quotient, mut o) = Float::from_float_prec_round_ref(
1272 self,
1273 prec,
1274 if *y_sign { rm } else { -rm },
1275 );
1276 if !y_sign {
1277 quotient.neg_assign();
1278 o = o.reverse();
1279 }
1280 return (quotient >> y_exp.checked_sub(1).unwrap(), o);
1281 }
1282 let sign = x_sign == y_sign;
1283 let (quotient, exp_offset, o) = div_float_significands_ref_ref(
1284 x,
1285 *x_prec,
1286 y,
1287 *y_prec,
1288 prec,
1289 if sign { rm } else { -rm },
1290 );
1291 let exp = x_exp
1292 .checked_sub(*y_exp)
1293 .unwrap()
1294 .checked_add(i32::exact_from(exp_offset))
1295 .unwrap();
1296 (
1297 Float(Finite {
1298 sign,
1299 exponent: exp,
1300 precision: prec,
1301 significand: quotient,
1302 }),
1303 if sign { o } else { o.reverse() },
1304 )
1305 }
1306 }
1307 }
1308
1309 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1310 /// Both [`Float`]s are taken by value. An [`Ordering`] is also returned, indicating whether the
1311 /// rounded quotient is less than, equal to, or greater than the exact quotient. Although `NaN`s
1312 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
1313 /// `Equal`.
1314 ///
1315 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1316 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1317 /// description of the `Nearest` rounding mode.
1318 ///
1319 /// $$
1320 /// f(x,y,p) = x/y+\varepsilon.
1321 /// $$
1322 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1323 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1324 ///
1325 /// If the output has a precision, it is `prec`.
1326 ///
1327 /// Special cases:
1328 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1329 /// \text{NaN}$
1330 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
1331 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
1332 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
1333 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
1334 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
1335 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
1336 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
1337 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
1338 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
1339 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
1340 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1341 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1342 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
1343 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
1344 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1345 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1346 ///
1347 /// If you want to use a rounding mode other than `Nearest`, consider using
1348 /// [`Float::div_prec_round`] instead. If you know that your target precision is the maximum of
1349 /// the precisions of the two inputs, consider using `/` instead.
1350 ///
1351 /// # Worst-case complexity
1352 /// $T(n) = O(n \log n \log\log n)$
1353 ///
1354 /// $M(n) = O(n \log n)$
1355 ///
1356 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1357 /// other.significant_bits(), `prec`)`.
1358 ///
1359 /// # Examples
1360 /// ```
1361 /// use core::f64::consts::{E, PI};
1362 /// use malachite_float::Float;
1363 /// use std::cmp::Ordering::*;
1364 ///
1365 /// let (quotient, o) = Float::from(PI).div_prec(Float::from(E), 5);
1366 /// assert_eq!(quotient.to_string(), "1.12");
1367 /// assert_eq!(o, Less);
1368 ///
1369 /// let (quotient, o) = Float::from(PI).div_prec(Float::from(E), 20);
1370 /// assert_eq!(quotient.to_string(), "1.155727");
1371 /// assert_eq!(o, Greater);
1372 /// ```
1373 #[inline]
1374 pub fn div_prec(self, other: Float, prec: u64) -> (Float, Ordering) {
1375 self.div_prec_round(other, prec, Nearest)
1376 }
1377
1378 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1379 /// The first [`Float`] is taken by value and the second by reference. An [`Ordering`] is also
1380 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
1381 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
1382 /// function returns a `NaN` it also returns `Equal`.
1383 ///
1384 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1385 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1386 /// description of the `Nearest` rounding mode.
1387 ///
1388 /// $$
1389 /// f(x,y,p) = x/y+\varepsilon.
1390 /// $$
1391 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1392 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1393 ///
1394 /// If the output has a precision, it is `prec`.
1395 ///
1396 /// Special cases:
1397 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1398 /// \text{NaN}$
1399 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
1400 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
1401 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
1402 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
1403 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
1404 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
1405 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
1406 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
1407 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
1408 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
1409 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1410 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1411 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
1412 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
1413 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1414 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1415 ///
1416 /// If you want to use a rounding mode other than `Nearest`, consider using
1417 /// [`Float::div_prec_round_val_ref`] instead. If you know that your target precision is the
1418 /// maximum of the precisions of the two inputs, consider using `/` instead.
1419 ///
1420 /// # Worst-case complexity
1421 /// $T(n) = O(n \log n \log\log n)$
1422 ///
1423 /// $M(n) = O(n \log n)$
1424 ///
1425 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1426 /// other.significant_bits(), `prec`)`.
1427 ///
1428 /// # Examples
1429 /// ```
1430 /// use core::f64::consts::{E, PI};
1431 /// use malachite_float::Float;
1432 /// use std::cmp::Ordering::*;
1433 ///
1434 /// let (quotient, o) = Float::from(PI).div_prec_val_ref(&Float::from(E), 5);
1435 /// assert_eq!(quotient.to_string(), "1.12");
1436 /// assert_eq!(o, Less);
1437 ///
1438 /// let (quotient, o) = Float::from(PI).div_prec_val_ref(&Float::from(E), 20);
1439 /// assert_eq!(quotient.to_string(), "1.155727");
1440 /// assert_eq!(o, Greater);
1441 /// ```
1442 #[inline]
1443 pub fn div_prec_val_ref(self, other: &Float, prec: u64) -> (Float, Ordering) {
1444 self.div_prec_round_val_ref(other, prec, Nearest)
1445 }
1446
1447 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1448 /// The first [`Float`] is taken by reference and the second by value. An [`Ordering`] is also
1449 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
1450 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
1451 /// function returns a `NaN` it also returns `Equal`.
1452 ///
1453 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1454 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1455 /// description of the `Nearest` rounding mode.
1456 ///
1457 /// $$
1458 /// f(x,y,p) = x/y+\varepsilon.
1459 /// $$
1460 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1461 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1462 ///
1463 /// If the output has a precision, it is `prec`.
1464 ///
1465 /// Special cases:
1466 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1467 /// \text{NaN}$
1468 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
1469 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
1470 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
1471 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
1472 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
1473 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
1474 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
1475 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
1476 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
1477 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
1478 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1479 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1480 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
1481 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
1482 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1483 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1484 ///
1485 /// If you want to use a rounding mode other than `Nearest`, consider using
1486 /// [`Float::div_prec_round_ref_val`] instead. If you know that your target precision is the
1487 /// maximum of the precisions of the two inputs, consider using `/` instead.
1488 ///
1489 /// # Worst-case complexity
1490 /// $T(n) = O(n \log n \log\log n)$
1491 ///
1492 /// $M(n) = O(n \log n)$
1493 ///
1494 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1495 /// other.significant_bits(), `prec`)`.
1496 ///
1497 /// # Examples
1498 /// ```
1499 /// use core::f64::consts::{E, PI};
1500 /// use malachite_float::Float;
1501 /// use std::cmp::Ordering::*;
1502 ///
1503 /// let (quotient, o) = Float::from(PI).div_prec_ref_val(Float::from(E), 5);
1504 /// assert_eq!(quotient.to_string(), "1.12");
1505 /// assert_eq!(o, Less);
1506 ///
1507 /// let (quotient, o) = Float::from(PI).div_prec_ref_val(Float::from(E), 20);
1508 /// assert_eq!(quotient.to_string(), "1.155727");
1509 /// assert_eq!(o, Greater);
1510 /// ```
1511 #[inline]
1512 pub fn div_prec_ref_val(&self, other: Float, prec: u64) -> (Float, Ordering) {
1513 self.div_prec_round_ref_val(other, prec, Nearest)
1514 }
1515
1516 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1517 /// Both [`Float`]s are taken by reference. An [`Ordering`] is also returned, indicating whether
1518 /// the rounded quotient is less than, equal to, or greater than the exact quotient. Although
1519 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
1520 /// returns `Equal`.
1521 ///
1522 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1523 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1524 /// description of the `Nearest` rounding mode.
1525 ///
1526 /// $$
1527 /// f(x,y,p) = x/y+\varepsilon.
1528 /// $$
1529 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1530 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1531 ///
1532 /// If the output has a precision, it is `prec`.
1533 ///
1534 /// Special cases:
1535 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1536 /// \text{NaN}$
1537 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
1538 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
1539 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
1540 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
1541 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
1542 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
1543 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
1544 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
1545 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
1546 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
1547 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1548 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1549 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
1550 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
1551 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1552 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1553 ///
1554 /// If you want to use a rounding mode other than `Nearest`, consider using
1555 /// [`Float::div_prec_round_ref_ref`] instead. If you know that your target precision is the
1556 /// maximum of the precisions of the two inputs, consider using `/` instead.
1557 ///
1558 /// # Worst-case complexity
1559 /// $T(n) = O(n \log n \log\log n)$
1560 ///
1561 /// $M(n) = O(n \log n)$
1562 ///
1563 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1564 /// other.significant_bits(), `prec`)`.
1565 ///
1566 /// # Examples
1567 /// ```
1568 /// use core::f64::consts::{E, PI};
1569 /// use malachite_float::Float;
1570 /// use std::cmp::Ordering::*;
1571 ///
1572 /// let (quotient, o) = Float::from(PI).div_prec_ref_ref(&Float::from(E), 5);
1573 /// assert_eq!(quotient.to_string(), "1.12");
1574 /// assert_eq!(o, Less);
1575 ///
1576 /// let (quotient, o) = Float::from(PI).div_prec_ref_ref(&Float::from(E), 20);
1577 /// assert_eq!(quotient.to_string(), "1.155727");
1578 /// assert_eq!(o, Greater);
1579 /// ```
1580 #[inline]
1581 pub fn div_prec_ref_ref(&self, other: &Float, prec: u64) -> (Float, Ordering) {
1582 self.div_prec_round_ref_ref(other, prec, Nearest)
1583 }
1584
1585 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. Both
1586 /// [`Float`]s are taken by value. An [`Ordering`] is also returned, indicating whether the
1587 /// rounded quotient is less than, equal to, or greater than the exact quotient. Although `NaN`s
1588 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
1589 /// `Equal`.
1590 ///
1591 /// The precision of the output is the maximum of the precision of the inputs. See
1592 /// [`RoundingMode`] for a description of the possible rounding modes.
1593 ///
1594 /// $$
1595 /// f(x,y,m) = x/y+\varepsilon.
1596 /// $$
1597 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1598 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1599 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
1600 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
1601 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
1602 ///
1603 /// If the output has a precision, it is the maximum of the precisions of the inputs.
1604 ///
1605 /// Special cases:
1606 /// - $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) =
1607 /// \text{NaN}$
1608 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
1609 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
1610 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
1611 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
1612 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
1613 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
1614 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
1615 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
1616 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
1617 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1618 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1619 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1620 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1621 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
1622 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1623 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1624 ///
1625 /// If you want to specify an output precision, consider using [`Float::div_prec_round`]
1626 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
1627 /// instead.
1628 ///
1629 /// # Worst-case complexity
1630 /// $T(n) = O(n \log n \log\log n)$
1631 ///
1632 /// $M(n) = O(n \log n)$
1633 ///
1634 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1635 /// other.significant_bits())`.
1636 ///
1637 /// # Panics
1638 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
1639 /// represent the output.
1640 ///
1641 /// # Examples
1642 /// ```
1643 /// use core::f64::consts::{E, PI};
1644 /// use malachite_base::rounding_modes::RoundingMode::*;
1645 /// use malachite_float::Float;
1646 /// use std::cmp::Ordering::*;
1647 ///
1648 /// let (quotient, o) = Float::from(PI).div_round(Float::from(E), Floor);
1649 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
1650 /// assert_eq!(o, Less);
1651 ///
1652 /// let (quotient, o) = Float::from(PI).div_round(Float::from(E), Ceiling);
1653 /// assert_eq!(quotient.to_string(), "1.155727349790922");
1654 /// assert_eq!(o, Greater);
1655 ///
1656 /// let (quotient, o) = Float::from(PI).div_round(Float::from(E), Nearest);
1657 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
1658 /// assert_eq!(o, Less);
1659 /// ```
1660 #[inline]
1661 pub fn div_round(self, other: Float, rm: RoundingMode) -> (Float, Ordering) {
1662 let prec = max(self.significant_bits(), other.significant_bits());
1663 self.div_prec_round(other, prec, rm)
1664 }
1665
1666 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. The first
1667 /// [`Float`] is taken by value and the second by reference. An [`Ordering`] is also returned,
1668 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
1669 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function
1670 /// returns a `NaN` it also returns `Equal`.
1671 ///
1672 /// The precision of the output is the maximum of the precision of the inputs. See
1673 /// [`RoundingMode`] for a description of the possible rounding modes.
1674 ///
1675 /// $$
1676 /// f(x,y,m) = x/y+\varepsilon.
1677 /// $$
1678 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1679 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1680 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
1681 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
1682 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
1683 ///
1684 /// If the output has a precision, it is the maximum of the precisions of the inputs.
1685 ///
1686 /// Special cases:
1687 /// - $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) =
1688 /// \text{NaN}$
1689 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
1690 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
1691 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
1692 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
1693 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
1694 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
1695 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
1696 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
1697 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
1698 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1699 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1700 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1701 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1702 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
1703 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1704 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1705 ///
1706 /// If you want to specify an output precision, consider using [`Float::div_prec_round_val_ref`]
1707 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
1708 /// instead.
1709 ///
1710 /// # Worst-case complexity
1711 /// $T(n) = O(n \log n \log\log n)$
1712 ///
1713 /// $M(n) = O(n \log n)$
1714 ///
1715 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1716 /// other.significant_bits())`.
1717 ///
1718 /// # Panics
1719 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
1720 /// represent the output.
1721 ///
1722 /// # Examples
1723 /// ```
1724 /// use core::f64::consts::{E, PI};
1725 /// use malachite_base::rounding_modes::RoundingMode::*;
1726 /// use malachite_float::Float;
1727 /// use std::cmp::Ordering::*;
1728 ///
1729 /// let (quotient, o) = Float::from(PI).div_round_val_ref(&Float::from(E), Floor);
1730 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
1731 /// assert_eq!(o, Less);
1732 ///
1733 /// let (quotient, o) = Float::from(PI).div_round_val_ref(&Float::from(E), Ceiling);
1734 /// assert_eq!(quotient.to_string(), "1.155727349790922");
1735 /// assert_eq!(o, Greater);
1736 ///
1737 /// let (quotient, o) = Float::from(PI).div_round_val_ref(&Float::from(E), Nearest);
1738 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
1739 /// assert_eq!(o, Less);
1740 /// ```
1741 #[inline]
1742 pub fn div_round_val_ref(self, other: &Float, rm: RoundingMode) -> (Float, Ordering) {
1743 let prec = max(self.significant_bits(), other.significant_bits());
1744 self.div_prec_round_val_ref(other, prec, rm)
1745 }
1746
1747 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. The first
1748 /// [`Float`] is taken by reference and the second by value. An [`Ordering`] is also returned,
1749 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
1750 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function
1751 /// returns a `NaN` it also returns `Equal`.
1752 ///
1753 /// The precision of the output is the maximum of the precision of the inputs. See
1754 /// [`RoundingMode`] for a description of the possible rounding modes.
1755 ///
1756 /// $$
1757 /// f(x,y,m) = x/y+\varepsilon.
1758 /// $$
1759 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1760 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1761 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
1762 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
1763 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
1764 ///
1765 /// If the output has a precision, it is the maximum of the precisions of the inputs.
1766 ///
1767 /// Special cases:
1768 /// - $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) =
1769 /// \text{NaN}$
1770 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
1771 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
1772 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
1773 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
1774 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
1775 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
1776 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
1777 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
1778 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
1779 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1780 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1781 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1782 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1783 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
1784 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1785 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1786 ///
1787 /// If you want to specify an output precision, consider using [`Float::div_prec_round_ref_val`]
1788 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
1789 /// instead.
1790 ///
1791 /// # Worst-case complexity
1792 /// $T(n) = O(n \log n \log\log n)$
1793 ///
1794 /// $M(n) = O(n \log n)$
1795 ///
1796 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1797 /// other.significant_bits())`.
1798 ///
1799 /// # Panics
1800 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
1801 /// represent the output.
1802 ///
1803 /// # Examples
1804 /// ```
1805 /// use core::f64::consts::{E, PI};
1806 /// use malachite_base::rounding_modes::RoundingMode::*;
1807 /// use malachite_float::Float;
1808 /// use std::cmp::Ordering::*;
1809 ///
1810 /// let (quotient, o) = Float::from(PI).div_round_ref_val(Float::from(E), Floor);
1811 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
1812 /// assert_eq!(o, Less);
1813 ///
1814 /// let (quotient, o) = Float::from(PI).div_round_ref_val(Float::from(E), Ceiling);
1815 /// assert_eq!(quotient.to_string(), "1.155727349790922");
1816 /// assert_eq!(o, Greater);
1817 ///
1818 /// let (quotient, o) = Float::from(PI).div_round_ref_val(Float::from(E), Nearest);
1819 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
1820 /// assert_eq!(o, Less);
1821 /// ```
1822 #[inline]
1823 pub fn div_round_ref_val(&self, other: Float, rm: RoundingMode) -> (Float, Ordering) {
1824 let prec = max(self.significant_bits(), other.significant_bits());
1825 self.div_prec_round_ref_val(other, prec, rm)
1826 }
1827
1828 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. Both
1829 /// [`Float`]s are taken by reference. An [`Ordering`] is also returned, indicating whether the
1830 /// rounded quotient is less than, equal to, or greater than the exact quotient. Although `NaN`s
1831 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
1832 /// `Equal`.
1833 ///
1834 /// The precision of the output is the maximum of the precision of the inputs. See
1835 /// [`RoundingMode`] for a description of the possible rounding modes.
1836 ///
1837 /// $$
1838 /// f(x,y,m) = x/y+\varepsilon.
1839 /// $$
1840 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1841 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1842 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
1843 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
1844 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
1845 ///
1846 /// If the output has a precision, it is the maximum of the precisions of the inputs.
1847 ///
1848 /// Special cases:
1849 /// - $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) =
1850 /// \text{NaN}$
1851 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
1852 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
1853 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
1854 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
1855 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
1856 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
1857 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
1858 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
1859 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
1860 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1861 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1862 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1863 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1864 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
1865 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1866 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1867 ///
1868 /// If you want to specify an output precision, consider using [`Float::div_prec_round_ref_ref`]
1869 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
1870 /// instead.
1871 ///
1872 /// # Worst-case complexity
1873 /// $T(n) = O(n \log n \log\log n)$
1874 ///
1875 /// $M(n) = O(n \log n)$
1876 ///
1877 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1878 /// other.significant_bits())`.
1879 ///
1880 /// # Panics
1881 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
1882 /// represent the output.
1883 ///
1884 /// # Examples
1885 /// ```
1886 /// use core::f64::consts::{E, PI};
1887 /// use malachite_base::rounding_modes::RoundingMode::*;
1888 /// use malachite_float::Float;
1889 /// use std::cmp::Ordering::*;
1890 ///
1891 /// let (quotient, o) = Float::from(PI).div_round_ref_ref(&Float::from(E), Floor);
1892 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
1893 /// assert_eq!(o, Less);
1894 ///
1895 /// let (quotient, o) = Float::from(PI).div_round_ref_ref(&Float::from(E), Ceiling);
1896 /// assert_eq!(quotient.to_string(), "1.155727349790922");
1897 /// assert_eq!(o, Greater);
1898 ///
1899 /// let (quotient, o) = Float::from(PI).div_round_ref_ref(&Float::from(E), Nearest);
1900 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
1901 /// assert_eq!(o, Less);
1902 /// ```
1903 #[inline]
1904 pub fn div_round_ref_ref(&self, other: &Float, rm: RoundingMode) -> (Float, Ordering) {
1905 let prec = max(self.significant_bits(), other.significant_bits());
1906 self.div_prec_round_ref_ref(other, prec, rm)
1907 }
1908
1909 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the specified precision
1910 /// and with the specified rounding mode. The [`Float`] on the right-hand side is taken by
1911 /// value. An [`Ordering`] is returned, indicating whether the rounded quotient is less than,
1912 /// equal to, or greater than the exact quotient. Although `NaN`s are not comparable to any
1913 /// [`Float`], whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
1914 ///
1915 /// See [`RoundingMode`] for a description of the possible rounding modes.
1916 ///
1917 /// $$
1918 /// x \gets x/y+\varepsilon.
1919 /// $$
1920 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1921 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1922 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
1923 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
1924 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1925 ///
1926 /// If the output has a precision, it is `prec`.
1927 ///
1928 /// See the [`Float::div_prec_round`] documentation for information on special cases.
1929 ///
1930 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_assign`] instead. If
1931 /// you know that your target precision is the maximum of the precisions of the two inputs,
1932 /// consider using [`Float::div_round_assign`] instead. If both of these things are true,
1933 /// consider using `/=` instead.
1934 ///
1935 /// # Worst-case complexity
1936 /// $T(n) = O(n \log n \log\log n)$
1937 ///
1938 /// $M(n) = O(n \log n)$
1939 ///
1940 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1941 /// other.significant_bits(), `prec`)`.
1942 ///
1943 /// # Panics
1944 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
1945 ///
1946 /// # Examples
1947 /// ```
1948 /// use core::f64::consts::{E, PI};
1949 /// use malachite_base::rounding_modes::RoundingMode::*;
1950 /// use malachite_float::Float;
1951 /// use std::cmp::Ordering::*;
1952 ///
1953 /// let mut quotient = Float::from(PI);
1954 /// assert_eq!(
1955 /// quotient.div_prec_round_assign(Float::from(E), 5, Floor),
1956 /// Less
1957 /// );
1958 /// assert_eq!(quotient.to_string(), "1.12");
1959 ///
1960 /// let mut quotient = Float::from(PI);
1961 /// assert_eq!(
1962 /// quotient.div_prec_round_assign(Float::from(E), 5, Ceiling),
1963 /// Greater
1964 /// );
1965 /// assert_eq!(quotient.to_string(), "1.19");
1966 ///
1967 /// let mut quotient = Float::from(PI);
1968 /// assert_eq!(
1969 /// quotient.div_prec_round_assign(Float::from(E), 5, Nearest),
1970 /// Less
1971 /// );
1972 /// assert_eq!(quotient.to_string(), "1.12");
1973 ///
1974 /// let mut quotient = Float::from(PI);
1975 /// assert_eq!(
1976 /// quotient.div_prec_round_assign(Float::from(E), 20, Floor),
1977 /// Less
1978 /// );
1979 /// assert_eq!(quotient.to_string(), "1.155725");
1980 ///
1981 /// let mut quotient = Float::from(PI);
1982 /// assert_eq!(
1983 /// quotient.div_prec_round_assign(Float::from(E), 20, Ceiling),
1984 /// Greater
1985 /// );
1986 /// assert_eq!(quotient.to_string(), "1.155727");
1987 ///
1988 /// let mut quotient = Float::from(PI);
1989 /// assert_eq!(
1990 /// quotient.div_prec_round_assign(Float::from(E), 20, Nearest),
1991 /// Greater
1992 /// );
1993 /// assert_eq!(quotient.to_string(), "1.155727");
1994 /// ```
1995 #[inline]
1996 pub fn div_prec_round_assign(&mut self, other: Float, prec: u64, rm: RoundingMode) -> Ordering {
1997 assert_ne!(prec, 0);
1998 match (&mut *self, other) {
1999 (float_nan!(), _)
2000 | (_, float_nan!())
2001 | (float_either_infinity!(), float_either_infinity!())
2002 | (float_either_zero!(), float_either_zero!()) => {
2003 *self = float_nan!();
2004 Equal
2005 }
2006 (
2007 Float(Infinity { sign: x_sign }),
2008 Float(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
2009 )
2010 | (Float(Finite { sign: x_sign, .. }), Float(Zero { sign: y_sign })) => {
2011 *self = Float(Infinity {
2012 sign: *x_sign == y_sign,
2013 });
2014 Equal
2015 }
2016 (
2017 Float(Zero { sign: x_sign }),
2018 Float(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
2019 )
2020 | (Float(Finite { sign: x_sign, .. }), Float(Infinity { sign: y_sign })) => {
2021 *self = Float(Zero {
2022 sign: *x_sign == y_sign,
2023 });
2024 Equal
2025 }
2026 (_, y) if abs_is_power_of_2(&y) => {
2027 if y < 0 {
2028 self.neg_assign();
2029 }
2030 let o = self.set_prec_round(prec, rm);
2031 *self >>= y.get_exponent().unwrap().checked_sub(1).unwrap();
2032 o
2033 }
2034 (
2035 Float(Finite {
2036 sign: x_sign,
2037 exponent: x_exp,
2038 precision: x_prec,
2039 significand: x,
2040 }),
2041 Float(Finite {
2042 sign: y_sign,
2043 exponent: y_exp,
2044 precision: y_prec,
2045 significand: mut y,
2046 }),
2047 ) => {
2048 let sign = *x_sign == y_sign;
2049 let (exp_offset, o) = div_float_significands_in_place(
2050 x,
2051 *x_prec,
2052 &mut y,
2053 y_prec,
2054 prec,
2055 if sign { rm } else { -rm },
2056 );
2057 *x_sign = sign;
2058 *x_exp = x_exp
2059 .checked_sub(y_exp)
2060 .unwrap()
2061 .checked_add(i32::exact_from(exp_offset))
2062 .unwrap();
2063 *x_prec = prec;
2064 if sign { o } else { o.reverse() }
2065 }
2066 }
2067 }
2068
2069 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the specified precision
2070 /// and with the specified rounding mode. The [`Float`] on the right-hand side is taken by
2071 /// reference. An [`Ordering`] is returned, indicating whether the rounded quotient is less
2072 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
2073 /// any [`Float`], whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
2074 ///
2075 /// See [`RoundingMode`] for a description of the possible rounding modes.
2076 ///
2077 /// $$
2078 /// x \gets x/y+\varepsilon.
2079 /// $$
2080 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2081 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2082 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2083 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2084 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2085 ///
2086 /// If the output has a precision, it is `prec`.
2087 ///
2088 /// See the [`Float::div_prec_round`] documentation for information on special cases.
2089 ///
2090 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_assign_ref`]
2091 /// instead. If you know that your target precision is the maximum of the precisions of the two
2092 /// inputs, consider using [`Float::div_round_assign_ref`] instead. If both of these things are
2093 /// true, consider using `/=` instead.
2094 ///
2095 /// # Worst-case complexity
2096 /// $T(n) = O(n \log n \log\log n)$
2097 ///
2098 /// $M(n) = O(n \log n)$
2099 ///
2100 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2101 /// other.significant_bits(), `prec`)`.
2102 ///
2103 /// # Panics
2104 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2105 ///
2106 /// # Examples
2107 /// ```
2108 /// use core::f64::consts::{E, PI};
2109 /// use malachite_base::rounding_modes::RoundingMode::*;
2110 /// use malachite_float::Float;
2111 /// use std::cmp::Ordering::*;
2112 ///
2113 /// let mut quotient = Float::from(PI);
2114 /// assert_eq!(
2115 /// quotient.div_prec_round_assign_ref(&Float::from(E), 5, Floor),
2116 /// Less
2117 /// );
2118 /// assert_eq!(quotient.to_string(), "1.12");
2119 ///
2120 /// let mut quotient = Float::from(PI);
2121 /// assert_eq!(
2122 /// quotient.div_prec_round_assign_ref(&Float::from(E), 5, Ceiling),
2123 /// Greater
2124 /// );
2125 /// assert_eq!(quotient.to_string(), "1.19");
2126 ///
2127 /// let mut quotient = Float::from(PI);
2128 /// assert_eq!(
2129 /// quotient.div_prec_round_assign_ref(&Float::from(E), 5, Nearest),
2130 /// Less
2131 /// );
2132 /// assert_eq!(quotient.to_string(), "1.12");
2133 ///
2134 /// let mut quotient = Float::from(PI);
2135 /// assert_eq!(
2136 /// quotient.div_prec_round_assign_ref(&Float::from(E), 20, Floor),
2137 /// Less
2138 /// );
2139 /// assert_eq!(quotient.to_string(), "1.155725");
2140 ///
2141 /// let mut quotient = Float::from(PI);
2142 /// assert_eq!(
2143 /// quotient.div_prec_round_assign_ref(&Float::from(E), 20, Ceiling),
2144 /// Greater
2145 /// );
2146 /// assert_eq!(quotient.to_string(), "1.155727");
2147 ///
2148 /// let mut quotient = Float::from(PI);
2149 /// assert_eq!(
2150 /// quotient.div_prec_round_assign_ref(&Float::from(E), 20, Nearest),
2151 /// Greater
2152 /// );
2153 /// assert_eq!(quotient.to_string(), "1.155727");
2154 /// ```
2155 #[inline]
2156 pub fn div_prec_round_assign_ref(
2157 &mut self,
2158 other: &Float,
2159 prec: u64,
2160 rm: RoundingMode,
2161 ) -> Ordering {
2162 assert_ne!(prec, 0);
2163 match (&mut *self, other) {
2164 (float_nan!(), _)
2165 | (_, float_nan!())
2166 | (float_either_infinity!(), float_either_infinity!())
2167 | (float_either_zero!(), float_either_zero!()) => {
2168 *self = float_nan!();
2169 Equal
2170 }
2171 (
2172 Float(Infinity { sign: x_sign }),
2173 Float(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
2174 )
2175 | (Float(Finite { sign: x_sign, .. }), Float(Zero { sign: y_sign })) => {
2176 *self = Float(Infinity {
2177 sign: x_sign == y_sign,
2178 });
2179 Equal
2180 }
2181 (
2182 Float(Zero { sign: x_sign }),
2183 Float(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
2184 )
2185 | (Float(Finite { sign: x_sign, .. }), Float(Infinity { sign: y_sign })) => {
2186 *self = Float(Zero {
2187 sign: x_sign == y_sign,
2188 });
2189 Equal
2190 }
2191 (_, y) if abs_is_power_of_2(y) => {
2192 if *y < 0 {
2193 self.neg_assign();
2194 }
2195 let o = self.set_prec_round(prec, rm);
2196 *self >>= y.get_exponent().unwrap().checked_sub(1).unwrap();
2197 o
2198 }
2199 (
2200 Float(Finite {
2201 sign: x_sign,
2202 exponent: x_exp,
2203 precision: x_prec,
2204 significand: x,
2205 }),
2206 Float(Finite {
2207 sign: y_sign,
2208 exponent: y_exp,
2209 precision: y_prec,
2210 significand: y,
2211 }),
2212 ) => {
2213 let sign = x_sign == y_sign;
2214 let (exp_offset, o) = div_float_significands_in_place_ref(
2215 x,
2216 *x_prec,
2217 y,
2218 *y_prec,
2219 prec,
2220 if sign { rm } else { -rm },
2221 );
2222 *x_sign = sign;
2223 *x_exp = x_exp
2224 .checked_sub(*y_exp)
2225 .unwrap()
2226 .checked_add(i32::exact_from(exp_offset))
2227 .unwrap();
2228 *x_prec = prec;
2229 if sign { o } else { o.reverse() }
2230 }
2231 }
2232 }
2233
2234 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the nearest value of the
2235 /// specified precision. The [`Float`] on the right-hand side is taken by value. An [`Ordering`]
2236 /// is returned, indicating whether the rounded quotient is less than, equal to, or greater than
2237 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
2238 /// function sets the [`Float`] to `NaN` it also returns `Equal`.
2239 ///
2240 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
2241 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2242 /// description of the `Nearest` rounding mode.
2243 ///
2244 /// $$
2245 /// x \gets x/y+\varepsilon.
2246 /// $$
2247 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2248 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2249 ///
2250 /// If the output has a precision, it is `prec`.
2251 ///
2252 /// See the [`Float::div_prec`] documentation for information on special cases.
2253 ///
2254 /// If you want to use a rounding mode other than `Nearest`, consider using
2255 /// [`Float::div_prec_round_assign`] instead. If you know that your target precision is the
2256 /// maximum of the precisions of the two inputs, consider using `/=` instead.
2257 ///
2258 /// # Worst-case complexity
2259 /// $T(n) = O(n \log n \log\log n)$
2260 ///
2261 /// $M(n) = O(n \log n)$
2262 ///
2263 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2264 /// other.significant_bits(), `prec`)`.
2265 ///
2266 /// # Examples
2267 /// ```
2268 /// use core::f64::consts::{E, PI};
2269 /// use malachite_float::Float;
2270 /// use std::cmp::Ordering::*;
2271 ///
2272 /// let mut x = Float::from(PI);
2273 /// assert_eq!(x.div_prec_assign(Float::from(E), 5), Less);
2274 /// assert_eq!(x.to_string(), "1.12");
2275 ///
2276 /// let mut x = Float::from(PI);
2277 /// assert_eq!(x.div_prec_assign(Float::from(E), 20), Greater);
2278 /// assert_eq!(x.to_string(), "1.155727");
2279 /// ```
2280 #[inline]
2281 pub fn div_prec_assign(&mut self, other: Float, prec: u64) -> Ordering {
2282 self.div_prec_round_assign(other, prec, Nearest)
2283 }
2284
2285 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the nearest value of the
2286 /// specified precision. The [`Float`] on the right-hand side is taken by reference. An
2287 /// [`Ordering`] is returned, indicating whether the rounded quotient is less than, equal to, or
2288 /// greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
2289 /// whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
2290 ///
2291 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
2292 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2293 /// description of the `Nearest` rounding mode.
2294 ///
2295 /// $$
2296 /// x \gets x/y+\varepsilon.
2297 /// $$
2298 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2299 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2300 ///
2301 /// If the output has a precision, it is `prec`.
2302 ///
2303 /// See the [`Float::div_prec`] documentation for information on special cases.
2304 ///
2305 /// If you want to use a rounding mode other than `Nearest`, consider using
2306 /// [`Float::div_prec_round_assign_ref`] instead. If you know that your target precision is the
2307 /// maximum of the precisions of the two inputs, consider using `/=` instead.
2308 ///
2309 /// # Worst-case complexity
2310 /// $T(n) = O(n \log n \log\log n)$
2311 ///
2312 /// $M(n) = O(n \log n)$
2313 ///
2314 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2315 /// other.significant_bits(), `prec`)`.
2316 ///
2317 /// # Examples
2318 /// ```
2319 /// use core::f64::consts::{E, PI};
2320 /// use malachite_float::Float;
2321 /// use std::cmp::Ordering::*;
2322 ///
2323 /// let mut x = Float::from(PI);
2324 /// assert_eq!(x.div_prec_assign_ref(&Float::from(E), 5), Less);
2325 /// assert_eq!(x.to_string(), "1.12");
2326 ///
2327 /// let mut x = Float::from(PI);
2328 /// assert_eq!(x.div_prec_assign_ref(&Float::from(E), 20), Greater);
2329 /// assert_eq!(x.to_string(), "1.155727");
2330 /// ```
2331 #[inline]
2332 pub fn div_prec_assign_ref(&mut self, other: &Float, prec: u64) -> Ordering {
2333 self.div_prec_round_assign_ref(other, prec, Nearest)
2334 }
2335
2336 /// Divides a [`Float`] by a [`Float`] in place, rounding the result with the specified rounding
2337 /// mode. The [`Float`] on the right-hand side is taken by value. An [`Ordering`] is returned,
2338 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
2339 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
2340 /// the [`Float`] to `NaN` it also returns `Equal`.
2341 ///
2342 /// The precision of the output is the maximum of the precision of the inputs. See
2343 /// [`RoundingMode`] for a description of the possible rounding modes.
2344 ///
2345 /// $$
2346 /// x \gets x/y+\varepsilon.
2347 /// $$
2348 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2349 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2350 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2351 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2352 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2353 ///
2354 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2355 ///
2356 /// See the [`Float::div_round`] documentation for information on special cases.
2357 ///
2358 /// If you want to specify an output precision, consider using [`Float::div_prec_round_assign`]
2359 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/=`
2360 /// instead.
2361 ///
2362 /// # Worst-case complexity
2363 /// $T(n) = O(n \log n \log\log n)$
2364 ///
2365 /// $M(n) = O(n \log n)$
2366 ///
2367 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2368 /// other.significant_bits())`.
2369 ///
2370 /// # Panics
2371 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
2372 /// represent the output.
2373 ///
2374 /// # Examples
2375 /// ```
2376 /// use core::f64::consts::{E, PI};
2377 /// use malachite_base::rounding_modes::RoundingMode::*;
2378 /// use malachite_float::Float;
2379 /// use std::cmp::Ordering::*;
2380 ///
2381 /// let mut x = Float::from(PI);
2382 /// assert_eq!(x.div_round_assign(Float::from(E), Floor), Less);
2383 /// assert_eq!(x.to_string(), "1.1557273497909217");
2384 ///
2385 /// let mut x = Float::from(PI);
2386 /// assert_eq!(x.div_round_assign(Float::from(E), Ceiling), Greater);
2387 /// assert_eq!(x.to_string(), "1.155727349790922");
2388 ///
2389 /// let mut x = Float::from(PI);
2390 /// assert_eq!(x.div_round_assign(Float::from(E), Nearest), Less);
2391 /// assert_eq!(x.to_string(), "1.1557273497909217");
2392 /// ```
2393 #[inline]
2394 pub fn div_round_assign(&mut self, other: Float, rm: RoundingMode) -> Ordering {
2395 let prec = max(self.significant_bits(), other.significant_bits());
2396 self.div_prec_round_assign(other, prec, rm)
2397 }
2398
2399 /// Divides a [`Float`] by a [`Float`] in place, rounding the result with the specified rounding
2400 /// mode. The [`Float`] on the right-hand side is taken by reference. An [`Ordering`] is
2401 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
2402 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
2403 /// function sets the [`Float`] to `NaN` it also returns `Equal`.
2404 ///
2405 /// The precision of the output is the maximum of the precision of the inputs. See
2406 /// [`RoundingMode`] for a description of the possible rounding modes.
2407 ///
2408 /// $$
2409 /// x \gets x/y+\varepsilon.
2410 /// $$
2411 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2412 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2413 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2414 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2415 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2416 ///
2417 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2418 ///
2419 /// See the [`Float::div_round`] documentation for information on special cases.
2420 ///
2421 /// If you want to specify an output precision, consider using
2422 /// [`Float::div_prec_round_assign_ref`] instead. If you know you'll be using the `Nearest`
2423 /// rounding mode, consider using `/=` instead.
2424 ///
2425 /// # Worst-case complexity
2426 /// $T(n) = O(n \log n \log\log n)$
2427 ///
2428 /// $M(n) = O(n \log n)$
2429 ///
2430 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2431 /// other.significant_bits())`.
2432 ///
2433 /// # Panics
2434 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
2435 /// represent the output.
2436 ///
2437 /// # Examples
2438 /// ```
2439 /// use core::f64::consts::{E, PI};
2440 /// use malachite_base::rounding_modes::RoundingMode::*;
2441 /// use malachite_float::Float;
2442 /// use std::cmp::Ordering::*;
2443 ///
2444 /// let mut x = Float::from(PI);
2445 /// assert_eq!(x.div_round_assign_ref(&Float::from(E), Floor), Less);
2446 /// assert_eq!(x.to_string(), "1.1557273497909217");
2447 ///
2448 /// let mut x = Float::from(PI);
2449 /// assert_eq!(x.div_round_assign_ref(&Float::from(E), Ceiling), Greater);
2450 /// assert_eq!(x.to_string(), "1.155727349790922");
2451 ///
2452 /// let mut x = Float::from(PI);
2453 /// assert_eq!(x.div_round_assign_ref(&Float::from(E), Nearest), Less);
2454 /// assert_eq!(x.to_string(), "1.1557273497909217");
2455 /// ```
2456 #[inline]
2457 pub fn div_round_assign_ref(&mut self, other: &Float, rm: RoundingMode) -> Ordering {
2458 let prec = max(self.significant_bits(), other.significant_bits());
2459 self.div_prec_round_assign_ref(other, prec, rm)
2460 }
2461
2462 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
2463 /// with the specified rounding mode. The [`Float`] and the [`Rational`] are both taken by
2464 /// value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
2465 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
2466 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2467 ///
2468 /// See [`RoundingMode`] for a description of the possible rounding modes.
2469 ///
2470 /// $$
2471 /// f(x,y,p,m) = x/y+\varepsilon.
2472 /// $$
2473 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2474 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2475 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2476 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2477 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2478 ///
2479 /// If the output has a precision, it is `prec`.
2480 ///
2481 /// Special cases:
2482 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
2483 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
2484 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
2485 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
2486 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
2487 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
2488 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
2489 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
2490 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
2491 ///
2492 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec`] instead.
2493 /// If you know that your target precision is the precision of the [`Float`] input, consider
2494 /// using [`Float::div_rational_round`] instead. If both of these things are true, consider
2495 /// using `/` instead.
2496 ///
2497 /// # Worst-case complexity
2498 /// $T(n) = O(n \log n \log\log n)$
2499 ///
2500 /// $M(n) = O(n \log n)$
2501 ///
2502 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2503 /// other.significant_bits(), prec)`.
2504 ///
2505 /// # Panics
2506 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2507 ///
2508 /// # Examples
2509 /// ```
2510 /// use core::f64::consts::PI;
2511 /// use malachite_base::rounding_modes::RoundingMode::*;
2512 /// use malachite_float::Float;
2513 /// use malachite_q::Rational;
2514 /// use std::cmp::Ordering::*;
2515 ///
2516 /// let (quotient, o) =
2517 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 5, Floor);
2518 /// assert_eq!(quotient.to_string(), "9.0");
2519 /// assert_eq!(o, Less);
2520 ///
2521 /// let (quotient, o) =
2522 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 5, Ceiling);
2523 /// assert_eq!(quotient.to_string(), "9.5");
2524 /// assert_eq!(o, Greater);
2525 ///
2526 /// let (quotient, o) =
2527 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 5, Nearest);
2528 /// assert_eq!(quotient.to_string(), "9.5");
2529 /// assert_eq!(o, Greater);
2530 ///
2531 /// let (quotient, o) =
2532 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 20, Floor);
2533 /// assert_eq!(quotient.to_string(), "9.42477");
2534 /// assert_eq!(o, Less);
2535 ///
2536 /// let (quotient, o) =
2537 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 20, Ceiling);
2538 /// assert_eq!(quotient.to_string(), "9.42479");
2539 /// assert_eq!(o, Greater);
2540 ///
2541 /// let (quotient, o) =
2542 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 20, Nearest);
2543 /// assert_eq!(quotient.to_string(), "9.42477");
2544 /// assert_eq!(o, Less);
2545 /// ```
2546 #[inline]
2547 pub fn div_rational_prec_round(
2548 mut self,
2549 other: Rational,
2550 prec: u64,
2551 rm: RoundingMode,
2552 ) -> (Float, Ordering) {
2553 let o = self.div_rational_prec_round_assign(other, prec, rm);
2554 (self, o)
2555 }
2556
2557 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
2558 /// with the specified rounding mode. The [`Float`] is taken by value and the [`Rational`] by
2559 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
2560 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
2561 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2562 ///
2563 /// See [`RoundingMode`] for a description of the possible rounding modes.
2564 ///
2565 /// $$
2566 /// f(x,y,p,m) = x/y+\varepsilon.
2567 /// $$
2568 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2569 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2570 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2571 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2572 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2573 ///
2574 /// If the output has a precision, it is `prec`.
2575 ///
2576 /// Special cases:
2577 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
2578 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
2579 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
2580 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
2581 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
2582 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
2583 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
2584 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
2585 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
2586 ///
2587 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_val_ref`]
2588 /// instead. If you know that your target precision is the precision of the [`Float`] input,
2589 /// consider using [`Float::div_rational_round_val_ref`] instead. If both of these things are
2590 /// true, consider using `/` instead.
2591 ///
2592 /// # Worst-case complexity
2593 /// $T(n) = O(n \log n \log\log n)$
2594 ///
2595 /// $M(n) = O(n \log n)$
2596 ///
2597 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2598 /// other.significant_bits(), prec)`.
2599 ///
2600 /// # Panics
2601 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2602 ///
2603 /// # Examples
2604 /// ```
2605 /// use core::f64::consts::PI;
2606 /// use malachite_base::rounding_modes::RoundingMode::*;
2607 /// use malachite_float::Float;
2608 /// use malachite_q::Rational;
2609 /// use std::cmp::Ordering::*;
2610 ///
2611 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
2612 /// &Rational::from_unsigneds(1u8, 3),
2613 /// 5,
2614 /// Floor,
2615 /// );
2616 /// assert_eq!(quotient.to_string(), "9.0");
2617 /// assert_eq!(o, Less);
2618 ///
2619 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
2620 /// &Rational::from_unsigneds(1u8, 3),
2621 /// 5,
2622 /// Ceiling,
2623 /// );
2624 /// assert_eq!(quotient.to_string(), "9.5");
2625 /// assert_eq!(o, Greater);
2626 ///
2627 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
2628 /// &Rational::from_unsigneds(1u8, 3),
2629 /// 5,
2630 /// Nearest,
2631 /// );
2632 /// assert_eq!(quotient.to_string(), "9.5");
2633 /// assert_eq!(o, Greater);
2634 ///
2635 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
2636 /// &Rational::from_unsigneds(1u8, 3),
2637 /// 20,
2638 /// Floor,
2639 /// );
2640 /// assert_eq!(quotient.to_string(), "9.42477");
2641 /// assert_eq!(o, Less);
2642 ///
2643 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
2644 /// &Rational::from_unsigneds(1u8, 3),
2645 /// 20,
2646 /// Ceiling,
2647 /// );
2648 /// assert_eq!(quotient.to_string(), "9.42479");
2649 /// assert_eq!(o, Greater);
2650 ///
2651 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
2652 /// &Rational::from_unsigneds(1u8, 3),
2653 /// 20,
2654 /// Nearest,
2655 /// );
2656 /// assert_eq!(quotient.to_string(), "9.42477");
2657 /// assert_eq!(o, Less);
2658 /// ```
2659 #[inline]
2660 pub fn div_rational_prec_round_val_ref(
2661 mut self,
2662 other: &Rational,
2663 prec: u64,
2664 rm: RoundingMode,
2665 ) -> (Float, Ordering) {
2666 let o = self.div_rational_prec_round_assign_ref(other, prec, rm);
2667 (self, o)
2668 }
2669
2670 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
2671 /// with the specified rounding mode. The [`Float`] is taken by reference and the [`Rational`]
2672 /// by value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
2673 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
2674 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2675 ///
2676 /// See [`RoundingMode`] for a description of the possible rounding modes.
2677 ///
2678 /// $$
2679 /// f(x,y,p,m) = x/y+\varepsilon.
2680 /// $$
2681 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2682 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2683 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2684 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2685 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2686 ///
2687 /// If the output has a precision, it is `prec`.
2688 ///
2689 /// Special cases:
2690 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
2691 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
2692 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
2693 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
2694 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
2695 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
2696 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
2697 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
2698 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
2699 ///
2700 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_ref_val`]
2701 /// instead. If you know that your target precision is the precision of the [`Float`] input,
2702 /// consider using [`Float::div_rational_round_ref_val`] instead. If both of these things are
2703 /// true, consider using `/` instead.
2704 ///
2705 /// # Worst-case complexity
2706 /// $T(n) = O(n \log n \log\log n)$
2707 ///
2708 /// $M(n) = O(n \log n)$
2709 ///
2710 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2711 /// other.significant_bits(), prec)`.
2712 ///
2713 /// # Panics
2714 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2715 ///
2716 /// # Examples
2717 /// ```
2718 /// use core::f64::consts::PI;
2719 /// use malachite_base::rounding_modes::RoundingMode::*;
2720 /// use malachite_float::Float;
2721 /// use malachite_q::Rational;
2722 /// use std::cmp::Ordering::*;
2723 ///
2724 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
2725 /// Rational::from_unsigneds(1u8, 3),
2726 /// 5,
2727 /// Floor,
2728 /// );
2729 /// assert_eq!(quotient.to_string(), "9.0");
2730 /// assert_eq!(o, Less);
2731 ///
2732 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
2733 /// Rational::from_unsigneds(1u8, 3),
2734 /// 5,
2735 /// Ceiling,
2736 /// );
2737 /// assert_eq!(quotient.to_string(), "9.5");
2738 /// assert_eq!(o, Greater);
2739 ///
2740 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
2741 /// Rational::from_unsigneds(1u8, 3),
2742 /// 5,
2743 /// Nearest,
2744 /// );
2745 /// assert_eq!(quotient.to_string(), "9.5");
2746 /// assert_eq!(o, Greater);
2747 ///
2748 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
2749 /// Rational::from_unsigneds(1u8, 3),
2750 /// 20,
2751 /// Floor,
2752 /// );
2753 /// assert_eq!(quotient.to_string(), "9.42477");
2754 /// assert_eq!(o, Less);
2755 ///
2756 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
2757 /// Rational::from_unsigneds(1u8, 3),
2758 /// 20,
2759 /// Ceiling,
2760 /// );
2761 /// assert_eq!(quotient.to_string(), "9.42479");
2762 /// assert_eq!(o, Greater);
2763 ///
2764 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
2765 /// Rational::from_unsigneds(1u8, 3),
2766 /// 20,
2767 /// Nearest,
2768 /// );
2769 /// assert_eq!(quotient.to_string(), "9.42477");
2770 /// assert_eq!(o, Less);
2771 /// ```
2772 #[inline]
2773 pub fn div_rational_prec_round_ref_val(
2774 &self,
2775 other: Rational,
2776 prec: u64,
2777 rm: RoundingMode,
2778 ) -> (Float, Ordering) {
2779 if max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD {
2780 div_rational_prec_round_naive_ref_val(self, other, prec, rm)
2781 } else {
2782 div_rational_prec_round_direct_ref_val(self, other, prec, rm)
2783 }
2784 }
2785
2786 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
2787 /// with the specified rounding mode. The [`Float`] and the [`Rational`] are both taken by
2788 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
2789 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
2790 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2791 ///
2792 /// See [`RoundingMode`] for a description of the possible rounding modes.
2793 ///
2794 /// $$
2795 /// f(x,y,p,m) = x/y+\varepsilon.
2796 /// $$
2797 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2798 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2799 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2800 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2801 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2802 ///
2803 /// If the output has a precision, it is `prec`.
2804 ///
2805 /// Special cases:
2806 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
2807 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
2808 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
2809 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
2810 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
2811 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
2812 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
2813 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
2814 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
2815 ///
2816 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_ref_ref`]
2817 /// instead. If you know that your target precision is the precision of the [`Float`] input,
2818 /// consider using [`Float::div_rational_round_ref_ref`] instead. If both of these things are
2819 /// true, consider using `/` instead.
2820 ///
2821 /// # Worst-case complexity
2822 /// $T(n) = O(n \log n \log\log n)$
2823 ///
2824 /// $M(n) = O(n \log n)$
2825 ///
2826 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2827 /// other.significant_bits(), prec)`.
2828 ///
2829 /// # Panics
2830 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2831 ///
2832 /// # Examples
2833 /// ```
2834 /// use core::f64::consts::PI;
2835 /// use malachite_base::rounding_modes::RoundingMode::*;
2836 /// use malachite_float::Float;
2837 /// use malachite_q::Rational;
2838 /// use std::cmp::Ordering::*;
2839 ///
2840 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
2841 /// &Rational::from_unsigneds(1u8, 3),
2842 /// 5,
2843 /// Floor,
2844 /// );
2845 /// assert_eq!(quotient.to_string(), "9.0");
2846 /// assert_eq!(o, Less);
2847 ///
2848 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
2849 /// &Rational::from_unsigneds(1u8, 3),
2850 /// 5,
2851 /// Ceiling,
2852 /// );
2853 /// assert_eq!(quotient.to_string(), "9.5");
2854 /// assert_eq!(o, Greater);
2855 ///
2856 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
2857 /// &Rational::from_unsigneds(1u8, 3),
2858 /// 5,
2859 /// Nearest,
2860 /// );
2861 /// assert_eq!(quotient.to_string(), "9.5");
2862 /// assert_eq!(o, Greater);
2863 ///
2864 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
2865 /// &Rational::from_unsigneds(1u8, 3),
2866 /// 20,
2867 /// Floor,
2868 /// );
2869 /// assert_eq!(quotient.to_string(), "9.42477");
2870 /// assert_eq!(o, Less);
2871 ///
2872 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
2873 /// &Rational::from_unsigneds(1u8, 3),
2874 /// 20,
2875 /// Ceiling,
2876 /// );
2877 /// assert_eq!(quotient.to_string(), "9.42479");
2878 /// assert_eq!(o, Greater);
2879 ///
2880 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
2881 /// &Rational::from_unsigneds(1u8, 3),
2882 /// 20,
2883 /// Nearest,
2884 /// );
2885 /// assert_eq!(quotient.to_string(), "9.42477");
2886 /// assert_eq!(o, Less);
2887 /// ```
2888 #[inline]
2889 pub fn div_rational_prec_round_ref_ref(
2890 &self,
2891 other: &Rational,
2892 prec: u64,
2893 rm: RoundingMode,
2894 ) -> (Float, Ordering) {
2895 if max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD {
2896 div_rational_prec_round_naive_ref_ref(self, other, prec, rm)
2897 } else {
2898 div_rational_prec_round_direct_ref_ref(self, other, prec, rm)
2899 }
2900 }
2901
2902 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
2903 /// specified precision. The [`Float`] and the [`Rational`] are both are taken by value. An
2904 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
2905 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
2906 /// whenever this function returns a `NaN` it also returns `Equal`.
2907 ///
2908 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
2909 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2910 /// description of the `Nearest` rounding mode.
2911 ///
2912 /// $$
2913 /// f(x,y,p) = x/y+\varepsilon.
2914 /// $$
2915 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2916 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2917 ///
2918 /// If the output has a precision, it is `prec`.
2919 ///
2920 /// Special cases:
2921 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
2922 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
2923 /// - $f(\infty,x,p)=-\infty$ if $x<0$
2924 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
2925 /// - $f(-\infty,x,p)=\infty$ if $x<0$
2926 /// - $f(0.0,x,p)=0.0$ if $x>0$
2927 /// - $f(0.0,x,p)=-0.0$ if $x<0$
2928 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
2929 /// - $f(-0.0,x,p)=0.0$ if $x<0$
2930 ///
2931 /// If you want to use a rounding mode other than `Nearest`, consider using
2932 /// [`Float::div_rational_prec_round`] instead. If you know that your target precision is the
2933 /// precision of the [`Float`] input, consider using `/` instead.
2934 ///
2935 /// # Worst-case complexity
2936 /// $T(n) = O(n \log n \log\log n)$
2937 ///
2938 /// $M(n) = O(n \log n)$
2939 ///
2940 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2941 /// other.significant_bits(), prec)`.
2942 ///
2943 /// # Examples
2944 /// ```
2945 /// use core::f64::consts::PI;
2946 /// use malachite_base::num::conversion::traits::ExactFrom;
2947 /// use malachite_float::Float;
2948 /// use malachite_q::Rational;
2949 /// use std::cmp::Ordering::*;
2950 ///
2951 /// let (quotient, o) = Float::from(PI).div_rational_prec(Rational::exact_from(1.5), 5);
2952 /// assert_eq!(quotient.to_string(), "2.1");
2953 /// assert_eq!(o, Greater);
2954 ///
2955 /// let (quotient, o) = Float::from(PI).div_rational_prec(Rational::exact_from(1.5), 20);
2956 /// assert_eq!(quotient.to_string(), "2.094395");
2957 /// assert_eq!(o, Less);
2958 /// ```
2959 #[inline]
2960 pub fn div_rational_prec(self, other: Rational, prec: u64) -> (Float, Ordering) {
2961 self.div_rational_prec_round(other, prec, Nearest)
2962 }
2963
2964 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
2965 /// specified precision. The [`Float`] is taken by value and the [`Rational`] by reference. An
2966 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
2967 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
2968 /// whenever this function returns a `NaN` it also returns `Equal`.
2969 ///
2970 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
2971 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2972 /// description of the `Nearest` rounding mode.
2973 ///
2974 /// $$
2975 /// f(x,y,p) = x/y+\varepsilon.
2976 /// $$
2977 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2978 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2979 ///
2980 /// If the output has a precision, it is `prec`.
2981 ///
2982 /// Special cases:
2983 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
2984 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
2985 /// - $f(\infty,x,p)=-\infty$ if $x<0$
2986 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
2987 /// - $f(-\infty,x,p)=\infty$ if $x<0$
2988 /// - $f(0.0,x,p)=0.0$ if $x>0$
2989 /// - $f(0.0,x,p)=-0.0$ if $x<0$
2990 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
2991 /// - $f(-0.0,x,p)=0.0$ if $x<0$
2992 ///
2993 /// If you want to use a rounding mode other than `Nearest`, consider using
2994 /// [`Float::div_rational_prec_round_val_ref`] instead. If you know that your target precision
2995 /// is the precision of the [`Float`] input, consider using `/` instead.
2996 ///
2997 /// # Worst-case complexity
2998 /// $T(n) = O(n \log n \log\log n)$
2999 ///
3000 /// $M(n) = O(n \log n)$
3001 ///
3002 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3003 /// other.significant_bits(), prec)`.
3004 ///
3005 /// # Examples
3006 /// ```
3007 /// use core::f64::consts::PI;
3008 /// use malachite_base::num::conversion::traits::ExactFrom;
3009 /// use malachite_float::Float;
3010 /// use malachite_q::Rational;
3011 /// use std::cmp::Ordering::*;
3012 ///
3013 /// let (quotient, o) =
3014 /// Float::from(PI).div_rational_prec_val_ref(&Rational::exact_from(1.5), 5);
3015 /// assert_eq!(quotient.to_string(), "2.1");
3016 /// assert_eq!(o, Greater);
3017 ///
3018 /// let (quotient, o) =
3019 /// Float::from(PI).div_rational_prec_val_ref(&Rational::exact_from(1.5), 20);
3020 /// assert_eq!(quotient.to_string(), "2.094395");
3021 /// assert_eq!(o, Less);
3022 /// ```
3023 #[inline]
3024 pub fn div_rational_prec_val_ref(self, other: &Rational, prec: u64) -> (Float, Ordering) {
3025 self.div_rational_prec_round_val_ref(other, prec, Nearest)
3026 }
3027
3028 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3029 /// specified precision. The [`Float`] is taken by reference and the [`Rational`] by value. An
3030 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3031 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3032 /// whenever this function returns a `NaN` it also returns `Equal`.
3033 ///
3034 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3035 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3036 /// description of the `Nearest` rounding mode.
3037 ///
3038 /// $$
3039 /// f(x,y,p) = x/y+\varepsilon.
3040 /// $$
3041 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3042 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3043 ///
3044 /// If the output has a precision, it is `prec`.
3045 ///
3046 /// Special cases:
3047 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
3048 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
3049 /// - $f(\infty,x,p)=-\infty$ if $x<0$
3050 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
3051 /// - $f(-\infty,x,p)=\infty$ if $x<0$
3052 /// - $f(0.0,x,p)=0.0$ if $x>0$
3053 /// - $f(0.0,x,p)=-0.0$ if $x<0$
3054 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
3055 /// - $f(-0.0,x,p)=0.0$ if $x<0$
3056 ///
3057 /// If you want to use a rounding mode other than `Nearest`, consider using
3058 /// [`Float::div_rational_prec_round_ref_val`] instead. If you know that your target precision
3059 /// is the precision of the [`Float`] input, consider using `/` instead.
3060 ///
3061 /// # Worst-case complexity
3062 /// $T(n) = O(n \log n \log\log n)$
3063 ///
3064 /// $M(n) = O(n \log n)$
3065 ///
3066 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3067 /// other.significant_bits(), prec)`.
3068 ///
3069 /// # Examples
3070 /// ```
3071 /// use core::f64::consts::PI;
3072 /// use malachite_base::num::conversion::traits::ExactFrom;
3073 /// use malachite_float::Float;
3074 /// use malachite_q::Rational;
3075 /// use std::cmp::Ordering::*;
3076 ///
3077 /// let (quotient, o) = Float::from(PI).div_rational_prec_ref_val(Rational::exact_from(1.5), 5);
3078 /// assert_eq!(quotient.to_string(), "2.1");
3079 /// assert_eq!(o, Greater);
3080 ///
3081 /// let (quotient, o) =
3082 /// Float::from(PI).div_rational_prec_ref_val(Rational::exact_from(1.5), 20);
3083 /// assert_eq!(quotient.to_string(), "2.094395");
3084 /// assert_eq!(o, Less);
3085 /// ```
3086 #[inline]
3087 pub fn div_rational_prec_ref_val(&self, other: Rational, prec: u64) -> (Float, Ordering) {
3088 self.div_rational_prec_round_ref_val(other, prec, Nearest)
3089 }
3090
3091 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3092 /// specified precision. The [`Float`] and the [`Rational`] are both are taken by reference. An
3093 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3094 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3095 /// whenever this function returns a `NaN` it also returns `Equal`.
3096 ///
3097 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3098 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3099 /// description of the `Nearest` rounding mode.
3100 ///
3101 /// $$
3102 /// f(x,y,p) = x/y+\varepsilon.
3103 /// $$
3104 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3105 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3106 ///
3107 /// If the output has a precision, it is `prec`.
3108 ///
3109 /// Special cases:
3110 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
3111 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
3112 /// - $f(\infty,x,p)=-\infty$ if $x<0$
3113 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
3114 /// - $f(-\infty,x,p)=\infty$ if $x<0$
3115 /// - $f(0.0,x,p)=0.0$ if $x>0$
3116 /// - $f(0.0,x,p)=-0.0$ if $x<0$
3117 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
3118 /// - $f(-0.0,x,p)=0.0$ if $x<0$
3119 ///
3120 /// If you want to use a rounding mode other than `Nearest`, consider using
3121 /// [`Float::div_rational_prec_round_ref_ref`] instead. If you know that your target precision
3122 /// is the precision of the [`Float`] input, consider using `/` instead.
3123 ///
3124 /// # Worst-case complexity
3125 /// $T(n) = O(n \log n \log\log n)$
3126 ///
3127 /// $M(n) = O(n \log n)$
3128 ///
3129 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3130 /// other.significant_bits(), prec)`.
3131 ///
3132 /// # Examples
3133 /// ```
3134 /// use core::f64::consts::PI;
3135 /// use malachite_base::num::conversion::traits::ExactFrom;
3136 /// use malachite_float::Float;
3137 /// use malachite_q::Rational;
3138 /// use std::cmp::Ordering::*;
3139 ///
3140 /// let (quotient, o) =
3141 /// Float::from(PI).div_rational_prec_ref_ref(&Rational::exact_from(1.5), 5);
3142 /// assert_eq!(quotient.to_string(), "2.1");
3143 /// assert_eq!(o, Greater);
3144 ///
3145 /// let (quotient, o) =
3146 /// Float::from(PI).div_rational_prec_ref_ref(&Rational::exact_from(1.5), 20);
3147 /// assert_eq!(quotient.to_string(), "2.094395");
3148 /// assert_eq!(o, Less);
3149 /// ```
3150 #[inline]
3151 pub fn div_rational_prec_ref_ref(&self, other: &Rational, prec: u64) -> (Float, Ordering) {
3152 self.div_rational_prec_round_ref_ref(other, prec, Nearest)
3153 }
3154
3155 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
3156 /// The [`Float`] and the [`Rational`] are both are taken by value. An [`Ordering`] is also
3157 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3158 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3159 /// function returns a `NaN` it also returns `Equal`.
3160 ///
3161 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
3162 /// for a description of the possible rounding modes.
3163 ///
3164 /// $$
3165 /// f(x,y,m) = x/y+\varepsilon.
3166 /// $$
3167 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3168 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3169 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3170 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3171 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3172 ///
3173 /// If the output has a precision, it is the precision of the [`Float`] input.
3174 ///
3175 /// Special cases:
3176 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
3177 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
3178 /// - $f(\infty,x,m)=-\infty$ if $x<0$
3179 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
3180 /// - $f(-\infty,x,m)=\infty$ if $x<0$
3181 /// - $f(0.0,x,m)=0.0$ if $x>0$
3182 /// - $f(0.0,x,m)=-0.0$ if $x<0$
3183 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
3184 /// - $f(-0.0,x,m)=0.0$ if $x<0$
3185 ///
3186 /// If you want to specify an output precision, consider using
3187 /// [`Float::div_rational_prec_round`] instead. If you know you'll be using the `Nearest`
3188 /// rounding mode, consider using `/` instead.
3189 ///
3190 /// # Worst-case complexity
3191 /// $T(n) = O(n \log n \log\log n)$
3192 ///
3193 /// $M(n) = O(n \log n)$
3194 ///
3195 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3196 /// other.significant_bits())`.
3197 ///
3198 /// # Panics
3199 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
3200 /// represent the output.
3201 ///
3202 /// # Examples
3203 /// ```
3204 /// use core::f64::consts::PI;
3205 /// use malachite_base::rounding_modes::RoundingMode::*;
3206 /// use malachite_float::Float;
3207 /// use malachite_q::Rational;
3208 /// use std::cmp::Ordering::*;
3209 ///
3210 /// let (quotient, o) =
3211 /// Float::from(PI).div_rational_round(Rational::from_unsigneds(1u8, 3), Floor);
3212 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3213 /// assert_eq!(o, Less);
3214 ///
3215 /// let (quotient, o) =
3216 /// Float::from(PI).div_rational_round(Rational::from_unsigneds(1u8, 3), Ceiling);
3217 /// assert_eq!(quotient.to_string(), "9.42477796076939");
3218 /// assert_eq!(o, Greater);
3219 ///
3220 /// let (quotient, o) =
3221 /// Float::from(PI).div_rational_round(Rational::from_unsigneds(1u8, 3), Nearest);
3222 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3223 /// assert_eq!(o, Less);
3224 /// ```
3225 #[inline]
3226 pub fn div_rational_round(self, other: Rational, rm: RoundingMode) -> (Float, Ordering) {
3227 let prec = self.significant_bits();
3228 self.div_rational_prec_round(other, prec, rm)
3229 }
3230
3231 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
3232 /// The [`Float`] is taken by value and the [`Rational`] by reference. An [`Ordering`] is also
3233 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3234 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3235 /// function returns a `NaN` it also returns `Equal`.
3236 ///
3237 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
3238 /// for a description of the possible rounding modes.
3239 ///
3240 /// $$
3241 /// f(x,y,m) = x/y+\varepsilon.
3242 /// $$
3243 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3244 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3245 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3246 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3247 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3248 ///
3249 /// If the output has a precision, it is the precision of the [`Float`] input.
3250 ///
3251 /// Special cases:
3252 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
3253 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
3254 /// - $f(\infty,x,m)=-\infty$ if $x<0$
3255 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
3256 /// - $f(-\infty,x,m)=\infty$ if $x<0$
3257 /// - $f(0.0,x,m)=0.0$ if $x>0$
3258 /// - $f(0.0,x,m)=-0.0$ if $x<0$
3259 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
3260 /// - $f(-0.0,x,m)=0.0$ if $x<0$
3261 ///
3262 /// If you want to specify an output precision, consider using
3263 /// [`Float::div_rational_prec_round_val_ref`] instead. If you know you'll be using the
3264 /// `Nearest` rounding mode, consider using `/` instead.
3265 ///
3266 /// # Worst-case complexity
3267 /// $T(n) = O(n \log n \log\log n)$
3268 ///
3269 /// $M(n) = O(n \log n)$
3270 ///
3271 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3272 /// other.significant_bits())`.
3273 ///
3274 /// # Panics
3275 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
3276 /// represent the output.
3277 ///
3278 /// # Examples
3279 /// ```
3280 /// use core::f64::consts::PI;
3281 /// use malachite_base::rounding_modes::RoundingMode::*;
3282 /// use malachite_float::Float;
3283 /// use malachite_q::Rational;
3284 /// use std::cmp::Ordering::*;
3285 ///
3286 /// let (quotient, o) =
3287 /// Float::from(PI).div_rational_round_val_ref(&Rational::from_unsigneds(1u8, 3), Floor);
3288 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3289 /// assert_eq!(o, Less);
3290 ///
3291 /// let (quotient, o) =
3292 /// Float::from(PI).div_rational_round_val_ref(&Rational::from_unsigneds(1u8, 3), Ceiling);
3293 /// assert_eq!(quotient.to_string(), "9.42477796076939");
3294 /// assert_eq!(o, Greater);
3295 ///
3296 /// let (quotient, o) =
3297 /// Float::from(PI).div_rational_round_val_ref(&Rational::from_unsigneds(1u8, 3), Nearest);
3298 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3299 /// assert_eq!(o, Less);
3300 /// ```
3301 #[inline]
3302 pub fn div_rational_round_val_ref(
3303 self,
3304 other: &Rational,
3305 rm: RoundingMode,
3306 ) -> (Float, Ordering) {
3307 let prec = self.significant_bits();
3308 self.div_rational_prec_round_val_ref(other, prec, rm)
3309 }
3310
3311 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
3312 /// The [`Float`] is taken by reference and the [`Rational`] by value. An [`Ordering`] is also
3313 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3314 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3315 /// function returns a `NaN` it also returns `Equal`.
3316 ///
3317 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
3318 /// for a description of the possible rounding modes.
3319 ///
3320 /// $$
3321 /// f(x,y,m) = x/y+\varepsilon.
3322 /// $$
3323 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3324 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3325 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3326 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3327 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3328 ///
3329 /// If the output has a precision, it is the precision of the [`Float`] input.
3330 ///
3331 /// Special cases:
3332 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
3333 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
3334 /// - $f(\infty,x,m)=-\infty$ if $x<0$
3335 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
3336 /// - $f(-\infty,x,m)=\infty$ if $x<0$
3337 /// - $f(0.0,x,m)=0.0$ if $x>0$
3338 /// - $f(0.0,x,m)=-0.0$ if $x<0$
3339 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
3340 /// - $f(-0.0,x,m)=0.0$ if $x<0$
3341 ///
3342 /// If you want to specify an output precision, consider using
3343 /// [`Float::div_rational_prec_round_ref_val`] instead. If you know you'll be using the
3344 /// `Nearest` rounding mode, consider using `/` instead.
3345 ///
3346 /// # Worst-case complexity
3347 /// $T(n) = O(n \log n \log\log n)$
3348 ///
3349 /// $M(n) = O(n \log n)$
3350 ///
3351 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3352 /// other.significant_bits())`.
3353 ///
3354 /// # Panics
3355 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
3356 /// represent the output.
3357 ///
3358 /// # Examples
3359 /// ```
3360 /// use core::f64::consts::PI;
3361 /// use malachite_base::rounding_modes::RoundingMode::*;
3362 /// use malachite_float::Float;
3363 /// use malachite_q::Rational;
3364 /// use std::cmp::Ordering::*;
3365 ///
3366 /// let (quotient, o) =
3367 /// Float::from(PI).div_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Floor);
3368 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3369 /// assert_eq!(o, Less);
3370 ///
3371 /// let (quotient, o) =
3372 /// Float::from(PI).div_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Ceiling);
3373 /// assert_eq!(quotient.to_string(), "9.42477796076939");
3374 /// assert_eq!(o, Greater);
3375 ///
3376 /// let (quotient, o) =
3377 /// Float::from(PI).div_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Nearest);
3378 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3379 /// assert_eq!(o, Less);
3380 /// ```
3381 #[inline]
3382 pub fn div_rational_round_ref_val(
3383 &self,
3384 other: Rational,
3385 rm: RoundingMode,
3386 ) -> (Float, Ordering) {
3387 let prec = self.significant_bits();
3388 self.div_rational_prec_round_ref_val(other, prec, rm)
3389 }
3390
3391 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
3392 /// The [`Float`] and the [`Rational`] are both are taken by reference. An [`Ordering`] is also
3393 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3394 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3395 /// function returns a `NaN` it also returns `Equal`.
3396 ///
3397 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
3398 /// for a description of the possible rounding modes.
3399 ///
3400 /// $$
3401 /// f(x,y,m) = x/y+\varepsilon.
3402 /// $$
3403 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3404 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3405 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3406 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3407 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3408 ///
3409 /// If the output has a precision, it is the precision of the [`Float`] input.
3410 ///
3411 /// Special cases:
3412 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
3413 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
3414 /// - $f(\infty,x,m)=-\infty$ if $x<0$
3415 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
3416 /// - $f(-\infty,x,m)=\infty$ if $x<0$
3417 /// - $f(0.0,x,m)=0.0$ if $x>0$
3418 /// - $f(0.0,x,m)=-0.0$ if $x<0$
3419 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
3420 /// - $f(-0.0,x,m)=0.0$ if $x<0$
3421 ///
3422 /// If you want to specify an output precision, consider using
3423 /// [`Float::div_rational_prec_round_ref_ref`] instead. If you know you'll be using the
3424 /// `Nearest` rounding mode, 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())`.
3433 ///
3434 /// # Panics
3435 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
3436 /// represent the output.
3437 ///
3438 /// # Examples
3439 /// ```
3440 /// use core::f64::consts::PI;
3441 /// use malachite_base::rounding_modes::RoundingMode::*;
3442 /// use malachite_float::Float;
3443 /// use malachite_q::Rational;
3444 /// use std::cmp::Ordering::*;
3445 ///
3446 /// let (quotient, o) =
3447 /// Float::from(PI).div_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Floor);
3448 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3449 /// assert_eq!(o, Less);
3450 ///
3451 /// let (quotient, o) =
3452 /// Float::from(PI).div_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Ceiling);
3453 /// assert_eq!(quotient.to_string(), "9.42477796076939");
3454 /// assert_eq!(o, Greater);
3455 ///
3456 /// let (quotient, o) =
3457 /// Float::from(PI).div_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Nearest);
3458 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3459 /// assert_eq!(o, Less);
3460 /// ```
3461 #[inline]
3462 pub fn div_rational_round_ref_ref(
3463 &self,
3464 other: &Rational,
3465 rm: RoundingMode,
3466 ) -> (Float, Ordering) {
3467 let prec = self.significant_bits();
3468 self.div_rational_prec_round_ref_ref(other, prec, rm)
3469 }
3470
3471 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the specified
3472 /// precision and with the specified rounding mode. The [`Rational`] is taken by value. An
3473 /// [`Ordering`] is returned, indicating whether the rounded quotient is less than, equal to, or
3474 /// greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3475 /// whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
3476 ///
3477 /// See [`RoundingMode`] for a description of the possible rounding modes.
3478 ///
3479 /// $$
3480 /// x \gets x/y+\varepsilon.
3481 /// $$
3482 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3483 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3484 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3485 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3486 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3487 ///
3488 /// If the output has a precision, it is `prec`.
3489 ///
3490 /// See the [`Float::div_rational_prec_round`] documentation for information on special cases.
3491 ///
3492 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_assign`]
3493 /// instead. If you know that your target precision is the precision of the [`Float`] input,
3494 /// consider using [`Float::div_rational_round_assign`] instead. If both of these things are
3495 /// true, consider using `/=` instead.
3496 ///
3497 /// # Worst-case complexity
3498 /// $T(n) = O(n \log n \log\log n)$
3499 ///
3500 /// $M(n) = O(n \log n)$
3501 ///
3502 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3503 /// other.significant_bits(), prec)`.
3504 ///
3505 /// # Panics
3506 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3507 ///
3508 /// # Examples
3509 /// ```
3510 /// use core::f64::consts::PI;
3511 /// use malachite_base::rounding_modes::RoundingMode::*;
3512 /// use malachite_float::Float;
3513 /// use malachite_q::Rational;
3514 /// use std::cmp::Ordering::*;
3515 ///
3516 /// let mut x = Float::from(PI);
3517 /// assert_eq!(
3518 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 5, Floor),
3519 /// Less
3520 /// );
3521 /// assert_eq!(x.to_string(), "9.0");
3522 ///
3523 /// let mut x = Float::from(PI);
3524 /// assert_eq!(
3525 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 5, Ceiling),
3526 /// Greater
3527 /// );
3528 /// assert_eq!(x.to_string(), "9.5");
3529 ///
3530 /// let mut x = Float::from(PI);
3531 /// assert_eq!(
3532 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 5, Nearest),
3533 /// Greater
3534 /// );
3535 /// assert_eq!(x.to_string(), "9.5");
3536 ///
3537 /// let mut x = Float::from(PI);
3538 /// assert_eq!(
3539 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 20, Floor),
3540 /// Less
3541 /// );
3542 /// assert_eq!(x.to_string(), "9.42477");
3543 ///
3544 /// let mut x = Float::from(PI);
3545 /// assert_eq!(
3546 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 20, Ceiling),
3547 /// Greater
3548 /// );
3549 /// assert_eq!(x.to_string(), "9.42479");
3550 ///
3551 /// let mut x = Float::from(PI);
3552 /// assert_eq!(
3553 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 20, Nearest),
3554 /// Less
3555 /// );
3556 /// assert_eq!(x.to_string(), "9.42477");
3557 /// ```
3558 #[inline]
3559 pub fn div_rational_prec_round_assign(
3560 &mut self,
3561 other: Rational,
3562 prec: u64,
3563 rm: RoundingMode,
3564 ) -> Ordering {
3565 if max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD {
3566 div_rational_prec_round_assign_naive(self, other, prec, rm)
3567 } else {
3568 div_rational_prec_round_assign_direct(self, other, prec, rm)
3569 }
3570 }
3571
3572 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the specified
3573 /// precision and with the specified rounding mode. The [`Rational`] is taken by reference. An
3574 /// [`Ordering`] is returned, indicating whether the rounded quotient is less than, equal to, or
3575 /// greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3576 /// whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
3577 ///
3578 /// See [`RoundingMode`] for a description of the possible rounding modes.
3579 ///
3580 /// $$
3581 /// x \gets x/y+\varepsilon.
3582 /// $$
3583 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3584 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3585 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3586 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3587 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3588 ///
3589 /// If the output has a precision, it is `prec`.
3590 ///
3591 /// See the [`Float::div_rational_prec_round`] documentation for information on special cases.
3592 ///
3593 /// If you know you'll be using `Nearest`, consider using
3594 /// [`Float::div_rational_prec_assign_ref`] instead. If you know that your target precision is
3595 /// the precision of the [`Float`] input, consider using
3596 /// [`Float::div_rational_round_assign_ref`] instead. If both of these things are true, consider
3597 /// using `/=` instead.
3598 ///
3599 /// # Worst-case complexity
3600 /// $T(n) = O(n \log n \log\log n)$
3601 ///
3602 /// $M(n) = O(n \log n)$
3603 ///
3604 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3605 /// other.significant_bits(), prec)`.
3606 ///
3607 /// # Panics
3608 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3609 ///
3610 /// # Examples
3611 /// ```
3612 /// use core::f64::consts::PI;
3613 /// use malachite_base::rounding_modes::RoundingMode::*;
3614 /// use malachite_float::Float;
3615 /// use malachite_q::Rational;
3616 /// use std::cmp::Ordering::*;
3617 ///
3618 /// let mut x = Float::from(PI);
3619 /// assert_eq!(
3620 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 5, Floor),
3621 /// Less
3622 /// );
3623 /// assert_eq!(x.to_string(), "9.0");
3624 ///
3625 /// let mut x = Float::from(PI);
3626 /// assert_eq!(
3627 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 5, Ceiling),
3628 /// Greater
3629 /// );
3630 /// assert_eq!(x.to_string(), "9.5");
3631 ///
3632 /// let mut x = Float::from(PI);
3633 /// assert_eq!(
3634 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 5, Nearest),
3635 /// Greater
3636 /// );
3637 /// assert_eq!(x.to_string(), "9.5");
3638 ///
3639 /// let mut x = Float::from(PI);
3640 /// assert_eq!(
3641 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 20, Floor),
3642 /// Less
3643 /// );
3644 /// assert_eq!(x.to_string(), "9.42477");
3645 ///
3646 /// let mut x = Float::from(PI);
3647 /// assert_eq!(
3648 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 20, Ceiling),
3649 /// Greater
3650 /// );
3651 /// assert_eq!(x.to_string(), "9.42479");
3652 ///
3653 /// let mut x = Float::from(PI);
3654 /// assert_eq!(
3655 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 20, Nearest),
3656 /// Less
3657 /// );
3658 /// assert_eq!(x.to_string(), "9.42477");
3659 /// ```
3660 #[inline]
3661 pub fn div_rational_prec_round_assign_ref(
3662 &mut self,
3663 other: &Rational,
3664 prec: u64,
3665 rm: RoundingMode,
3666 ) -> Ordering {
3667 if max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD {
3668 div_rational_prec_round_assign_naive_ref(self, other, prec, rm)
3669 } else {
3670 div_rational_prec_round_assign_direct_ref(self, other, prec, rm)
3671 }
3672 }
3673
3674 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the nearest value of
3675 /// the specified precision. The [`Rational`] is taken by value. An [`Ordering`] is returned,
3676 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
3677 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
3678 /// the [`Float`] to `NaN` it also returns `Equal`.
3679 ///
3680 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3681 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3682 /// description of the `Nearest` rounding mode.
3683 ///
3684 /// $$
3685 /// x \gets x/y+\varepsilon.
3686 /// $$
3687 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3688 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3689 ///
3690 /// If the output has a precision, it is `prec`.
3691 ///
3692 /// See the [`Float::div_rational_prec`] documentation for information on special cases.
3693 ///
3694 /// If you want to use a rounding mode other than `Nearest`, consider using
3695 /// [`Float::div_rational_prec_round_assign`] instead. If you know that your target precision is
3696 /// the maximum of the precisions of the two inputs, consider using `/=` instead.
3697 ///
3698 /// # Worst-case complexity
3699 /// $T(n) = O(n \log n \log\log n)$
3700 ///
3701 /// $M(n) = O(n \log n)$
3702 ///
3703 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3704 /// other.significant_bits(), prec)`.
3705 ///
3706 /// # Examples
3707 /// ```
3708 /// use core::f64::consts::PI;
3709 /// use malachite_base::num::conversion::traits::ExactFrom;
3710 /// use malachite_float::Float;
3711 /// use malachite_q::Rational;
3712 /// use std::cmp::Ordering::*;
3713 ///
3714 /// let mut x = Float::from(PI);
3715 /// assert_eq!(
3716 /// x.div_rational_prec_assign(Rational::exact_from(1.5), 5),
3717 /// Greater
3718 /// );
3719 /// assert_eq!(x.to_string(), "2.1");
3720 ///
3721 /// let mut x = Float::from(PI);
3722 /// assert_eq!(
3723 /// x.div_rational_prec_assign(Rational::exact_from(1.5), 20),
3724 /// Less
3725 /// );
3726 /// assert_eq!(x.to_string(), "2.094395");
3727 /// ```
3728 #[inline]
3729 pub fn div_rational_prec_assign(&mut self, other: Rational, prec: u64) -> Ordering {
3730 self.div_rational_prec_round_assign(other, prec, Nearest)
3731 }
3732
3733 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the nearest value of
3734 /// the specified precision. The [`Rational`] is taken by reference. An [`Ordering`] is
3735 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3736 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3737 /// function sets the [`Float`] to `NaN` it also returns `Equal`.
3738 ///
3739 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3740 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3741 /// description of the `Nearest` rounding mode.
3742 ///
3743 /// $$
3744 /// x \gets x/y+\varepsilon.
3745 /// $$
3746 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3747 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3748 ///
3749 /// If the output has a precision, it is `prec`.
3750 ///
3751 /// See the [`Float::div_rational_prec`] documentation for information on special cases.
3752 ///
3753 /// If you want to use a rounding mode other than `Nearest`, consider using
3754 /// [`Float::div_rational_prec_round_assign`] instead. If you know that your target precision is
3755 /// the maximum of the precisions of the two inputs, 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(), prec)`.
3764 ///
3765 /// # Examples
3766 /// ```
3767 /// use core::f64::consts::PI;
3768 /// use malachite_base::num::conversion::traits::ExactFrom;
3769 /// use malachite_float::Float;
3770 /// use malachite_q::Rational;
3771 /// use std::cmp::Ordering::*;
3772 ///
3773 /// let mut x = Float::from(PI);
3774 /// assert_eq!(
3775 /// x.div_rational_prec_assign_ref(&Rational::exact_from(1.5), 5),
3776 /// Greater
3777 /// );
3778 /// assert_eq!(x.to_string(), "2.1");
3779 ///
3780 /// let mut x = Float::from(PI);
3781 /// assert_eq!(
3782 /// x.div_rational_prec_assign_ref(&Rational::exact_from(1.5), 20),
3783 /// Less
3784 /// );
3785 /// assert_eq!(x.to_string(), "2.094395");
3786 /// ```
3787 #[inline]
3788 pub fn div_rational_prec_assign_ref(&mut self, other: &Rational, prec: u64) -> Ordering {
3789 self.div_rational_prec_round_assign_ref(other, prec, Nearest)
3790 }
3791
3792 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result with the specified
3793 /// rounding mode. The [`Rational`] is taken by value. An [`Ordering`] is returned, indicating
3794 /// whether the rounded quotient is less than, equal to, or greater than the exact quotient.
3795 /// Although `NaN`s are not comparable to any [`Float`], whenever this function sets the
3796 /// [`Float`] to `NaN` it also returns `Equal`.
3797 ///
3798 /// The precision of the output is the precision of the input [`Float`]. See [`RoundingMode`]
3799 /// for a description of the possible rounding modes.
3800 ///
3801 /// $$
3802 /// x \gets x/y+\varepsilon.
3803 /// $$
3804 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3805 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3806 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3807 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3808 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3809 ///
3810 /// If the output has a precision, it is the precision of the input [`Float`].
3811 ///
3812 /// See the [`Float::div_rational_round`] documentation for information on special cases.
3813 ///
3814 /// If you want to specify an output precision, consider using
3815 /// [`Float::div_rational_prec_round_assign`] instead. If you know you'll be using the `Nearest`
3816 /// rounding mode, consider using `/=` instead.
3817 ///
3818 /// # Worst-case complexity
3819 /// $T(n) = O(n \log n \log\log n)$
3820 ///
3821 /// $M(n) = O(n \log n)$
3822 ///
3823 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3824 /// other.significant_bits())`.
3825 ///
3826 /// # Panics
3827 /// Panics if `rm` is `Exact` but the precision of the input [`Float`] is not high enough to
3828 /// represent the output.
3829 ///
3830 /// # Examples
3831 /// ```
3832 /// use core::f64::consts::PI;
3833 /// use malachite_base::rounding_modes::RoundingMode::*;
3834 /// use malachite_float::Float;
3835 /// use malachite_q::Rational;
3836 /// use std::cmp::Ordering::*;
3837 ///
3838 /// let mut x = Float::from(PI);
3839 /// assert_eq!(
3840 /// x.div_rational_round_assign(Rational::from_unsigneds(1u8, 3), Floor),
3841 /// Less
3842 /// );
3843 /// assert_eq!(x.to_string(), "9.42477796076938");
3844 ///
3845 /// let mut x = Float::from(PI);
3846 /// assert_eq!(
3847 /// x.div_rational_round_assign(Rational::from_unsigneds(1u8, 3), Ceiling),
3848 /// Greater
3849 /// );
3850 /// assert_eq!(x.to_string(), "9.42477796076939");
3851 ///
3852 /// let mut x = Float::from(PI);
3853 /// assert_eq!(
3854 /// x.div_rational_round_assign(Rational::from_unsigneds(1u8, 3), Nearest),
3855 /// Less
3856 /// );
3857 /// assert_eq!(x.to_string(), "9.42477796076938");
3858 /// ```
3859 #[inline]
3860 pub fn div_rational_round_assign(&mut self, other: Rational, rm: RoundingMode) -> Ordering {
3861 let prec = self.significant_bits();
3862 self.div_rational_prec_round_assign(other, prec, rm)
3863 }
3864
3865 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result with the specified
3866 /// rounding mode. The [`Rational`] is taken by reference. An [`Ordering`] is returned,
3867 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
3868 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
3869 /// the [`Float`] to `NaN` it also returns `Equal`.
3870 ///
3871 /// The precision of the output is the precision of the input [`Float`]. See [`RoundingMode`]
3872 /// for a description of the possible rounding modes.
3873 ///
3874 /// $$
3875 /// x \gets x/y+\varepsilon.
3876 /// $$
3877 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3878 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3879 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3880 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3881 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3882 ///
3883 /// If the output has a precision, it is the precision of the input [`Float`].
3884 ///
3885 /// See the [`Float::div_rational_round`] documentation for information on special cases.
3886 ///
3887 /// If you want to specify an output precision, consider using
3888 /// [`Float::div_rational_prec_round_assign`] instead. If you know you'll be using the `Nearest`
3889 /// rounding mode, consider using `/=` instead.
3890 ///
3891 /// # Worst-case complexity
3892 /// $T(n) = O(n \log n \log\log n)$
3893 ///
3894 /// $M(n) = O(n \log n)$
3895 ///
3896 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3897 /// other.significant_bits())`.
3898 ///
3899 /// # Panics
3900 /// Panics if `rm` is `Exact` but the precision of the input [`Float`] is not high enough to
3901 /// represent the output.
3902 ///
3903 /// # Examples
3904 /// ```
3905 /// use core::f64::consts::PI;
3906 /// use malachite_base::rounding_modes::RoundingMode::*;
3907 /// use malachite_float::Float;
3908 /// use malachite_q::Rational;
3909 /// use std::cmp::Ordering::*;
3910 ///
3911 /// let mut x = Float::from(PI);
3912 /// assert_eq!(
3913 /// x.div_rational_round_assign_ref(&Rational::from_unsigneds(1u8, 3), Floor),
3914 /// Less
3915 /// );
3916 /// assert_eq!(x.to_string(), "9.42477796076938");
3917 ///
3918 /// let mut x = Float::from(PI);
3919 /// assert_eq!(
3920 /// x.div_rational_round_assign_ref(&Rational::from_unsigneds(1u8, 3), Ceiling),
3921 /// Greater
3922 /// );
3923 /// assert_eq!(x.to_string(), "9.42477796076939");
3924 ///
3925 /// let mut x = Float::from(PI);
3926 /// assert_eq!(
3927 /// x.div_rational_round_assign_ref(&Rational::from_unsigneds(1u8, 3), Nearest),
3928 /// Less
3929 /// );
3930 /// assert_eq!(x.to_string(), "9.42477796076938");
3931 /// ```
3932 #[inline]
3933 pub fn div_rational_round_assign_ref(
3934 &mut self,
3935 other: &Rational,
3936 rm: RoundingMode,
3937 ) -> Ordering {
3938 let prec = self.significant_bits();
3939 self.div_rational_prec_round_assign_ref(other, prec, rm)
3940 }
3941
3942 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
3943 /// with the specified rounding mode. The [`Rational`] and the [`Float`] are both taken by
3944 /// value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
3945 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
3946 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3947 ///
3948 /// See [`RoundingMode`] for a description of the possible rounding modes.
3949 ///
3950 /// $$
3951 /// f(x,y,p,m) = x/y+\varepsilon.
3952 /// $$
3953 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3954 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3955 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3956 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3957 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3958 ///
3959 /// If the output has a precision, it is `prec`.
3960 ///
3961 /// Special cases:
3962 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
3963 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
3964 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
3965 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
3966 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
3967 /// - $f(0,x,p,m)=0.0$ if $x>0$
3968 /// - $f(0,x,p,m)=-0.0$ if $x<0$
3969 ///
3970 /// If you know you'll be using `Nearest`, consider using [`Float::rational_div_float_prec`]
3971 /// instead. If you know that your target precision is the precision of the [`Float`] input,
3972 /// consider using [`Float::rational_div_float_round`] instead. If both of these things are
3973 /// true, consider using `/` instead.
3974 ///
3975 /// # Worst-case complexity
3976 /// $T(n) = O(n \log n \log\log n)$
3977 ///
3978 /// $M(n) = O(n \log n)$
3979 ///
3980 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
3981 /// y.significant_bits(), prec)`.
3982 ///
3983 /// # Panics
3984 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3985 ///
3986 /// # Examples
3987 /// ```
3988 /// use core::f64::consts::PI;
3989 /// use malachite_base::rounding_modes::RoundingMode::*;
3990 /// use malachite_float::Float;
3991 /// use malachite_q::Rational;
3992 /// use std::cmp::Ordering::*;
3993 ///
3994 /// let (quotient, o) =
3995 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 5, Floor);
3996 /// assert_eq!(quotient.to_string(), "0.94");
3997 /// assert_eq!(o, Less);
3998 ///
3999 /// let (quotient, o) =
4000 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 5, Ceiling);
4001 /// assert_eq!(quotient.to_string(), "0.97");
4002 /// assert_eq!(o, Greater);
4003 ///
4004 /// let (quotient, o) =
4005 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 5, Nearest);
4006 /// assert_eq!(quotient.to_string(), "0.97");
4007 /// assert_eq!(o, Greater);
4008 ///
4009 /// let (quotient, o) =
4010 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 20, Floor);
4011 /// assert_eq!(quotient.to_string(), "0.954929");
4012 /// assert_eq!(o, Less);
4013 ///
4014 /// let (quotient, o) =
4015 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 20, Ceiling);
4016 /// assert_eq!(quotient.to_string(), "0.95493");
4017 /// assert_eq!(o, Greater);
4018 ///
4019 /// let (quotient, o) =
4020 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 20, Nearest);
4021 /// assert_eq!(quotient.to_string(), "0.954929");
4022 /// assert_eq!(o, Less);
4023 /// ```
4024 #[inline]
4025 pub fn rational_div_float_prec_round(
4026 x: Rational,
4027 y: Float,
4028 prec: u64,
4029 rm: RoundingMode,
4030 ) -> (Float, Ordering) {
4031 if max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
4032 rational_div_float_prec_round_naive(x, y, prec, rm)
4033 } else {
4034 rational_div_float_prec_round_direct(x, y, prec, rm)
4035 }
4036 }
4037
4038 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
4039 /// with the specified rounding mode. The [`Rational`] is taken by value and the [`Float`] by
4040 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
4041 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
4042 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4043 ///
4044 /// See [`RoundingMode`] for a description of the possible rounding modes.
4045 ///
4046 /// $$
4047 /// f(x,y,p,m) = x/y+\varepsilon.
4048 /// $$
4049 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4050 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4051 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4052 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4053 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4054 ///
4055 /// If the output has a precision, it is `prec`.
4056 ///
4057 /// Special cases:
4058 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
4059 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
4060 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4061 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
4062 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
4063 /// - $f(0,x,p,m)=0.0$ if $x>0$
4064 /// - $f(0,x,p,m)=-0.0$ if $x<0$
4065 ///
4066 /// If you know you'll be using `Nearest`, consider using
4067 /// [`Float::rational_div_float_prec_val_ref`] instead. If you know that your target precision
4068 /// is the precision of the [`Float`] input, consider using
4069 /// [`Float::rational_div_float_round_val_ref`] instead. If both of these things are true,
4070 /// consider using `/` instead.
4071 ///
4072 /// # Worst-case complexity
4073 /// $T(n) = O(n \log n \log\log n)$
4074 ///
4075 /// $M(n) = O(n \log n)$
4076 ///
4077 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4078 /// y.significant_bits(), prec)`.
4079 ///
4080 /// # Panics
4081 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4082 ///
4083 /// # Examples
4084 /// ```
4085 /// use core::f64::consts::PI;
4086 /// use malachite_base::rounding_modes::RoundingMode::*;
4087 /// use malachite_float::Float;
4088 /// use malachite_q::Rational;
4089 /// use std::cmp::Ordering::*;
4090 ///
4091 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4092 /// Rational::from(3),
4093 /// &Float::from(PI),
4094 /// 5,
4095 /// Floor,
4096 /// );
4097 /// assert_eq!(quotient.to_string(), "0.94");
4098 /// assert_eq!(o, Less);
4099 ///
4100 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4101 /// Rational::from(3),
4102 /// &Float::from(PI),
4103 /// 5,
4104 /// Ceiling,
4105 /// );
4106 /// assert_eq!(quotient.to_string(), "0.97");
4107 /// assert_eq!(o, Greater);
4108 ///
4109 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4110 /// Rational::from(3),
4111 /// &Float::from(PI),
4112 /// 5,
4113 /// Nearest,
4114 /// );
4115 /// assert_eq!(quotient.to_string(), "0.97");
4116 /// assert_eq!(o, Greater);
4117 ///
4118 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4119 /// Rational::from(3),
4120 /// &Float::from(PI),
4121 /// 20,
4122 /// Floor,
4123 /// );
4124 /// assert_eq!(quotient.to_string(), "0.954929");
4125 /// assert_eq!(o, Less);
4126 ///
4127 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4128 /// Rational::from(3),
4129 /// &Float::from(PI),
4130 /// 20,
4131 /// Ceiling,
4132 /// );
4133 /// assert_eq!(quotient.to_string(), "0.95493");
4134 /// assert_eq!(o, Greater);
4135 ///
4136 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4137 /// Rational::from(3),
4138 /// &Float::from(PI),
4139 /// 20,
4140 /// Nearest,
4141 /// );
4142 /// assert_eq!(quotient.to_string(), "0.954929");
4143 /// assert_eq!(o, Less);
4144 /// ```
4145 #[inline]
4146 pub fn rational_div_float_prec_round_val_ref(
4147 x: Rational,
4148 y: &Float,
4149 prec: u64,
4150 rm: RoundingMode,
4151 ) -> (Float, Ordering) {
4152 if max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
4153 rational_div_float_prec_round_naive_val_ref(x, y, prec, rm)
4154 } else {
4155 rational_div_float_prec_round_direct_val_ref(x, y, prec, rm)
4156 }
4157 }
4158
4159 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
4160 /// with the specified rounding mode. The [`Rational`] is taken by reference and the [`Float`]
4161 /// by value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
4162 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
4163 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4164 ///
4165 /// See [`RoundingMode`] for a description of the possible rounding modes.
4166 ///
4167 /// $$
4168 /// f(x,y,p,m) = x/y+\varepsilon.
4169 /// $$
4170 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4171 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4172 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4173 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4174 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4175 ///
4176 /// If the output has a precision, it is `prec`.
4177 ///
4178 /// Special cases:
4179 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
4180 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
4181 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4182 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
4183 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
4184 /// - $f(0,x,p,m)=0.0$ if $x>0$
4185 /// - $f(0,x,p,m)=-0.0$ if $x<0$
4186 ///
4187 /// If you know you'll be using `Nearest`, consider using
4188 /// [`Float::rational_div_float_prec_ref_val`] instead. If you know that your target precision
4189 /// is the precision of the [`Float`] input, consider using
4190 /// [`Float::rational_div_float_round_ref_val`] instead. If both of these things are true,
4191 /// consider using `/` instead.
4192 ///
4193 /// # Worst-case complexity
4194 /// $T(n) = O(n \log n \log\log n)$
4195 ///
4196 /// $M(n) = O(n \log n)$
4197 ///
4198 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4199 /// y.significant_bits(), prec)`.
4200 ///
4201 /// # Panics
4202 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4203 ///
4204 /// # Examples
4205 /// ```
4206 /// use core::f64::consts::PI;
4207 /// use malachite_base::rounding_modes::RoundingMode::*;
4208 /// use malachite_float::Float;
4209 /// use malachite_q::Rational;
4210 /// use std::cmp::Ordering::*;
4211 ///
4212 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4213 /// &Rational::from(3),
4214 /// Float::from(PI),
4215 /// 5,
4216 /// Floor,
4217 /// );
4218 /// assert_eq!(quotient.to_string(), "0.94");
4219 /// assert_eq!(o, Less);
4220 ///
4221 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4222 /// &Rational::from(3),
4223 /// Float::from(PI),
4224 /// 5,
4225 /// Ceiling,
4226 /// );
4227 /// assert_eq!(quotient.to_string(), "0.97");
4228 /// assert_eq!(o, Greater);
4229 ///
4230 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4231 /// &Rational::from(3),
4232 /// Float::from(PI),
4233 /// 5,
4234 /// Nearest,
4235 /// );
4236 /// assert_eq!(quotient.to_string(), "0.97");
4237 /// assert_eq!(o, Greater);
4238 ///
4239 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4240 /// &Rational::from(3),
4241 /// Float::from(PI),
4242 /// 20,
4243 /// Floor,
4244 /// );
4245 /// assert_eq!(quotient.to_string(), "0.954929");
4246 /// assert_eq!(o, Less);
4247 ///
4248 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4249 /// &Rational::from(3),
4250 /// Float::from(PI),
4251 /// 20,
4252 /// Ceiling,
4253 /// );
4254 /// assert_eq!(quotient.to_string(), "0.95493");
4255 /// assert_eq!(o, Greater);
4256 ///
4257 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4258 /// &Rational::from(3),
4259 /// Float::from(PI),
4260 /// 20,
4261 /// Nearest,
4262 /// );
4263 /// assert_eq!(quotient.to_string(), "0.954929");
4264 /// assert_eq!(o, Less);
4265 /// ```
4266 #[inline]
4267 pub fn rational_div_float_prec_round_ref_val(
4268 x: &Rational,
4269 y: Float,
4270 prec: u64,
4271 rm: RoundingMode,
4272 ) -> (Float, Ordering) {
4273 if max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
4274 rational_div_float_prec_round_naive_ref_val(x, y, prec, rm)
4275 } else {
4276 rational_div_float_prec_round_direct_ref_val(x, y, prec, rm)
4277 }
4278 }
4279
4280 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
4281 /// with the specified rounding mode. The [`Rational`] and the [`Float`] are both taken by
4282 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
4283 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
4284 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4285 ///
4286 /// See [`RoundingMode`] for a description of the possible rounding modes.
4287 ///
4288 /// $$
4289 /// f(x,y,p,m) = x/y+\varepsilon.
4290 /// $$
4291 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4292 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4293 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4294 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4295 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4296 ///
4297 /// If the output has a precision, it is `prec`.
4298 ///
4299 /// Special cases:
4300 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
4301 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
4302 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4303 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
4304 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
4305 /// - $f(0,x,p,m)=0.0$ if $x>0$
4306 /// - $f(0,x,p,m)=-0.0$ if $x<0$
4307 ///
4308 /// If you know you'll be using `Nearest`, consider using
4309 /// [`Float::rational_div_float_prec_ref_ref`] instead. If you know that your target precision
4310 /// is the precision of the [`Float`] input, consider using
4311 /// [`Float::rational_div_float_round_ref_ref`] instead. If both of these things are true,
4312 /// consider using `/` instead.
4313 ///
4314 /// # Worst-case complexity
4315 /// $T(n) = O(n \log n \log\log n)$
4316 ///
4317 /// $M(n) = O(n \log n)$
4318 ///
4319 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4320 /// y.significant_bits(), prec)`.
4321 ///
4322 /// # Panics
4323 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4324 ///
4325 /// # Examples
4326 /// ```
4327 /// use core::f64::consts::PI;
4328 /// use malachite_base::rounding_modes::RoundingMode::*;
4329 /// use malachite_float::Float;
4330 /// use malachite_q::Rational;
4331 /// use std::cmp::Ordering::*;
4332 ///
4333 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4334 /// &Rational::from(3),
4335 /// &Float::from(PI),
4336 /// 5,
4337 /// Floor,
4338 /// );
4339 /// assert_eq!(quotient.to_string(), "0.94");
4340 /// assert_eq!(o, Less);
4341 ///
4342 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4343 /// &Rational::from(3),
4344 /// &Float::from(PI),
4345 /// 5,
4346 /// Ceiling,
4347 /// );
4348 /// assert_eq!(quotient.to_string(), "0.97");
4349 /// assert_eq!(o, Greater);
4350 ///
4351 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4352 /// &Rational::from(3),
4353 /// &Float::from(PI),
4354 /// 5,
4355 /// Nearest,
4356 /// );
4357 /// assert_eq!(quotient.to_string(), "0.97");
4358 /// assert_eq!(o, Greater);
4359 ///
4360 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4361 /// &Rational::from(3),
4362 /// &Float::from(PI),
4363 /// 20,
4364 /// Floor,
4365 /// );
4366 /// assert_eq!(quotient.to_string(), "0.954929");
4367 /// assert_eq!(o, Less);
4368 ///
4369 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4370 /// &Rational::from(3),
4371 /// &Float::from(PI),
4372 /// 20,
4373 /// Ceiling,
4374 /// );
4375 /// assert_eq!(quotient.to_string(), "0.95493");
4376 /// assert_eq!(o, Greater);
4377 ///
4378 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4379 /// &Rational::from(3),
4380 /// &Float::from(PI),
4381 /// 20,
4382 /// Nearest,
4383 /// );
4384 /// assert_eq!(quotient.to_string(), "0.954929");
4385 /// assert_eq!(o, Less);
4386 /// ```
4387 #[inline]
4388 pub fn rational_div_float_prec_round_ref_ref(
4389 x: &Rational,
4390 y: &Float,
4391 prec: u64,
4392 rm: RoundingMode,
4393 ) -> (Float, Ordering) {
4394 if max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
4395 rational_div_float_prec_round_naive_ref_ref(x, y, prec, rm)
4396 } else {
4397 rational_div_float_prec_round_direct_ref_ref(x, y, prec, rm)
4398 }
4399 }
4400
4401 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
4402 /// specified precision. The [`Rational`] and the [`Float`] are both are taken by value. An
4403 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
4404 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
4405 /// whenever this function returns a `NaN` it also returns `Equal`.
4406 ///
4407 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4408 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4409 /// description of the `Nearest` rounding mode.
4410 ///
4411 /// $$
4412 /// f(x,y,p) = x/y+\varepsilon.
4413 /// $$
4414 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4415 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4416 ///
4417 /// If the output has a precision, it is `prec`.
4418 ///
4419 /// Special cases:
4420 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
4421 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
4422 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
4423 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
4424 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
4425 /// - $f(0,x,p)=0.0$ if $x>0$
4426 /// - $f(0,x,p)=-0.0$ if $x<0$
4427 ///
4428 /// If you want to use a rounding mode other than `Nearest`, consider using
4429 /// [`Float::rational_div_float_prec_round`] instead. If you know that your target precision is
4430 /// the precision of the [`Float`] input, consider using `/` instead.
4431 ///
4432 /// # Worst-case complexity
4433 /// $T(n) = O(n \log n \log\log n)$
4434 ///
4435 /// $M(n) = O(n \log n)$
4436 ///
4437 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4438 /// y.significant_bits(), prec)`.
4439 ///
4440 /// # Examples
4441 /// ```
4442 /// use core::f64::consts::PI;
4443 /// use malachite_float::Float;
4444 /// use malachite_q::Rational;
4445 /// use std::cmp::Ordering::*;
4446 ///
4447 /// let (quotient, o) = Float::rational_div_float_prec(Rational::from(3), Float::from(PI), 5);
4448 /// assert_eq!(quotient.to_string(), "0.97");
4449 /// assert_eq!(o, Greater);
4450 ///
4451 /// let (quotient, o) = Float::rational_div_float_prec(Rational::from(3), Float::from(PI), 20);
4452 /// assert_eq!(quotient.to_string(), "0.954929");
4453 /// assert_eq!(o, Less);
4454 /// ```
4455 #[inline]
4456 pub fn rational_div_float_prec(x: Rational, y: Float, prec: u64) -> (Float, Ordering) {
4457 Float::rational_div_float_prec_round(x, y, prec, Nearest)
4458 }
4459
4460 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
4461 /// specified precision. The [`Rational`] is taken by value and the [`Float`] by reference. An
4462 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
4463 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
4464 /// whenever this function returns a `NaN` it also returns `Equal`.
4465 ///
4466 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4467 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4468 /// description of the `Nearest` rounding mode.
4469 ///
4470 /// $$
4471 /// f(x,y,p) = x/y+\varepsilon.
4472 /// $$
4473 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4474 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4475 ///
4476 /// If the output has a precision, it is `prec`.
4477 ///
4478 /// Special cases:
4479 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
4480 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
4481 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
4482 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
4483 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
4484 /// - $f(0,x,p)=0.0$ if $x>0$
4485 /// - $f(0,x,p)=-0.0$ if $x<0$
4486 ///
4487 /// If you want to use a rounding mode other than `Nearest`, consider using
4488 /// [`Float::rational_div_float_prec_round_val_ref`] instead. If you know that your target
4489 /// precision is the precision of the [`Float`] input, consider using `/` instead.
4490 ///
4491 /// # Worst-case complexity
4492 /// $T(n) = O(n \log n \log\log n)$
4493 ///
4494 /// $M(n) = O(n \log n)$
4495 ///
4496 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4497 /// y.significant_bits(), prec)`.
4498 ///
4499 /// # Examples
4500 /// ```
4501 /// use core::f64::consts::PI;
4502 /// use malachite_float::Float;
4503 /// use malachite_q::Rational;
4504 /// use std::cmp::Ordering::*;
4505 ///
4506 /// let (quotient, o) =
4507 /// Float::rational_div_float_prec_val_ref(Rational::from(3), &Float::from(PI), 5);
4508 /// assert_eq!(quotient.to_string(), "0.97");
4509 /// assert_eq!(o, Greater);
4510 ///
4511 /// let (quotient, o) =
4512 /// Float::rational_div_float_prec_val_ref(Rational::from(3), &Float::from(PI), 20);
4513 /// assert_eq!(quotient.to_string(), "0.954929");
4514 /// assert_eq!(o, Less);
4515 /// ```
4516 #[inline]
4517 pub fn rational_div_float_prec_val_ref(x: Rational, y: &Float, prec: u64) -> (Float, Ordering) {
4518 Float::rational_div_float_prec_round_val_ref(x, y, prec, Nearest)
4519 }
4520
4521 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
4522 /// specified precision. The [`Rational`] is taken by reference and the [`Float`] by value. An
4523 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
4524 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
4525 /// whenever this function returns a `NaN` it also returns `Equal`.
4526 ///
4527 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4528 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4529 /// description of the `Nearest` rounding mode.
4530 ///
4531 /// $$
4532 /// f(x,y,p) = x/y+\varepsilon.
4533 /// $$
4534 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4535 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4536 ///
4537 /// If the output has a precision, it is `prec`.
4538 ///
4539 /// Special cases:
4540 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
4541 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
4542 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
4543 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
4544 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
4545 /// - $f(0,x,p)=0.0$ if $x>0$
4546 /// - $f(0,x,p)=-0.0$ if $x<0$
4547 ///
4548 /// If you want to use a rounding mode other than `Nearest`, consider using
4549 /// [`Float::rational_div_float_prec_round_ref_val`] instead. If you know that your target
4550 /// precision is the precision of the [`Float`] input, consider using `/` instead.
4551 ///
4552 /// # Worst-case complexity
4553 /// $T(n) = O(n \log n \log\log n)$
4554 ///
4555 /// $M(n) = O(n \log n)$
4556 ///
4557 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4558 /// y.significant_bits(), prec)`.
4559 ///
4560 /// # Examples
4561 /// ```
4562 /// use core::f64::consts::PI;
4563 /// use malachite_float::Float;
4564 /// use malachite_q::Rational;
4565 /// use std::cmp::Ordering::*;
4566 ///
4567 /// let (quotient, o) =
4568 /// Float::rational_div_float_prec_ref_val(&Rational::from(3), Float::from(PI), 5);
4569 /// assert_eq!(quotient.to_string(), "0.97");
4570 /// assert_eq!(o, Greater);
4571 ///
4572 /// let (quotient, o) =
4573 /// Float::rational_div_float_prec_ref_val(&Rational::from(3), Float::from(PI), 20);
4574 /// assert_eq!(quotient.to_string(), "0.954929");
4575 /// assert_eq!(o, Less);
4576 /// ```
4577 #[inline]
4578 pub fn rational_div_float_prec_ref_val(x: &Rational, y: Float, prec: u64) -> (Float, Ordering) {
4579 Float::rational_div_float_prec_round_ref_val(x, y, prec, Nearest)
4580 }
4581
4582 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
4583 /// specified precision. The [`Rational`] and the [`Float`] are both are taken by reference. An
4584 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
4585 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
4586 /// whenever this function returns a `NaN` it also returns `Equal`.
4587 ///
4588 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4589 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4590 /// description of the `Nearest` rounding mode.
4591 ///
4592 /// $$
4593 /// f(x,y,p) = x/y+\varepsilon.
4594 /// $$
4595 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4596 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4597 ///
4598 /// If the output has a precision, it is `prec`.
4599 ///
4600 /// Special cases:
4601 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
4602 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
4603 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
4604 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
4605 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
4606 /// - $f(0,x,p)=0.0$ if $x>0$
4607 /// - $f(0,x,p)=-0.0$ if $x<0$
4608 ///
4609 /// If you want to use a rounding mode other than `Nearest`, consider using
4610 /// [`Float::rational_div_float_prec_round_ref_ref`] instead. If you know that your target
4611 /// precision is the precision of the [`Float`] input, consider using `/` instead.
4612 ///
4613 /// # Worst-case complexity
4614 /// $T(n) = O(n \log n \log\log n)$
4615 ///
4616 /// $M(n) = O(n \log n)$
4617 ///
4618 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4619 /// y.significant_bits(), prec)`.
4620 ///
4621 /// # Examples
4622 /// ```
4623 /// use core::f64::consts::PI;
4624 /// use malachite_float::Float;
4625 /// use malachite_q::Rational;
4626 /// use std::cmp::Ordering::*;
4627 ///
4628 /// let (quotient, o) =
4629 /// Float::rational_div_float_prec_ref_ref(&Rational::from(3), &Float::from(PI), 5);
4630 /// assert_eq!(quotient.to_string(), "0.97");
4631 /// assert_eq!(o, Greater);
4632 ///
4633 /// let (quotient, o) =
4634 /// Float::rational_div_float_prec_ref_ref(&Rational::from(3), &Float::from(PI), 20);
4635 /// assert_eq!(quotient.to_string(), "0.954929");
4636 /// assert_eq!(o, Less);
4637 /// ```
4638 #[inline]
4639 pub fn rational_div_float_prec_ref_ref(
4640 x: &Rational,
4641 y: &Float,
4642 prec: u64,
4643 ) -> (Float, Ordering) {
4644 Float::rational_div_float_prec_round_ref_ref(x, y, prec, Nearest)
4645 }
4646
4647 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
4648 /// The [`Rational`] and the [`Float`] are both are taken by value. An [`Ordering`] is also
4649 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4650 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4651 /// function returns a `NaN` it also returns `Equal`.
4652 ///
4653 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4654 /// for a description of the possible rounding modes.
4655 ///
4656 /// $$
4657 /// f(x,y,m) = x/y+\varepsilon.
4658 /// $$
4659 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4660 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4661 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4662 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4663 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4664 ///
4665 /// If the output has a precision, it is the precision of the [`Float`] input.
4666 ///
4667 /// Special cases:
4668 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
4669 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
4670 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4671 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
4672 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
4673 /// - $f(0,x,m)=0.0$ if $x>0$
4674 /// - $f(0,x,m)=-0.0$ if $x<0$
4675 ///
4676 /// If you want to specify an output precision, consider using
4677 /// [`Float::rational_div_float_prec_round`] instead. If you know you'll be using the `Nearest`
4678 /// rounding mode, consider using `/` instead.
4679 ///
4680 /// # Worst-case complexity
4681 /// $T(n) = O(n \log n \log\log n)$
4682 ///
4683 /// $M(n) = O(n \log n)$
4684 ///
4685 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4686 /// y.significant_bits())`.
4687 ///
4688 /// # Panics
4689 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4690 /// represent the output.
4691 ///
4692 /// # Examples
4693 /// ```
4694 /// use core::f64::consts::PI;
4695 /// use malachite_base::rounding_modes::RoundingMode::*;
4696 /// use malachite_float::Float;
4697 /// use malachite_q::Rational;
4698 /// use std::cmp::Ordering::*;
4699 ///
4700 /// let (quotient, o) =
4701 /// Float::rational_div_float_round(Rational::from(3), Float::from(PI), Floor);
4702 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
4703 /// assert_eq!(o, Less);
4704 ///
4705 /// let (quotient, o) =
4706 /// Float::rational_div_float_round(Rational::from(3), Float::from(PI), Ceiling);
4707 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4708 /// assert_eq!(o, Greater);
4709 ///
4710 /// let (quotient, o) =
4711 /// Float::rational_div_float_round(Rational::from(3), Float::from(PI), Nearest);
4712 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4713 /// assert_eq!(o, Greater);
4714 /// ```
4715 #[inline]
4716 pub fn rational_div_float_round(x: Rational, y: Float, rm: RoundingMode) -> (Float, Ordering) {
4717 let prec = y.significant_bits();
4718 Float::rational_div_float_prec_round(x, y, prec, rm)
4719 }
4720
4721 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
4722 /// The [`Rational`] is taken by value and the [`Float`] by reference. An [`Ordering`] is also
4723 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4724 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4725 /// function returns a `NaN` it also returns `Equal`.
4726 ///
4727 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4728 /// for a description of the possible rounding modes.
4729 ///
4730 /// $$
4731 /// f(x,y,m) = x/y+\varepsilon.
4732 /// $$
4733 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4734 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4735 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4736 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4737 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4738 ///
4739 /// If the output has a precision, it is the precision of the [`Float`] input.
4740 ///
4741 /// Special cases:
4742 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
4743 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
4744 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4745 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
4746 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
4747 /// - $f(0,x,m)=0.0$ if $x>0$
4748 /// - $f(0,x,m)=-0.0$ if $x<0$
4749 ///
4750 /// If you want to specify an output precision, consider using
4751 /// [`Float::rational_div_float_prec_round_val_ref`] instead. If you know you'll be using the
4752 /// `Nearest` rounding mode, consider using `/` instead.
4753 ///
4754 /// # Worst-case complexity
4755 /// $T(n) = O(n \log n \log\log n)$
4756 ///
4757 /// $M(n) = O(n \log n)$
4758 ///
4759 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4760 /// y.significant_bits())`.
4761 ///
4762 /// # Panics
4763 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4764 /// represent the output.
4765 ///
4766 /// # Examples
4767 /// ```
4768 /// use core::f64::consts::PI;
4769 /// use malachite_base::rounding_modes::RoundingMode::*;
4770 /// use malachite_float::Float;
4771 /// use malachite_q::Rational;
4772 /// use std::cmp::Ordering::*;
4773 ///
4774 /// let (quotient, o) =
4775 /// Float::rational_div_float_round_val_ref(Rational::from(3), &Float::from(PI), Floor);
4776 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
4777 /// assert_eq!(o, Less);
4778 ///
4779 /// let (quotient, o) =
4780 /// Float::rational_div_float_round_val_ref(Rational::from(3), &Float::from(PI), Ceiling);
4781 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4782 /// assert_eq!(o, Greater);
4783 ///
4784 /// let (quotient, o) =
4785 /// Float::rational_div_float_round_val_ref(Rational::from(3), &Float::from(PI), Nearest);
4786 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4787 /// assert_eq!(o, Greater);
4788 /// ```
4789 #[inline]
4790 pub fn rational_div_float_round_val_ref(
4791 x: Rational,
4792 y: &Float,
4793 rm: RoundingMode,
4794 ) -> (Float, Ordering) {
4795 let prec = y.significant_bits();
4796 Float::rational_div_float_prec_round_val_ref(x, y, prec, rm)
4797 }
4798
4799 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
4800 /// The [`Rational`] is taken by reference and the [`Float`] by value. An [`Ordering`] is also
4801 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4802 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4803 /// function returns a `NaN` it also returns `Equal`.
4804 ///
4805 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4806 /// for a description of the possible rounding modes.
4807 ///
4808 /// $$
4809 /// f(x,y,m) = x/y+\varepsilon.
4810 /// $$
4811 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4812 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4813 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4814 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4815 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4816 ///
4817 /// If the output has a precision, it is the precision of the [`Float`] input.
4818 ///
4819 /// Special cases:
4820 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
4821 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
4822 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4823 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
4824 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
4825 /// - $f(0,x,m)=0.0$ if $x>0$
4826 /// - $f(0,x,m)=-0.0$ if $x<0$
4827 ///
4828 /// If you want to specify an output precision, consider using
4829 /// [`Float::rational_div_float_prec_round_ref_val`] instead. If you know you'll be using the
4830 /// `Nearest` rounding mode, consider using `/` instead.
4831 ///
4832 /// # Worst-case complexity
4833 /// $T(n) = O(n \log n \log\log n)$
4834 ///
4835 /// $M(n) = O(n \log n)$
4836 ///
4837 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4838 /// y.significant_bits())`.
4839 ///
4840 /// # Panics
4841 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4842 /// represent the output.
4843 ///
4844 /// # Examples
4845 /// ```
4846 /// use core::f64::consts::PI;
4847 /// use malachite_base::rounding_modes::RoundingMode::*;
4848 /// use malachite_float::Float;
4849 /// use malachite_q::Rational;
4850 /// use std::cmp::Ordering::*;
4851 ///
4852 /// let (quotient, o) =
4853 /// Float::rational_div_float_round_ref_val(&Rational::from(3), Float::from(PI), Floor);
4854 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
4855 /// assert_eq!(o, Less);
4856 ///
4857 /// let (quotient, o) =
4858 /// Float::rational_div_float_round_ref_val(&Rational::from(3), Float::from(PI), Ceiling);
4859 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4860 /// assert_eq!(o, Greater);
4861 ///
4862 /// let (quotient, o) =
4863 /// Float::rational_div_float_round_ref_val(&Rational::from(3), Float::from(PI), Nearest);
4864 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4865 /// assert_eq!(o, Greater);
4866 /// ```
4867 #[inline]
4868 pub fn rational_div_float_round_ref_val(
4869 x: &Rational,
4870 y: Float,
4871 rm: RoundingMode,
4872 ) -> (Float, Ordering) {
4873 let prec = y.significant_bits();
4874 Float::rational_div_float_prec_round_ref_val(x, y, prec, rm)
4875 }
4876
4877 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
4878 /// The [`Rational`] and the [`Float`] are both are taken by reference. An [`Ordering`] is also
4879 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4880 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4881 /// function returns a `NaN` it also returns `Equal`.
4882 ///
4883 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4884 /// for a description of the possible rounding modes.
4885 ///
4886 /// $$
4887 /// f(x,y,m) = x/y+\varepsilon.
4888 /// $$
4889 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4890 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4891 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4892 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4893 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4894 ///
4895 /// If the output has a precision, it is the precision of the [`Float`] input.
4896 ///
4897 /// Special cases:
4898 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
4899 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
4900 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4901 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
4902 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
4903 /// - $f(0,x,m)=0.0$ if $x>0$
4904 /// - $f(0,x,m)=-0.0$ if $x<0$
4905 ///
4906 /// If you want to specify an output precision, consider using
4907 /// [`Float::rational_div_float_prec_round_ref_ref`] instead. If you know you'll be using the
4908 /// `Nearest` rounding mode, consider using `/` instead.
4909 ///
4910 /// # Worst-case complexity
4911 /// $T(n) = O(n \log n \log\log n)$
4912 ///
4913 /// $M(n) = O(n \log n)$
4914 ///
4915 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4916 /// y.significant_bits())`.
4917 ///
4918 /// # Panics
4919 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4920 /// represent the output.
4921 ///
4922 /// # Examples
4923 /// ```
4924 /// use core::f64::consts::PI;
4925 /// use malachite_base::rounding_modes::RoundingMode::*;
4926 /// use malachite_float::Float;
4927 /// use malachite_q::Rational;
4928 /// use std::cmp::Ordering::*;
4929 ///
4930 /// let (quotient, o) =
4931 /// Float::rational_div_float_round_ref_ref(&Rational::from(3), &Float::from(PI), Floor);
4932 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
4933 /// assert_eq!(o, Less);
4934 ///
4935 /// let (quotient, o) =
4936 /// Float::rational_div_float_round_ref_ref(&Rational::from(3), &Float::from(PI), Ceiling);
4937 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4938 /// assert_eq!(o, Greater);
4939 ///
4940 /// let (quotient, o) =
4941 /// Float::rational_div_float_round_ref_ref(&Rational::from(3), &Float::from(PI), Nearest);
4942 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4943 /// assert_eq!(o, Greater);
4944 /// ```
4945 #[inline]
4946 pub fn rational_div_float_round_ref_ref(
4947 x: &Rational,
4948 y: &Float,
4949 rm: RoundingMode,
4950 ) -> (Float, Ordering) {
4951 let prec = y.significant_bits();
4952 Float::rational_div_float_prec_round_ref_ref(x, y, prec, rm)
4953 }
4954}
4955
4956impl Div<Float> for Float {
4957 type Output = Float;
4958
4959 /// Divides two [`Float`]s, taking both by value.
4960 ///
4961 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
4962 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
4963 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
4964 /// `Nearest` rounding mode.
4965 ///
4966 /// $$
4967 /// f(x,y) = x/y+\varepsilon.
4968 /// $$
4969 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4970 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
4971 /// where $p$ is the maximum precision of the inputs.
4972 ///
4973 /// Special cases:
4974 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
4975 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
4976 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
4977 /// - $f(x,0.0)=\infty$ if $x>0.0$
4978 /// - $f(x,0.0)=-\infty$ if $x<0.0$
4979 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
4980 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
4981 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
4982 /// - $f(x,-0.0)=\infty$ if $x<0.0$
4983 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
4984 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
4985 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
4986 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
4987 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
4988 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
4989 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
4990 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
4991 ///
4992 /// If you want to use a rounding mode other than `Nearest`, consider using [`Float::div_prec`]
4993 /// instead. If you want to specify the output precision, consider using [`Float::div_round`].
4994 /// If you want both of these things, consider using [`Float::div_prec_round`].
4995 ///
4996 /// # Worst-case complexity
4997 /// $T(n) = O(n \log n \log\log n)$
4998 ///
4999 /// $M(n) = O(n \log n)$
5000 ///
5001 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5002 /// other.significant_bits())`.
5003 ///
5004 /// # Examples
5005 /// ```
5006 /// use malachite_base::num::basic::traits::{
5007 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5008 /// };
5009 /// use malachite_float::Float;
5010 ///
5011 /// assert!((Float::from(1.5) / Float::NAN).is_nan());
5012 /// assert_eq!(Float::from(1.5) / Float::ZERO, Float::INFINITY);
5013 /// assert_eq!(
5014 /// Float::from(1.5) / Float::NEGATIVE_ZERO,
5015 /// Float::NEGATIVE_INFINITY
5016 /// );
5017 /// assert_eq!(Float::from(-1.5) / Float::ZERO, Float::NEGATIVE_INFINITY);
5018 /// assert_eq!(Float::from(-1.5) / Float::NEGATIVE_ZERO, Float::INFINITY);
5019 /// assert!((Float::ZERO / Float::ZERO).is_nan());
5020 ///
5021 /// assert_eq!((Float::from(1.5) / Float::from(2.5)).to_string(), "0.6");
5022 /// assert_eq!((Float::from(1.5) / Float::from(-2.5)).to_string(), "-0.6");
5023 /// assert_eq!((Float::from(-1.5) / Float::from(2.5)).to_string(), "-0.6");
5024 /// assert_eq!((Float::from(-1.5) / Float::from(-2.5)).to_string(), "0.6");
5025 /// ```
5026 #[inline]
5027 fn div(self, other: Float) -> Float {
5028 let prec = max(self.significant_bits(), other.significant_bits());
5029 self.div_prec_round(other, prec, Nearest).0
5030 }
5031}
5032
5033impl Div<&Float> for Float {
5034 type Output = Float;
5035
5036 /// Divides two [`Float`]s, taking the first by value and the second by reference.
5037 ///
5038 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5039 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5040 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5041 /// `Nearest` rounding mode.
5042 ///
5043 /// $$
5044 /// f(x,y) = x/y+\varepsilon.
5045 /// $$
5046 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5047 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5048 /// where $p$ is the maximum precision of the inputs.
5049 ///
5050 /// Special cases:
5051 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
5052 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
5053 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
5054 /// - $f(x,0.0)=\infty$ if $x>0.0$
5055 /// - $f(x,0.0)=-\infty$ if $x<0.0$
5056 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
5057 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
5058 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
5059 /// - $f(x,-0.0)=\infty$ if $x<0.0$
5060 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
5061 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
5062 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5063 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5064 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
5065 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
5066 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5067 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5068 ///
5069 /// If you want to use a rounding mode other than `Nearest`, consider using
5070 /// [`Float::div_prec_val_ref`] instead. If you want to specify the output precision, consider
5071 /// using [`Float::div_round_val_ref`]. If you want both of these things, consider using
5072 /// [`Float::div_prec_round_val_ref`].
5073 ///
5074 /// # Worst-case complexity
5075 /// $T(n) = O(n \log n \log\log n)$
5076 ///
5077 /// $M(n) = O(n \log n)$
5078 ///
5079 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5080 /// other.significant_bits())`.
5081 ///
5082 /// # Examples
5083 /// ```
5084 /// use malachite_base::num::basic::traits::{
5085 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5086 /// };
5087 /// use malachite_float::Float;
5088 ///
5089 /// assert!((Float::from(1.5) / &Float::NAN).is_nan());
5090 /// assert_eq!(Float::from(1.5) / &Float::ZERO, Float::INFINITY);
5091 /// assert_eq!(
5092 /// Float::from(1.5) / &Float::NEGATIVE_ZERO,
5093 /// Float::NEGATIVE_INFINITY
5094 /// );
5095 /// assert_eq!(Float::from(-1.5) / &Float::ZERO, Float::NEGATIVE_INFINITY);
5096 /// assert_eq!(Float::from(-1.5) / &Float::NEGATIVE_ZERO, Float::INFINITY);
5097 /// assert!((Float::ZERO / &Float::ZERO).is_nan());
5098 ///
5099 /// assert_eq!((Float::from(1.5) / &Float::from(2.5)).to_string(), "0.6");
5100 /// assert_eq!((Float::from(1.5) / &Float::from(-2.5)).to_string(), "-0.6");
5101 /// assert_eq!((Float::from(-1.5) / &Float::from(2.5)).to_string(), "-0.6");
5102 /// assert_eq!((Float::from(-1.5) / &Float::from(-2.5)).to_string(), "0.6");
5103 /// ```
5104 #[inline]
5105 fn div(self, other: &Float) -> Float {
5106 let prec = max(self.significant_bits(), other.significant_bits());
5107 self.div_prec_round_val_ref(other, prec, Nearest).0
5108 }
5109}
5110
5111impl Div<Float> for &Float {
5112 type Output = Float;
5113
5114 /// Divides two [`Float`]s, taking the first by reference and the second by value.
5115 ///
5116 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5117 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5118 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5119 /// `Nearest` rounding mode.
5120 ///
5121 /// $$
5122 /// f(x,y) = x/y+\varepsilon.
5123 /// $$
5124 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5125 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5126 /// where $p$ is the maximum precision of the inputs.
5127 ///
5128 /// Special cases:
5129 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
5130 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
5131 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
5132 /// - $f(x,0.0)=\infty$ if $x>0.0$
5133 /// - $f(x,0.0)=-\infty$ if $x<0.0$
5134 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
5135 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
5136 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
5137 /// - $f(x,-0.0)=\infty$ if $x<0.0$
5138 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
5139 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
5140 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5141 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5142 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
5143 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
5144 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5145 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5146 ///
5147 /// If you want to use a rounding mode other than `Nearest`, consider using
5148 /// [`Float::div_prec_ref_val`] instead. If you want to specify the output precision, consider
5149 /// using [`Float::div_round_ref_val`]. If you want both of these things, consider using
5150 /// [`Float::div_prec_round_ref_val`].
5151 ///
5152 /// # Worst-case complexity
5153 /// $T(n) = O(n \log n \log\log n)$
5154 ///
5155 /// $M(n) = O(n \log n)$
5156 ///
5157 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5158 /// other.significant_bits())`.
5159 ///
5160 /// # Examples
5161 /// ```
5162 /// use malachite_base::num::basic::traits::{
5163 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5164 /// };
5165 /// use malachite_float::Float;
5166 ///
5167 /// assert!((&Float::from(1.5) / Float::NAN).is_nan());
5168 /// assert_eq!(&Float::from(1.5) / Float::ZERO, Float::INFINITY);
5169 /// assert_eq!(
5170 /// &Float::from(1.5) / Float::NEGATIVE_ZERO,
5171 /// Float::NEGATIVE_INFINITY
5172 /// );
5173 /// assert_eq!(&Float::from(-1.5) / Float::ZERO, Float::NEGATIVE_INFINITY);
5174 /// assert_eq!(&Float::from(-1.5) / Float::NEGATIVE_ZERO, Float::INFINITY);
5175 /// assert!((&Float::ZERO / Float::ZERO).is_nan());
5176 ///
5177 /// assert_eq!((&Float::from(1.5) / Float::from(2.5)).to_string(), "0.6");
5178 /// assert_eq!((&Float::from(1.5) / Float::from(-2.5)).to_string(), "-0.6");
5179 /// assert_eq!((&Float::from(-1.5) / Float::from(2.5)).to_string(), "-0.6");
5180 /// assert_eq!((&Float::from(-1.5) / Float::from(-2.5)).to_string(), "0.6");
5181 /// ```
5182 #[inline]
5183 fn div(self, other: Float) -> Float {
5184 let prec = max(self.significant_bits(), other.significant_bits());
5185 self.div_prec_round_ref_val(other, prec, Nearest).0
5186 }
5187}
5188
5189impl Div<&Float> for &Float {
5190 type Output = Float;
5191
5192 /// Divides two [`Float`]s, taking both by reference.
5193 ///
5194 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5195 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5196 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5197 /// `Nearest` rounding mode.
5198 ///
5199 /// $$
5200 /// f(x,y) = x/y+\varepsilon.
5201 /// $$
5202 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5203 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5204 /// where $p$ is the maximum precision of the inputs.
5205 ///
5206 /// Special cases:
5207 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
5208 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
5209 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
5210 /// - $f(x,0.0)=\infty$ if $x>0.0$
5211 /// - $f(x,0.0)=-\infty$ if $x<0.0$
5212 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
5213 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
5214 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
5215 /// - $f(x,-0.0)=\infty$ if $x<0.0$
5216 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
5217 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
5218 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5219 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5220 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
5221 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
5222 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5223 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5224 ///
5225 /// If you want to use a rounding mode other than `Nearest`, consider using
5226 /// [`Float::div_prec_ref_ref`] instead. If you want to specify the output precision, consider
5227 /// using [`Float::div_round_ref_ref`]. If you want both of these things, consider using
5228 /// [`Float::div_prec_round_ref_ref`].
5229 ///
5230 /// # Worst-case complexity
5231 /// $T(n) = O(n \log n \log\log n)$
5232 ///
5233 /// $M(n) = O(n \log n)$
5234 ///
5235 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5236 /// other.significant_bits())`.
5237 ///
5238 /// # Examples
5239 /// ```
5240 /// use malachite_base::num::basic::traits::{
5241 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5242 /// };
5243 /// use malachite_float::Float;
5244 ///
5245 /// assert!((&Float::from(1.5) / &Float::NAN).is_nan());
5246 /// assert_eq!(&Float::from(1.5) / &Float::ZERO, Float::INFINITY);
5247 /// assert_eq!(
5248 /// &Float::from(1.5) / &Float::NEGATIVE_ZERO,
5249 /// Float::NEGATIVE_INFINITY
5250 /// );
5251 /// assert_eq!(&Float::from(-1.5) / &Float::ZERO, Float::NEGATIVE_INFINITY);
5252 /// assert_eq!(&Float::from(-1.5) / &Float::NEGATIVE_ZERO, Float::INFINITY);
5253 /// assert!((&Float::ZERO / &Float::ZERO).is_nan());
5254 ///
5255 /// assert_eq!((&Float::from(1.5) / &Float::from(2.5)).to_string(), "0.6");
5256 /// assert_eq!((&Float::from(1.5) / &Float::from(-2.5)).to_string(), "-0.6");
5257 /// assert_eq!((&Float::from(-1.5) / &Float::from(2.5)).to_string(), "-0.6");
5258 /// assert_eq!((&Float::from(-1.5) / &Float::from(-2.5)).to_string(), "0.6");
5259 /// ```
5260 #[inline]
5261 fn div(self, other: &Float) -> Float {
5262 let prec = max(self.significant_bits(), other.significant_bits());
5263 self.div_prec_round_ref_ref(other, prec, Nearest).0
5264 }
5265}
5266
5267impl DivAssign<Float> for Float {
5268 /// Divides a [`Float`] by a [`Float`] in place, taking the [`Float`] on the right-hand side by
5269 /// value.
5270 ///
5271 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5272 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5273 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5274 /// `Nearest` rounding mode.
5275 ///
5276 /// $$
5277 /// x\gets = x/y+\varepsilon.
5278 /// $$
5279 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5280 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5281 /// where $p$ is the maximum precision of the inputs.
5282 ///
5283 /// See the `/` documentation for information on special cases.
5284 ///
5285 /// If you want to use a rounding mode other than `Nearest`, consider using
5286 /// [`Float::div_prec_assign`] instead. If you want to specify the output precision, consider
5287 /// using [`Float::div_round_assign`]. If you want both of these things, consider using
5288 /// [`Float::div_prec_round_assign`].
5289 ///
5290 /// # Worst-case complexity
5291 /// $T(n) = O(n \log n \log\log n)$
5292 ///
5293 /// $M(n) = O(n \log n)$
5294 ///
5295 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5296 /// other.significant_bits())`.
5297 ///
5298 /// # Examples
5299 /// ```
5300 /// use malachite_base::num::basic::traits::{
5301 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5302 /// };
5303 /// use malachite_float::Float;
5304 ///
5305 /// let mut x = Float::from(1.5);
5306 /// x /= Float::NAN;
5307 /// assert!(x.is_nan());
5308 ///
5309 /// let mut x = Float::from(1.5);
5310 /// x /= Float::ZERO;
5311 /// assert_eq!(x, Float::INFINITY);
5312 ///
5313 /// let mut x = Float::from(1.5);
5314 /// x /= Float::NEGATIVE_ZERO;
5315 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5316 ///
5317 /// let mut x = Float::from(-1.5);
5318 /// x /= Float::ZERO;
5319 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5320 ///
5321 /// let mut x = Float::from(-1.5);
5322 /// x /= Float::NEGATIVE_ZERO;
5323 /// assert_eq!(x, Float::INFINITY);
5324 ///
5325 /// let mut x = Float::INFINITY;
5326 /// x /= Float::INFINITY;
5327 /// assert!(x.is_nan());
5328 ///
5329 /// let mut x = Float::from(1.5);
5330 /// x /= Float::from(2.5);
5331 /// assert_eq!(x.to_string(), "0.6");
5332 ///
5333 /// let mut x = Float::from(1.5);
5334 /// x /= Float::from(-2.5);
5335 /// assert_eq!(x.to_string(), "-0.6");
5336 ///
5337 /// let mut x = Float::from(-1.5);
5338 /// x /= Float::from(2.5);
5339 /// assert_eq!(x.to_string(), "-0.6");
5340 ///
5341 /// let mut x = Float::from(-1.5);
5342 /// x /= Float::from(-2.5);
5343 /// assert_eq!(x.to_string(), "0.6");
5344 /// ```
5345 #[inline]
5346 fn div_assign(&mut self, other: Float) {
5347 let prec = max(self.significant_bits(), other.significant_bits());
5348 self.div_prec_round_assign(other, prec, Nearest);
5349 }
5350}
5351
5352impl DivAssign<&Float> for Float {
5353 /// Divides a [`Float`] by a [`Float`] in place, taking the [`Float`] on the right-hand side by
5354 /// reference.
5355 ///
5356 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5357 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5358 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5359 /// `Nearest` rounding mode.
5360 ///
5361 /// $$
5362 /// x\gets = x/y+\varepsilon.
5363 /// $$
5364 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5365 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5366 /// where $p$ is the maximum precision of the inputs.
5367 ///
5368 /// See the `/` documentation for information on special cases.
5369 ///
5370 /// If you want to use a rounding mode other than `Nearest`, consider using
5371 /// [`Float::div_prec_assign`] instead. If you want to specify the output precision, consider
5372 /// using [`Float::div_round_assign`]. If you want both of these things, consider using
5373 /// [`Float::div_prec_round_assign`].
5374 ///
5375 /// # Worst-case complexity
5376 /// $T(n) = O(n \log n \log\log n)$
5377 ///
5378 /// $M(n) = O(n \log n)$
5379 ///
5380 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5381 /// other.significant_bits())`.
5382 ///
5383 /// # Examples
5384 /// ```
5385 /// use malachite_base::num::basic::traits::{
5386 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5387 /// };
5388 /// use malachite_float::Float;
5389 ///
5390 /// let mut x = Float::from(1.5);
5391 /// x /= &Float::NAN;
5392 /// assert!(x.is_nan());
5393 ///
5394 /// let mut x = Float::from(1.5);
5395 /// x /= &Float::ZERO;
5396 /// assert_eq!(x, Float::INFINITY);
5397 ///
5398 /// let mut x = Float::from(1.5);
5399 /// x /= &Float::NEGATIVE_ZERO;
5400 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5401 ///
5402 /// let mut x = Float::from(-1.5);
5403 /// x /= &Float::ZERO;
5404 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5405 ///
5406 /// let mut x = Float::from(-1.5);
5407 /// x /= &Float::NEGATIVE_ZERO;
5408 /// assert_eq!(x, Float::INFINITY);
5409 ///
5410 /// let mut x = Float::INFINITY;
5411 /// x /= &Float::INFINITY;
5412 /// assert!(x.is_nan());
5413 ///
5414 /// let mut x = Float::from(1.5);
5415 /// x /= &Float::from(2.5);
5416 /// assert_eq!(x.to_string(), "0.6");
5417 ///
5418 /// let mut x = Float::from(1.5);
5419 /// x /= &Float::from(-2.5);
5420 /// assert_eq!(x.to_string(), "-0.6");
5421 ///
5422 /// let mut x = Float::from(-1.5);
5423 /// x /= &Float::from(2.5);
5424 /// assert_eq!(x.to_string(), "-0.6");
5425 ///
5426 /// let mut x = Float::from(-1.5);
5427 /// x /= &Float::from(-2.5);
5428 /// assert_eq!(x.to_string(), "0.6");
5429 /// ```
5430 #[inline]
5431 fn div_assign(&mut self, other: &Float) {
5432 let prec = max(self.significant_bits(), other.significant_bits());
5433 self.div_prec_round_assign_ref(other, prec, Nearest);
5434 }
5435}
5436
5437impl Div<Rational> for Float {
5438 type Output = Float;
5439
5440 /// Divides a [`Float`] by a [`Rational`], taking both by value.
5441 ///
5442 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
5443 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
5444 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
5445 /// rounding mode.
5446 ///
5447 /// $$
5448 /// f(x,y) = x/y+\varepsilon.
5449 /// $$
5450 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5451 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5452 /// where $p$ is the precision of the input [`Float`].
5453 ///
5454 /// Special cases:
5455 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
5456 /// - $f(\infty,x)=\infty$ if $x\geq 0$
5457 /// - $f(\infty,x)=-\infty$ if $x<0$
5458 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
5459 /// - $f(-\infty,x)=\infty$ if $x<0$
5460 /// - $f(0.0,x)=0.0$ if $x>0$
5461 /// - $f(0.0,x)=-0.0$ if $x<0$
5462 /// - $f(-0.0,x)=-0.0$ if $x>0$
5463 /// - $f(-0.0,x)=0.0$ if $x<0$
5464 ///
5465 /// If you want to use a rounding mode other than `Nearest`, consider using
5466 /// [`Float::div_rational_prec`] instead. If you want to specify the output precision, consider
5467 /// using [`Float::div_rational_round`]. If you want both of these things, consider using
5468 /// [`Float::div_rational_prec_round`].
5469 ///
5470 /// # Worst-case complexity
5471 /// $T(n) = O(n \log n \log\log n)$
5472 ///
5473 /// $M(n) = O(n \log n)$
5474 ///
5475 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5476 /// other.significant_bits())`.
5477 ///
5478 /// # Examples
5479 /// ```
5480 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
5481 /// use malachite_base::num::conversion::traits::ExactFrom;
5482 /// use malachite_float::Float;
5483 /// use malachite_q::Rational;
5484 ///
5485 /// assert!((Float::NAN / Rational::exact_from(1.5)).is_nan());
5486 /// assert_eq!(Float::INFINITY / Rational::exact_from(1.5), Float::INFINITY);
5487 /// assert_eq!(
5488 /// Float::NEGATIVE_INFINITY / Rational::exact_from(1.5),
5489 /// Float::NEGATIVE_INFINITY
5490 /// );
5491 /// assert_eq!(
5492 /// Float::INFINITY / Rational::exact_from(-1.5),
5493 /// Float::NEGATIVE_INFINITY
5494 /// );
5495 /// assert_eq!(
5496 /// Float::NEGATIVE_INFINITY / Rational::exact_from(-1.5),
5497 /// Float::INFINITY
5498 /// );
5499 ///
5500 /// assert_eq!(
5501 /// (Float::from(2.5) / Rational::exact_from(1.5)).to_string(),
5502 /// "1.8"
5503 /// );
5504 /// assert_eq!(
5505 /// (Float::from(2.5) / Rational::exact_from(-1.5)).to_string(),
5506 /// "-1.8"
5507 /// );
5508 /// assert_eq!(
5509 /// (Float::from(-2.5) / Rational::exact_from(1.5)).to_string(),
5510 /// "-1.8"
5511 /// );
5512 /// assert_eq!(
5513 /// (Float::from(-2.5) / Rational::exact_from(-1.5)).to_string(),
5514 /// "1.8"
5515 /// );
5516 /// ```
5517 #[inline]
5518 fn div(self, other: Rational) -> Float {
5519 let prec = self.significant_bits();
5520 self.div_rational_prec_round(other, prec, Nearest).0
5521 }
5522}
5523
5524impl Div<&Rational> for Float {
5525 type Output = Float;
5526
5527 /// Divides a [`Float`] by a [`Rational`], taking the first by value and the second by
5528 /// reference.
5529 ///
5530 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
5531 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
5532 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
5533 /// rounding mode.
5534 ///
5535 /// $$
5536 /// f(x,y) = x/y+\varepsilon.
5537 /// $$
5538 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5539 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5540 /// where $p$ is the precision of the input [`Float`].
5541 ///
5542 /// Special cases:
5543 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
5544 /// - $f(\infty,x)=\infty$ if $x\geq 0$
5545 /// - $f(\infty,x)=-\infty$ if $x<0$
5546 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
5547 /// - $f(-\infty,x)=\infty$ if $x<0$
5548 /// - $f(0.0,x)=0.0$ if $x>0$
5549 /// - $f(0.0,x)=-0.0$ if $x<0$
5550 /// - $f(-0.0,x)=-0.0$ if $x>0$
5551 /// - $f(-0.0,x)=0.0$ if $x<0$
5552 ///
5553 /// If you want to use a rounding mode other than `Nearest`, consider using
5554 /// [`Float::div_rational_prec_val_ref`] instead. If you want to specify the output precision,
5555 /// consider using [`Float::div_rational_round_val_ref`]. If you want both of these things,
5556 /// consider using [`Float::div_rational_prec_round_val_ref`].
5557 ///
5558 /// # Worst-case complexity
5559 /// $T(n) = O(n \log n \log\log n)$
5560 ///
5561 /// $M(n) = O(n \log n)$
5562 ///
5563 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5564 /// other.significant_bits())`.
5565 ///
5566 /// # Examples
5567 /// ```
5568 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
5569 /// use malachite_base::num::conversion::traits::ExactFrom;
5570 /// use malachite_float::Float;
5571 /// use malachite_q::Rational;
5572 ///
5573 /// assert!((Float::NAN / &Rational::exact_from(1.5)).is_nan());
5574 /// assert_eq!(
5575 /// Float::INFINITY / &Rational::exact_from(1.5),
5576 /// Float::INFINITY
5577 /// );
5578 /// assert_eq!(
5579 /// Float::NEGATIVE_INFINITY / &Rational::exact_from(1.5),
5580 /// Float::NEGATIVE_INFINITY
5581 /// );
5582 /// assert_eq!(
5583 /// Float::INFINITY / &Rational::exact_from(-1.5),
5584 /// Float::NEGATIVE_INFINITY
5585 /// );
5586 /// assert_eq!(
5587 /// Float::NEGATIVE_INFINITY / &Rational::exact_from(-1.5),
5588 /// Float::INFINITY
5589 /// );
5590 ///
5591 /// assert_eq!(
5592 /// (Float::from(2.5) / &Rational::exact_from(1.5)).to_string(),
5593 /// "1.8"
5594 /// );
5595 /// assert_eq!(
5596 /// (Float::from(2.5) / &Rational::exact_from(-1.5)).to_string(),
5597 /// "-1.8"
5598 /// );
5599 /// assert_eq!(
5600 /// (Float::from(-2.5) / &Rational::exact_from(1.5)).to_string(),
5601 /// "-1.8"
5602 /// );
5603 /// assert_eq!(
5604 /// (Float::from(-2.5) / &Rational::exact_from(-1.5)).to_string(),
5605 /// "1.8"
5606 /// );
5607 /// ```
5608 #[inline]
5609 fn div(self, other: &Rational) -> Float {
5610 let prec = self.significant_bits();
5611 self.div_rational_prec_round_val_ref(other, prec, Nearest).0
5612 }
5613}
5614
5615impl Div<Rational> for &Float {
5616 type Output = Float;
5617
5618 /// Divides a [`Float`] by a [`Rational`], taking the first by reference and the second by
5619 /// value.
5620 ///
5621 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
5622 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
5623 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
5624 /// rounding mode.
5625 ///
5626 /// $$
5627 /// f(x,y) = x/y+\varepsilon.
5628 /// $$
5629 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5630 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5631 /// where $p$ is the precision of the input [`Float`].
5632 ///
5633 /// Special cases:
5634 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
5635 /// - $f(\infty,x)=\infty$ if $x\geq 0$
5636 /// - $f(\infty,x)=-\infty$ if $x<0$
5637 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
5638 /// - $f(-\infty,x)=\infty$ if $x<0$
5639 /// - $f(0.0,x)=0.0$ if $x>0$
5640 /// - $f(0.0,x)=-0.0$ if $x<0$
5641 /// - $f(-0.0,x)=-0.0$ if $x>0$
5642 /// - $f(-0.0,x)=0.0$ if $x<0$
5643 ///
5644 /// If you want to use a rounding mode other than `Nearest`, consider using
5645 /// [`Float::div_rational_prec_ref_val`] instead. If you want to specify the output precision,
5646 /// consider using [`Float::div_rational_round_ref_val`]. If you want both of these things,
5647 /// consider using [`Float::div_rational_prec_round_ref_val`].
5648 ///
5649 /// # Worst-case complexity
5650 /// $T(n) = O(n \log n \log\log n)$
5651 ///
5652 /// $M(n) = O(n \log n)$
5653 ///
5654 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5655 /// other.significant_bits())`.
5656 ///
5657 /// # Examples
5658 /// ```
5659 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
5660 /// use malachite_base::num::conversion::traits::ExactFrom;
5661 /// use malachite_float::Float;
5662 /// use malachite_q::Rational;
5663 ///
5664 /// assert!((&Float::NAN / Rational::exact_from(1.5)).is_nan());
5665 /// assert_eq!(
5666 /// &Float::INFINITY / Rational::exact_from(1.5),
5667 /// Float::INFINITY
5668 /// );
5669 /// assert_eq!(
5670 /// &Float::NEGATIVE_INFINITY / Rational::exact_from(1.5),
5671 /// Float::NEGATIVE_INFINITY
5672 /// );
5673 /// assert_eq!(
5674 /// &Float::INFINITY / Rational::exact_from(-1.5),
5675 /// Float::NEGATIVE_INFINITY
5676 /// );
5677 /// assert_eq!(
5678 /// &Float::NEGATIVE_INFINITY / Rational::exact_from(-1.5),
5679 /// Float::INFINITY
5680 /// );
5681 ///
5682 /// assert_eq!(
5683 /// (&Float::from(2.5) / Rational::exact_from(1.5)).to_string(),
5684 /// "1.8"
5685 /// );
5686 /// assert_eq!(
5687 /// (&Float::from(2.5) / Rational::exact_from(-1.5)).to_string(),
5688 /// "-1.8"
5689 /// );
5690 /// assert_eq!(
5691 /// (&Float::from(-2.5) / Rational::exact_from(1.5)).to_string(),
5692 /// "-1.8"
5693 /// );
5694 /// assert_eq!(
5695 /// (&Float::from(-2.5) / Rational::exact_from(-1.5)).to_string(),
5696 /// "1.8"
5697 /// );
5698 /// ```
5699 #[inline]
5700 fn div(self, other: Rational) -> Float {
5701 let prec = self.significant_bits();
5702 self.div_rational_prec_round_ref_val(other, prec, Nearest).0
5703 }
5704}
5705
5706impl Div<&Rational> for &Float {
5707 type Output = Float;
5708
5709 /// Divides a [`Float`] by a [`Rational`], taking both by reference.
5710 ///
5711 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
5712 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
5713 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
5714 /// rounding mode.
5715 ///
5716 /// $$
5717 /// f(x,y) = x/y+\varepsilon.
5718 /// $$
5719 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5720 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5721 /// where $p$ is the precision of the input [`Float`].
5722 ///
5723 /// Special cases:
5724 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
5725 /// - $f(\infty,x)=\infty$ if $x\geq 0$
5726 /// - $f(\infty,x)=-\infty$ if $x<0$
5727 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
5728 /// - $f(-\infty,x)=\infty$ if $x<0$
5729 /// - $f(0.0,x)=0.0$ if $x>0$
5730 /// - $f(0.0,x)=-0.0$ if $x<0$
5731 /// - $f(-0.0,x)=-0.0$ if $x>0$
5732 /// - $f(-0.0,x)=0.0$ if $x<0$
5733 ///
5734 /// If you want to use a rounding mode other than `Nearest`, consider using
5735 /// [`Float::div_rational_prec_ref_ref`] instead. If you want to specify the output precision,
5736 /// consider using [`Float::div_rational_round_ref_ref`]. If you want both of these things,
5737 /// consider using [`Float::div_rational_prec_round_ref_ref`].
5738 ///
5739 /// # Worst-case complexity
5740 /// $T(n) = O(n \log n \log\log n)$
5741 ///
5742 /// $M(n) = O(n \log n)$
5743 ///
5744 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5745 /// other.significant_bits())`.
5746 ///
5747 /// # Examples
5748 /// ```
5749 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
5750 /// use malachite_base::num::conversion::traits::ExactFrom;
5751 /// use malachite_float::Float;
5752 /// use malachite_q::Rational;
5753 ///
5754 /// assert!((&Float::NAN / &Rational::exact_from(1.5)).is_nan());
5755 /// assert_eq!(
5756 /// &Float::INFINITY / &Rational::exact_from(1.5),
5757 /// Float::INFINITY
5758 /// );
5759 /// assert_eq!(
5760 /// &Float::NEGATIVE_INFINITY / &Rational::exact_from(1.5),
5761 /// Float::NEGATIVE_INFINITY
5762 /// );
5763 /// assert_eq!(
5764 /// &Float::INFINITY / &Rational::exact_from(-1.5),
5765 /// Float::NEGATIVE_INFINITY
5766 /// );
5767 /// assert_eq!(
5768 /// &Float::NEGATIVE_INFINITY / &Rational::exact_from(-1.5),
5769 /// Float::INFINITY
5770 /// );
5771 ///
5772 /// assert_eq!(
5773 /// (&Float::from(2.5) / &Rational::exact_from(1.5)).to_string(),
5774 /// "1.8"
5775 /// );
5776 /// assert_eq!(
5777 /// (&Float::from(2.5) / &Rational::exact_from(-1.5)).to_string(),
5778 /// "-1.8"
5779 /// );
5780 /// assert_eq!(
5781 /// (&Float::from(-2.5) / &Rational::exact_from(1.5)).to_string(),
5782 /// "-1.8"
5783 /// );
5784 /// assert_eq!(
5785 /// (&Float::from(-2.5) / &Rational::exact_from(-1.5)).to_string(),
5786 /// "1.8"
5787 /// );
5788 /// ```
5789 #[inline]
5790 fn div(self, other: &Rational) -> Float {
5791 let prec = self.significant_bits();
5792 self.div_rational_prec_round_ref_ref(other, prec, Nearest).0
5793 }
5794}
5795
5796impl DivAssign<Rational> for Float {
5797 /// Divides a [`Float`] by a [`Rational`] in place, taking the [`Rational`] by value.
5798 ///
5799 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
5800 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
5801 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
5802 /// rounding mode.
5803 ///
5804 /// $$
5805 /// x\gets = x/y+\varepsilon.
5806 /// $$
5807 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5808 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5809 /// where $p$ is the precision of the input [`Float`].
5810 ///
5811 /// See the `/` documentation for information on special cases.
5812 ///
5813 /// If you want to use a rounding mode other than `Nearest`, consider using
5814 /// [`Float::div_rational_prec_assign`] instead. If you want to specify the output precision,
5815 /// consider using [`Float::div_rational_round_assign`]. If you want both of these things,
5816 /// consider using [`Float::div_rational_prec_round_assign`].
5817 ///
5818 /// # Worst-case complexity
5819 /// $T(n) = O(n \log n \log\log n)$
5820 ///
5821 /// $M(n) = O(n \log n)$
5822 ///
5823 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5824 /// other.significant_bits())`.
5825 ///
5826 /// # Examples
5827 /// ```
5828 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
5829 /// use malachite_base::num::conversion::traits::ExactFrom;
5830 /// use malachite_float::Float;
5831 /// use malachite_q::Rational;
5832 ///
5833 /// let mut x = Float::NAN;
5834 /// x /= Rational::exact_from(1.5);
5835 /// assert!(x.is_nan());
5836 ///
5837 /// let mut x = Float::INFINITY;
5838 /// x /= Rational::exact_from(1.5);
5839 /// assert_eq!(x, Float::INFINITY);
5840 ///
5841 /// let mut x = Float::NEGATIVE_INFINITY;
5842 /// x /= Rational::exact_from(1.5);
5843 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5844 ///
5845 /// let mut x = Float::INFINITY;
5846 /// x /= Rational::exact_from(-1.5);
5847 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5848 ///
5849 /// let mut x = Float::NEGATIVE_INFINITY;
5850 /// x /= Rational::exact_from(-1.5);
5851 /// assert_eq!(x, Float::INFINITY);
5852 ///
5853 /// let mut x = Float::from(2.5);
5854 /// x /= Rational::exact_from(1.5);
5855 /// assert_eq!(x.to_string(), "1.8");
5856 /// ```
5857 #[inline]
5858 fn div_assign(&mut self, other: Rational) {
5859 let prec = self.significant_bits();
5860 self.div_rational_prec_round_assign(other, prec, Nearest);
5861 }
5862}
5863
5864impl DivAssign<&Rational> for Float {
5865 /// Divides a [`Float`] by a [`Rational`] in place, taking the [`Rational`] by reference.
5866 ///
5867 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
5868 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
5869 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
5870 /// rounding mode.
5871 ///
5872 /// $$
5873 /// x\gets = x/y+\varepsilon.
5874 /// $$
5875 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5876 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5877 /// where $p$ is the precision of the input [`Float`].
5878 ///
5879 /// See the `/` documentation for information on special cases.
5880 ///
5881 /// If you want to use a rounding mode other than `Nearest`, consider using
5882 /// [`Float::div_rational_prec_assign_ref`] instead. If you want to specify the output
5883 /// precision, consider using [`Float::div_rational_round_assign_ref`]. If you want both of
5884 /// these things, consider using [`Float::div_rational_prec_round_assign_ref`].
5885 ///
5886 /// # Worst-case complexity
5887 /// $T(n) = O(n \log n \log\log n)$
5888 ///
5889 /// $M(n) = O(n \log n)$
5890 ///
5891 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5892 /// other.significant_bits())`.
5893 ///
5894 /// # Examples
5895 /// ```
5896 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
5897 /// use malachite_base::num::conversion::traits::ExactFrom;
5898 /// use malachite_float::Float;
5899 /// use malachite_q::Rational;
5900 ///
5901 /// let mut x = Float::NAN;
5902 /// x /= &Rational::exact_from(1.5);
5903 /// assert!(x.is_nan());
5904 ///
5905 /// let mut x = Float::INFINITY;
5906 /// x /= &Rational::exact_from(1.5);
5907 /// assert_eq!(x, Float::INFINITY);
5908 ///
5909 /// let mut x = Float::NEGATIVE_INFINITY;
5910 /// x /= &Rational::exact_from(1.5);
5911 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5912 ///
5913 /// let mut x = Float::INFINITY;
5914 /// x /= &Rational::exact_from(-1.5);
5915 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5916 ///
5917 /// let mut x = Float::NEGATIVE_INFINITY;
5918 /// x /= &Rational::exact_from(-1.5);
5919 /// assert_eq!(x, Float::INFINITY);
5920 ///
5921 /// let mut x = Float::from(2.5);
5922 /// x /= &Rational::exact_from(1.5);
5923 /// assert_eq!(x.to_string(), "1.8");
5924 /// ```
5925 #[inline]
5926 fn div_assign(&mut self, other: &Rational) {
5927 let prec = self.significant_bits();
5928 self.div_rational_prec_round_assign_ref(other, prec, Nearest);
5929 }
5930}
5931
5932impl Div<Float> for Rational {
5933 type Output = Float;
5934
5935 /// Divides a [`Rational`] by a [`Float`], taking both by value.
5936 ///
5937 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
5938 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
5939 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
5940 /// rounding mode.
5941 ///
5942 /// $$
5943 /// f(x,y) = x/y+\varepsilon.
5944 /// $$
5945 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5946 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5947 /// where $p$ is the precision of the input [`Float`].
5948 ///
5949 /// Special cases:
5950 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
5951 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
5952 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
5953 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
5954 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
5955 /// - $f(0,x,p,m)=0.0$ if $x>0$
5956 /// - $f(0,x,p,m)=-0.0$ if $x<0$
5957 ///
5958 /// # Worst-case complexity
5959 /// $T(n) = O(n \log n \log\log n)$
5960 ///
5961 /// $M(n) = O(n \log n)$
5962 ///
5963 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5964 /// other.significant_bits())`.
5965 ///
5966 /// # Examples
5967 /// ```
5968 /// use malachite_base::num::basic::traits::{
5969 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5970 /// };
5971 /// use malachite_base::num::conversion::traits::ExactFrom;
5972 /// use malachite_float::Float;
5973 /// use malachite_q::Rational;
5974 ///
5975 /// assert!((Rational::exact_from(1.5) / Float::NAN).is_nan());
5976 /// assert_eq!(Rational::exact_from(1.5) / Float::ZERO, Float::INFINITY);
5977 /// assert_eq!(
5978 /// Rational::exact_from(1.5) / Float::NEGATIVE_ZERO,
5979 /// Float::NEGATIVE_INFINITY
5980 /// );
5981 /// assert_eq!(
5982 /// Rational::exact_from(-1.5) / Float::ZERO,
5983 /// Float::NEGATIVE_INFINITY
5984 /// );
5985 /// assert_eq!(
5986 /// Rational::exact_from(-1.5) / Float::NEGATIVE_ZERO,
5987 /// Float::INFINITY
5988 /// );
5989 ///
5990 /// assert_eq!(
5991 /// (Rational::exact_from(1.5) / Float::from(2.5)).to_string(),
5992 /// "0.6"
5993 /// );
5994 /// assert_eq!(
5995 /// (Rational::exact_from(-1.5) / Float::from(2.5)).to_string(),
5996 /// "-0.6"
5997 /// );
5998 /// assert_eq!(
5999 /// (Rational::exact_from(1.5) / Float::from(-2.5)).to_string(),
6000 /// "-0.6"
6001 /// );
6002 /// assert_eq!(
6003 /// (Rational::exact_from(-1.5) / Float::from(-2.5)).to_string(),
6004 /// "0.6"
6005 /// );
6006 /// ```
6007 #[inline]
6008 fn div(self, other: Float) -> Float {
6009 let prec = other.significant_bits();
6010 Float::rational_div_float_prec_round(self, other, prec, Nearest).0
6011 }
6012}
6013
6014impl Div<&Float> for Rational {
6015 type Output = Float;
6016
6017 /// Divides a [`Rational`] by a [`Float`], taking the [`Rational`] by value and the [`Float`] by
6018 /// reference.
6019 ///
6020 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6021 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6022 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6023 /// rounding mode.
6024 ///
6025 /// $$
6026 /// f(x,y) = x/y+\varepsilon.
6027 /// $$
6028 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6029 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6030 /// where $p$ is the precision of the input [`Float`].
6031 ///
6032 /// Special cases:
6033 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
6034 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
6035 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
6036 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
6037 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
6038 /// - $f(0,x,p,m)=0.0$ if $x>0$
6039 /// - $f(0,x,p,m)=-0.0$ if $x<0$
6040 ///
6041 /// # Worst-case complexity
6042 /// $T(n) = O(n \log n \log\log n)$
6043 ///
6044 /// $M(n) = O(n \log n)$
6045 ///
6046 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6047 /// other.significant_bits())`.
6048 ///
6049 /// # Examples
6050 /// ```
6051 /// use malachite_base::num::basic::traits::{
6052 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6053 /// };
6054 /// use malachite_base::num::conversion::traits::ExactFrom;
6055 /// use malachite_float::Float;
6056 /// use malachite_q::Rational;
6057 ///
6058 /// assert!((Rational::exact_from(1.5) / &Float::NAN).is_nan());
6059 /// assert_eq!(Rational::exact_from(1.5) / &Float::ZERO, Float::INFINITY);
6060 /// assert_eq!(
6061 /// Rational::exact_from(1.5) / &Float::NEGATIVE_ZERO,
6062 /// Float::NEGATIVE_INFINITY
6063 /// );
6064 /// assert_eq!(
6065 /// Rational::exact_from(-1.5) / &Float::ZERO,
6066 /// Float::NEGATIVE_INFINITY
6067 /// );
6068 /// assert_eq!(
6069 /// Rational::exact_from(-1.5) / &Float::NEGATIVE_ZERO,
6070 /// Float::INFINITY
6071 /// );
6072 ///
6073 /// assert_eq!(
6074 /// (Rational::exact_from(1.5) / &Float::from(2.5)).to_string(),
6075 /// "0.6"
6076 /// );
6077 /// assert_eq!(
6078 /// (Rational::exact_from(-1.5) / &Float::from(2.5)).to_string(),
6079 /// "-0.6"
6080 /// );
6081 /// assert_eq!(
6082 /// (Rational::exact_from(1.5) / &Float::from(-2.5)).to_string(),
6083 /// "-0.6"
6084 /// );
6085 /// assert_eq!(
6086 /// (Rational::exact_from(-1.5) / &Float::from(-2.5)).to_string(),
6087 /// "0.6"
6088 /// );
6089 /// ```
6090 #[inline]
6091 fn div(self, other: &Float) -> Float {
6092 let prec = other.significant_bits();
6093 Float::rational_div_float_prec_round_val_ref(self, other, prec, Nearest).0
6094 }
6095}
6096
6097impl Div<Float> for &Rational {
6098 type Output = Float;
6099
6100 /// Divides a [`Rational`] by a [`Float`], taking the [`Rational`] by reference and the
6101 /// [`Float`] by value.
6102 ///
6103 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6104 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6105 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6106 /// rounding mode.
6107 ///
6108 /// $$
6109 /// f(x,y) = x/y+\varepsilon.
6110 /// $$
6111 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6112 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6113 /// where $p$ is the precision of the input [`Float`].
6114 ///
6115 /// Special cases:
6116 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
6117 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
6118 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
6119 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
6120 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
6121 /// - $f(0,x,p,m)=0.0$ if $x>0$
6122 /// - $f(0,x,p,m)=-0.0$ if $x<0$
6123 ///
6124 /// # Worst-case complexity
6125 /// $T(n) = O(n \log n \log\log n)$
6126 ///
6127 /// $M(n) = O(n \log n)$
6128 ///
6129 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6130 /// other.significant_bits())`.
6131 ///
6132 /// # Examples
6133 /// ```
6134 /// use malachite_base::num::basic::traits::{
6135 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6136 /// };
6137 /// use malachite_base::num::conversion::traits::ExactFrom;
6138 /// use malachite_float::Float;
6139 /// use malachite_q::Rational;
6140 ///
6141 /// assert!((&Rational::exact_from(1.5) / Float::NAN).is_nan());
6142 /// assert_eq!(&Rational::exact_from(1.5) / Float::ZERO, Float::INFINITY);
6143 /// assert_eq!(
6144 /// &Rational::exact_from(1.5) / Float::NEGATIVE_ZERO,
6145 /// Float::NEGATIVE_INFINITY
6146 /// );
6147 /// assert_eq!(
6148 /// &Rational::exact_from(-1.5) / Float::ZERO,
6149 /// Float::NEGATIVE_INFINITY
6150 /// );
6151 /// assert_eq!(
6152 /// &Rational::exact_from(-1.5) / Float::NEGATIVE_ZERO,
6153 /// Float::INFINITY
6154 /// );
6155 ///
6156 /// assert_eq!(
6157 /// (&Rational::exact_from(1.5) / Float::from(2.5)).to_string(),
6158 /// "0.6"
6159 /// );
6160 /// assert_eq!(
6161 /// (&Rational::exact_from(-1.5) / Float::from(2.5)).to_string(),
6162 /// "-0.6"
6163 /// );
6164 /// assert_eq!(
6165 /// (&Rational::exact_from(1.5) / Float::from(-2.5)).to_string(),
6166 /// "-0.6"
6167 /// );
6168 /// assert_eq!(
6169 /// (&Rational::exact_from(-1.5) / Float::from(-2.5)).to_string(),
6170 /// "0.6"
6171 /// );
6172 /// ```
6173 #[inline]
6174 fn div(self, other: Float) -> Float {
6175 let prec = other.significant_bits();
6176 Float::rational_div_float_prec_round_ref_val(self, other, prec, Nearest).0
6177 }
6178}
6179
6180impl Div<&Float> for &Rational {
6181 type Output = Float;
6182
6183 /// Divides a [`Rational`] by a [`Float`], taking both by reference.
6184 ///
6185 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6186 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6187 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6188 /// rounding mode.
6189 ///
6190 /// $$
6191 /// f(x,y) = x/y+\varepsilon.
6192 /// $$
6193 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6194 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6195 /// where $p$ is the precision of the input [`Float`].
6196 ///
6197 /// Special cases:
6198 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
6199 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
6200 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
6201 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
6202 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
6203 /// - $f(0,x,p,m)=0.0$ if $x>0$
6204 /// - $f(0,x,p,m)=-0.0$ if $x<0$
6205 ///
6206 /// # Worst-case complexity
6207 /// $T(n) = O(n \log n \log\log n)$
6208 ///
6209 /// $M(n) = O(n \log n)$
6210 ///
6211 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6212 /// other.significant_bits())`.
6213 ///
6214 /// # Examples
6215 /// ```
6216 /// use malachite_base::num::basic::traits::{
6217 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6218 /// };
6219 /// use malachite_base::num::conversion::traits::ExactFrom;
6220 /// use malachite_float::Float;
6221 /// use malachite_q::Rational;
6222 ///
6223 /// assert!((&Rational::exact_from(1.5) / &Float::NAN).is_nan());
6224 /// assert_eq!(&Rational::exact_from(1.5) / &Float::ZERO, Float::INFINITY);
6225 /// assert_eq!(
6226 /// &Rational::exact_from(1.5) / &Float::NEGATIVE_ZERO,
6227 /// Float::NEGATIVE_INFINITY
6228 /// );
6229 /// assert_eq!(
6230 /// &Rational::exact_from(-1.5) / &Float::ZERO,
6231 /// Float::NEGATIVE_INFINITY
6232 /// );
6233 /// assert_eq!(
6234 /// &Rational::exact_from(-1.5) / &Float::NEGATIVE_ZERO,
6235 /// Float::INFINITY
6236 /// );
6237 ///
6238 /// assert_eq!(
6239 /// (&Rational::exact_from(1.5) / &Float::from(2.5)).to_string(),
6240 /// "0.6"
6241 /// );
6242 /// assert_eq!(
6243 /// (&Rational::exact_from(-1.5) / &Float::from(2.5)).to_string(),
6244 /// "-0.6"
6245 /// );
6246 /// assert_eq!(
6247 /// (&Rational::exact_from(1.5) / &Float::from(-2.5)).to_string(),
6248 /// "-0.6"
6249 /// );
6250 /// assert_eq!(
6251 /// (&Rational::exact_from(-1.5) / &Float::from(-2.5)).to_string(),
6252 /// "0.6"
6253 /// );
6254 /// ```
6255 #[inline]
6256 fn div(self, other: &Float) -> Float {
6257 let prec = other.significant_bits();
6258 Float::rational_div_float_prec_round_ref_ref(self, other, prec, Nearest).0
6259 }
6260}