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::arithmetic::is_power_of_2::abs_is_power_of_2;
10use crate::InnerFloat::{Finite, Infinity, NaN, Zero};
11use crate::{float_either_infinity, float_either_zero, float_nan, Float};
12use core::cmp::max;
13use core::cmp::Ordering::{self, *};
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: ref mut x_sign,
2037 exponent: ref mut x_exp,
2038 precision: ref mut x_prec,
2039 significand: ref mut 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 {
2065 o
2066 } else {
2067 o.reverse()
2068 }
2069 }
2070 }
2071 }
2072
2073 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the specified precision
2074 /// and with the specified rounding mode. The [`Float`] on the right-hand side is taken by
2075 /// reference. An [`Ordering`] is returned, indicating whether the rounded quotient is less
2076 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
2077 /// any [`Float`], whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
2078 ///
2079 /// See [`RoundingMode`] for a description of the possible rounding modes.
2080 ///
2081 /// $$
2082 /// x \gets x/y+\varepsilon.
2083 /// $$
2084 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2085 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2086 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2087 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2088 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2089 ///
2090 /// If the output has a precision, it is `prec`.
2091 ///
2092 /// See the [`Float::div_prec_round`] documentation for information on special cases.
2093 ///
2094 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_assign_ref`]
2095 /// instead. If you know that your target precision is the maximum of the precisions of the two
2096 /// inputs, consider using [`Float::div_round_assign_ref`] instead. If both of these things are
2097 /// true, consider using `/=` instead.
2098 ///
2099 /// # Worst-case complexity
2100 /// $T(n) = O(n \log n \log\log n)$
2101 ///
2102 /// $M(n) = O(n \log n)$
2103 ///
2104 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2105 /// other.significant_bits(), `prec`)`.
2106 ///
2107 /// # Panics
2108 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2109 ///
2110 /// # Examples
2111 /// ```
2112 /// use core::f64::consts::{E, PI};
2113 /// use malachite_base::rounding_modes::RoundingMode::*;
2114 /// use malachite_float::Float;
2115 /// use std::cmp::Ordering::*;
2116 ///
2117 /// let mut quotient = Float::from(PI);
2118 /// assert_eq!(
2119 /// quotient.div_prec_round_assign_ref(&Float::from(E), 5, Floor),
2120 /// Less
2121 /// );
2122 /// assert_eq!(quotient.to_string(), "1.12");
2123 ///
2124 /// let mut quotient = Float::from(PI);
2125 /// assert_eq!(
2126 /// quotient.div_prec_round_assign_ref(&Float::from(E), 5, Ceiling),
2127 /// Greater
2128 /// );
2129 /// assert_eq!(quotient.to_string(), "1.19");
2130 ///
2131 /// let mut quotient = Float::from(PI);
2132 /// assert_eq!(
2133 /// quotient.div_prec_round_assign_ref(&Float::from(E), 5, Nearest),
2134 /// Less
2135 /// );
2136 /// assert_eq!(quotient.to_string(), "1.12");
2137 ///
2138 /// let mut quotient = Float::from(PI);
2139 /// assert_eq!(
2140 /// quotient.div_prec_round_assign_ref(&Float::from(E), 20, Floor),
2141 /// Less
2142 /// );
2143 /// assert_eq!(quotient.to_string(), "1.155725");
2144 ///
2145 /// let mut quotient = Float::from(PI);
2146 /// assert_eq!(
2147 /// quotient.div_prec_round_assign_ref(&Float::from(E), 20, Ceiling),
2148 /// Greater
2149 /// );
2150 /// assert_eq!(quotient.to_string(), "1.155727");
2151 ///
2152 /// let mut quotient = Float::from(PI);
2153 /// assert_eq!(
2154 /// quotient.div_prec_round_assign_ref(&Float::from(E), 20, Nearest),
2155 /// Greater
2156 /// );
2157 /// assert_eq!(quotient.to_string(), "1.155727");
2158 /// ```
2159 #[inline]
2160 pub fn div_prec_round_assign_ref(
2161 &mut self,
2162 other: &Float,
2163 prec: u64,
2164 rm: RoundingMode,
2165 ) -> Ordering {
2166 assert_ne!(prec, 0);
2167 match (&mut *self, other) {
2168 (float_nan!(), _)
2169 | (_, float_nan!())
2170 | (float_either_infinity!(), float_either_infinity!())
2171 | (float_either_zero!(), float_either_zero!()) => {
2172 *self = float_nan!();
2173 Equal
2174 }
2175 (
2176 Float(Infinity { sign: x_sign }),
2177 Float(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
2178 )
2179 | (Float(Finite { sign: x_sign, .. }), Float(Zero { sign: y_sign })) => {
2180 *self = Float(Infinity {
2181 sign: x_sign == y_sign,
2182 });
2183 Equal
2184 }
2185 (
2186 Float(Zero { sign: x_sign }),
2187 Float(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
2188 )
2189 | (Float(Finite { sign: x_sign, .. }), Float(Infinity { sign: y_sign })) => {
2190 *self = Float(Zero {
2191 sign: x_sign == y_sign,
2192 });
2193 Equal
2194 }
2195 (_, y) if abs_is_power_of_2(y) => {
2196 if *y < 0 {
2197 self.neg_assign();
2198 }
2199 let o = self.set_prec_round(prec, rm);
2200 *self >>= y.get_exponent().unwrap().checked_sub(1).unwrap();
2201 o
2202 }
2203 (
2204 Float(Finite {
2205 sign: ref mut x_sign,
2206 exponent: ref mut x_exp,
2207 precision: ref mut x_prec,
2208 significand: ref mut x,
2209 }),
2210 Float(Finite {
2211 sign: y_sign,
2212 exponent: y_exp,
2213 precision: y_prec,
2214 significand: y,
2215 }),
2216 ) => {
2217 let sign = x_sign == y_sign;
2218 let (exp_offset, o) = div_float_significands_in_place_ref(
2219 x,
2220 *x_prec,
2221 y,
2222 *y_prec,
2223 prec,
2224 if sign { rm } else { -rm },
2225 );
2226 *x_sign = sign;
2227 *x_exp = x_exp
2228 .checked_sub(*y_exp)
2229 .unwrap()
2230 .checked_add(i32::exact_from(exp_offset))
2231 .unwrap();
2232 *x_prec = prec;
2233 if sign {
2234 o
2235 } else {
2236 o.reverse()
2237 }
2238 }
2239 }
2240 }
2241
2242 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the nearest value of the
2243 /// specified precision. The [`Float`] on the right-hand side is taken by value. An [`Ordering`]
2244 /// is returned, indicating whether the rounded quotient is less than, equal to, or greater than
2245 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
2246 /// function sets the [`Float`] to `NaN` it also returns `Equal`.
2247 ///
2248 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
2249 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2250 /// description of the `Nearest` rounding mode.
2251 ///
2252 /// $$
2253 /// x \gets x/y+\varepsilon.
2254 /// $$
2255 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2256 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2257 ///
2258 /// If the output has a precision, it is `prec`.
2259 ///
2260 /// See the [`Float::div_prec`] documentation for information on special cases.
2261 ///
2262 /// If you want to use a rounding mode other than `Nearest`, consider using
2263 /// [`Float::div_prec_round_assign`] instead. If you know that your target precision is the
2264 /// maximum of the precisions of the two inputs, consider using `/=` instead.
2265 ///
2266 /// # Worst-case complexity
2267 /// $T(n) = O(n \log n \log\log n)$
2268 ///
2269 /// $M(n) = O(n \log n)$
2270 ///
2271 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2272 /// other.significant_bits(), `prec`)`.
2273 ///
2274 /// # Examples
2275 /// ```
2276 /// use core::f64::consts::{E, PI};
2277 /// use malachite_float::Float;
2278 /// use std::cmp::Ordering::*;
2279 ///
2280 /// let mut x = Float::from(PI);
2281 /// assert_eq!(x.div_prec_assign(Float::from(E), 5), Less);
2282 /// assert_eq!(x.to_string(), "1.12");
2283 ///
2284 /// let mut x = Float::from(PI);
2285 /// assert_eq!(x.div_prec_assign(Float::from(E), 20), Greater);
2286 /// assert_eq!(x.to_string(), "1.155727");
2287 /// ```
2288 #[inline]
2289 pub fn div_prec_assign(&mut self, other: Float, prec: u64) -> Ordering {
2290 self.div_prec_round_assign(other, prec, Nearest)
2291 }
2292
2293 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the nearest value of the
2294 /// specified precision. The [`Float`] on the right-hand side is taken by reference. An
2295 /// [`Ordering`] is returned, indicating whether the rounded quotient is less than, equal to, or
2296 /// greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
2297 /// whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
2298 ///
2299 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
2300 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2301 /// description of the `Nearest` rounding mode.
2302 ///
2303 /// $$
2304 /// x \gets x/y+\varepsilon.
2305 /// $$
2306 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2307 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2308 ///
2309 /// If the output has a precision, it is `prec`.
2310 ///
2311 /// See the [`Float::div_prec`] documentation for information on special cases.
2312 ///
2313 /// If you want to use a rounding mode other than `Nearest`, consider using
2314 /// [`Float::div_prec_round_assign_ref`] instead. If you know that your target precision is the
2315 /// maximum of the precisions of the two inputs, consider using `/=` instead.
2316 ///
2317 /// # Worst-case complexity
2318 /// $T(n) = O(n \log n \log\log n)$
2319 ///
2320 /// $M(n) = O(n \log n)$
2321 ///
2322 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2323 /// other.significant_bits(), `prec`)`.
2324 ///
2325 /// # Examples
2326 /// ```
2327 /// use core::f64::consts::{E, PI};
2328 /// use malachite_float::Float;
2329 /// use std::cmp::Ordering::*;
2330 ///
2331 /// let mut x = Float::from(PI);
2332 /// assert_eq!(x.div_prec_assign_ref(&Float::from(E), 5), Less);
2333 /// assert_eq!(x.to_string(), "1.12");
2334 ///
2335 /// let mut x = Float::from(PI);
2336 /// assert_eq!(x.div_prec_assign_ref(&Float::from(E), 20), Greater);
2337 /// assert_eq!(x.to_string(), "1.155727");
2338 /// ```
2339 #[inline]
2340 pub fn div_prec_assign_ref(&mut self, other: &Float, prec: u64) -> Ordering {
2341 self.div_prec_round_assign_ref(other, prec, Nearest)
2342 }
2343
2344 /// Divides a [`Float`] by a [`Float`] in place, rounding the result with the specified rounding
2345 /// mode. The [`Float`] on the right-hand side is taken by value. An [`Ordering`] is returned,
2346 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
2347 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
2348 /// the [`Float`] to `NaN` it also returns `Equal`.
2349 ///
2350 /// The precision of the output is the maximum of the precision of the inputs. See
2351 /// [`RoundingMode`] for a description of the possible rounding modes.
2352 ///
2353 /// $$
2354 /// x \gets x/y+\varepsilon.
2355 /// $$
2356 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2357 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2358 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2359 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2360 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2361 ///
2362 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2363 ///
2364 /// See the [`Float::div_round`] documentation for information on special cases.
2365 ///
2366 /// If you want to specify an output precision, consider using [`Float::div_prec_round_assign`]
2367 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/=`
2368 /// instead.
2369 ///
2370 /// # Worst-case complexity
2371 /// $T(n) = O(n \log n \log\log n)$
2372 ///
2373 /// $M(n) = O(n \log n)$
2374 ///
2375 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2376 /// other.significant_bits())`.
2377 ///
2378 /// # Panics
2379 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
2380 /// represent the output.
2381 ///
2382 /// # Examples
2383 /// ```
2384 /// use core::f64::consts::{E, PI};
2385 /// use malachite_base::rounding_modes::RoundingMode::*;
2386 /// use malachite_float::Float;
2387 /// use std::cmp::Ordering::*;
2388 ///
2389 /// let mut x = Float::from(PI);
2390 /// assert_eq!(x.div_round_assign(Float::from(E), Floor), Less);
2391 /// assert_eq!(x.to_string(), "1.1557273497909217");
2392 ///
2393 /// let mut x = Float::from(PI);
2394 /// assert_eq!(x.div_round_assign(Float::from(E), Ceiling), Greater);
2395 /// assert_eq!(x.to_string(), "1.155727349790922");
2396 ///
2397 /// let mut x = Float::from(PI);
2398 /// assert_eq!(x.div_round_assign(Float::from(E), Nearest), Less);
2399 /// assert_eq!(x.to_string(), "1.1557273497909217");
2400 /// ```
2401 #[inline]
2402 pub fn div_round_assign(&mut self, other: Float, rm: RoundingMode) -> Ordering {
2403 let prec = max(self.significant_bits(), other.significant_bits());
2404 self.div_prec_round_assign(other, prec, rm)
2405 }
2406
2407 /// Divides a [`Float`] by a [`Float`] in place, rounding the result with the specified rounding
2408 /// mode. The [`Float`] on the right-hand side is taken by reference. An [`Ordering`] is
2409 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
2410 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
2411 /// function sets the [`Float`] to `NaN` it also returns `Equal`.
2412 ///
2413 /// The precision of the output is the maximum of the precision of the inputs. See
2414 /// [`RoundingMode`] for a description of the possible rounding modes.
2415 ///
2416 /// $$
2417 /// x \gets x/y+\varepsilon.
2418 /// $$
2419 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2420 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2421 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2422 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2423 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2424 ///
2425 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2426 ///
2427 /// See the [`Float::div_round`] documentation for information on special cases.
2428 ///
2429 /// If you want to specify an output precision, consider using
2430 /// [`Float::div_prec_round_assign_ref`] instead. If you know you'll be using the `Nearest`
2431 /// rounding mode, consider using `/=` instead.
2432 ///
2433 /// # Worst-case complexity
2434 /// $T(n) = O(n \log n \log\log n)$
2435 ///
2436 /// $M(n) = O(n \log n)$
2437 ///
2438 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2439 /// other.significant_bits())`.
2440 ///
2441 /// # Panics
2442 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
2443 /// represent the output.
2444 ///
2445 /// # Examples
2446 /// ```
2447 /// use core::f64::consts::{E, PI};
2448 /// use malachite_base::rounding_modes::RoundingMode::*;
2449 /// use malachite_float::Float;
2450 /// use std::cmp::Ordering::*;
2451 ///
2452 /// let mut x = Float::from(PI);
2453 /// assert_eq!(x.div_round_assign_ref(&Float::from(E), Floor), Less);
2454 /// assert_eq!(x.to_string(), "1.1557273497909217");
2455 ///
2456 /// let mut x = Float::from(PI);
2457 /// assert_eq!(x.div_round_assign_ref(&Float::from(E), Ceiling), Greater);
2458 /// assert_eq!(x.to_string(), "1.155727349790922");
2459 ///
2460 /// let mut x = Float::from(PI);
2461 /// assert_eq!(x.div_round_assign_ref(&Float::from(E), Nearest), Less);
2462 /// assert_eq!(x.to_string(), "1.1557273497909217");
2463 /// ```
2464 #[inline]
2465 pub fn div_round_assign_ref(&mut self, other: &Float, rm: RoundingMode) -> Ordering {
2466 let prec = max(self.significant_bits(), other.significant_bits());
2467 self.div_prec_round_assign_ref(other, prec, rm)
2468 }
2469
2470 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
2471 /// with the specified rounding mode. The [`Float`] and the [`Rational`] are both taken by
2472 /// value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
2473 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
2474 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2475 ///
2476 /// See [`RoundingMode`] for a description of the possible rounding modes.
2477 ///
2478 /// $$
2479 /// f(x,y,p,m) = x/y+\varepsilon.
2480 /// $$
2481 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2482 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2483 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2484 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2485 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2486 ///
2487 /// If the output has a precision, it is `prec`.
2488 ///
2489 /// Special cases:
2490 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
2491 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
2492 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
2493 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
2494 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
2495 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
2496 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
2497 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
2498 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
2499 ///
2500 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec`] instead.
2501 /// If you know that your target precision is the precision of the [`Float`] input, consider
2502 /// using [`Float::div_rational_round`] instead. If both of these things are true, consider
2503 /// using `/` instead.
2504 ///
2505 /// # Worst-case complexity
2506 /// $T(n) = O(n \log n \log\log n)$
2507 ///
2508 /// $M(n) = O(n \log n)$
2509 ///
2510 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2511 /// other.significant_bits(), prec)`.
2512 ///
2513 /// # Panics
2514 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2515 ///
2516 /// # Examples
2517 /// ```
2518 /// use core::f64::consts::PI;
2519 /// use malachite_base::rounding_modes::RoundingMode::*;
2520 /// use malachite_float::Float;
2521 /// use malachite_q::Rational;
2522 /// use std::cmp::Ordering::*;
2523 ///
2524 /// let (quotient, o) =
2525 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 5, Floor);
2526 /// assert_eq!(quotient.to_string(), "9.0");
2527 /// assert_eq!(o, Less);
2528 ///
2529 /// let (quotient, o) =
2530 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 5, Ceiling);
2531 /// assert_eq!(quotient.to_string(), "9.5");
2532 /// assert_eq!(o, Greater);
2533 ///
2534 /// let (quotient, o) =
2535 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 5, Nearest);
2536 /// assert_eq!(quotient.to_string(), "9.5");
2537 /// assert_eq!(o, Greater);
2538 ///
2539 /// let (quotient, o) =
2540 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 20, Floor);
2541 /// assert_eq!(quotient.to_string(), "9.42477");
2542 /// assert_eq!(o, Less);
2543 ///
2544 /// let (quotient, o) =
2545 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 20, Ceiling);
2546 /// assert_eq!(quotient.to_string(), "9.42479");
2547 /// assert_eq!(o, Greater);
2548 ///
2549 /// let (quotient, o) =
2550 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 20, Nearest);
2551 /// assert_eq!(quotient.to_string(), "9.42477");
2552 /// assert_eq!(o, Less);
2553 /// ```
2554 #[inline]
2555 pub fn div_rational_prec_round(
2556 mut self,
2557 other: Rational,
2558 prec: u64,
2559 rm: RoundingMode,
2560 ) -> (Float, Ordering) {
2561 let o = self.div_rational_prec_round_assign(other, prec, rm);
2562 (self, o)
2563 }
2564
2565 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
2566 /// with the specified rounding mode. The [`Float`] is taken by value and the [`Rational`] by
2567 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
2568 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
2569 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2570 ///
2571 /// See [`RoundingMode`] for a description of the possible rounding modes.
2572 ///
2573 /// $$
2574 /// f(x,y,p,m) = x/y+\varepsilon.
2575 /// $$
2576 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2577 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2578 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2579 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2580 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2581 ///
2582 /// If the output has a precision, it is `prec`.
2583 ///
2584 /// Special cases:
2585 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
2586 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
2587 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
2588 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
2589 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
2590 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
2591 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
2592 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
2593 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
2594 ///
2595 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_val_ref`]
2596 /// instead. If you know that your target precision is the precision of the [`Float`] input,
2597 /// consider using [`Float::div_rational_round_val_ref`] instead. If both of these things are
2598 /// true, consider using `/` instead.
2599 ///
2600 /// # Worst-case complexity
2601 /// $T(n) = O(n \log n \log\log n)$
2602 ///
2603 /// $M(n) = O(n \log n)$
2604 ///
2605 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2606 /// other.significant_bits(), prec)`.
2607 ///
2608 /// # Panics
2609 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2610 ///
2611 /// # Examples
2612 /// ```
2613 /// use core::f64::consts::PI;
2614 /// use malachite_base::rounding_modes::RoundingMode::*;
2615 /// use malachite_float::Float;
2616 /// use malachite_q::Rational;
2617 /// use std::cmp::Ordering::*;
2618 ///
2619 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
2620 /// &Rational::from_unsigneds(1u8, 3),
2621 /// 5,
2622 /// Floor,
2623 /// );
2624 /// assert_eq!(quotient.to_string(), "9.0");
2625 /// assert_eq!(o, Less);
2626 ///
2627 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
2628 /// &Rational::from_unsigneds(1u8, 3),
2629 /// 5,
2630 /// Ceiling,
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 /// 5,
2638 /// Nearest,
2639 /// );
2640 /// assert_eq!(quotient.to_string(), "9.5");
2641 /// assert_eq!(o, Greater);
2642 ///
2643 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
2644 /// &Rational::from_unsigneds(1u8, 3),
2645 /// 20,
2646 /// Floor,
2647 /// );
2648 /// assert_eq!(quotient.to_string(), "9.42477");
2649 /// assert_eq!(o, Less);
2650 ///
2651 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
2652 /// &Rational::from_unsigneds(1u8, 3),
2653 /// 20,
2654 /// Ceiling,
2655 /// );
2656 /// assert_eq!(quotient.to_string(), "9.42479");
2657 /// assert_eq!(o, Greater);
2658 ///
2659 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
2660 /// &Rational::from_unsigneds(1u8, 3),
2661 /// 20,
2662 /// Nearest,
2663 /// );
2664 /// assert_eq!(quotient.to_string(), "9.42477");
2665 /// assert_eq!(o, Less);
2666 /// ```
2667 #[inline]
2668 pub fn div_rational_prec_round_val_ref(
2669 mut self,
2670 other: &Rational,
2671 prec: u64,
2672 rm: RoundingMode,
2673 ) -> (Float, Ordering) {
2674 let o = self.div_rational_prec_round_assign_ref(other, prec, rm);
2675 (self, o)
2676 }
2677
2678 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
2679 /// with the specified rounding mode. The [`Float`] is taken by reference and the [`Rational`]
2680 /// by value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
2681 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
2682 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2683 ///
2684 /// See [`RoundingMode`] for a description of the possible rounding modes.
2685 ///
2686 /// $$
2687 /// f(x,y,p,m) = x/y+\varepsilon.
2688 /// $$
2689 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2690 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2691 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2692 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2693 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2694 ///
2695 /// If the output has a precision, it is `prec`.
2696 ///
2697 /// Special cases:
2698 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
2699 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
2700 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
2701 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
2702 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
2703 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
2704 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
2705 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
2706 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
2707 ///
2708 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_ref_val`]
2709 /// instead. If you know that your target precision is the precision of the [`Float`] input,
2710 /// consider using [`Float::div_rational_round_ref_val`] instead. If both of these things are
2711 /// true, consider using `/` instead.
2712 ///
2713 /// # Worst-case complexity
2714 /// $T(n) = O(n \log n \log\log n)$
2715 ///
2716 /// $M(n) = O(n \log n)$
2717 ///
2718 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2719 /// other.significant_bits(), prec)`.
2720 ///
2721 /// # Panics
2722 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2723 ///
2724 /// # Examples
2725 /// ```
2726 /// use core::f64::consts::PI;
2727 /// use malachite_base::rounding_modes::RoundingMode::*;
2728 /// use malachite_float::Float;
2729 /// use malachite_q::Rational;
2730 /// use std::cmp::Ordering::*;
2731 ///
2732 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
2733 /// Rational::from_unsigneds(1u8, 3),
2734 /// 5,
2735 /// Floor,
2736 /// );
2737 /// assert_eq!(quotient.to_string(), "9.0");
2738 /// assert_eq!(o, Less);
2739 ///
2740 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
2741 /// Rational::from_unsigneds(1u8, 3),
2742 /// 5,
2743 /// Ceiling,
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 /// 5,
2751 /// Nearest,
2752 /// );
2753 /// assert_eq!(quotient.to_string(), "9.5");
2754 /// assert_eq!(o, Greater);
2755 ///
2756 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
2757 /// Rational::from_unsigneds(1u8, 3),
2758 /// 20,
2759 /// Floor,
2760 /// );
2761 /// assert_eq!(quotient.to_string(), "9.42477");
2762 /// assert_eq!(o, Less);
2763 ///
2764 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
2765 /// Rational::from_unsigneds(1u8, 3),
2766 /// 20,
2767 /// Ceiling,
2768 /// );
2769 /// assert_eq!(quotient.to_string(), "9.42479");
2770 /// assert_eq!(o, Greater);
2771 ///
2772 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
2773 /// Rational::from_unsigneds(1u8, 3),
2774 /// 20,
2775 /// Nearest,
2776 /// );
2777 /// assert_eq!(quotient.to_string(), "9.42477");
2778 /// assert_eq!(o, Less);
2779 /// ```
2780 #[inline]
2781 pub fn div_rational_prec_round_ref_val(
2782 &self,
2783 other: Rational,
2784 prec: u64,
2785 rm: RoundingMode,
2786 ) -> (Float, Ordering) {
2787 if max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD {
2788 div_rational_prec_round_naive_ref_val(self, other, prec, rm)
2789 } else {
2790 div_rational_prec_round_direct_ref_val(self, other, prec, rm)
2791 }
2792 }
2793
2794 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
2795 /// with the specified rounding mode. The [`Float`] and the [`Rational`] are both taken by
2796 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
2797 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
2798 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2799 ///
2800 /// See [`RoundingMode`] for a description of the possible rounding modes.
2801 ///
2802 /// $$
2803 /// f(x,y,p,m) = x/y+\varepsilon.
2804 /// $$
2805 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2806 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2807 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2808 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2809 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2810 ///
2811 /// If the output has a precision, it is `prec`.
2812 ///
2813 /// Special cases:
2814 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
2815 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
2816 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
2817 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
2818 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
2819 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
2820 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
2821 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
2822 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
2823 ///
2824 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_ref_ref`]
2825 /// instead. If you know that your target precision is the precision of the [`Float`] input,
2826 /// consider using [`Float::div_rational_round_ref_ref`] instead. If both of these things are
2827 /// true, consider using `/` instead.
2828 ///
2829 /// # Worst-case complexity
2830 /// $T(n) = O(n \log n \log\log n)$
2831 ///
2832 /// $M(n) = O(n \log n)$
2833 ///
2834 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2835 /// other.significant_bits(), prec)`.
2836 ///
2837 /// # Panics
2838 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2839 ///
2840 /// # Examples
2841 /// ```
2842 /// use core::f64::consts::PI;
2843 /// use malachite_base::rounding_modes::RoundingMode::*;
2844 /// use malachite_float::Float;
2845 /// use malachite_q::Rational;
2846 /// use std::cmp::Ordering::*;
2847 ///
2848 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
2849 /// &Rational::from_unsigneds(1u8, 3),
2850 /// 5,
2851 /// Floor,
2852 /// );
2853 /// assert_eq!(quotient.to_string(), "9.0");
2854 /// assert_eq!(o, Less);
2855 ///
2856 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
2857 /// &Rational::from_unsigneds(1u8, 3),
2858 /// 5,
2859 /// Ceiling,
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 /// 5,
2867 /// Nearest,
2868 /// );
2869 /// assert_eq!(quotient.to_string(), "9.5");
2870 /// assert_eq!(o, Greater);
2871 ///
2872 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
2873 /// &Rational::from_unsigneds(1u8, 3),
2874 /// 20,
2875 /// Floor,
2876 /// );
2877 /// assert_eq!(quotient.to_string(), "9.42477");
2878 /// assert_eq!(o, Less);
2879 ///
2880 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
2881 /// &Rational::from_unsigneds(1u8, 3),
2882 /// 20,
2883 /// Ceiling,
2884 /// );
2885 /// assert_eq!(quotient.to_string(), "9.42479");
2886 /// assert_eq!(o, Greater);
2887 ///
2888 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
2889 /// &Rational::from_unsigneds(1u8, 3),
2890 /// 20,
2891 /// Nearest,
2892 /// );
2893 /// assert_eq!(quotient.to_string(), "9.42477");
2894 /// assert_eq!(o, Less);
2895 /// ```
2896 #[inline]
2897 pub fn div_rational_prec_round_ref_ref(
2898 &self,
2899 other: &Rational,
2900 prec: u64,
2901 rm: RoundingMode,
2902 ) -> (Float, Ordering) {
2903 if max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD {
2904 div_rational_prec_round_naive_ref_ref(self, other, prec, rm)
2905 } else {
2906 div_rational_prec_round_direct_ref_ref(self, other, prec, rm)
2907 }
2908 }
2909
2910 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
2911 /// specified precision. The [`Float`] and the [`Rational`] are both are taken by value. An
2912 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
2913 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
2914 /// whenever this function returns a `NaN` it also returns `Equal`.
2915 ///
2916 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
2917 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2918 /// description of the `Nearest` rounding mode.
2919 ///
2920 /// $$
2921 /// f(x,y,p) = x/y+\varepsilon.
2922 /// $$
2923 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2924 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2925 ///
2926 /// If the output has a precision, it is `prec`.
2927 ///
2928 /// Special cases:
2929 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
2930 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
2931 /// - $f(\infty,x,p)=-\infty$ if $x<0$
2932 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
2933 /// - $f(-\infty,x,p)=\infty$ if $x<0$
2934 /// - $f(0.0,x,p)=0.0$ if $x>0$
2935 /// - $f(0.0,x,p)=-0.0$ if $x<0$
2936 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
2937 /// - $f(-0.0,x,p)=0.0$ if $x<0$
2938 ///
2939 /// If you want to use a rounding mode other than `Nearest`, consider using
2940 /// [`Float::div_rational_prec_round`] instead. If you know that your target precision is the
2941 /// precision of the [`Float`] input, consider using `/` instead.
2942 ///
2943 /// # Worst-case complexity
2944 /// $T(n) = O(n \log n \log\log n)$
2945 ///
2946 /// $M(n) = O(n \log n)$
2947 ///
2948 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2949 /// other.significant_bits(), prec)`.
2950 ///
2951 /// # Examples
2952 /// ```
2953 /// use core::f64::consts::PI;
2954 /// use malachite_base::num::conversion::traits::ExactFrom;
2955 /// use malachite_float::Float;
2956 /// use malachite_q::Rational;
2957 /// use std::cmp::Ordering::*;
2958 ///
2959 /// let (quotient, o) = Float::from(PI).div_rational_prec(Rational::exact_from(1.5), 5);
2960 /// assert_eq!(quotient.to_string(), "2.1");
2961 /// assert_eq!(o, Greater);
2962 ///
2963 /// let (quotient, o) = Float::from(PI).div_rational_prec(Rational::exact_from(1.5), 20);
2964 /// assert_eq!(quotient.to_string(), "2.094395");
2965 /// assert_eq!(o, Less);
2966 /// ```
2967 #[inline]
2968 pub fn div_rational_prec(self, other: Rational, prec: u64) -> (Float, Ordering) {
2969 self.div_rational_prec_round(other, prec, Nearest)
2970 }
2971
2972 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
2973 /// specified precision. The [`Float`] is taken by value and the [`Rational`] by reference. An
2974 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
2975 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
2976 /// whenever this function returns a `NaN` it also returns `Equal`.
2977 ///
2978 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
2979 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2980 /// description of the `Nearest` rounding mode.
2981 ///
2982 /// $$
2983 /// f(x,y,p) = x/y+\varepsilon.
2984 /// $$
2985 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2986 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2987 ///
2988 /// If the output has a precision, it is `prec`.
2989 ///
2990 /// Special cases:
2991 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
2992 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
2993 /// - $f(\infty,x,p)=-\infty$ if $x<0$
2994 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
2995 /// - $f(-\infty,x,p)=\infty$ if $x<0$
2996 /// - $f(0.0,x,p)=0.0$ if $x>0$
2997 /// - $f(0.0,x,p)=-0.0$ if $x<0$
2998 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
2999 /// - $f(-0.0,x,p)=0.0$ if $x<0$
3000 ///
3001 /// If you want to use a rounding mode other than `Nearest`, consider using
3002 /// [`Float::div_rational_prec_round_val_ref`] instead. If you know that your target precision
3003 /// is the precision of the [`Float`] input, consider using `/` instead.
3004 ///
3005 /// # Worst-case complexity
3006 /// $T(n) = O(n \log n \log\log n)$
3007 ///
3008 /// $M(n) = O(n \log n)$
3009 ///
3010 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3011 /// other.significant_bits(), prec)`.
3012 ///
3013 /// # Examples
3014 /// ```
3015 /// use core::f64::consts::PI;
3016 /// use malachite_base::num::conversion::traits::ExactFrom;
3017 /// use malachite_float::Float;
3018 /// use malachite_q::Rational;
3019 /// use std::cmp::Ordering::*;
3020 ///
3021 /// let (quotient, o) =
3022 /// Float::from(PI).div_rational_prec_val_ref(&Rational::exact_from(1.5), 5);
3023 /// assert_eq!(quotient.to_string(), "2.1");
3024 /// assert_eq!(o, Greater);
3025 ///
3026 /// let (quotient, o) =
3027 /// Float::from(PI).div_rational_prec_val_ref(&Rational::exact_from(1.5), 20);
3028 /// assert_eq!(quotient.to_string(), "2.094395");
3029 /// assert_eq!(o, Less);
3030 /// ```
3031 #[inline]
3032 pub fn div_rational_prec_val_ref(self, other: &Rational, prec: u64) -> (Float, Ordering) {
3033 self.div_rational_prec_round_val_ref(other, prec, Nearest)
3034 }
3035
3036 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3037 /// specified precision. The [`Float`] is taken by reference and the [`Rational`] by value. An
3038 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3039 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3040 /// whenever this function returns a `NaN` it also returns `Equal`.
3041 ///
3042 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3043 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3044 /// description of the `Nearest` rounding mode.
3045 ///
3046 /// $$
3047 /// f(x,y,p) = x/y+\varepsilon.
3048 /// $$
3049 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3050 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3051 ///
3052 /// If the output has a precision, it is `prec`.
3053 ///
3054 /// Special cases:
3055 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
3056 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
3057 /// - $f(\infty,x,p)=-\infty$ if $x<0$
3058 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
3059 /// - $f(-\infty,x,p)=\infty$ if $x<0$
3060 /// - $f(0.0,x,p)=0.0$ if $x>0$
3061 /// - $f(0.0,x,p)=-0.0$ if $x<0$
3062 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
3063 /// - $f(-0.0,x,p)=0.0$ if $x<0$
3064 ///
3065 /// If you want to use a rounding mode other than `Nearest`, consider using
3066 /// [`Float::div_rational_prec_round_ref_val`] instead. If you know that your target precision
3067 /// is the precision of the [`Float`] input, consider using `/` instead.
3068 ///
3069 /// # Worst-case complexity
3070 /// $T(n) = O(n \log n \log\log n)$
3071 ///
3072 /// $M(n) = O(n \log n)$
3073 ///
3074 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3075 /// other.significant_bits(), prec)`.
3076 ///
3077 /// # Examples
3078 /// ```
3079 /// use core::f64::consts::PI;
3080 /// use malachite_base::num::conversion::traits::ExactFrom;
3081 /// use malachite_float::Float;
3082 /// use malachite_q::Rational;
3083 /// use std::cmp::Ordering::*;
3084 ///
3085 /// let (quotient, o) = Float::from(PI).div_rational_prec_ref_val(Rational::exact_from(1.5), 5);
3086 /// assert_eq!(quotient.to_string(), "2.1");
3087 /// assert_eq!(o, Greater);
3088 ///
3089 /// let (quotient, o) =
3090 /// Float::from(PI).div_rational_prec_ref_val(Rational::exact_from(1.5), 20);
3091 /// assert_eq!(quotient.to_string(), "2.094395");
3092 /// assert_eq!(o, Less);
3093 /// ```
3094 #[inline]
3095 pub fn div_rational_prec_ref_val(&self, other: Rational, prec: u64) -> (Float, Ordering) {
3096 self.div_rational_prec_round_ref_val(other, prec, Nearest)
3097 }
3098
3099 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3100 /// specified precision. The [`Float`] and the [`Rational`] are both are taken by reference. An
3101 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3102 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3103 /// whenever this function returns a `NaN` it also returns `Equal`.
3104 ///
3105 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3106 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3107 /// description of the `Nearest` rounding mode.
3108 ///
3109 /// $$
3110 /// f(x,y,p) = x/y+\varepsilon.
3111 /// $$
3112 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3113 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3114 ///
3115 /// If the output has a precision, it is `prec`.
3116 ///
3117 /// Special cases:
3118 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
3119 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
3120 /// - $f(\infty,x,p)=-\infty$ if $x<0$
3121 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
3122 /// - $f(-\infty,x,p)=\infty$ if $x<0$
3123 /// - $f(0.0,x,p)=0.0$ if $x>0$
3124 /// - $f(0.0,x,p)=-0.0$ if $x<0$
3125 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
3126 /// - $f(-0.0,x,p)=0.0$ if $x<0$
3127 ///
3128 /// If you want to use a rounding mode other than `Nearest`, consider using
3129 /// [`Float::div_rational_prec_round_ref_ref`] instead. If you know that your target precision
3130 /// is the precision of the [`Float`] input, consider using `/` instead.
3131 ///
3132 /// # Worst-case complexity
3133 /// $T(n) = O(n \log n \log\log n)$
3134 ///
3135 /// $M(n) = O(n \log n)$
3136 ///
3137 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3138 /// other.significant_bits(), prec)`.
3139 ///
3140 /// # Examples
3141 /// ```
3142 /// use core::f64::consts::PI;
3143 /// use malachite_base::num::conversion::traits::ExactFrom;
3144 /// use malachite_float::Float;
3145 /// use malachite_q::Rational;
3146 /// use std::cmp::Ordering::*;
3147 ///
3148 /// let (quotient, o) =
3149 /// Float::from(PI).div_rational_prec_ref_ref(&Rational::exact_from(1.5), 5);
3150 /// assert_eq!(quotient.to_string(), "2.1");
3151 /// assert_eq!(o, Greater);
3152 ///
3153 /// let (quotient, o) =
3154 /// Float::from(PI).div_rational_prec_ref_ref(&Rational::exact_from(1.5), 20);
3155 /// assert_eq!(quotient.to_string(), "2.094395");
3156 /// assert_eq!(o, Less);
3157 /// ```
3158 #[inline]
3159 pub fn div_rational_prec_ref_ref(&self, other: &Rational, prec: u64) -> (Float, Ordering) {
3160 self.div_rational_prec_round_ref_ref(other, prec, Nearest)
3161 }
3162
3163 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
3164 /// The [`Float`] and the [`Rational`] are both are taken by value. An [`Ordering`] is also
3165 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3166 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3167 /// function returns a `NaN` it also returns `Equal`.
3168 ///
3169 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
3170 /// for a description of the possible rounding modes.
3171 ///
3172 /// $$
3173 /// f(x,y,m) = x/y+\varepsilon.
3174 /// $$
3175 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3176 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3177 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3178 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3179 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3180 ///
3181 /// If the output has a precision, it is the precision of the [`Float`] input.
3182 ///
3183 /// Special cases:
3184 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
3185 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
3186 /// - $f(\infty,x,m)=-\infty$ if $x<0$
3187 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
3188 /// - $f(-\infty,x,m)=\infty$ if $x<0$
3189 /// - $f(0.0,x,m)=0.0$ if $x>0$
3190 /// - $f(0.0,x,m)=-0.0$ if $x<0$
3191 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
3192 /// - $f(-0.0,x,m)=0.0$ if $x<0$
3193 ///
3194 /// If you want to specify an output precision, consider using
3195 /// [`Float::div_rational_prec_round`] instead. If you know you'll be using the `Nearest`
3196 /// rounding mode, consider using `/` instead.
3197 ///
3198 /// # Worst-case complexity
3199 /// $T(n) = O(n \log n \log\log n)$
3200 ///
3201 /// $M(n) = O(n \log n)$
3202 ///
3203 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3204 /// other.significant_bits())`.
3205 ///
3206 /// # Panics
3207 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
3208 /// represent the output.
3209 ///
3210 /// # Examples
3211 /// ```
3212 /// use core::f64::consts::PI;
3213 /// use malachite_base::rounding_modes::RoundingMode::*;
3214 /// use malachite_float::Float;
3215 /// use malachite_q::Rational;
3216 /// use std::cmp::Ordering::*;
3217 ///
3218 /// let (quotient, o) =
3219 /// Float::from(PI).div_rational_round(Rational::from_unsigneds(1u8, 3), Floor);
3220 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3221 /// assert_eq!(o, Less);
3222 ///
3223 /// let (quotient, o) =
3224 /// Float::from(PI).div_rational_round(Rational::from_unsigneds(1u8, 3), Ceiling);
3225 /// assert_eq!(quotient.to_string(), "9.42477796076939");
3226 /// assert_eq!(o, Greater);
3227 ///
3228 /// let (quotient, o) =
3229 /// Float::from(PI).div_rational_round(Rational::from_unsigneds(1u8, 3), Nearest);
3230 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3231 /// assert_eq!(o, Less);
3232 /// ```
3233 #[inline]
3234 pub fn div_rational_round(self, other: Rational, rm: RoundingMode) -> (Float, Ordering) {
3235 let prec = self.significant_bits();
3236 self.div_rational_prec_round(other, prec, rm)
3237 }
3238
3239 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
3240 /// The [`Float`] is taken by value and the [`Rational`] by reference. An [`Ordering`] is also
3241 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3242 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3243 /// function returns a `NaN` it also returns `Equal`.
3244 ///
3245 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
3246 /// for a description of the possible rounding modes.
3247 ///
3248 /// $$
3249 /// f(x,y,m) = x/y+\varepsilon.
3250 /// $$
3251 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3252 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3253 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3254 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3255 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3256 ///
3257 /// If the output has a precision, it is the precision of the [`Float`] input.
3258 ///
3259 /// Special cases:
3260 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
3261 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
3262 /// - $f(\infty,x,m)=-\infty$ if $x<0$
3263 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
3264 /// - $f(-\infty,x,m)=\infty$ if $x<0$
3265 /// - $f(0.0,x,m)=0.0$ if $x>0$
3266 /// - $f(0.0,x,m)=-0.0$ if $x<0$
3267 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
3268 /// - $f(-0.0,x,m)=0.0$ if $x<0$
3269 ///
3270 /// If you want to specify an output precision, consider using
3271 /// [`Float::div_rational_prec_round_val_ref`] instead. If you know you'll be using the
3272 /// `Nearest` rounding mode, consider using `/` instead.
3273 ///
3274 /// # Worst-case complexity
3275 /// $T(n) = O(n \log n \log\log n)$
3276 ///
3277 /// $M(n) = O(n \log n)$
3278 ///
3279 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3280 /// other.significant_bits())`.
3281 ///
3282 /// # Panics
3283 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
3284 /// represent the output.
3285 ///
3286 /// # Examples
3287 /// ```
3288 /// use core::f64::consts::PI;
3289 /// use malachite_base::rounding_modes::RoundingMode::*;
3290 /// use malachite_float::Float;
3291 /// use malachite_q::Rational;
3292 /// use std::cmp::Ordering::*;
3293 ///
3294 /// let (quotient, o) =
3295 /// Float::from(PI).div_rational_round_val_ref(&Rational::from_unsigneds(1u8, 3), Floor);
3296 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3297 /// assert_eq!(o, Less);
3298 ///
3299 /// let (quotient, o) =
3300 /// Float::from(PI).div_rational_round_val_ref(&Rational::from_unsigneds(1u8, 3), Ceiling);
3301 /// assert_eq!(quotient.to_string(), "9.42477796076939");
3302 /// assert_eq!(o, Greater);
3303 ///
3304 /// let (quotient, o) =
3305 /// Float::from(PI).div_rational_round_val_ref(&Rational::from_unsigneds(1u8, 3), Nearest);
3306 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3307 /// assert_eq!(o, Less);
3308 /// ```
3309 #[inline]
3310 pub fn div_rational_round_val_ref(
3311 self,
3312 other: &Rational,
3313 rm: RoundingMode,
3314 ) -> (Float, Ordering) {
3315 let prec = self.significant_bits();
3316 self.div_rational_prec_round_val_ref(other, prec, rm)
3317 }
3318
3319 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
3320 /// The [`Float`] is taken by reference and the [`Rational`] by value. An [`Ordering`] is also
3321 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3322 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3323 /// function returns a `NaN` it also returns `Equal`.
3324 ///
3325 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
3326 /// for a description of the possible rounding modes.
3327 ///
3328 /// $$
3329 /// f(x,y,m) = x/y+\varepsilon.
3330 /// $$
3331 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3332 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3333 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3334 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3335 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3336 ///
3337 /// If the output has a precision, it is the precision of the [`Float`] input.
3338 ///
3339 /// Special cases:
3340 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
3341 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
3342 /// - $f(\infty,x,m)=-\infty$ if $x<0$
3343 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
3344 /// - $f(-\infty,x,m)=\infty$ if $x<0$
3345 /// - $f(0.0,x,m)=0.0$ if $x>0$
3346 /// - $f(0.0,x,m)=-0.0$ if $x<0$
3347 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
3348 /// - $f(-0.0,x,m)=0.0$ if $x<0$
3349 ///
3350 /// If you want to specify an output precision, consider using
3351 /// [`Float::div_rational_prec_round_ref_val`] instead. If you know you'll be using the
3352 /// `Nearest` rounding mode, consider using `/` instead.
3353 ///
3354 /// # Worst-case complexity
3355 /// $T(n) = O(n \log n \log\log n)$
3356 ///
3357 /// $M(n) = O(n \log n)$
3358 ///
3359 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3360 /// other.significant_bits())`.
3361 ///
3362 /// # Panics
3363 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
3364 /// represent the output.
3365 ///
3366 /// # Examples
3367 /// ```
3368 /// use core::f64::consts::PI;
3369 /// use malachite_base::rounding_modes::RoundingMode::*;
3370 /// use malachite_float::Float;
3371 /// use malachite_q::Rational;
3372 /// use std::cmp::Ordering::*;
3373 ///
3374 /// let (quotient, o) =
3375 /// Float::from(PI).div_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Floor);
3376 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3377 /// assert_eq!(o, Less);
3378 ///
3379 /// let (quotient, o) =
3380 /// Float::from(PI).div_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Ceiling);
3381 /// assert_eq!(quotient.to_string(), "9.42477796076939");
3382 /// assert_eq!(o, Greater);
3383 ///
3384 /// let (quotient, o) =
3385 /// Float::from(PI).div_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Nearest);
3386 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3387 /// assert_eq!(o, Less);
3388 /// ```
3389 #[inline]
3390 pub fn div_rational_round_ref_val(
3391 &self,
3392 other: Rational,
3393 rm: RoundingMode,
3394 ) -> (Float, Ordering) {
3395 let prec = self.significant_bits();
3396 self.div_rational_prec_round_ref_val(other, prec, rm)
3397 }
3398
3399 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
3400 /// The [`Float`] and the [`Rational`] are both are taken by reference. An [`Ordering`] is also
3401 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3402 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3403 /// function returns a `NaN` it also returns `Equal`.
3404 ///
3405 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
3406 /// for a description of the possible rounding modes.
3407 ///
3408 /// $$
3409 /// f(x,y,m) = x/y+\varepsilon.
3410 /// $$
3411 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3412 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3413 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3414 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3415 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3416 ///
3417 /// If the output has a precision, it is the precision of the [`Float`] input.
3418 ///
3419 /// Special cases:
3420 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
3421 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
3422 /// - $f(\infty,x,m)=-\infty$ if $x<0$
3423 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
3424 /// - $f(-\infty,x,m)=\infty$ if $x<0$
3425 /// - $f(0.0,x,m)=0.0$ if $x>0$
3426 /// - $f(0.0,x,m)=-0.0$ if $x<0$
3427 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
3428 /// - $f(-0.0,x,m)=0.0$ if $x<0$
3429 ///
3430 /// If you want to specify an output precision, consider using
3431 /// [`Float::div_rational_prec_round_ref_ref`] instead. If you know you'll be using the
3432 /// `Nearest` rounding mode, consider using `/` instead.
3433 ///
3434 /// # Worst-case complexity
3435 /// $T(n) = O(n \log n \log\log n)$
3436 ///
3437 /// $M(n) = O(n \log n)$
3438 ///
3439 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3440 /// other.significant_bits())`.
3441 ///
3442 /// # Panics
3443 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
3444 /// represent the output.
3445 ///
3446 /// # Examples
3447 /// ```
3448 /// use core::f64::consts::PI;
3449 /// use malachite_base::rounding_modes::RoundingMode::*;
3450 /// use malachite_float::Float;
3451 /// use malachite_q::Rational;
3452 /// use std::cmp::Ordering::*;
3453 ///
3454 /// let (quotient, o) =
3455 /// Float::from(PI).div_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Floor);
3456 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3457 /// assert_eq!(o, Less);
3458 ///
3459 /// let (quotient, o) =
3460 /// Float::from(PI).div_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Ceiling);
3461 /// assert_eq!(quotient.to_string(), "9.42477796076939");
3462 /// assert_eq!(o, Greater);
3463 ///
3464 /// let (quotient, o) =
3465 /// Float::from(PI).div_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Nearest);
3466 /// assert_eq!(quotient.to_string(), "9.42477796076938");
3467 /// assert_eq!(o, Less);
3468 /// ```
3469 #[inline]
3470 pub fn div_rational_round_ref_ref(
3471 &self,
3472 other: &Rational,
3473 rm: RoundingMode,
3474 ) -> (Float, Ordering) {
3475 let prec = self.significant_bits();
3476 self.div_rational_prec_round_ref_ref(other, prec, rm)
3477 }
3478
3479 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the specified
3480 /// precision and with the specified rounding mode. The [`Rational`] is taken by value. An
3481 /// [`Ordering`] is returned, indicating whether the rounded quotient is less than, equal to, or
3482 /// greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3483 /// whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
3484 ///
3485 /// See [`RoundingMode`] for a description of the possible rounding modes.
3486 ///
3487 /// $$
3488 /// x \gets x/y+\varepsilon.
3489 /// $$
3490 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3491 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3492 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3493 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3494 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3495 ///
3496 /// If the output has a precision, it is `prec`.
3497 ///
3498 /// See the [`Float::div_rational_prec_round`] documentation for information on special cases.
3499 ///
3500 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_assign`]
3501 /// instead. If you know that your target precision is the precision of the [`Float`] input,
3502 /// consider using [`Float::div_rational_round_assign`] instead. If both of these things are
3503 /// true, consider using `/=` instead.
3504 ///
3505 /// # Worst-case complexity
3506 /// $T(n) = O(n \log n \log\log n)$
3507 ///
3508 /// $M(n) = O(n \log n)$
3509 ///
3510 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3511 /// other.significant_bits(), prec)`.
3512 ///
3513 /// # Panics
3514 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3515 ///
3516 /// # Examples
3517 /// ```
3518 /// use core::f64::consts::PI;
3519 /// use malachite_base::rounding_modes::RoundingMode::*;
3520 /// use malachite_float::Float;
3521 /// use malachite_q::Rational;
3522 /// use std::cmp::Ordering::*;
3523 ///
3524 /// let mut x = Float::from(PI);
3525 /// assert_eq!(
3526 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 5, Floor),
3527 /// Less
3528 /// );
3529 /// assert_eq!(x.to_string(), "9.0");
3530 ///
3531 /// let mut x = Float::from(PI);
3532 /// assert_eq!(
3533 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 5, Ceiling),
3534 /// Greater
3535 /// );
3536 /// assert_eq!(x.to_string(), "9.5");
3537 ///
3538 /// let mut x = Float::from(PI);
3539 /// assert_eq!(
3540 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 5, Nearest),
3541 /// Greater
3542 /// );
3543 /// assert_eq!(x.to_string(), "9.5");
3544 ///
3545 /// let mut x = Float::from(PI);
3546 /// assert_eq!(
3547 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 20, Floor),
3548 /// Less
3549 /// );
3550 /// assert_eq!(x.to_string(), "9.42477");
3551 ///
3552 /// let mut x = Float::from(PI);
3553 /// assert_eq!(
3554 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 20, Ceiling),
3555 /// Greater
3556 /// );
3557 /// assert_eq!(x.to_string(), "9.42479");
3558 ///
3559 /// let mut x = Float::from(PI);
3560 /// assert_eq!(
3561 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 20, Nearest),
3562 /// Less
3563 /// );
3564 /// assert_eq!(x.to_string(), "9.42477");
3565 /// ```
3566 #[inline]
3567 pub fn div_rational_prec_round_assign(
3568 &mut self,
3569 other: Rational,
3570 prec: u64,
3571 rm: RoundingMode,
3572 ) -> Ordering {
3573 if max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD {
3574 div_rational_prec_round_assign_naive(self, other, prec, rm)
3575 } else {
3576 div_rational_prec_round_assign_direct(self, other, prec, rm)
3577 }
3578 }
3579
3580 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the specified
3581 /// precision and with the specified rounding mode. The [`Rational`] is taken by reference. An
3582 /// [`Ordering`] is returned, indicating whether the rounded quotient is less than, equal to, or
3583 /// greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3584 /// whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
3585 ///
3586 /// See [`RoundingMode`] for a description of the possible rounding modes.
3587 ///
3588 /// $$
3589 /// x \gets x/y+\varepsilon.
3590 /// $$
3591 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3592 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3593 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3594 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3595 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3596 ///
3597 /// If the output has a precision, it is `prec`.
3598 ///
3599 /// See the [`Float::div_rational_prec_round`] documentation for information on special cases.
3600 ///
3601 /// If you know you'll be using `Nearest`, consider using
3602 /// [`Float::div_rational_prec_assign_ref`] instead. If you know that your target precision is
3603 /// the precision of the [`Float`] input, consider using
3604 /// [`Float::div_rational_round_assign_ref`] instead. If both of these things are true, consider
3605 /// using `/=` instead.
3606 ///
3607 /// # Worst-case complexity
3608 /// $T(n) = O(n \log n \log\log n)$
3609 ///
3610 /// $M(n) = O(n \log n)$
3611 ///
3612 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3613 /// other.significant_bits(), prec)`.
3614 ///
3615 /// # Panics
3616 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3617 ///
3618 /// # Examples
3619 /// ```
3620 /// use core::f64::consts::PI;
3621 /// use malachite_base::rounding_modes::RoundingMode::*;
3622 /// use malachite_float::Float;
3623 /// use malachite_q::Rational;
3624 /// use std::cmp::Ordering::*;
3625 ///
3626 /// let mut x = Float::from(PI);
3627 /// assert_eq!(
3628 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 5, Floor),
3629 /// Less
3630 /// );
3631 /// assert_eq!(x.to_string(), "9.0");
3632 ///
3633 /// let mut x = Float::from(PI);
3634 /// assert_eq!(
3635 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 5, Ceiling),
3636 /// Greater
3637 /// );
3638 /// assert_eq!(x.to_string(), "9.5");
3639 ///
3640 /// let mut x = Float::from(PI);
3641 /// assert_eq!(
3642 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 5, Nearest),
3643 /// Greater
3644 /// );
3645 /// assert_eq!(x.to_string(), "9.5");
3646 ///
3647 /// let mut x = Float::from(PI);
3648 /// assert_eq!(
3649 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 20, Floor),
3650 /// Less
3651 /// );
3652 /// assert_eq!(x.to_string(), "9.42477");
3653 ///
3654 /// let mut x = Float::from(PI);
3655 /// assert_eq!(
3656 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 20, Ceiling),
3657 /// Greater
3658 /// );
3659 /// assert_eq!(x.to_string(), "9.42479");
3660 ///
3661 /// let mut x = Float::from(PI);
3662 /// assert_eq!(
3663 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 20, Nearest),
3664 /// Less
3665 /// );
3666 /// assert_eq!(x.to_string(), "9.42477");
3667 /// ```
3668 #[inline]
3669 pub fn div_rational_prec_round_assign_ref(
3670 &mut self,
3671 other: &Rational,
3672 prec: u64,
3673 rm: RoundingMode,
3674 ) -> Ordering {
3675 if max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD {
3676 div_rational_prec_round_assign_naive_ref(self, other, prec, rm)
3677 } else {
3678 div_rational_prec_round_assign_direct_ref(self, other, prec, rm)
3679 }
3680 }
3681
3682 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the nearest value of
3683 /// the specified precision. The [`Rational`] is taken by value. An [`Ordering`] is returned,
3684 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
3685 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
3686 /// the [`Float`] to `NaN` it also returns `Equal`.
3687 ///
3688 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3689 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3690 /// description of the `Nearest` rounding mode.
3691 ///
3692 /// $$
3693 /// x \gets x/y+\varepsilon.
3694 /// $$
3695 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3696 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3697 ///
3698 /// If the output has a precision, it is `prec`.
3699 ///
3700 /// See the [`Float::div_rational_prec`] documentation for information on special cases.
3701 ///
3702 /// If you want to use a rounding mode other than `Nearest`, consider using
3703 /// [`Float::div_rational_prec_round_assign`] instead. If you know that your target precision is
3704 /// the maximum of the precisions of the two inputs, consider using `/=` instead.
3705 ///
3706 /// # Worst-case complexity
3707 /// $T(n) = O(n \log n \log\log n)$
3708 ///
3709 /// $M(n) = O(n \log n)$
3710 ///
3711 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3712 /// other.significant_bits(), prec)`.
3713 ///
3714 /// # Examples
3715 /// ```
3716 /// use core::f64::consts::PI;
3717 /// use malachite_base::num::conversion::traits::ExactFrom;
3718 /// use malachite_float::Float;
3719 /// use malachite_q::Rational;
3720 /// use std::cmp::Ordering::*;
3721 ///
3722 /// let mut x = Float::from(PI);
3723 /// assert_eq!(
3724 /// x.div_rational_prec_assign(Rational::exact_from(1.5), 5),
3725 /// Greater
3726 /// );
3727 /// assert_eq!(x.to_string(), "2.1");
3728 ///
3729 /// let mut x = Float::from(PI);
3730 /// assert_eq!(
3731 /// x.div_rational_prec_assign(Rational::exact_from(1.5), 20),
3732 /// Less
3733 /// );
3734 /// assert_eq!(x.to_string(), "2.094395");
3735 /// ```
3736 #[inline]
3737 pub fn div_rational_prec_assign(&mut self, other: Rational, prec: u64) -> Ordering {
3738 self.div_rational_prec_round_assign(other, prec, Nearest)
3739 }
3740
3741 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the nearest value of
3742 /// the specified precision. The [`Rational`] is taken by reference. An [`Ordering`] is
3743 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3744 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3745 /// function sets the [`Float`] to `NaN` it also returns `Equal`.
3746 ///
3747 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3748 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3749 /// description of the `Nearest` rounding mode.
3750 ///
3751 /// $$
3752 /// x \gets x/y+\varepsilon.
3753 /// $$
3754 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3755 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3756 ///
3757 /// If the output has a precision, it is `prec`.
3758 ///
3759 /// See the [`Float::div_rational_prec`] documentation for information on special cases.
3760 ///
3761 /// If you want to use a rounding mode other than `Nearest`, consider using
3762 /// [`Float::div_rational_prec_round_assign`] instead. If you know that your target precision is
3763 /// the maximum of the precisions of the two inputs, consider using `/=` instead.
3764 ///
3765 /// # Worst-case complexity
3766 /// $T(n) = O(n \log n \log\log n)$
3767 ///
3768 /// $M(n) = O(n \log n)$
3769 ///
3770 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3771 /// other.significant_bits(), prec)`.
3772 ///
3773 /// # Examples
3774 /// ```
3775 /// use core::f64::consts::PI;
3776 /// use malachite_base::num::conversion::traits::ExactFrom;
3777 /// use malachite_float::Float;
3778 /// use malachite_q::Rational;
3779 /// use std::cmp::Ordering::*;
3780 ///
3781 /// let mut x = Float::from(PI);
3782 /// assert_eq!(
3783 /// x.div_rational_prec_assign_ref(&Rational::exact_from(1.5), 5),
3784 /// Greater
3785 /// );
3786 /// assert_eq!(x.to_string(), "2.1");
3787 ///
3788 /// let mut x = Float::from(PI);
3789 /// assert_eq!(
3790 /// x.div_rational_prec_assign_ref(&Rational::exact_from(1.5), 20),
3791 /// Less
3792 /// );
3793 /// assert_eq!(x.to_string(), "2.094395");
3794 /// ```
3795 #[inline]
3796 pub fn div_rational_prec_assign_ref(&mut self, other: &Rational, prec: u64) -> Ordering {
3797 self.div_rational_prec_round_assign_ref(other, prec, Nearest)
3798 }
3799
3800 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result with the specified
3801 /// rounding mode. The [`Rational`] is taken by value. An [`Ordering`] is returned, indicating
3802 /// whether the rounded quotient is less than, equal to, or greater than the exact quotient.
3803 /// Although `NaN`s are not comparable to any [`Float`], whenever this function sets the
3804 /// [`Float`] to `NaN` it also returns `Equal`.
3805 ///
3806 /// The precision of the output is the precision of the input [`Float`]. See [`RoundingMode`]
3807 /// for a description of the possible rounding modes.
3808 ///
3809 /// $$
3810 /// x \gets x/y+\varepsilon.
3811 /// $$
3812 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3813 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3814 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3815 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3816 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3817 ///
3818 /// If the output has a precision, it is the precision of the input [`Float`].
3819 ///
3820 /// See the [`Float::div_rational_round`] documentation for information on special cases.
3821 ///
3822 /// If you want to specify an output precision, consider using
3823 /// [`Float::div_rational_prec_round_assign`] instead. If you know you'll be using the `Nearest`
3824 /// rounding mode, consider using `/=` instead.
3825 ///
3826 /// # Worst-case complexity
3827 /// $T(n) = O(n \log n \log\log n)$
3828 ///
3829 /// $M(n) = O(n \log n)$
3830 ///
3831 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3832 /// other.significant_bits())`.
3833 ///
3834 /// # Panics
3835 /// Panics if `rm` is `Exact` but the precision of the input [`Float`] is not high enough to
3836 /// represent the output.
3837 ///
3838 /// # Examples
3839 /// ```
3840 /// use core::f64::consts::PI;
3841 /// use malachite_base::rounding_modes::RoundingMode::*;
3842 /// use malachite_float::Float;
3843 /// use malachite_q::Rational;
3844 /// use std::cmp::Ordering::*;
3845 ///
3846 /// let mut x = Float::from(PI);
3847 /// assert_eq!(
3848 /// x.div_rational_round_assign(Rational::from_unsigneds(1u8, 3), Floor),
3849 /// Less
3850 /// );
3851 /// assert_eq!(x.to_string(), "9.42477796076938");
3852 ///
3853 /// let mut x = Float::from(PI);
3854 /// assert_eq!(
3855 /// x.div_rational_round_assign(Rational::from_unsigneds(1u8, 3), Ceiling),
3856 /// Greater
3857 /// );
3858 /// assert_eq!(x.to_string(), "9.42477796076939");
3859 ///
3860 /// let mut x = Float::from(PI);
3861 /// assert_eq!(
3862 /// x.div_rational_round_assign(Rational::from_unsigneds(1u8, 3), Nearest),
3863 /// Less
3864 /// );
3865 /// assert_eq!(x.to_string(), "9.42477796076938");
3866 /// ```
3867 #[inline]
3868 pub fn div_rational_round_assign(&mut self, other: Rational, rm: RoundingMode) -> Ordering {
3869 let prec = self.significant_bits();
3870 self.div_rational_prec_round_assign(other, prec, rm)
3871 }
3872
3873 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result with the specified
3874 /// rounding mode. The [`Rational`] is taken by reference. An [`Ordering`] is returned,
3875 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
3876 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
3877 /// the [`Float`] to `NaN` it also returns `Equal`.
3878 ///
3879 /// The precision of the output is the precision of the input [`Float`]. See [`RoundingMode`]
3880 /// for a description of the possible rounding modes.
3881 ///
3882 /// $$
3883 /// x \gets x/y+\varepsilon.
3884 /// $$
3885 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3886 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3887 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
3888 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3889 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
3890 ///
3891 /// If the output has a precision, it is the precision of the input [`Float`].
3892 ///
3893 /// See the [`Float::div_rational_round`] documentation for information on special cases.
3894 ///
3895 /// If you want to specify an output precision, consider using
3896 /// [`Float::div_rational_prec_round_assign`] instead. If you know you'll be using the `Nearest`
3897 /// rounding mode, consider using `/=` instead.
3898 ///
3899 /// # Worst-case complexity
3900 /// $T(n) = O(n \log n \log\log n)$
3901 ///
3902 /// $M(n) = O(n \log n)$
3903 ///
3904 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3905 /// other.significant_bits())`.
3906 ///
3907 /// # Panics
3908 /// Panics if `rm` is `Exact` but the precision of the input [`Float`] is not high enough to
3909 /// represent the output.
3910 ///
3911 /// # Examples
3912 /// ```
3913 /// use core::f64::consts::PI;
3914 /// use malachite_base::rounding_modes::RoundingMode::*;
3915 /// use malachite_float::Float;
3916 /// use malachite_q::Rational;
3917 /// use std::cmp::Ordering::*;
3918 ///
3919 /// let mut x = Float::from(PI);
3920 /// assert_eq!(
3921 /// x.div_rational_round_assign_ref(&Rational::from_unsigneds(1u8, 3), Floor),
3922 /// Less
3923 /// );
3924 /// assert_eq!(x.to_string(), "9.42477796076938");
3925 ///
3926 /// let mut x = Float::from(PI);
3927 /// assert_eq!(
3928 /// x.div_rational_round_assign_ref(&Rational::from_unsigneds(1u8, 3), Ceiling),
3929 /// Greater
3930 /// );
3931 /// assert_eq!(x.to_string(), "9.42477796076939");
3932 ///
3933 /// let mut x = Float::from(PI);
3934 /// assert_eq!(
3935 /// x.div_rational_round_assign_ref(&Rational::from_unsigneds(1u8, 3), Nearest),
3936 /// Less
3937 /// );
3938 /// assert_eq!(x.to_string(), "9.42477796076938");
3939 /// ```
3940 #[inline]
3941 pub fn div_rational_round_assign_ref(
3942 &mut self,
3943 other: &Rational,
3944 rm: RoundingMode,
3945 ) -> Ordering {
3946 let prec = self.significant_bits();
3947 self.div_rational_prec_round_assign_ref(other, prec, rm)
3948 }
3949
3950 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
3951 /// with the specified rounding mode. The [`Rational`] and the [`Float`] are both taken by
3952 /// value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
3953 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
3954 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3955 ///
3956 /// See [`RoundingMode`] for a description of the possible rounding modes.
3957 ///
3958 /// $$
3959 /// f(x,y,p,m) = x/y+\varepsilon.
3960 /// $$
3961 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3962 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3963 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3964 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3965 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3966 ///
3967 /// If the output has a precision, it is `prec`.
3968 ///
3969 /// Special cases:
3970 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
3971 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
3972 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
3973 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
3974 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
3975 /// - $f(0,x,p,m)=0.0$ if $x>0$
3976 /// - $f(0,x,p,m)=-0.0$ if $x<0$
3977 ///
3978 /// If you know you'll be using `Nearest`, consider using [`Float::rational_div_float_prec`]
3979 /// instead. If you know that your target precision is the precision of the [`Float`] input,
3980 /// consider using [`Float::rational_div_float_round`] instead. If both of these things are
3981 /// true, consider using `/` instead.
3982 ///
3983 /// # Worst-case complexity
3984 /// $T(n) = O(n \log n \log\log n)$
3985 ///
3986 /// $M(n) = O(n \log n)$
3987 ///
3988 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
3989 /// y.significant_bits(), prec)`.
3990 ///
3991 /// # Panics
3992 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3993 ///
3994 /// # Examples
3995 /// ```
3996 /// use core::f64::consts::PI;
3997 /// use malachite_base::rounding_modes::RoundingMode::*;
3998 /// use malachite_float::Float;
3999 /// use malachite_q::Rational;
4000 /// use std::cmp::Ordering::*;
4001 ///
4002 /// let (quotient, o) =
4003 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 5, Floor);
4004 /// assert_eq!(quotient.to_string(), "0.94");
4005 /// assert_eq!(o, Less);
4006 ///
4007 /// let (quotient, o) =
4008 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 5, Ceiling);
4009 /// assert_eq!(quotient.to_string(), "0.97");
4010 /// assert_eq!(o, Greater);
4011 ///
4012 /// let (quotient, o) =
4013 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 5, Nearest);
4014 /// assert_eq!(quotient.to_string(), "0.97");
4015 /// assert_eq!(o, Greater);
4016 ///
4017 /// let (quotient, o) =
4018 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 20, Floor);
4019 /// assert_eq!(quotient.to_string(), "0.954929");
4020 /// assert_eq!(o, Less);
4021 ///
4022 /// let (quotient, o) =
4023 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 20, Ceiling);
4024 /// assert_eq!(quotient.to_string(), "0.95493");
4025 /// assert_eq!(o, Greater);
4026 ///
4027 /// let (quotient, o) =
4028 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 20, Nearest);
4029 /// assert_eq!(quotient.to_string(), "0.954929");
4030 /// assert_eq!(o, Less);
4031 /// ```
4032 #[inline]
4033 pub fn rational_div_float_prec_round(
4034 x: Rational,
4035 y: Float,
4036 prec: u64,
4037 rm: RoundingMode,
4038 ) -> (Float, Ordering) {
4039 if max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
4040 rational_div_float_prec_round_naive(x, y, prec, rm)
4041 } else {
4042 rational_div_float_prec_round_direct(x, y, prec, rm)
4043 }
4044 }
4045
4046 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
4047 /// with the specified rounding mode. The [`Rational`] is taken by value and the [`Float`] by
4048 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
4049 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
4050 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4051 ///
4052 /// See [`RoundingMode`] for a description of the possible rounding modes.
4053 ///
4054 /// $$
4055 /// f(x,y,p,m) = x/y+\varepsilon.
4056 /// $$
4057 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4058 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4059 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4060 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4061 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4062 ///
4063 /// If the output has a precision, it is `prec`.
4064 ///
4065 /// Special cases:
4066 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
4067 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
4068 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4069 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
4070 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
4071 /// - $f(0,x,p,m)=0.0$ if $x>0$
4072 /// - $f(0,x,p,m)=-0.0$ if $x<0$
4073 ///
4074 /// If you know you'll be using `Nearest`, consider using
4075 /// [`Float::rational_div_float_prec_val_ref`] instead. If you know that your target precision
4076 /// is the precision of the [`Float`] input, consider using
4077 /// [`Float::rational_div_float_round_val_ref`] instead. If both of these things are true,
4078 /// consider using `/` instead.
4079 ///
4080 /// # Worst-case complexity
4081 /// $T(n) = O(n \log n \log\log n)$
4082 ///
4083 /// $M(n) = O(n \log n)$
4084 ///
4085 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4086 /// y.significant_bits(), prec)`.
4087 ///
4088 /// # Panics
4089 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4090 ///
4091 /// # Examples
4092 /// ```
4093 /// use core::f64::consts::PI;
4094 /// use malachite_base::rounding_modes::RoundingMode::*;
4095 /// use malachite_float::Float;
4096 /// use malachite_q::Rational;
4097 /// use std::cmp::Ordering::*;
4098 ///
4099 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4100 /// Rational::from(3),
4101 /// &Float::from(PI),
4102 /// 5,
4103 /// Floor,
4104 /// );
4105 /// assert_eq!(quotient.to_string(), "0.94");
4106 /// assert_eq!(o, Less);
4107 ///
4108 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4109 /// Rational::from(3),
4110 /// &Float::from(PI),
4111 /// 5,
4112 /// Ceiling,
4113 /// );
4114 /// assert_eq!(quotient.to_string(), "0.97");
4115 /// assert_eq!(o, Greater);
4116 ///
4117 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4118 /// Rational::from(3),
4119 /// &Float::from(PI),
4120 /// 5,
4121 /// Nearest,
4122 /// );
4123 /// assert_eq!(quotient.to_string(), "0.97");
4124 /// assert_eq!(o, Greater);
4125 ///
4126 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4127 /// Rational::from(3),
4128 /// &Float::from(PI),
4129 /// 20,
4130 /// Floor,
4131 /// );
4132 /// assert_eq!(quotient.to_string(), "0.954929");
4133 /// assert_eq!(o, Less);
4134 ///
4135 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4136 /// Rational::from(3),
4137 /// &Float::from(PI),
4138 /// 20,
4139 /// Ceiling,
4140 /// );
4141 /// assert_eq!(quotient.to_string(), "0.95493");
4142 /// assert_eq!(o, Greater);
4143 ///
4144 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
4145 /// Rational::from(3),
4146 /// &Float::from(PI),
4147 /// 20,
4148 /// Nearest,
4149 /// );
4150 /// assert_eq!(quotient.to_string(), "0.954929");
4151 /// assert_eq!(o, Less);
4152 /// ```
4153 #[inline]
4154 pub fn rational_div_float_prec_round_val_ref(
4155 x: Rational,
4156 y: &Float,
4157 prec: u64,
4158 rm: RoundingMode,
4159 ) -> (Float, Ordering) {
4160 if max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
4161 rational_div_float_prec_round_naive_val_ref(x, y, prec, rm)
4162 } else {
4163 rational_div_float_prec_round_direct_val_ref(x, y, prec, rm)
4164 }
4165 }
4166
4167 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
4168 /// with the specified rounding mode. The [`Rational`] is taken by reference and the [`Float`]
4169 /// by value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
4170 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
4171 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4172 ///
4173 /// See [`RoundingMode`] for a description of the possible rounding modes.
4174 ///
4175 /// $$
4176 /// f(x,y,p,m) = x/y+\varepsilon.
4177 /// $$
4178 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4179 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4180 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4181 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4182 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4183 ///
4184 /// If the output has a precision, it is `prec`.
4185 ///
4186 /// Special cases:
4187 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
4188 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
4189 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4190 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
4191 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
4192 /// - $f(0,x,p,m)=0.0$ if $x>0$
4193 /// - $f(0,x,p,m)=-0.0$ if $x<0$
4194 ///
4195 /// If you know you'll be using `Nearest`, consider using
4196 /// [`Float::rational_div_float_prec_ref_val`] instead. If you know that your target precision
4197 /// is the precision of the [`Float`] input, consider using
4198 /// [`Float::rational_div_float_round_ref_val`] instead. If both of these things are true,
4199 /// consider using `/` instead.
4200 ///
4201 /// # Worst-case complexity
4202 /// $T(n) = O(n \log n \log\log n)$
4203 ///
4204 /// $M(n) = O(n \log n)$
4205 ///
4206 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4207 /// y.significant_bits(), prec)`.
4208 ///
4209 /// # Panics
4210 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4211 ///
4212 /// # Examples
4213 /// ```
4214 /// use core::f64::consts::PI;
4215 /// use malachite_base::rounding_modes::RoundingMode::*;
4216 /// use malachite_float::Float;
4217 /// use malachite_q::Rational;
4218 /// use std::cmp::Ordering::*;
4219 ///
4220 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4221 /// &Rational::from(3),
4222 /// Float::from(PI),
4223 /// 5,
4224 /// Floor,
4225 /// );
4226 /// assert_eq!(quotient.to_string(), "0.94");
4227 /// assert_eq!(o, Less);
4228 ///
4229 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4230 /// &Rational::from(3),
4231 /// Float::from(PI),
4232 /// 5,
4233 /// Ceiling,
4234 /// );
4235 /// assert_eq!(quotient.to_string(), "0.97");
4236 /// assert_eq!(o, Greater);
4237 ///
4238 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4239 /// &Rational::from(3),
4240 /// Float::from(PI),
4241 /// 5,
4242 /// Nearest,
4243 /// );
4244 /// assert_eq!(quotient.to_string(), "0.97");
4245 /// assert_eq!(o, Greater);
4246 ///
4247 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4248 /// &Rational::from(3),
4249 /// Float::from(PI),
4250 /// 20,
4251 /// Floor,
4252 /// );
4253 /// assert_eq!(quotient.to_string(), "0.954929");
4254 /// assert_eq!(o, Less);
4255 ///
4256 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4257 /// &Rational::from(3),
4258 /// Float::from(PI),
4259 /// 20,
4260 /// Ceiling,
4261 /// );
4262 /// assert_eq!(quotient.to_string(), "0.95493");
4263 /// assert_eq!(o, Greater);
4264 ///
4265 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
4266 /// &Rational::from(3),
4267 /// Float::from(PI),
4268 /// 20,
4269 /// Nearest,
4270 /// );
4271 /// assert_eq!(quotient.to_string(), "0.954929");
4272 /// assert_eq!(o, Less);
4273 /// ```
4274 #[inline]
4275 pub fn rational_div_float_prec_round_ref_val(
4276 x: &Rational,
4277 y: Float,
4278 prec: u64,
4279 rm: RoundingMode,
4280 ) -> (Float, Ordering) {
4281 if max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
4282 rational_div_float_prec_round_naive_ref_val(x, y, prec, rm)
4283 } else {
4284 rational_div_float_prec_round_direct_ref_val(x, y, prec, rm)
4285 }
4286 }
4287
4288 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
4289 /// with the specified rounding mode. The [`Rational`] and the [`Float`] are both taken by
4290 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
4291 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
4292 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4293 ///
4294 /// See [`RoundingMode`] for a description of the possible rounding modes.
4295 ///
4296 /// $$
4297 /// f(x,y,p,m) = x/y+\varepsilon.
4298 /// $$
4299 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4300 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4301 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4302 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4303 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4304 ///
4305 /// If the output has a precision, it is `prec`.
4306 ///
4307 /// Special cases:
4308 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
4309 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
4310 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4311 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
4312 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
4313 /// - $f(0,x,p,m)=0.0$ if $x>0$
4314 /// - $f(0,x,p,m)=-0.0$ if $x<0$
4315 ///
4316 /// If you know you'll be using `Nearest`, consider using
4317 /// [`Float::rational_div_float_prec_ref_ref`] instead. If you know that your target precision
4318 /// is the precision of the [`Float`] input, consider using
4319 /// [`Float::rational_div_float_round_ref_ref`] instead. If both of these things are true,
4320 /// consider using `/` instead.
4321 ///
4322 /// # Worst-case complexity
4323 /// $T(n) = O(n \log n \log\log n)$
4324 ///
4325 /// $M(n) = O(n \log n)$
4326 ///
4327 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4328 /// y.significant_bits(), prec)`.
4329 ///
4330 /// # Panics
4331 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4332 ///
4333 /// # Examples
4334 /// ```
4335 /// use core::f64::consts::PI;
4336 /// use malachite_base::rounding_modes::RoundingMode::*;
4337 /// use malachite_float::Float;
4338 /// use malachite_q::Rational;
4339 /// use std::cmp::Ordering::*;
4340 ///
4341 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4342 /// &Rational::from(3),
4343 /// &Float::from(PI),
4344 /// 5,
4345 /// Floor,
4346 /// );
4347 /// assert_eq!(quotient.to_string(), "0.94");
4348 /// assert_eq!(o, Less);
4349 ///
4350 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4351 /// &Rational::from(3),
4352 /// &Float::from(PI),
4353 /// 5,
4354 /// Ceiling,
4355 /// );
4356 /// assert_eq!(quotient.to_string(), "0.97");
4357 /// assert_eq!(o, Greater);
4358 ///
4359 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4360 /// &Rational::from(3),
4361 /// &Float::from(PI),
4362 /// 5,
4363 /// Nearest,
4364 /// );
4365 /// assert_eq!(quotient.to_string(), "0.97");
4366 /// assert_eq!(o, Greater);
4367 ///
4368 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4369 /// &Rational::from(3),
4370 /// &Float::from(PI),
4371 /// 20,
4372 /// Floor,
4373 /// );
4374 /// assert_eq!(quotient.to_string(), "0.954929");
4375 /// assert_eq!(o, Less);
4376 ///
4377 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4378 /// &Rational::from(3),
4379 /// &Float::from(PI),
4380 /// 20,
4381 /// Ceiling,
4382 /// );
4383 /// assert_eq!(quotient.to_string(), "0.95493");
4384 /// assert_eq!(o, Greater);
4385 ///
4386 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
4387 /// &Rational::from(3),
4388 /// &Float::from(PI),
4389 /// 20,
4390 /// Nearest,
4391 /// );
4392 /// assert_eq!(quotient.to_string(), "0.954929");
4393 /// assert_eq!(o, Less);
4394 /// ```
4395 #[inline]
4396 pub fn rational_div_float_prec_round_ref_ref(
4397 x: &Rational,
4398 y: &Float,
4399 prec: u64,
4400 rm: RoundingMode,
4401 ) -> (Float, Ordering) {
4402 if max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
4403 rational_div_float_prec_round_naive_ref_ref(x, y, prec, rm)
4404 } else {
4405 rational_div_float_prec_round_direct_ref_ref(x, y, prec, rm)
4406 }
4407 }
4408
4409 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
4410 /// specified precision. The [`Rational`] and the [`Float`] are both are taken by value. An
4411 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
4412 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
4413 /// whenever this function returns a `NaN` it also returns `Equal`.
4414 ///
4415 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4416 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4417 /// description of the `Nearest` rounding mode.
4418 ///
4419 /// $$
4420 /// f(x,y,p) = x/y+\varepsilon.
4421 /// $$
4422 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4423 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4424 ///
4425 /// If the output has a precision, it is `prec`.
4426 ///
4427 /// Special cases:
4428 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
4429 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
4430 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
4431 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
4432 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
4433 /// - $f(0,x,p)=0.0$ if $x>0$
4434 /// - $f(0,x,p)=-0.0$ if $x<0$
4435 ///
4436 /// If you want to use a rounding mode other than `Nearest`, consider using
4437 /// [`Float::rational_div_float_prec_round`] instead. If you know that your target precision is
4438 /// the precision of the [`Float`] input, consider using `/` instead.
4439 ///
4440 /// # Worst-case complexity
4441 /// $T(n) = O(n \log n \log\log n)$
4442 ///
4443 /// $M(n) = O(n \log n)$
4444 ///
4445 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4446 /// y.significant_bits(), prec)`.
4447 ///
4448 /// # Examples
4449 /// ```
4450 /// use core::f64::consts::PI;
4451 /// use malachite_float::Float;
4452 /// use malachite_q::Rational;
4453 /// use std::cmp::Ordering::*;
4454 ///
4455 /// let (quotient, o) = Float::rational_div_float_prec(Rational::from(3), Float::from(PI), 5);
4456 /// assert_eq!(quotient.to_string(), "0.97");
4457 /// assert_eq!(o, Greater);
4458 ///
4459 /// let (quotient, o) = Float::rational_div_float_prec(Rational::from(3), Float::from(PI), 20);
4460 /// assert_eq!(quotient.to_string(), "0.954929");
4461 /// assert_eq!(o, Less);
4462 /// ```
4463 #[inline]
4464 pub fn rational_div_float_prec(x: Rational, y: Float, prec: u64) -> (Float, Ordering) {
4465 Float::rational_div_float_prec_round(x, y, prec, Nearest)
4466 }
4467
4468 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
4469 /// specified precision. The [`Rational`] is taken by value and the [`Float`] by reference. An
4470 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
4471 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
4472 /// whenever this function returns a `NaN` it also returns `Equal`.
4473 ///
4474 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4475 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4476 /// description of the `Nearest` rounding mode.
4477 ///
4478 /// $$
4479 /// f(x,y,p) = x/y+\varepsilon.
4480 /// $$
4481 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4482 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4483 ///
4484 /// If the output has a precision, it is `prec`.
4485 ///
4486 /// Special cases:
4487 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
4488 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
4489 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
4490 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
4491 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
4492 /// - $f(0,x,p)=0.0$ if $x>0$
4493 /// - $f(0,x,p)=-0.0$ if $x<0$
4494 ///
4495 /// If you want to use a rounding mode other than `Nearest`, consider using
4496 /// [`Float::rational_div_float_prec_round_val_ref`] instead. If you know that your target
4497 /// precision is the precision of the [`Float`] input, consider using `/` instead.
4498 ///
4499 /// # Worst-case complexity
4500 /// $T(n) = O(n \log n \log\log n)$
4501 ///
4502 /// $M(n) = O(n \log n)$
4503 ///
4504 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4505 /// y.significant_bits(), prec)`.
4506 ///
4507 /// # Examples
4508 /// ```
4509 /// use core::f64::consts::PI;
4510 /// use malachite_float::Float;
4511 /// use malachite_q::Rational;
4512 /// use std::cmp::Ordering::*;
4513 ///
4514 /// let (quotient, o) =
4515 /// Float::rational_div_float_prec_val_ref(Rational::from(3), &Float::from(PI), 5);
4516 /// assert_eq!(quotient.to_string(), "0.97");
4517 /// assert_eq!(o, Greater);
4518 ///
4519 /// let (quotient, o) =
4520 /// Float::rational_div_float_prec_val_ref(Rational::from(3), &Float::from(PI), 20);
4521 /// assert_eq!(quotient.to_string(), "0.954929");
4522 /// assert_eq!(o, Less);
4523 /// ```
4524 #[inline]
4525 pub fn rational_div_float_prec_val_ref(x: Rational, y: &Float, prec: u64) -> (Float, Ordering) {
4526 Float::rational_div_float_prec_round_val_ref(x, y, prec, Nearest)
4527 }
4528
4529 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
4530 /// specified precision. The [`Rational`] is taken by reference and the [`Float`] by value. An
4531 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
4532 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
4533 /// whenever this function returns a `NaN` it also returns `Equal`.
4534 ///
4535 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4536 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4537 /// description of the `Nearest` rounding mode.
4538 ///
4539 /// $$
4540 /// f(x,y,p) = x/y+\varepsilon.
4541 /// $$
4542 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4543 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4544 ///
4545 /// If the output has a precision, it is `prec`.
4546 ///
4547 /// Special cases:
4548 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
4549 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
4550 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
4551 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
4552 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
4553 /// - $f(0,x,p)=0.0$ if $x>0$
4554 /// - $f(0,x,p)=-0.0$ if $x<0$
4555 ///
4556 /// If you want to use a rounding mode other than `Nearest`, consider using
4557 /// [`Float::rational_div_float_prec_round_ref_val`] instead. If you know that your target
4558 /// precision is the precision of the [`Float`] input, consider using `/` instead.
4559 ///
4560 /// # Worst-case complexity
4561 /// $T(n) = O(n \log n \log\log n)$
4562 ///
4563 /// $M(n) = O(n \log n)$
4564 ///
4565 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4566 /// y.significant_bits(), prec)`.
4567 ///
4568 /// # Examples
4569 /// ```
4570 /// use core::f64::consts::PI;
4571 /// use malachite_float::Float;
4572 /// use malachite_q::Rational;
4573 /// use std::cmp::Ordering::*;
4574 ///
4575 /// let (quotient, o) =
4576 /// Float::rational_div_float_prec_ref_val(&Rational::from(3), Float::from(PI), 5);
4577 /// assert_eq!(quotient.to_string(), "0.97");
4578 /// assert_eq!(o, Greater);
4579 ///
4580 /// let (quotient, o) =
4581 /// Float::rational_div_float_prec_ref_val(&Rational::from(3), Float::from(PI), 20);
4582 /// assert_eq!(quotient.to_string(), "0.954929");
4583 /// assert_eq!(o, Less);
4584 /// ```
4585 #[inline]
4586 pub fn rational_div_float_prec_ref_val(x: &Rational, y: Float, prec: u64) -> (Float, Ordering) {
4587 Float::rational_div_float_prec_round_ref_val(x, y, prec, Nearest)
4588 }
4589
4590 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
4591 /// specified precision. The [`Rational`] and the [`Float`] are both are taken by reference. An
4592 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
4593 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
4594 /// whenever this function returns a `NaN` it also returns `Equal`.
4595 ///
4596 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4597 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4598 /// description of the `Nearest` rounding mode.
4599 ///
4600 /// $$
4601 /// f(x,y,p) = x/y+\varepsilon.
4602 /// $$
4603 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4604 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4605 ///
4606 /// If the output has a precision, it is `prec`.
4607 ///
4608 /// Special cases:
4609 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
4610 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
4611 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
4612 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
4613 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
4614 /// - $f(0,x,p)=0.0$ if $x>0$
4615 /// - $f(0,x,p)=-0.0$ if $x<0$
4616 ///
4617 /// If you want to use a rounding mode other than `Nearest`, consider using
4618 /// [`Float::rational_div_float_prec_round_ref_ref`] instead. If you know that your target
4619 /// precision is the precision of the [`Float`] input, consider using `/` instead.
4620 ///
4621 /// # Worst-case complexity
4622 /// $T(n) = O(n \log n \log\log n)$
4623 ///
4624 /// $M(n) = O(n \log n)$
4625 ///
4626 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4627 /// y.significant_bits(), prec)`.
4628 ///
4629 /// # Examples
4630 /// ```
4631 /// use core::f64::consts::PI;
4632 /// use malachite_float::Float;
4633 /// use malachite_q::Rational;
4634 /// use std::cmp::Ordering::*;
4635 ///
4636 /// let (quotient, o) =
4637 /// Float::rational_div_float_prec_ref_ref(&Rational::from(3), &Float::from(PI), 5);
4638 /// assert_eq!(quotient.to_string(), "0.97");
4639 /// assert_eq!(o, Greater);
4640 ///
4641 /// let (quotient, o) =
4642 /// Float::rational_div_float_prec_ref_ref(&Rational::from(3), &Float::from(PI), 20);
4643 /// assert_eq!(quotient.to_string(), "0.954929");
4644 /// assert_eq!(o, Less);
4645 /// ```
4646 #[inline]
4647 pub fn rational_div_float_prec_ref_ref(
4648 x: &Rational,
4649 y: &Float,
4650 prec: u64,
4651 ) -> (Float, Ordering) {
4652 Float::rational_div_float_prec_round_ref_ref(x, y, prec, Nearest)
4653 }
4654
4655 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
4656 /// The [`Rational`] and the [`Float`] are both are taken by value. An [`Ordering`] is also
4657 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4658 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4659 /// function returns a `NaN` it also returns `Equal`.
4660 ///
4661 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4662 /// for a description of the possible rounding modes.
4663 ///
4664 /// $$
4665 /// f(x,y,m) = x/y+\varepsilon.
4666 /// $$
4667 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4668 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4669 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4670 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4671 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4672 ///
4673 /// If the output has a precision, it is the precision of the [`Float`] input.
4674 ///
4675 /// Special cases:
4676 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
4677 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
4678 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4679 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
4680 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
4681 /// - $f(0,x,m)=0.0$ if $x>0$
4682 /// - $f(0,x,m)=-0.0$ if $x<0$
4683 ///
4684 /// If you want to specify an output precision, consider using
4685 /// [`Float::rational_div_float_prec_round`] instead. If you know you'll be using the `Nearest`
4686 /// rounding mode, consider using `/` instead.
4687 ///
4688 /// # Worst-case complexity
4689 /// $T(n) = O(n \log n \log\log n)$
4690 ///
4691 /// $M(n) = O(n \log n)$
4692 ///
4693 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4694 /// y.significant_bits())`.
4695 ///
4696 /// # Panics
4697 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4698 /// represent the output.
4699 ///
4700 /// # Examples
4701 /// ```
4702 /// use core::f64::consts::PI;
4703 /// use malachite_base::rounding_modes::RoundingMode::*;
4704 /// use malachite_float::Float;
4705 /// use malachite_q::Rational;
4706 /// use std::cmp::Ordering::*;
4707 ///
4708 /// let (quotient, o) =
4709 /// Float::rational_div_float_round(Rational::from(3), Float::from(PI), Floor);
4710 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
4711 /// assert_eq!(o, Less);
4712 ///
4713 /// let (quotient, o) =
4714 /// Float::rational_div_float_round(Rational::from(3), Float::from(PI), Ceiling);
4715 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4716 /// assert_eq!(o, Greater);
4717 ///
4718 /// let (quotient, o) =
4719 /// Float::rational_div_float_round(Rational::from(3), Float::from(PI), Nearest);
4720 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4721 /// assert_eq!(o, Greater);
4722 /// ```
4723 #[inline]
4724 pub fn rational_div_float_round(x: Rational, y: Float, rm: RoundingMode) -> (Float, Ordering) {
4725 let prec = y.significant_bits();
4726 Float::rational_div_float_prec_round(x, y, prec, rm)
4727 }
4728
4729 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
4730 /// The [`Rational`] is taken by value and the [`Float`] by reference. An [`Ordering`] is also
4731 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4732 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4733 /// function returns a `NaN` it also returns `Equal`.
4734 ///
4735 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4736 /// for a description of the possible rounding modes.
4737 ///
4738 /// $$
4739 /// f(x,y,m) = x/y+\varepsilon.
4740 /// $$
4741 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4742 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4743 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4744 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4745 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4746 ///
4747 /// If the output has a precision, it is the precision of the [`Float`] input.
4748 ///
4749 /// Special cases:
4750 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
4751 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
4752 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4753 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
4754 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
4755 /// - $f(0,x,m)=0.0$ if $x>0$
4756 /// - $f(0,x,m)=-0.0$ if $x<0$
4757 ///
4758 /// If you want to specify an output precision, consider using
4759 /// [`Float::rational_div_float_prec_round_val_ref`] instead. If you know you'll be using the
4760 /// `Nearest` rounding mode, consider using `/` instead.
4761 ///
4762 /// # Worst-case complexity
4763 /// $T(n) = O(n \log n \log\log n)$
4764 ///
4765 /// $M(n) = O(n \log n)$
4766 ///
4767 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4768 /// y.significant_bits())`.
4769 ///
4770 /// # Panics
4771 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4772 /// represent the output.
4773 ///
4774 /// # Examples
4775 /// ```
4776 /// use core::f64::consts::PI;
4777 /// use malachite_base::rounding_modes::RoundingMode::*;
4778 /// use malachite_float::Float;
4779 /// use malachite_q::Rational;
4780 /// use std::cmp::Ordering::*;
4781 ///
4782 /// let (quotient, o) =
4783 /// Float::rational_div_float_round_val_ref(Rational::from(3), &Float::from(PI), Floor);
4784 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
4785 /// assert_eq!(o, Less);
4786 ///
4787 /// let (quotient, o) =
4788 /// Float::rational_div_float_round_val_ref(Rational::from(3), &Float::from(PI), Ceiling);
4789 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4790 /// assert_eq!(o, Greater);
4791 ///
4792 /// let (quotient, o) =
4793 /// Float::rational_div_float_round_val_ref(Rational::from(3), &Float::from(PI), Nearest);
4794 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4795 /// assert_eq!(o, Greater);
4796 /// ```
4797 #[inline]
4798 pub fn rational_div_float_round_val_ref(
4799 x: Rational,
4800 y: &Float,
4801 rm: RoundingMode,
4802 ) -> (Float, Ordering) {
4803 let prec = y.significant_bits();
4804 Float::rational_div_float_prec_round_val_ref(x, y, prec, rm)
4805 }
4806
4807 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
4808 /// The [`Rational`] is taken by reference and the [`Float`] by value. An [`Ordering`] is also
4809 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4810 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4811 /// function returns a `NaN` it also returns `Equal`.
4812 ///
4813 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4814 /// for a description of the possible rounding modes.
4815 ///
4816 /// $$
4817 /// f(x,y,m) = x/y+\varepsilon.
4818 /// $$
4819 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4820 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4821 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4822 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4823 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4824 ///
4825 /// If the output has a precision, it is the precision of the [`Float`] input.
4826 ///
4827 /// Special cases:
4828 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
4829 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
4830 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4831 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
4832 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
4833 /// - $f(0,x,m)=0.0$ if $x>0$
4834 /// - $f(0,x,m)=-0.0$ if $x<0$
4835 ///
4836 /// If you want to specify an output precision, consider using
4837 /// [`Float::rational_div_float_prec_round_ref_val`] instead. If you know you'll be using the
4838 /// `Nearest` rounding mode, consider using `/` instead.
4839 ///
4840 /// # Worst-case complexity
4841 /// $T(n) = O(n \log n \log\log n)$
4842 ///
4843 /// $M(n) = O(n \log n)$
4844 ///
4845 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4846 /// y.significant_bits())`.
4847 ///
4848 /// # Panics
4849 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4850 /// represent the output.
4851 ///
4852 /// # Examples
4853 /// ```
4854 /// use core::f64::consts::PI;
4855 /// use malachite_base::rounding_modes::RoundingMode::*;
4856 /// use malachite_float::Float;
4857 /// use malachite_q::Rational;
4858 /// use std::cmp::Ordering::*;
4859 ///
4860 /// let (quotient, o) =
4861 /// Float::rational_div_float_round_ref_val(&Rational::from(3), Float::from(PI), Floor);
4862 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
4863 /// assert_eq!(o, Less);
4864 ///
4865 /// let (quotient, o) =
4866 /// Float::rational_div_float_round_ref_val(&Rational::from(3), Float::from(PI), Ceiling);
4867 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4868 /// assert_eq!(o, Greater);
4869 ///
4870 /// let (quotient, o) =
4871 /// Float::rational_div_float_round_ref_val(&Rational::from(3), Float::from(PI), Nearest);
4872 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4873 /// assert_eq!(o, Greater);
4874 /// ```
4875 #[inline]
4876 pub fn rational_div_float_round_ref_val(
4877 x: &Rational,
4878 y: Float,
4879 rm: RoundingMode,
4880 ) -> (Float, Ordering) {
4881 let prec = y.significant_bits();
4882 Float::rational_div_float_prec_round_ref_val(x, y, prec, rm)
4883 }
4884
4885 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
4886 /// The [`Rational`] and the [`Float`] are both are taken by reference. An [`Ordering`] is also
4887 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4888 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4889 /// function returns a `NaN` it also returns `Equal`.
4890 ///
4891 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4892 /// for a description of the possible rounding modes.
4893 ///
4894 /// $$
4895 /// f(x,y,m) = x/y+\varepsilon.
4896 /// $$
4897 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4898 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4899 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4900 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4901 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4902 ///
4903 /// If the output has a precision, it is the precision of the [`Float`] input.
4904 ///
4905 /// Special cases:
4906 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
4907 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
4908 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4909 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
4910 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
4911 /// - $f(0,x,m)=0.0$ if $x>0$
4912 /// - $f(0,x,m)=-0.0$ if $x<0$
4913 ///
4914 /// If you want to specify an output precision, consider using
4915 /// [`Float::rational_div_float_prec_round_ref_ref`] instead. If you know you'll be using the
4916 /// `Nearest` rounding mode, consider using `/` instead.
4917 ///
4918 /// # Worst-case complexity
4919 /// $T(n) = O(n \log n \log\log n)$
4920 ///
4921 /// $M(n) = O(n \log n)$
4922 ///
4923 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4924 /// y.significant_bits())`.
4925 ///
4926 /// # Panics
4927 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4928 /// represent the output.
4929 ///
4930 /// # Examples
4931 /// ```
4932 /// use core::f64::consts::PI;
4933 /// use malachite_base::rounding_modes::RoundingMode::*;
4934 /// use malachite_float::Float;
4935 /// use malachite_q::Rational;
4936 /// use std::cmp::Ordering::*;
4937 ///
4938 /// let (quotient, o) =
4939 /// Float::rational_div_float_round_ref_ref(&Rational::from(3), &Float::from(PI), Floor);
4940 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
4941 /// assert_eq!(o, Less);
4942 ///
4943 /// let (quotient, o) =
4944 /// Float::rational_div_float_round_ref_ref(&Rational::from(3), &Float::from(PI), Ceiling);
4945 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4946 /// assert_eq!(o, Greater);
4947 ///
4948 /// let (quotient, o) =
4949 /// Float::rational_div_float_round_ref_ref(&Rational::from(3), &Float::from(PI), Nearest);
4950 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
4951 /// assert_eq!(o, Greater);
4952 /// ```
4953 #[inline]
4954 pub fn rational_div_float_round_ref_ref(
4955 x: &Rational,
4956 y: &Float,
4957 rm: RoundingMode,
4958 ) -> (Float, Ordering) {
4959 let prec = y.significant_bits();
4960 Float::rational_div_float_prec_round_ref_ref(x, y, prec, rm)
4961 }
4962}
4963
4964impl Div<Float> for Float {
4965 type Output = Float;
4966
4967 /// Divides two [`Float`]s, taking both by value.
4968 ///
4969 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
4970 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
4971 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
4972 /// `Nearest` rounding mode.
4973 ///
4974 /// $$
4975 /// f(x,y) = x/y+\varepsilon.
4976 /// $$
4977 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4978 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
4979 /// where $p$ is the maximum precision of the inputs.
4980 ///
4981 /// Special cases:
4982 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
4983 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
4984 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
4985 /// - $f(x,0.0)=\infty$ if $x>0.0$
4986 /// - $f(x,0.0)=-\infty$ if $x<0.0$
4987 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
4988 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
4989 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
4990 /// - $f(x,-0.0)=\infty$ if $x<0.0$
4991 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
4992 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
4993 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
4994 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
4995 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
4996 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
4997 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
4998 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
4999 ///
5000 /// If you want to use a rounding mode other than `Nearest`, consider using [`Float::div_prec`]
5001 /// instead. If you want to specify the output precision, consider using [`Float::div_round`].
5002 /// If you want both of these things, consider using [`Float::div_prec_round`].
5003 ///
5004 /// # Worst-case complexity
5005 /// $T(n) = O(n \log n \log\log n)$
5006 ///
5007 /// $M(n) = O(n \log n)$
5008 ///
5009 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5010 /// other.significant_bits())`.
5011 ///
5012 /// # Examples
5013 /// ```
5014 /// use malachite_base::num::basic::traits::{
5015 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5016 /// };
5017 /// use malachite_float::Float;
5018 ///
5019 /// assert!((Float::from(1.5) / Float::NAN).is_nan());
5020 /// assert_eq!(Float::from(1.5) / Float::ZERO, Float::INFINITY);
5021 /// assert_eq!(
5022 /// Float::from(1.5) / Float::NEGATIVE_ZERO,
5023 /// Float::NEGATIVE_INFINITY
5024 /// );
5025 /// assert_eq!(Float::from(-1.5) / Float::ZERO, Float::NEGATIVE_INFINITY);
5026 /// assert_eq!(Float::from(-1.5) / Float::NEGATIVE_ZERO, Float::INFINITY);
5027 /// assert!((Float::ZERO / Float::ZERO).is_nan());
5028 ///
5029 /// assert_eq!((Float::from(1.5) / Float::from(2.5)).to_string(), "0.6");
5030 /// assert_eq!((Float::from(1.5) / Float::from(-2.5)).to_string(), "-0.6");
5031 /// assert_eq!((Float::from(-1.5) / Float::from(2.5)).to_string(), "-0.6");
5032 /// assert_eq!((Float::from(-1.5) / Float::from(-2.5)).to_string(), "0.6");
5033 /// ```
5034 #[inline]
5035 fn div(self, other: Float) -> Float {
5036 let prec = max(self.significant_bits(), other.significant_bits());
5037 self.div_prec_round(other, prec, Nearest).0
5038 }
5039}
5040
5041impl Div<&Float> for Float {
5042 type Output = Float;
5043
5044 /// Divides two [`Float`]s, taking the first by value and the second by reference.
5045 ///
5046 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5047 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5048 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5049 /// `Nearest` rounding mode.
5050 ///
5051 /// $$
5052 /// f(x,y) = x/y+\varepsilon.
5053 /// $$
5054 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5055 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5056 /// where $p$ is the maximum precision of the inputs.
5057 ///
5058 /// Special cases:
5059 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
5060 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
5061 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
5062 /// - $f(x,0.0)=\infty$ if $x>0.0$
5063 /// - $f(x,0.0)=-\infty$ if $x<0.0$
5064 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
5065 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
5066 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
5067 /// - $f(x,-0.0)=\infty$ if $x<0.0$
5068 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
5069 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
5070 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5071 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5072 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
5073 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
5074 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5075 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5076 ///
5077 /// If you want to use a rounding mode other than `Nearest`, consider using
5078 /// [`Float::div_prec_val_ref`] instead. If you want to specify the output precision, consider
5079 /// using [`Float::div_round_val_ref`]. If you want both of these things, consider using
5080 /// [`Float::div_prec_round_val_ref`].
5081 ///
5082 /// # Worst-case complexity
5083 /// $T(n) = O(n \log n \log\log n)$
5084 ///
5085 /// $M(n) = O(n \log n)$
5086 ///
5087 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5088 /// other.significant_bits())`.
5089 ///
5090 /// # Examples
5091 /// ```
5092 /// use malachite_base::num::basic::traits::{
5093 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5094 /// };
5095 /// use malachite_float::Float;
5096 ///
5097 /// assert!((Float::from(1.5) / &Float::NAN).is_nan());
5098 /// assert_eq!(Float::from(1.5) / &Float::ZERO, Float::INFINITY);
5099 /// assert_eq!(
5100 /// Float::from(1.5) / &Float::NEGATIVE_ZERO,
5101 /// Float::NEGATIVE_INFINITY
5102 /// );
5103 /// assert_eq!(Float::from(-1.5) / &Float::ZERO, Float::NEGATIVE_INFINITY);
5104 /// assert_eq!(Float::from(-1.5) / &Float::NEGATIVE_ZERO, Float::INFINITY);
5105 /// assert!((Float::ZERO / &Float::ZERO).is_nan());
5106 ///
5107 /// assert_eq!((Float::from(1.5) / &Float::from(2.5)).to_string(), "0.6");
5108 /// assert_eq!((Float::from(1.5) / &Float::from(-2.5)).to_string(), "-0.6");
5109 /// assert_eq!((Float::from(-1.5) / &Float::from(2.5)).to_string(), "-0.6");
5110 /// assert_eq!((Float::from(-1.5) / &Float::from(-2.5)).to_string(), "0.6");
5111 /// ```
5112 #[inline]
5113 fn div(self, other: &Float) -> Float {
5114 let prec = max(self.significant_bits(), other.significant_bits());
5115 self.div_prec_round_val_ref(other, prec, Nearest).0
5116 }
5117}
5118
5119impl Div<Float> for &Float {
5120 type Output = Float;
5121
5122 /// Divides two [`Float`]s, taking the first by reference and the second by value.
5123 ///
5124 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5125 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5126 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5127 /// `Nearest` rounding mode.
5128 ///
5129 /// $$
5130 /// f(x,y) = x/y+\varepsilon.
5131 /// $$
5132 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5133 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5134 /// where $p$ is the maximum precision of the inputs.
5135 ///
5136 /// Special cases:
5137 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
5138 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
5139 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
5140 /// - $f(x,0.0)=\infty$ if $x>0.0$
5141 /// - $f(x,0.0)=-\infty$ if $x<0.0$
5142 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
5143 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
5144 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
5145 /// - $f(x,-0.0)=\infty$ if $x<0.0$
5146 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
5147 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
5148 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5149 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5150 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
5151 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
5152 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5153 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5154 ///
5155 /// If you want to use a rounding mode other than `Nearest`, consider using
5156 /// [`Float::div_prec_ref_val`] instead. If you want to specify the output precision, consider
5157 /// using [`Float::div_round_ref_val`]. If you want both of these things, consider using
5158 /// [`Float::div_prec_round_ref_val`].
5159 ///
5160 /// # Worst-case complexity
5161 /// $T(n) = O(n \log n \log\log n)$
5162 ///
5163 /// $M(n) = O(n \log n)$
5164 ///
5165 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5166 /// other.significant_bits())`.
5167 ///
5168 /// # Examples
5169 /// ```
5170 /// use malachite_base::num::basic::traits::{
5171 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5172 /// };
5173 /// use malachite_float::Float;
5174 ///
5175 /// assert!((&Float::from(1.5) / Float::NAN).is_nan());
5176 /// assert_eq!(&Float::from(1.5) / Float::ZERO, Float::INFINITY);
5177 /// assert_eq!(
5178 /// &Float::from(1.5) / Float::NEGATIVE_ZERO,
5179 /// Float::NEGATIVE_INFINITY
5180 /// );
5181 /// assert_eq!(&Float::from(-1.5) / Float::ZERO, Float::NEGATIVE_INFINITY);
5182 /// assert_eq!(&Float::from(-1.5) / Float::NEGATIVE_ZERO, Float::INFINITY);
5183 /// assert!((&Float::ZERO / Float::ZERO).is_nan());
5184 ///
5185 /// assert_eq!((&Float::from(1.5) / Float::from(2.5)).to_string(), "0.6");
5186 /// assert_eq!((&Float::from(1.5) / Float::from(-2.5)).to_string(), "-0.6");
5187 /// assert_eq!((&Float::from(-1.5) / Float::from(2.5)).to_string(), "-0.6");
5188 /// assert_eq!((&Float::from(-1.5) / Float::from(-2.5)).to_string(), "0.6");
5189 /// ```
5190 #[inline]
5191 fn div(self, other: Float) -> Float {
5192 let prec = max(self.significant_bits(), other.significant_bits());
5193 self.div_prec_round_ref_val(other, prec, Nearest).0
5194 }
5195}
5196
5197impl Div<&Float> for &Float {
5198 type Output = Float;
5199
5200 /// Divides two [`Float`]s, taking both by reference.
5201 ///
5202 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5203 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5204 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5205 /// `Nearest` rounding mode.
5206 ///
5207 /// $$
5208 /// f(x,y) = x/y+\varepsilon.
5209 /// $$
5210 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5211 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5212 /// where $p$ is the maximum precision of the inputs.
5213 ///
5214 /// Special cases:
5215 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
5216 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
5217 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
5218 /// - $f(x,0.0)=\infty$ if $x>0.0$
5219 /// - $f(x,0.0)=-\infty$ if $x<0.0$
5220 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
5221 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
5222 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
5223 /// - $f(x,-0.0)=\infty$ if $x<0.0$
5224 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
5225 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
5226 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5227 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5228 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
5229 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
5230 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
5231 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
5232 ///
5233 /// If you want to use a rounding mode other than `Nearest`, consider using
5234 /// [`Float::div_prec_ref_ref`] instead. If you want to specify the output precision, consider
5235 /// using [`Float::div_round_ref_ref`]. If you want both of these things, consider using
5236 /// [`Float::div_prec_round_ref_ref`].
5237 ///
5238 /// # Worst-case complexity
5239 /// $T(n) = O(n \log n \log\log n)$
5240 ///
5241 /// $M(n) = O(n \log n)$
5242 ///
5243 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5244 /// other.significant_bits())`.
5245 ///
5246 /// # Examples
5247 /// ```
5248 /// use malachite_base::num::basic::traits::{
5249 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5250 /// };
5251 /// use malachite_float::Float;
5252 ///
5253 /// assert!((&Float::from(1.5) / &Float::NAN).is_nan());
5254 /// assert_eq!(&Float::from(1.5) / &Float::ZERO, Float::INFINITY);
5255 /// assert_eq!(
5256 /// &Float::from(1.5) / &Float::NEGATIVE_ZERO,
5257 /// Float::NEGATIVE_INFINITY
5258 /// );
5259 /// assert_eq!(&Float::from(-1.5) / &Float::ZERO, Float::NEGATIVE_INFINITY);
5260 /// assert_eq!(&Float::from(-1.5) / &Float::NEGATIVE_ZERO, Float::INFINITY);
5261 /// assert!((&Float::ZERO / &Float::ZERO).is_nan());
5262 ///
5263 /// assert_eq!((&Float::from(1.5) / &Float::from(2.5)).to_string(), "0.6");
5264 /// assert_eq!((&Float::from(1.5) / &Float::from(-2.5)).to_string(), "-0.6");
5265 /// assert_eq!((&Float::from(-1.5) / &Float::from(2.5)).to_string(), "-0.6");
5266 /// assert_eq!((&Float::from(-1.5) / &Float::from(-2.5)).to_string(), "0.6");
5267 /// ```
5268 #[inline]
5269 fn div(self, other: &Float) -> Float {
5270 let prec = max(self.significant_bits(), other.significant_bits());
5271 self.div_prec_round_ref_ref(other, prec, Nearest).0
5272 }
5273}
5274
5275impl DivAssign<Float> for Float {
5276 /// Divides a [`Float`] by a [`Float`] in place, taking the [`Float`] on the right-hand side by
5277 /// value.
5278 ///
5279 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5280 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5281 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5282 /// `Nearest` rounding mode.
5283 ///
5284 /// $$
5285 /// x\gets = x/y+\varepsilon.
5286 /// $$
5287 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5288 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5289 /// where $p$ is the maximum precision of the inputs.
5290 ///
5291 /// See the `/` documentation for information on special cases.
5292 ///
5293 /// If you want to use a rounding mode other than `Nearest`, consider using
5294 /// [`Float::div_prec_assign`] instead. If you want to specify the output precision, consider
5295 /// using [`Float::div_round_assign`]. If you want both of these things, consider using
5296 /// [`Float::div_prec_round_assign`].
5297 ///
5298 /// # Worst-case complexity
5299 /// $T(n) = O(n \log n \log\log n)$
5300 ///
5301 /// $M(n) = O(n \log n)$
5302 ///
5303 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5304 /// other.significant_bits())`.
5305 ///
5306 /// # Examples
5307 /// ```
5308 /// use malachite_base::num::basic::traits::{
5309 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5310 /// };
5311 /// use malachite_float::Float;
5312 ///
5313 /// let mut x = Float::from(1.5);
5314 /// x /= Float::NAN;
5315 /// assert!(x.is_nan());
5316 ///
5317 /// let mut x = Float::from(1.5);
5318 /// x /= Float::ZERO;
5319 /// assert_eq!(x, Float::INFINITY);
5320 ///
5321 /// let mut x = Float::from(1.5);
5322 /// x /= Float::NEGATIVE_ZERO;
5323 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5324 ///
5325 /// let mut x = Float::from(-1.5);
5326 /// x /= Float::ZERO;
5327 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5328 ///
5329 /// let mut x = Float::from(-1.5);
5330 /// x /= Float::NEGATIVE_ZERO;
5331 /// assert_eq!(x, Float::INFINITY);
5332 ///
5333 /// let mut x = Float::INFINITY;
5334 /// x /= Float::INFINITY;
5335 /// assert!(x.is_nan());
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 /// let mut x = Float::from(-1.5);
5346 /// x /= Float::from(2.5);
5347 /// assert_eq!(x.to_string(), "-0.6");
5348 ///
5349 /// let mut x = Float::from(-1.5);
5350 /// x /= Float::from(-2.5);
5351 /// assert_eq!(x.to_string(), "0.6");
5352 /// ```
5353 #[inline]
5354 fn div_assign(&mut self, other: Float) {
5355 let prec = max(self.significant_bits(), other.significant_bits());
5356 self.div_prec_round_assign(other, prec, Nearest);
5357 }
5358}
5359
5360impl DivAssign<&Float> for Float {
5361 /// Divides a [`Float`] by a [`Float`] in place, taking the [`Float`] on the right-hand side by
5362 /// reference.
5363 ///
5364 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
5365 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
5366 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
5367 /// `Nearest` rounding mode.
5368 ///
5369 /// $$
5370 /// x\gets = x/y+\varepsilon.
5371 /// $$
5372 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5373 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5374 /// where $p$ is the maximum precision of the inputs.
5375 ///
5376 /// See the `/` documentation for information on special cases.
5377 ///
5378 /// If you want to use a rounding mode other than `Nearest`, consider using
5379 /// [`Float::div_prec_assign`] instead. If you want to specify the output precision, consider
5380 /// using [`Float::div_round_assign`]. If you want both of these things, consider using
5381 /// [`Float::div_prec_round_assign`].
5382 ///
5383 /// # Worst-case complexity
5384 /// $T(n) = O(n \log n \log\log n)$
5385 ///
5386 /// $M(n) = O(n \log n)$
5387 ///
5388 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5389 /// other.significant_bits())`.
5390 ///
5391 /// # Examples
5392 /// ```
5393 /// use malachite_base::num::basic::traits::{
5394 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5395 /// };
5396 /// use malachite_float::Float;
5397 ///
5398 /// let mut x = Float::from(1.5);
5399 /// x /= &Float::NAN;
5400 /// assert!(x.is_nan());
5401 ///
5402 /// let mut x = Float::from(1.5);
5403 /// x /= &Float::ZERO;
5404 /// assert_eq!(x, Float::INFINITY);
5405 ///
5406 /// let mut x = Float::from(1.5);
5407 /// x /= &Float::NEGATIVE_ZERO;
5408 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5409 ///
5410 /// let mut x = Float::from(-1.5);
5411 /// x /= &Float::ZERO;
5412 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5413 ///
5414 /// let mut x = Float::from(-1.5);
5415 /// x /= &Float::NEGATIVE_ZERO;
5416 /// assert_eq!(x, Float::INFINITY);
5417 ///
5418 /// let mut x = Float::INFINITY;
5419 /// x /= &Float::INFINITY;
5420 /// assert!(x.is_nan());
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 /// let mut x = Float::from(-1.5);
5431 /// x /= &Float::from(2.5);
5432 /// assert_eq!(x.to_string(), "-0.6");
5433 ///
5434 /// let mut x = Float::from(-1.5);
5435 /// x /= &Float::from(-2.5);
5436 /// assert_eq!(x.to_string(), "0.6");
5437 /// ```
5438 #[inline]
5439 fn div_assign(&mut self, other: &Float) {
5440 let prec = max(self.significant_bits(), other.significant_bits());
5441 self.div_prec_round_assign_ref(other, prec, Nearest);
5442 }
5443}
5444
5445impl Div<Rational> for Float {
5446 type Output = Float;
5447
5448 /// Divides a [`Float`] by a [`Rational`], taking both by value.
5449 ///
5450 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
5451 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
5452 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
5453 /// rounding mode.
5454 ///
5455 /// $$
5456 /// f(x,y) = x/y+\varepsilon.
5457 /// $$
5458 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5459 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5460 /// where $p$ is the precision of the input [`Float`].
5461 ///
5462 /// Special cases:
5463 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
5464 /// - $f(\infty,x)=\infty$ if $x\geq 0$
5465 /// - $f(\infty,x)=-\infty$ if $x<0$
5466 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
5467 /// - $f(-\infty,x)=\infty$ if $x<0$
5468 /// - $f(0.0,x)=0.0$ if $x>0$
5469 /// - $f(0.0,x)=-0.0$ if $x<0$
5470 /// - $f(-0.0,x)=-0.0$ if $x>0$
5471 /// - $f(-0.0,x)=0.0$ if $x<0$
5472 ///
5473 /// If you want to use a rounding mode other than `Nearest`, consider using
5474 /// [`Float::div_rational_prec`] instead. If you want to specify the output precision, consider
5475 /// using [`Float::div_rational_round`]. If you want both of these things, consider using
5476 /// [`Float::div_rational_prec_round`].
5477 ///
5478 /// # Worst-case complexity
5479 /// $T(n) = O(n \log n \log\log n)$
5480 ///
5481 /// $M(n) = O(n \log n)$
5482 ///
5483 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5484 /// other.significant_bits())`.
5485 ///
5486 /// # Examples
5487 /// ```
5488 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
5489 /// use malachite_base::num::conversion::traits::ExactFrom;
5490 /// use malachite_float::Float;
5491 /// use malachite_q::Rational;
5492 ///
5493 /// assert!((Float::NAN / Rational::exact_from(1.5)).is_nan());
5494 /// assert_eq!(Float::INFINITY / Rational::exact_from(1.5), Float::INFINITY);
5495 /// assert_eq!(
5496 /// Float::NEGATIVE_INFINITY / Rational::exact_from(1.5),
5497 /// Float::NEGATIVE_INFINITY
5498 /// );
5499 /// assert_eq!(
5500 /// Float::INFINITY / Rational::exact_from(-1.5),
5501 /// Float::NEGATIVE_INFINITY
5502 /// );
5503 /// assert_eq!(
5504 /// Float::NEGATIVE_INFINITY / Rational::exact_from(-1.5),
5505 /// Float::INFINITY
5506 /// );
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 /// assert_eq!(
5517 /// (Float::from(-2.5) / Rational::exact_from(1.5)).to_string(),
5518 /// "-1.8"
5519 /// );
5520 /// assert_eq!(
5521 /// (Float::from(-2.5) / Rational::exact_from(-1.5)).to_string(),
5522 /// "1.8"
5523 /// );
5524 /// ```
5525 #[inline]
5526 fn div(self, other: Rational) -> Float {
5527 let prec = self.significant_bits();
5528 self.div_rational_prec_round(other, prec, Nearest).0
5529 }
5530}
5531
5532impl Div<&Rational> for Float {
5533 type Output = Float;
5534
5535 /// Divides a [`Float`] by a [`Rational`], taking the first by value and the second by
5536 /// reference.
5537 ///
5538 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
5539 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
5540 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
5541 /// rounding mode.
5542 ///
5543 /// $$
5544 /// f(x,y) = x/y+\varepsilon.
5545 /// $$
5546 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5547 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5548 /// where $p$ is the precision of the input [`Float`].
5549 ///
5550 /// Special cases:
5551 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
5552 /// - $f(\infty,x)=\infty$ if $x\geq 0$
5553 /// - $f(\infty,x)=-\infty$ if $x<0$
5554 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
5555 /// - $f(-\infty,x)=\infty$ if $x<0$
5556 /// - $f(0.0,x)=0.0$ if $x>0$
5557 /// - $f(0.0,x)=-0.0$ if $x<0$
5558 /// - $f(-0.0,x)=-0.0$ if $x>0$
5559 /// - $f(-0.0,x)=0.0$ if $x<0$
5560 ///
5561 /// If you want to use a rounding mode other than `Nearest`, consider using
5562 /// [`Float::div_rational_prec_val_ref`] instead. If you want to specify the output precision,
5563 /// consider using [`Float::div_rational_round_val_ref`]. If you want both of these things,
5564 /// consider using [`Float::div_rational_prec_round_val_ref`].
5565 ///
5566 /// # Worst-case complexity
5567 /// $T(n) = O(n \log n \log\log n)$
5568 ///
5569 /// $M(n) = O(n \log n)$
5570 ///
5571 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5572 /// other.significant_bits())`.
5573 ///
5574 /// # Examples
5575 /// ```
5576 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
5577 /// use malachite_base::num::conversion::traits::ExactFrom;
5578 /// use malachite_float::Float;
5579 /// use malachite_q::Rational;
5580 ///
5581 /// assert!((Float::NAN / &Rational::exact_from(1.5)).is_nan());
5582 /// assert_eq!(
5583 /// Float::INFINITY / &Rational::exact_from(1.5),
5584 /// Float::INFINITY
5585 /// );
5586 /// assert_eq!(
5587 /// Float::NEGATIVE_INFINITY / &Rational::exact_from(1.5),
5588 /// Float::NEGATIVE_INFINITY
5589 /// );
5590 /// assert_eq!(
5591 /// Float::INFINITY / &Rational::exact_from(-1.5),
5592 /// Float::NEGATIVE_INFINITY
5593 /// );
5594 /// assert_eq!(
5595 /// Float::NEGATIVE_INFINITY / &Rational::exact_from(-1.5),
5596 /// Float::INFINITY
5597 /// );
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 /// assert_eq!(
5608 /// (Float::from(-2.5) / &Rational::exact_from(1.5)).to_string(),
5609 /// "-1.8"
5610 /// );
5611 /// assert_eq!(
5612 /// (Float::from(-2.5) / &Rational::exact_from(-1.5)).to_string(),
5613 /// "1.8"
5614 /// );
5615 /// ```
5616 #[inline]
5617 fn div(self, other: &Rational) -> Float {
5618 let prec = self.significant_bits();
5619 self.div_rational_prec_round_val_ref(other, prec, Nearest).0
5620 }
5621}
5622
5623impl Div<Rational> for &Float {
5624 type Output = Float;
5625
5626 /// Divides a [`Float`] by a [`Rational`], taking the first by reference and the second by
5627 /// value.
5628 ///
5629 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
5630 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
5631 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
5632 /// rounding mode.
5633 ///
5634 /// $$
5635 /// f(x,y) = x/y+\varepsilon.
5636 /// $$
5637 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5638 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5639 /// where $p$ is the precision of the input [`Float`].
5640 ///
5641 /// Special cases:
5642 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
5643 /// - $f(\infty,x)=\infty$ if $x\geq 0$
5644 /// - $f(\infty,x)=-\infty$ if $x<0$
5645 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
5646 /// - $f(-\infty,x)=\infty$ if $x<0$
5647 /// - $f(0.0,x)=0.0$ if $x>0$
5648 /// - $f(0.0,x)=-0.0$ if $x<0$
5649 /// - $f(-0.0,x)=-0.0$ if $x>0$
5650 /// - $f(-0.0,x)=0.0$ if $x<0$
5651 ///
5652 /// If you want to use a rounding mode other than `Nearest`, consider using
5653 /// [`Float::div_rational_prec_ref_val`] instead. If you want to specify the output precision,
5654 /// consider using [`Float::div_rational_round_ref_val`]. If you want both of these things,
5655 /// consider using [`Float::div_rational_prec_round_ref_val`].
5656 ///
5657 /// # Worst-case complexity
5658 /// $T(n) = O(n \log n \log\log n)$
5659 ///
5660 /// $M(n) = O(n \log n)$
5661 ///
5662 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5663 /// other.significant_bits())`.
5664 ///
5665 /// # Examples
5666 /// ```
5667 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
5668 /// use malachite_base::num::conversion::traits::ExactFrom;
5669 /// use malachite_float::Float;
5670 /// use malachite_q::Rational;
5671 ///
5672 /// assert!((&Float::NAN / Rational::exact_from(1.5)).is_nan());
5673 /// assert_eq!(
5674 /// &Float::INFINITY / Rational::exact_from(1.5),
5675 /// Float::INFINITY
5676 /// );
5677 /// assert_eq!(
5678 /// &Float::NEGATIVE_INFINITY / Rational::exact_from(1.5),
5679 /// Float::NEGATIVE_INFINITY
5680 /// );
5681 /// assert_eq!(
5682 /// &Float::INFINITY / Rational::exact_from(-1.5),
5683 /// Float::NEGATIVE_INFINITY
5684 /// );
5685 /// assert_eq!(
5686 /// &Float::NEGATIVE_INFINITY / Rational::exact_from(-1.5),
5687 /// Float::INFINITY
5688 /// );
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 /// assert_eq!(
5699 /// (&Float::from(-2.5) / Rational::exact_from(1.5)).to_string(),
5700 /// "-1.8"
5701 /// );
5702 /// assert_eq!(
5703 /// (&Float::from(-2.5) / Rational::exact_from(-1.5)).to_string(),
5704 /// "1.8"
5705 /// );
5706 /// ```
5707 #[inline]
5708 fn div(self, other: Rational) -> Float {
5709 let prec = self.significant_bits();
5710 self.div_rational_prec_round_ref_val(other, prec, Nearest).0
5711 }
5712}
5713
5714impl Div<&Rational> for &Float {
5715 type Output = Float;
5716
5717 /// Divides a [`Float`] by a [`Rational`], taking both by reference.
5718 ///
5719 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
5720 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
5721 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
5722 /// rounding mode.
5723 ///
5724 /// $$
5725 /// f(x,y) = x/y+\varepsilon.
5726 /// $$
5727 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5728 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5729 /// where $p$ is the precision of the input [`Float`].
5730 ///
5731 /// Special cases:
5732 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
5733 /// - $f(\infty,x)=\infty$ if $x\geq 0$
5734 /// - $f(\infty,x)=-\infty$ if $x<0$
5735 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
5736 /// - $f(-\infty,x)=\infty$ if $x<0$
5737 /// - $f(0.0,x)=0.0$ if $x>0$
5738 /// - $f(0.0,x)=-0.0$ if $x<0$
5739 /// - $f(-0.0,x)=-0.0$ if $x>0$
5740 /// - $f(-0.0,x)=0.0$ if $x<0$
5741 ///
5742 /// If you want to use a rounding mode other than `Nearest`, consider using
5743 /// [`Float::div_rational_prec_ref_ref`] instead. If you want to specify the output precision,
5744 /// consider using [`Float::div_rational_round_ref_ref`]. If you want both of these things,
5745 /// consider using [`Float::div_rational_prec_round_ref_ref`].
5746 ///
5747 /// # Worst-case complexity
5748 /// $T(n) = O(n \log n \log\log n)$
5749 ///
5750 /// $M(n) = O(n \log n)$
5751 ///
5752 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5753 /// other.significant_bits())`.
5754 ///
5755 /// # Examples
5756 /// ```
5757 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
5758 /// use malachite_base::num::conversion::traits::ExactFrom;
5759 /// use malachite_float::Float;
5760 /// use malachite_q::Rational;
5761 ///
5762 /// assert!((&Float::NAN / &Rational::exact_from(1.5)).is_nan());
5763 /// assert_eq!(
5764 /// &Float::INFINITY / &Rational::exact_from(1.5),
5765 /// Float::INFINITY
5766 /// );
5767 /// assert_eq!(
5768 /// &Float::NEGATIVE_INFINITY / &Rational::exact_from(1.5),
5769 /// Float::NEGATIVE_INFINITY
5770 /// );
5771 /// assert_eq!(
5772 /// &Float::INFINITY / &Rational::exact_from(-1.5),
5773 /// Float::NEGATIVE_INFINITY
5774 /// );
5775 /// assert_eq!(
5776 /// &Float::NEGATIVE_INFINITY / &Rational::exact_from(-1.5),
5777 /// Float::INFINITY
5778 /// );
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 /// assert_eq!(
5789 /// (&Float::from(-2.5) / &Rational::exact_from(1.5)).to_string(),
5790 /// "-1.8"
5791 /// );
5792 /// assert_eq!(
5793 /// (&Float::from(-2.5) / &Rational::exact_from(-1.5)).to_string(),
5794 /// "1.8"
5795 /// );
5796 /// ```
5797 #[inline]
5798 fn div(self, other: &Rational) -> Float {
5799 let prec = self.significant_bits();
5800 self.div_rational_prec_round_ref_ref(other, prec, Nearest).0
5801 }
5802}
5803
5804impl DivAssign<Rational> for Float {
5805 /// Divides a [`Float`] by a [`Rational`] in place, taking the [`Rational`] by value.
5806 ///
5807 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
5808 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
5809 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
5810 /// rounding mode.
5811 ///
5812 /// $$
5813 /// x\gets = x/y+\varepsilon.
5814 /// $$
5815 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5816 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5817 /// where $p$ is the precision of the input [`Float`].
5818 ///
5819 /// See the `/` documentation for information on special cases.
5820 ///
5821 /// If you want to use a rounding mode other than `Nearest`, consider using
5822 /// [`Float::div_rational_prec_assign`] instead. If you want to specify the output precision,
5823 /// consider using [`Float::div_rational_round_assign`]. If you want both of these things,
5824 /// consider using [`Float::div_rational_prec_round_assign`].
5825 ///
5826 /// # Worst-case complexity
5827 /// $T(n) = O(n \log n \log\log n)$
5828 ///
5829 /// $M(n) = O(n \log n)$
5830 ///
5831 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5832 /// other.significant_bits())`.
5833 ///
5834 /// # Examples
5835 /// ```
5836 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
5837 /// use malachite_base::num::conversion::traits::ExactFrom;
5838 /// use malachite_float::Float;
5839 /// use malachite_q::Rational;
5840 ///
5841 /// let mut x = Float::NAN;
5842 /// x /= Rational::exact_from(1.5);
5843 /// assert!(x.is_nan());
5844 ///
5845 /// let mut x = Float::INFINITY;
5846 /// x /= Rational::exact_from(1.5);
5847 /// assert_eq!(x, Float::INFINITY);
5848 ///
5849 /// let mut x = Float::NEGATIVE_INFINITY;
5850 /// x /= Rational::exact_from(1.5);
5851 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5852 ///
5853 /// let mut x = Float::INFINITY;
5854 /// x /= Rational::exact_from(-1.5);
5855 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5856 ///
5857 /// let mut x = Float::NEGATIVE_INFINITY;
5858 /// x /= Rational::exact_from(-1.5);
5859 /// assert_eq!(x, Float::INFINITY);
5860 ///
5861 /// let mut x = Float::from(2.5);
5862 /// x /= Rational::exact_from(1.5);
5863 /// assert_eq!(x.to_string(), "1.8");
5864 /// ```
5865 #[inline]
5866 fn div_assign(&mut self, other: Rational) {
5867 let prec = self.significant_bits();
5868 self.div_rational_prec_round_assign(other, prec, Nearest);
5869 }
5870}
5871
5872impl DivAssign<&Rational> for Float {
5873 /// Divides a [`Float`] by a [`Rational`] in place, taking the [`Rational`] by reference.
5874 ///
5875 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
5876 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
5877 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
5878 /// rounding mode.
5879 ///
5880 /// $$
5881 /// x\gets = x/y+\varepsilon.
5882 /// $$
5883 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5884 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5885 /// where $p$ is the precision of the input [`Float`].
5886 ///
5887 /// See the `/` documentation for information on special cases.
5888 ///
5889 /// If you want to use a rounding mode other than `Nearest`, consider using
5890 /// [`Float::div_rational_prec_assign_ref`] instead. If you want to specify the output
5891 /// precision, consider using [`Float::div_rational_round_assign_ref`]. If you want both of
5892 /// these things, consider using [`Float::div_rational_prec_round_assign_ref`].
5893 ///
5894 /// # Worst-case complexity
5895 /// $T(n) = O(n \log n \log\log n)$
5896 ///
5897 /// $M(n) = O(n \log n)$
5898 ///
5899 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5900 /// other.significant_bits())`.
5901 ///
5902 /// # Examples
5903 /// ```
5904 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
5905 /// use malachite_base::num::conversion::traits::ExactFrom;
5906 /// use malachite_float::Float;
5907 /// use malachite_q::Rational;
5908 ///
5909 /// let mut x = Float::NAN;
5910 /// x /= &Rational::exact_from(1.5);
5911 /// assert!(x.is_nan());
5912 ///
5913 /// let mut x = Float::INFINITY;
5914 /// x /= &Rational::exact_from(1.5);
5915 /// assert_eq!(x, Float::INFINITY);
5916 ///
5917 /// let mut x = Float::NEGATIVE_INFINITY;
5918 /// x /= &Rational::exact_from(1.5);
5919 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5920 ///
5921 /// let mut x = Float::INFINITY;
5922 /// x /= &Rational::exact_from(-1.5);
5923 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
5924 ///
5925 /// let mut x = Float::NEGATIVE_INFINITY;
5926 /// x /= &Rational::exact_from(-1.5);
5927 /// assert_eq!(x, Float::INFINITY);
5928 ///
5929 /// let mut x = Float::from(2.5);
5930 /// x /= &Rational::exact_from(1.5);
5931 /// assert_eq!(x.to_string(), "1.8");
5932 /// ```
5933 #[inline]
5934 fn div_assign(&mut self, other: &Rational) {
5935 let prec = self.significant_bits();
5936 self.div_rational_prec_round_assign_ref(other, prec, Nearest);
5937 }
5938}
5939
5940impl Div<Float> for Rational {
5941 type Output = Float;
5942
5943 /// Divides a [`Rational`] by a [`Float`], taking both by value.
5944 ///
5945 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
5946 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
5947 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
5948 /// rounding mode.
5949 ///
5950 /// $$
5951 /// f(x,y) = x/y+\varepsilon.
5952 /// $$
5953 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5954 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
5955 /// where $p$ is the precision of the input [`Float`].
5956 ///
5957 /// Special cases:
5958 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
5959 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
5960 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
5961 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
5962 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
5963 /// - $f(0,x,p,m)=0.0$ if $x>0$
5964 /// - $f(0,x,p,m)=-0.0$ if $x<0$
5965 ///
5966 /// # Worst-case complexity
5967 /// $T(n) = O(n \log n \log\log n)$
5968 ///
5969 /// $M(n) = O(n \log n)$
5970 ///
5971 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
5972 /// other.significant_bits())`.
5973 ///
5974 /// # Examples
5975 /// ```
5976 /// use malachite_base::num::basic::traits::{
5977 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
5978 /// };
5979 /// use malachite_base::num::conversion::traits::ExactFrom;
5980 /// use malachite_float::Float;
5981 /// use malachite_q::Rational;
5982 ///
5983 /// assert!((Rational::exact_from(1.5) / Float::NAN).is_nan());
5984 /// assert_eq!(Rational::exact_from(1.5) / Float::ZERO, Float::INFINITY);
5985 /// assert_eq!(
5986 /// Rational::exact_from(1.5) / Float::NEGATIVE_ZERO,
5987 /// Float::NEGATIVE_INFINITY
5988 /// );
5989 /// assert_eq!(
5990 /// Rational::exact_from(-1.5) / Float::ZERO,
5991 /// Float::NEGATIVE_INFINITY
5992 /// );
5993 /// assert_eq!(
5994 /// Rational::exact_from(-1.5) / Float::NEGATIVE_ZERO,
5995 /// Float::INFINITY
5996 /// );
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 /// assert_eq!(
6007 /// (Rational::exact_from(1.5) / Float::from(-2.5)).to_string(),
6008 /// "-0.6"
6009 /// );
6010 /// assert_eq!(
6011 /// (Rational::exact_from(-1.5) / Float::from(-2.5)).to_string(),
6012 /// "0.6"
6013 /// );
6014 /// ```
6015 #[inline]
6016 fn div(self, other: Float) -> Float {
6017 let prec = other.significant_bits();
6018 Float::rational_div_float_prec_round(self, other, prec, Nearest).0
6019 }
6020}
6021
6022impl Div<&Float> for Rational {
6023 type Output = Float;
6024
6025 /// Divides a [`Rational`] by a [`Float`], taking the [`Rational`] by value and the [`Float`] by
6026 /// reference.
6027 ///
6028 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6029 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6030 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6031 /// rounding mode.
6032 ///
6033 /// $$
6034 /// f(x,y) = x/y+\varepsilon.
6035 /// $$
6036 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6037 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6038 /// where $p$ is the precision of the input [`Float`].
6039 ///
6040 /// Special cases:
6041 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
6042 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
6043 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
6044 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
6045 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
6046 /// - $f(0,x,p,m)=0.0$ if $x>0$
6047 /// - $f(0,x,p,m)=-0.0$ if $x<0$
6048 ///
6049 /// # Worst-case complexity
6050 /// $T(n) = O(n \log n \log\log n)$
6051 ///
6052 /// $M(n) = O(n \log n)$
6053 ///
6054 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6055 /// other.significant_bits())`.
6056 ///
6057 /// # Examples
6058 /// ```
6059 /// use malachite_base::num::basic::traits::{
6060 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6061 /// };
6062 /// use malachite_base::num::conversion::traits::ExactFrom;
6063 /// use malachite_float::Float;
6064 /// use malachite_q::Rational;
6065 ///
6066 /// assert!((Rational::exact_from(1.5) / &Float::NAN).is_nan());
6067 /// assert_eq!(Rational::exact_from(1.5) / &Float::ZERO, Float::INFINITY);
6068 /// assert_eq!(
6069 /// Rational::exact_from(1.5) / &Float::NEGATIVE_ZERO,
6070 /// Float::NEGATIVE_INFINITY
6071 /// );
6072 /// assert_eq!(
6073 /// Rational::exact_from(-1.5) / &Float::ZERO,
6074 /// Float::NEGATIVE_INFINITY
6075 /// );
6076 /// assert_eq!(
6077 /// Rational::exact_from(-1.5) / &Float::NEGATIVE_ZERO,
6078 /// Float::INFINITY
6079 /// );
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 /// assert_eq!(
6090 /// (Rational::exact_from(1.5) / &Float::from(-2.5)).to_string(),
6091 /// "-0.6"
6092 /// );
6093 /// assert_eq!(
6094 /// (Rational::exact_from(-1.5) / &Float::from(-2.5)).to_string(),
6095 /// "0.6"
6096 /// );
6097 /// ```
6098 #[inline]
6099 fn div(self, other: &Float) -> Float {
6100 let prec = other.significant_bits();
6101 Float::rational_div_float_prec_round_val_ref(self, other, prec, Nearest).0
6102 }
6103}
6104
6105impl Div<Float> for &Rational {
6106 type Output = Float;
6107
6108 /// Divides a [`Rational`] by a [`Float`], taking the [`Rational`] by reference and the
6109 /// [`Float`] by value.
6110 ///
6111 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6112 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6113 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6114 /// rounding mode.
6115 ///
6116 /// $$
6117 /// f(x,y) = x/y+\varepsilon.
6118 /// $$
6119 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6120 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6121 /// where $p$ is the precision of the input [`Float`].
6122 ///
6123 /// Special cases:
6124 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
6125 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
6126 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
6127 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
6128 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
6129 /// - $f(0,x,p,m)=0.0$ if $x>0$
6130 /// - $f(0,x,p,m)=-0.0$ if $x<0$
6131 ///
6132 /// # Worst-case complexity
6133 /// $T(n) = O(n \log n \log\log n)$
6134 ///
6135 /// $M(n) = O(n \log n)$
6136 ///
6137 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6138 /// other.significant_bits())`.
6139 ///
6140 /// # Examples
6141 /// ```
6142 /// use malachite_base::num::basic::traits::{
6143 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6144 /// };
6145 /// use malachite_base::num::conversion::traits::ExactFrom;
6146 /// use malachite_float::Float;
6147 /// use malachite_q::Rational;
6148 ///
6149 /// assert!((&Rational::exact_from(1.5) / Float::NAN).is_nan());
6150 /// assert_eq!(&Rational::exact_from(1.5) / Float::ZERO, Float::INFINITY);
6151 /// assert_eq!(
6152 /// &Rational::exact_from(1.5) / Float::NEGATIVE_ZERO,
6153 /// Float::NEGATIVE_INFINITY
6154 /// );
6155 /// assert_eq!(
6156 /// &Rational::exact_from(-1.5) / Float::ZERO,
6157 /// Float::NEGATIVE_INFINITY
6158 /// );
6159 /// assert_eq!(
6160 /// &Rational::exact_from(-1.5) / Float::NEGATIVE_ZERO,
6161 /// Float::INFINITY
6162 /// );
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 /// assert_eq!(
6173 /// (&Rational::exact_from(1.5) / Float::from(-2.5)).to_string(),
6174 /// "-0.6"
6175 /// );
6176 /// assert_eq!(
6177 /// (&Rational::exact_from(-1.5) / Float::from(-2.5)).to_string(),
6178 /// "0.6"
6179 /// );
6180 /// ```
6181 #[inline]
6182 fn div(self, other: Float) -> Float {
6183 let prec = other.significant_bits();
6184 Float::rational_div_float_prec_round_ref_val(self, other, prec, Nearest).0
6185 }
6186}
6187
6188impl Div<&Float> for &Rational {
6189 type Output = Float;
6190
6191 /// Divides a [`Rational`] by a [`Float`], taking both by reference.
6192 ///
6193 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6194 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6195 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6196 /// rounding mode.
6197 ///
6198 /// $$
6199 /// f(x,y) = x/y+\varepsilon.
6200 /// $$
6201 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6202 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6203 /// where $p$ is the precision of the input [`Float`].
6204 ///
6205 /// Special cases:
6206 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
6207 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
6208 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
6209 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
6210 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
6211 /// - $f(0,x,p,m)=0.0$ if $x>0$
6212 /// - $f(0,x,p,m)=-0.0$ if $x<0$
6213 ///
6214 /// # Worst-case complexity
6215 /// $T(n) = O(n \log n \log\log n)$
6216 ///
6217 /// $M(n) = O(n \log n)$
6218 ///
6219 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6220 /// other.significant_bits())`.
6221 ///
6222 /// # Examples
6223 /// ```
6224 /// use malachite_base::num::basic::traits::{
6225 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6226 /// };
6227 /// use malachite_base::num::conversion::traits::ExactFrom;
6228 /// use malachite_float::Float;
6229 /// use malachite_q::Rational;
6230 ///
6231 /// assert!((&Rational::exact_from(1.5) / &Float::NAN).is_nan());
6232 /// assert_eq!(&Rational::exact_from(1.5) / &Float::ZERO, Float::INFINITY);
6233 /// assert_eq!(
6234 /// &Rational::exact_from(1.5) / &Float::NEGATIVE_ZERO,
6235 /// Float::NEGATIVE_INFINITY
6236 /// );
6237 /// assert_eq!(
6238 /// &Rational::exact_from(-1.5) / &Float::ZERO,
6239 /// Float::NEGATIVE_INFINITY
6240 /// );
6241 /// assert_eq!(
6242 /// &Rational::exact_from(-1.5) / &Float::NEGATIVE_ZERO,
6243 /// Float::INFINITY
6244 /// );
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 /// assert_eq!(
6255 /// (&Rational::exact_from(1.5) / &Float::from(-2.5)).to_string(),
6256 /// "-0.6"
6257 /// );
6258 /// assert_eq!(
6259 /// (&Rational::exact_from(-1.5) / &Float::from(-2.5)).to_string(),
6260 /// "0.6"
6261 /// );
6262 /// ```
6263 #[inline]
6264 fn div(self, other: &Float) -> Float {
6265 let prec = other.significant_bits();
6266 Float::rational_div_float_prec_round_ref_ref(self, other, prec, Nearest).0
6267 }
6268}