malachite_float/arithmetic/div.rs
1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::InnerFloat::{Finite, Infinity, NaN, Zero};
10use crate::arithmetic::is_power_of_2::abs_is_power_of_2;
11use crate::conversion::from_natural::{from_natural_zero_exponent, from_natural_zero_exponent_ref};
12use crate::{
13 Float, float_either_infinity, float_either_zero, float_infinity, float_nan,
14 float_negative_infinity, float_negative_zero, float_zero,
15};
16use core::cmp::Ordering::{self, *};
17use core::cmp::max;
18use core::mem::swap;
19use core::ops::{Div, DivAssign};
20use malachite_base::num::arithmetic::traits::{
21 CheckedLogBase2, FloorLogBase2, IsPowerOf2, NegAssign, Sign,
22};
23use malachite_base::num::basic::traits::{
24 Infinity as InfinityTrait, NaN as NaNTrait, NegativeInfinity, NegativeZero, Zero as ZeroTrait,
25};
26use malachite_base::num::conversion::traits::ExactFrom;
27use malachite_base::num::logic::traits::{NotAssign, SignificantBits};
28use malachite_base::rounding_modes::RoundingMode::{self, *};
29use malachite_nz::natural::arithmetic::float_div::{
30 div_float_significands_in_place, div_float_significands_in_place_ref,
31 div_float_significands_ref_ref, div_float_significands_ref_val,
32};
33use malachite_q::Rational;
34
35const DIV_RATIONAL_THRESHOLD: u64 = 50;
36const RATIONAL_DIV_THRESHOLD: u64 = 50;
37
38fn div_rational_prec_round_assign_naive(
39 x: &mut Float,
40 y: Rational,
41 prec: u64,
42 rm: RoundingMode,
43) -> Ordering {
44 assert_ne!(prec, 0);
45 match (&mut *x, y) {
46 (float_nan!(), _) => Equal,
47 (Float(Infinity { sign }), y) => {
48 if y < 0 {
49 sign.not_assign();
50 };
51 Equal
52 }
53 (Float(Zero { sign }), y) => {
54 match y.sign() {
55 Equal => *x = float_nan!(),
56 Greater => {}
57 Less => sign.not_assign(),
58 }
59 Equal
60 }
61 (x, y) => {
62 if y == 0 {
63 *x = Float(Infinity { sign: *x > 0u32 });
64 Equal
65 } else {
66 let not_sign = *x < 0;
67 let mut z = Float::ZERO;
68 swap(x, &mut z);
69 let (mut quotient, o) =
70 Float::from_rational_prec_round(Rational::exact_from(z) / y, prec, rm);
71 if quotient == 0u32 && not_sign {
72 quotient.neg_assign();
73 }
74 *x = quotient;
75 o
76 }
77 }
78 }
79}
80
81fn div_rational_prec_round_assign_naive_ref(
82 x: &mut Float,
83 y: &Rational,
84 prec: u64,
85 rm: RoundingMode,
86) -> Ordering {
87 assert_ne!(prec, 0);
88 match (&mut *x, y) {
89 (float_nan!(), _) => Equal,
90 (Float(Infinity { sign }), y) => {
91 if *y < 0 {
92 sign.not_assign();
93 };
94 Equal
95 }
96 (Float(Zero { sign }), y) => {
97 match y.sign() {
98 Equal => *x = float_nan!(),
99 Greater => {}
100 Less => sign.not_assign(),
101 }
102 Equal
103 }
104 (x, y) => {
105 if *y == 0 {
106 *x = Float(Infinity { sign: *x > 0u32 });
107 Equal
108 } else {
109 let not_sign = *x < 0;
110 let mut z = Float::ZERO;
111 swap(x, &mut z);
112 let (mut quotient, o) =
113 Float::from_rational_prec_round(Rational::exact_from(z) / y, prec, rm);
114 if quotient == 0u32 && not_sign {
115 quotient.neg_assign();
116 }
117 *x = quotient;
118 o
119 }
120 }
121 }
122}
123
124pub_test! {div_rational_prec_round_naive(
125 mut x: Float,
126 y: Rational,
127 prec: u64,
128 rm: RoundingMode,
129) -> (Float, Ordering) {
130 let o = div_rational_prec_round_assign_naive(&mut x, y, prec, rm);
131 (x, o)
132}}
133
134pub_test! {div_rational_prec_round_naive_val_ref(
135 mut x: Float,
136 y: &Rational,
137 prec: u64,
138 rm: RoundingMode,
139) -> (Float, Ordering) {
140 let o = div_rational_prec_round_assign_naive_ref(&mut x, y, prec, rm);
141 (x, o)
142}}
143
144pub_test! {div_rational_prec_round_naive_ref_val(
145 x: &Float,
146 y: Rational,
147 prec: u64,
148 rm: RoundingMode,
149) -> (Float, Ordering) {
150 assert_ne!(prec, 0);
151 match (x, y) {
152 (float_nan!(), _) => (float_nan!(), Equal),
153 (Float(Infinity { sign }), y) => (
154 if y >= 0u32 {
155 Float(Infinity { sign: *sign })
156 } else {
157 Float(Infinity { sign: !*sign })
158 },
159 Equal,
160 ),
161 (Float(Zero { sign }), y) => (
162 match y.sign() {
163 Equal => float_nan!(),
164 Greater => Float(Zero { sign: *sign }),
165 Less => Float(Zero { sign: !*sign }),
166 },
167 Equal,
168 ),
169 (x, y) => {
170 if y == 0 {
171 (Float(Infinity { sign: *x > 0u32 }), Equal)
172 } else {
173 let (mut quotient, o) =
174 Float::from_rational_prec_round(Rational::exact_from(x) / y, prec, rm);
175 if quotient == 0u32 && *x < 0 {
176 quotient.neg_assign();
177 }
178 (quotient, o)
179 }
180 }
181 }
182}}
183
184pub_test! {div_rational_prec_round_naive_ref_ref(
185 x: &Float,
186 y: &Rational,
187 prec: u64,
188 rm: RoundingMode,
189) -> (Float, Ordering) {
190 assert_ne!(prec, 0);
191 match (x, y) {
192 (float_nan!(), _) => (float_nan!(), Equal),
193 (Float(Infinity { sign }), y) => (
194 if *y >= 0u32 {
195 Float(Infinity { sign: *sign })
196 } else {
197 Float(Infinity { sign: !*sign })
198 },
199 Equal,
200 ),
201 (Float(Zero { sign }), y) => (
202 match y.sign() {
203 Equal => float_nan!(),
204 Greater => Float(Zero { sign: *sign }),
205 Less => Float(Zero { sign: !*sign }),
206 },
207 Equal,
208 ),
209 (x, y) => {
210 if *y == 0 {
211 (Float(Infinity { sign: *x > 0u32 }), Equal)
212 } else {
213 let (mut quotient, o) =
214 Float::from_rational_prec_round(Rational::exact_from(x) / y, prec, rm);
215 if quotient == 0u32 && *x < 0 {
216 quotient.neg_assign();
217 }
218 (quotient, o)
219 }
220 }
221 }
222}}
223
224fn div_rational_prec_round_assign_direct(
225 x: &mut Float,
226 y: Rational,
227 prec: u64,
228 mut rm: RoundingMode,
229) -> Ordering {
230 assert_ne!(prec, 0);
231 if y == 0u32 {
232 *x = match (*x).partial_cmp(&0u32) {
233 Some(Greater) => Float::INFINITY,
234 Some(Less) => Float::NEGATIVE_INFINITY,
235 _ => Float::NAN,
236 };
237 return Equal;
238 }
239 let sign = y >= 0;
240 let (n, d) = y.into_numerator_and_denominator();
241 if !sign {
242 rm.neg_assign();
243 }
244 let o = match (n.checked_log_base_2(), d.checked_log_base_2()) {
245 (Some(log_n), Some(log_d)) => {
246 x.shl_prec_round_assign(i128::from(log_d) - i128::from(log_n), prec, rm)
247 }
248 (None, Some(log_d)) => {
249 let x_exp = x.get_exponent().unwrap();
250 let n_exp = n.floor_log_base_2();
251 *x >>= x_exp;
252 let o = x.div_prec_round_assign(from_natural_zero_exponent(n), prec, rm);
253 x.shl_prec_round_assign_helper(
254 i128::from(x_exp) - i128::from(n_exp) + i128::from(log_d) - 1,
255 prec,
256 rm,
257 o,
258 )
259 }
260 (Some(log_n), None) => {
261 let x_exp = x.get_exponent().unwrap();
262 let d_exp = d.floor_log_base_2();
263 *x >>= x_exp;
264 let o = x.mul_prec_round_assign(from_natural_zero_exponent(d), prec, rm);
265 x.shl_prec_round_assign_helper(
266 i128::from(x_exp) - i128::from(log_n) + i128::from(d_exp) + 1,
267 prec,
268 rm,
269 o,
270 )
271 }
272 (None, None) => {
273 let x_exp = x.get_exponent().unwrap();
274 let n_exp = n.floor_log_base_2();
275 let d_exp = d.floor_log_base_2();
276 let n = from_natural_zero_exponent(n);
277 let d = from_natural_zero_exponent(d);
278 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
279 *x >>= x_exp;
280 x.mul_prec_round_assign(d, mul_prec, Floor);
281 let o = x.div_prec_round_assign(n, prec, rm);
282 x.shl_prec_round_assign_helper(
283 i128::from(x_exp) - i128::from(n_exp) + i128::from(d_exp),
284 prec,
285 rm,
286 o,
287 )
288 }
289 };
290 if sign {
291 o
292 } else {
293 x.neg_assign();
294 o.reverse()
295 }
296}
297
298fn div_rational_prec_round_assign_direct_ref(
299 x: &mut Float,
300 y: &Rational,
301 prec: u64,
302 mut rm: RoundingMode,
303) -> Ordering {
304 assert_ne!(prec, 0);
305 if *y == 0u32 {
306 *x = match (*x).partial_cmp(&0u32) {
307 Some(Greater) => Float::INFINITY,
308 Some(Less) => Float::NEGATIVE_INFINITY,
309 _ => Float::NAN,
310 };
311 return Equal;
312 }
313 let sign = *y >= 0;
314 let (n, d) = y.numerator_and_denominator_ref();
315 if !sign {
316 rm.neg_assign();
317 }
318 let o = match (n.checked_log_base_2(), d.checked_log_base_2()) {
319 (Some(log_n), Some(log_d)) => {
320 x.shl_prec_round_assign(i128::from(log_d) - i128::from(log_n), prec, rm)
321 }
322 (None, Some(log_d)) => {
323 let x_exp = x.get_exponent().unwrap();
324 let n_exp = n.floor_log_base_2();
325 *x >>= x_exp;
326 let o = x.div_prec_round_assign(from_natural_zero_exponent_ref(n), prec, rm);
327 x.shl_prec_round_assign_helper(
328 i128::from(x_exp) - i128::from(n_exp) + i128::from(log_d) - 1,
329 prec,
330 rm,
331 o,
332 )
333 }
334 (Some(log_n), None) => {
335 let x_exp = x.get_exponent().unwrap();
336 let d_exp = d.floor_log_base_2();
337 *x >>= x_exp;
338 let o = x.mul_prec_round_assign(from_natural_zero_exponent_ref(d), prec, rm);
339 x.shl_prec_round_assign_helper(
340 i128::from(x_exp) - i128::from(log_n) + i128::from(d_exp) + 1,
341 prec,
342 rm,
343 o,
344 )
345 }
346 (None, None) => {
347 let x_exp = x.get_exponent().unwrap();
348 let n_exp = n.floor_log_base_2();
349 let d_exp = d.floor_log_base_2();
350 let n = from_natural_zero_exponent_ref(n);
351 let d = from_natural_zero_exponent_ref(d);
352 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
353 *x >>= x_exp;
354 x.mul_prec_round_assign(d, mul_prec, Floor);
355 let o = x.div_prec_round_assign(n, prec, rm);
356 x.shl_prec_round_assign_helper(
357 i128::from(x_exp) - i128::from(n_exp) + i128::from(d_exp),
358 prec,
359 rm,
360 o,
361 )
362 }
363 };
364 if sign {
365 o
366 } else {
367 x.neg_assign();
368 o.reverse()
369 }
370}
371
372pub_test! {div_rational_prec_round_direct(
373 mut x: Float,
374 y: Rational,
375 prec: u64,
376 rm: RoundingMode,
377) -> (Float, Ordering) {
378 let o = div_rational_prec_round_assign_direct(&mut x, y, prec, rm);
379 (x, o)
380}}
381
382pub_test! {div_rational_prec_round_direct_val_ref(
383 mut x: Float,
384 y: &Rational,
385 prec: u64,
386 rm: RoundingMode,
387) -> (Float, Ordering) {
388 let o = div_rational_prec_round_assign_direct_ref(&mut x, y, prec, rm);
389 (x, o)
390}}
391
392pub_test! {div_rational_prec_round_direct_ref_val(
393 x: &Float,
394 y: Rational,
395 prec: u64,
396 mut rm: RoundingMode,
397) -> (Float, Ordering) {
398 assert_ne!(prec, 0);
399 let sign = y >= 0;
400 if y == 0u32 {
401 return (
402 match x.partial_cmp(&0u32) {
403 Some(Greater) => Float::INFINITY,
404 Some(Less) => Float::NEGATIVE_INFINITY,
405 _ => Float::NAN,
406 },
407 Equal,
408 );
409 }
410 let (n, d) = y.into_numerator_and_denominator();
411 if !sign {
412 rm.neg_assign();
413 }
414 let (quotient, o) = match (n.checked_log_base_2(), d.checked_log_base_2()) {
415 (Some(log_n), Some(log_d)) => {
416 x.shl_prec_round_ref(i128::from(log_d) - i128::from(log_n), prec, rm)
417 }
418 (None, Some(log_d)) => {
419 let x_exp = x.get_exponent().unwrap();
420 let n_exp = n.floor_log_base_2();
421 let mut x = x >> x_exp;
422 let o = x.div_prec_round_assign(from_natural_zero_exponent(n), prec, rm);
423 let o = x.shl_prec_round_assign_helper(
424 i128::from(x_exp) - i128::from(n_exp) + i128::from(log_d) - 1,
425 prec,
426 rm,
427 o,
428 );
429 (x, o)
430 }
431 (Some(log_n), None) => {
432 let x_exp = x.get_exponent().unwrap();
433 let d_exp = d.floor_log_base_2();
434 let mut x = x >> x_exp;
435 let o = x.mul_prec_round_assign(from_natural_zero_exponent(d), prec, rm);
436 let o = x.shl_prec_round_assign_helper(
437 i128::from(x_exp) - i128::from(log_n) + i128::from(d_exp) + 1,
438 prec,
439 rm,
440 o,
441 );
442 (x, o)
443 }
444 (None, None) => {
445 let x_exp = x.get_exponent().unwrap();
446 let n_exp = n.floor_log_base_2();
447 let d_exp = d.floor_log_base_2();
448 let n = from_natural_zero_exponent(n);
449 let d = from_natural_zero_exponent(d);
450 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
451 let mut x = x >> x_exp;
452 x.mul_prec_round_assign(d, mul_prec, Floor);
453 let o = x.div_prec_round_assign(n, prec, rm);
454 let o = x.shl_prec_round_assign_helper(
455 i128::from(x_exp) - i128::from(n_exp) + i128::from(d_exp),
456 prec,
457 rm,
458 o,
459 );
460 (x, o)
461 }
462 };
463 if sign {
464 (quotient, o)
465 } else {
466 (-quotient, o.reverse())
467 }
468}}
469
470pub_test! {div_rational_prec_round_direct_ref_ref(
471 x: &Float,
472 y: &Rational,
473 prec: u64,
474 mut rm: RoundingMode,
475) -> (Float, Ordering) {
476 assert_ne!(prec, 0);
477 if *y == 0u32 {
478 return (
479 match x.partial_cmp(&0u32) {
480 Some(Greater) => Float::INFINITY,
481 Some(Less) => Float::NEGATIVE_INFINITY,
482 _ => Float::NAN,
483 },
484 Equal,
485 );
486 }
487 let sign = *y >= 0;
488 let (n, d) = y.numerator_and_denominator_ref();
489 if !sign {
490 rm.neg_assign();
491 }
492 let (quotient, o) = match (n.checked_log_base_2(), d.checked_log_base_2()) {
493 (Some(log_n), Some(log_d)) => {
494 x.shl_prec_round_ref(i128::from(log_d) - i128::from(log_n), prec, rm)
495 }
496 (None, Some(log_d)) => {
497 let x_exp = x.get_exponent().unwrap();
498 let n_exp = n.floor_log_base_2();
499 let mut x = x >> x_exp;
500 let o = x.div_prec_round_assign(from_natural_zero_exponent_ref(n), prec, rm);
501 let o = x.shl_prec_round_assign_helper(
502 i128::from(x_exp) - i128::from(n_exp) + i128::from(log_d) - 1,
503 prec,
504 rm,
505 o,
506 );
507 (x, o)
508 }
509 (Some(log_n), None) => {
510 let x_exp = x.get_exponent().unwrap();
511 let d_exp = d.floor_log_base_2();
512 let mut x = x >> x_exp;
513 let o = x.mul_prec_round_assign(from_natural_zero_exponent_ref(d), prec, rm);
514 let o = x.shl_prec_round_assign_helper(
515 i128::from(x_exp) - i128::from(log_n) + i128::from(d_exp) + 1,
516 prec,
517 rm,
518 o,
519 );
520 (x, o)
521 }
522 (None, None) => {
523 let x_exp = x.get_exponent().unwrap();
524 let n_exp = n.floor_log_base_2();
525 let d_exp = d.floor_log_base_2();
526 let n = from_natural_zero_exponent_ref(n);
527 let d = from_natural_zero_exponent_ref(d);
528 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
529 let mut x = x >> x_exp;
530 x.mul_prec_round_assign(d, mul_prec, Floor);
531 let o = x.div_prec_round_assign(n, prec, rm);
532 let o = x.shl_prec_round_assign_helper(
533 i128::from(x_exp) - i128::from(n_exp) + i128::from(d_exp),
534 prec,
535 rm,
536 o,
537 );
538 (x, o)
539 }
540 };
541 if sign {
542 (quotient, o)
543 } else {
544 (-quotient, o.reverse())
545 }
546}}
547
548pub_test! {rational_div_float_prec_round_naive(
549 x: Rational,
550 y: Float,
551 prec: u64,
552 rm: RoundingMode,
553) -> (Float, Ordering) {
554 assert_ne!(prec, 0);
555 match (x, y) {
556 (_, float_nan!()) => (float_nan!(), Equal),
557 (x, Float(Infinity { sign })) => (
558 if x >= 0u32 {
559 Float(Zero { sign })
560 } else {
561 Float(Zero { sign: !sign })
562 },
563 Equal,
564 ),
565 (x, Float(Zero { sign })) => (
566 match x.sign() {
567 Equal => float_nan!(),
568 Greater => Float(Infinity { sign }),
569 Less => Float(Infinity { sign: !sign }),
570 },
571 Equal,
572 ),
573 (x, y) => {
574 let not_sign = y < 0;
575 let (mut quotient, o) =
576 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
577 if quotient == 0u32 && not_sign {
578 quotient.neg_assign();
579 }
580 (quotient, o)
581 }
582 }
583}}
584
585pub_test! {rational_div_float_prec_round_naive_val_ref(
586 x: Rational,
587 y: &Float,
588 prec: u64,
589 rm: RoundingMode,
590) -> (Float, Ordering) {
591 assert_ne!(prec, 0);
592 match (x, y) {
593 (_, float_nan!()) => (float_nan!(), Equal),
594 (x, Float(Infinity { sign })) => (
595 if x >= 0u32 {
596 Float(Zero { sign: *sign })
597 } else {
598 Float(Zero { sign: !*sign })
599 },
600 Equal,
601 ),
602 (x, Float(Zero { sign })) => (
603 match x.sign() {
604 Equal => float_nan!(),
605 Greater => Float(Infinity { sign: *sign }),
606 Less => Float(Infinity { sign: !*sign }),
607 },
608 Equal,
609 ),
610 (x, y) => {
611 let (mut quotient, o) =
612 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
613 if quotient == 0u32 && *y < 0 {
614 quotient.neg_assign();
615 }
616 (quotient, o)
617 }
618 }
619}}
620
621pub_test! {rational_div_float_prec_round_naive_ref_val(
622 x: &Rational,
623 y: Float,
624 prec: u64,
625 rm: RoundingMode,
626) -> (Float, Ordering) {
627 assert_ne!(prec, 0);
628 match (x, y) {
629 (_, float_nan!()) => (float_nan!(), Equal),
630 (x, Float(Infinity { sign })) => (
631 if *x >= 0u32 {
632 Float(Zero { sign })
633 } else {
634 Float(Zero { sign: !sign })
635 },
636 Equal,
637 ),
638 (x, Float(Zero { sign })) => (
639 match x.sign() {
640 Equal => float_nan!(),
641 Greater => Float(Infinity { sign }),
642 Less => Float(Infinity { sign: !sign }),
643 },
644 Equal,
645 ),
646 (x, y) => {
647 let not_sign = y < 0;
648 let (mut quotient, o) =
649 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
650 if quotient == 0u32 && not_sign {
651 quotient.neg_assign();
652 }
653 (quotient, o)
654 }
655 }
656}}
657
658pub_test! {rational_div_float_prec_round_naive_ref_ref(
659 x: &Rational,
660 y: &Float,
661 prec: u64,
662 rm: RoundingMode,
663) -> (Float, Ordering) {
664 assert_ne!(prec, 0);
665 match (x, y) {
666 (_, float_nan!()) => (float_nan!(), Equal),
667 (x, Float(Infinity { sign })) => (
668 if *x >= 0u32 {
669 Float(Zero { sign: *sign })
670 } else {
671 Float(Zero { sign: !*sign })
672 },
673 Equal,
674 ),
675 (x, Float(Zero { sign })) => (
676 match x.sign() {
677 Equal => float_nan!(),
678 Greater => Float(Infinity { sign: *sign }),
679 Less => Float(Infinity { sign: !*sign }),
680 },
681 Equal,
682 ),
683 (x, y) => {
684 let (mut quotient, o) =
685 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
686 if quotient == 0u32 && *y < 0 {
687 quotient.neg_assign();
688 }
689 (quotient, o)
690 }
691 }
692}}
693
694pub_test! {rational_div_float_prec_round_direct(
695 x: Rational,
696 y: Float,
697 prec: u64,
698 mut rm: RoundingMode,
699) -> (Float, Ordering) {
700 assert_ne!(prec, 0);
701 if x == 0u32 {
702 return (
703 if y > 0u32 {
704 Float::ZERO
705 } else {
706 Float::NEGATIVE_ZERO
707 },
708 Equal,
709 );
710 }
711 let sign = x >= 0;
712 let (n, d) = x.into_numerator_and_denominator();
713 if !sign {
714 rm.neg_assign();
715 }
716 let (quotient, o) = match (n.checked_log_base_2(), d.checked_log_base_2()) {
717 (Some(log_n), Some(log_d)) => {
718 let y_exp = y.get_exponent().unwrap();
719 let (mut quotient, o) = (y >> y_exp).reciprocal_prec_round(prec, rm);
720 let o = quotient.shl_prec_round_assign_helper(
721 i128::from(log_n) - i128::from(log_d) - i128::from(y_exp),
722 prec,
723 rm,
724 o,
725 );
726 (quotient, o)
727 }
728 (None, Some(log_d)) => {
729 let y_exp = y.get_exponent().unwrap();
730 let n_exp = n.floor_log_base_2();
731 let mut quotient = from_natural_zero_exponent(n);
732 let o = quotient.div_prec_round_assign(y >> y_exp, prec, rm);
733 let o = quotient.shl_prec_round_assign_helper(
734 i128::from(n_exp) - i128::from(log_d) - i128::from(y_exp) + 1,
735 prec,
736 rm,
737 o,
738 );
739 (quotient, o)
740 }
741 (Some(log_n), None) => {
742 let y_exp = y.get_exponent().unwrap();
743 let d_exp = d.floor_log_base_2();
744 let mut y = y >> y_exp;
745 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
746 y.mul_prec_round_assign(from_natural_zero_exponent(d), mul_prec, Floor);
747 let (mut quotient, o) = y.reciprocal_prec_round(prec, rm);
748 let o = quotient.shl_prec_round_assign_helper(
749 i128::from(log_n) - i128::from(d_exp) - i128::from(y_exp) - 1,
750 prec,
751 rm,
752 o,
753 );
754 (quotient, o)
755 }
756 (None, None) => {
757 let y_exp = y.get_exponent().unwrap();
758 let n_exp = n.floor_log_base_2();
759 let d_exp = d.floor_log_base_2();
760 let mut quotient = from_natural_zero_exponent(n);
761 let d = from_natural_zero_exponent(d);
762 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
763 let mut y = y >> y_exp;
764 y.mul_prec_round_assign(d, mul_prec, Floor);
765 let o = quotient.div_prec_round_assign(y, prec, rm);
766 let o = quotient.shl_prec_round_assign_helper(
767 -i128::from(y_exp) + i128::from(n_exp) - i128::from(d_exp),
768 prec,
769 rm,
770 o,
771 );
772 (quotient, o)
773 }
774 };
775 if sign {
776 (quotient, o)
777 } else {
778 (-quotient, o.reverse())
779 }
780}}
781
782pub_test! {rational_div_float_prec_round_direct_val_ref(
783 x: Rational,
784 y: &Float,
785 prec: u64,
786 mut rm: RoundingMode,
787) -> (Float, Ordering) {
788 assert_ne!(prec, 0);
789 if x == 0u32 {
790 return (
791 if *y > 0u32 {
792 Float::ZERO
793 } else {
794 Float::NEGATIVE_ZERO
795 },
796 Equal,
797 );
798 }
799 let sign = x >= 0;
800 let (n, d) = x.into_numerator_and_denominator();
801 if !sign {
802 rm.neg_assign();
803 }
804 let (quotient, o) = match (n.checked_log_base_2(), d.checked_log_base_2()) {
805 (Some(log_n), Some(log_d)) => {
806 let y_exp = y.get_exponent().unwrap();
807 let (mut quotient, o) = (y >> y_exp).reciprocal_prec_round(prec, rm);
808 let o = quotient.shl_prec_round_assign_helper(
809 i128::from(log_n) - i128::from(log_d) - i128::from(y_exp),
810 prec,
811 rm,
812 o,
813 );
814 (quotient, o)
815 }
816 (None, Some(log_d)) => {
817 let y_exp = y.get_exponent().unwrap();
818 let n_exp = n.floor_log_base_2();
819 let mut quotient = from_natural_zero_exponent(n);
820 let o = quotient.div_prec_round_assign(y >> y_exp, prec, rm);
821 let o = quotient.shl_prec_round_assign_helper(
822 i128::from(n_exp) - i128::from(log_d) - i128::from(y_exp) + 1,
823 prec,
824 rm,
825 o,
826 );
827 (quotient, o)
828 }
829 (Some(log_n), None) => {
830 let y_exp = y.get_exponent().unwrap();
831 let d_exp = d.floor_log_base_2();
832 let mut y = y >> y_exp;
833 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
834 y.mul_prec_round_assign(from_natural_zero_exponent(d), mul_prec, Floor);
835 let (mut quotient, o) = y.reciprocal_prec_round(prec, rm);
836 let o = quotient.shl_prec_round_assign_helper(
837 i128::from(log_n) - i128::from(d_exp) - i128::from(y_exp) - 1,
838 prec,
839 rm,
840 o,
841 );
842 (quotient, o)
843 }
844 (None, None) => {
845 let y_exp = y.get_exponent().unwrap();
846 let n_exp = n.floor_log_base_2();
847 let d_exp = d.floor_log_base_2();
848 let mut quotient = from_natural_zero_exponent(n);
849 let d = from_natural_zero_exponent(d);
850 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
851 let mut y = y >> y_exp;
852 y.mul_prec_round_assign(d, mul_prec, Floor);
853 let o = quotient.div_prec_round_assign(y, prec, rm);
854 let o = quotient.shl_prec_round_assign_helper(
855 -i128::from(y_exp) + i128::from(n_exp) - i128::from(d_exp),
856 prec,
857 rm,
858 o,
859 );
860 (quotient, o)
861 }
862 };
863 if sign {
864 (quotient, o)
865 } else {
866 (-quotient, o.reverse())
867 }
868}}
869
870pub_test! {rational_div_float_prec_round_direct_ref_val(
871 x: &Rational,
872 y: Float,
873 prec: u64,
874 mut rm: RoundingMode,
875) -> (Float, Ordering) {
876 assert_ne!(prec, 0);
877 if *x == 0u32 {
878 return (
879 if y > 0u32 {
880 Float::ZERO
881 } else {
882 Float::NEGATIVE_ZERO
883 },
884 Equal,
885 );
886 }
887 let sign = *x >= 0;
888 let (n, d) = x.numerator_and_denominator_ref();
889 if !sign {
890 rm.neg_assign();
891 }
892 let (quotient, o) = match (n.checked_log_base_2(), d.checked_log_base_2()) {
893 (Some(log_n), Some(log_d)) => {
894 let y_exp = y.get_exponent().unwrap();
895 let (mut quotient, o) = (y >> y_exp).reciprocal_prec_round(prec, rm);
896 let o = quotient.shl_prec_round_assign_helper(
897 i128::from(log_n) - i128::from(log_d) - i128::from(y_exp),
898 prec,
899 rm,
900 o,
901 );
902 (quotient, o)
903 }
904 (None, Some(log_d)) => {
905 let y_exp = y.get_exponent().unwrap();
906 let n_exp = n.floor_log_base_2();
907 let mut quotient = from_natural_zero_exponent_ref(n);
908 let o = quotient.div_prec_round_assign(y >> y_exp, prec, rm);
909 let o = quotient.shl_prec_round_assign_helper(
910 i128::from(n_exp) - i128::from(log_d) - i128::from(y_exp) + 1,
911 prec,
912 rm,
913 o,
914 );
915 (quotient, o)
916 }
917 (Some(log_n), None) => {
918 let y_exp = y.get_exponent().unwrap();
919 let d_exp = d.floor_log_base_2();
920 let mut y = y >> y_exp;
921 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
922 y.mul_prec_round_assign(from_natural_zero_exponent_ref(d), mul_prec, Floor);
923 let (mut quotient, o) = y.reciprocal_prec_round(prec, rm);
924 let o = quotient.shl_prec_round_assign_helper(
925 i128::from(log_n) - i128::from(d_exp) - i128::from(y_exp) - 1,
926 prec,
927 rm,
928 o,
929 );
930 (quotient, o)
931 }
932 (None, None) => {
933 let y_exp = y.get_exponent().unwrap();
934 let n_exp = n.floor_log_base_2();
935 let d_exp = d.floor_log_base_2();
936 let mut quotient = from_natural_zero_exponent_ref(n);
937 let d = from_natural_zero_exponent_ref(d);
938 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
939 let mut y = y >> y_exp;
940 y.mul_prec_round_assign(d, mul_prec, Floor);
941 let o = quotient.div_prec_round_assign(y, prec, rm);
942 let o = quotient.shl_prec_round_assign_helper(
943 -i128::from(y_exp) + i128::from(n_exp) - i128::from(d_exp),
944 prec,
945 rm,
946 o,
947 );
948 (quotient, o)
949 }
950 };
951 if sign {
952 (quotient, o)
953 } else {
954 (-quotient, o.reverse())
955 }
956}}
957
958pub_test! {rational_div_float_prec_round_direct_ref_ref(
959 x: &Rational,
960 y: &Float,
961 prec: u64,
962 mut rm: RoundingMode,
963) -> (Float, Ordering) {
964 assert_ne!(prec, 0);
965 if *x == 0u32 {
966 return (
967 if *y > 0u32 {
968 Float::ZERO
969 } else {
970 Float::NEGATIVE_ZERO
971 },
972 Equal,
973 );
974 }
975 let sign = *x >= 0;
976 let (n, d) = x.numerator_and_denominator_ref();
977 if !sign {
978 rm.neg_assign();
979 }
980 let (quotient, o) = match (n.checked_log_base_2(), d.checked_log_base_2()) {
981 (Some(log_n), Some(log_d)) => {
982 let y_exp = y.get_exponent().unwrap();
983 let (mut quotient, o) = (y >> y_exp).reciprocal_prec_round(prec, rm);
984 let o = quotient.shl_prec_round_assign_helper(
985 i128::from(log_n) - i128::from(log_d) - i128::from(y_exp),
986 prec,
987 rm,
988 o,
989 );
990 (quotient, o)
991 }
992 (None, Some(log_d)) => {
993 let y_exp = y.get_exponent().unwrap();
994 let n_exp = n.floor_log_base_2();
995 let mut quotient = from_natural_zero_exponent_ref(n);
996 let o = quotient.div_prec_round_assign(y >> y_exp, prec, rm);
997 let o = quotient.shl_prec_round_assign_helper(
998 i128::from(n_exp) - i128::from(log_d) - i128::from(y_exp) + 1,
999 prec,
1000 rm,
1001 o,
1002 );
1003 (quotient, o)
1004 }
1005 (Some(log_n), None) => {
1006 let y_exp = y.get_exponent().unwrap();
1007 let d_exp = d.floor_log_base_2();
1008 let mut y = y >> y_exp;
1009 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
1010 y.mul_prec_round_assign(from_natural_zero_exponent_ref(d), mul_prec, Floor);
1011 let (mut quotient, o) = y.reciprocal_prec_round(prec, rm);
1012 let o = quotient.shl_prec_round_assign_helper(
1013 i128::from(log_n) - i128::from(d_exp) - i128::from(y_exp) - 1,
1014 prec,
1015 rm,
1016 o,
1017 );
1018 (quotient, o)
1019 }
1020 (None, None) => {
1021 let y_exp = y.get_exponent().unwrap();
1022 let n_exp = n.floor_log_base_2();
1023 let d_exp = d.floor_log_base_2();
1024 let mut quotient = from_natural_zero_exponent_ref(n);
1025 let d = from_natural_zero_exponent_ref(d);
1026 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
1027 let mut y = y >> y_exp;
1028 y.mul_prec_round_assign(d, mul_prec, Floor);
1029 let o = quotient.div_prec_round_assign(y, prec, rm);
1030 let o = quotient.shl_prec_round_assign_helper(
1031 -i128::from(y_exp) + i128::from(n_exp) - i128::from(d_exp),
1032 prec,
1033 rm,
1034 o,
1035 );
1036 (quotient, o)
1037 }
1038 };
1039 if sign {
1040 (quotient, o)
1041 } else {
1042 (-quotient, o.reverse())
1043 }
1044}}
1045
1046impl Float {
1047 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
1048 /// specified rounding mode. Both [`Float`]s are taken by value. An [`Ordering`] is also
1049 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
1050 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
1051 /// function returns a `NaN` it also returns `Equal`.
1052 ///
1053 /// See [`RoundingMode`] for a description of the possible rounding modes.
1054 ///
1055 /// $$
1056 /// f(x,y,p,m) = x/y+\varepsilon.
1057 /// $$
1058 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1059 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1060 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
1061 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
1062 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1063 ///
1064 /// If the output has a precision, it is `prec`.
1065 ///
1066 /// Special cases:
1067 /// - $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) =
1068 /// \text{NaN}$
1069 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
1070 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
1071 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
1072 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
1073 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
1074 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
1075 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
1076 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
1077 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
1078 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1079 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1080 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1081 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1082 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $x<0.0$
1083 /// - $f(x,-\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1084 /// - $f(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1085 ///
1086 /// Overflow and underflow:
1087 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1088 /// returned instead.
1089 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
1090 /// is returned instead, where `p` is the precision of the input.
1091 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
1092 /// returned instead.
1093 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
1094 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
1095 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1096 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1097 /// instead.
1098 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1099 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1100 /// instead.
1101 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
1102 /// instead.
1103 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
1104 /// instead.
1105 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1106 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
1107 /// returned instead.
1108 ///
1109 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec`] instead. If you
1110 /// know that your target precision is the maximum of the precisions of the two inputs, consider
1111 /// using [`Float::div_round`] instead. If both of these things are true, consider using `/`
1112 /// instead.
1113 ///
1114 /// # Worst-case complexity
1115 /// $T(n) = O(n \log n \log\log n)$
1116 ///
1117 /// $M(n) = O(n \log n)$
1118 ///
1119 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1120 /// other.significant_bits(), `prec`)`.
1121 ///
1122 /// # Panics
1123 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
1124 ///
1125 /// # Examples
1126 /// ```
1127 /// use core::f64::consts::{E, PI};
1128 /// use malachite_base::rounding_modes::RoundingMode::*;
1129 /// use malachite_float::Float;
1130 /// use std::cmp::Ordering::*;
1131 ///
1132 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 5, Floor);
1133 /// assert_eq!(quotient.to_string(), "1.12");
1134 /// assert_eq!(o, Less);
1135 ///
1136 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 5, Ceiling);
1137 /// assert_eq!(quotient.to_string(), "1.19");
1138 /// assert_eq!(o, Greater);
1139 ///
1140 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 5, Nearest);
1141 /// assert_eq!(quotient.to_string(), "1.12");
1142 /// assert_eq!(o, Less);
1143 ///
1144 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 20, Floor);
1145 /// assert_eq!(quotient.to_string(), "1.155725");
1146 /// assert_eq!(o, Less);
1147 ///
1148 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 20, Ceiling);
1149 /// assert_eq!(quotient.to_string(), "1.155727");
1150 /// assert_eq!(o, Greater);
1151 ///
1152 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 20, Nearest);
1153 /// assert_eq!(quotient.to_string(), "1.155727");
1154 /// assert_eq!(o, Greater);
1155 /// ```
1156 #[inline]
1157 pub fn div_prec_round(mut self, other: Self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
1158 let o = self.div_prec_round_assign(other, prec, rm);
1159 (self, o)
1160 }
1161
1162 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
1163 /// specified rounding mode. The first [`Float`] is are taken by value and the second by
1164 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
1165 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
1166 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1167 ///
1168 /// See [`RoundingMode`] for a description of the possible rounding modes.
1169 ///
1170 /// $$
1171 /// f(x,y,p,m) = x/y+\varepsilon.
1172 /// $$
1173 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1174 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1175 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
1176 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
1177 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1178 ///
1179 /// If the output has a precision, it is `prec`.
1180 ///
1181 /// Special cases:
1182 /// - $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) =
1183 /// \text{NaN}$
1184 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
1185 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
1186 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
1187 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
1188 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
1189 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
1190 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
1191 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
1192 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
1193 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1194 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1195 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1196 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1197 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $x<0.0$
1198 /// - $f(x,-\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1199 /// - $f(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1200 ///
1201 /// Overflow and underflow:
1202 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1203 /// returned instead.
1204 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
1205 /// is returned instead, where `p` is the precision of the input.
1206 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
1207 /// returned instead.
1208 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
1209 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
1210 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1211 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1212 /// instead.
1213 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1214 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1215 /// instead.
1216 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
1217 /// instead.
1218 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
1219 /// instead.
1220 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1221 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
1222 /// returned instead.
1223 ///
1224 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_val_ref`] instead.
1225 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1226 /// consider using [`Float::div_round_val_ref`] instead. If both of these things are true,
1227 /// consider using `/` instead.
1228 ///
1229 /// # Worst-case complexity
1230 /// $T(n) = O(n \log n \log\log n)$
1231 ///
1232 /// $M(n) = O(n \log n)$
1233 ///
1234 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1235 /// other.significant_bits(), `prec`)`.
1236 ///
1237 /// # Panics
1238 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
1239 ///
1240 /// # Examples
1241 /// ```
1242 /// use core::f64::consts::{E, PI};
1243 /// use malachite_base::rounding_modes::RoundingMode::*;
1244 /// use malachite_float::Float;
1245 /// use std::cmp::Ordering::*;
1246 ///
1247 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 5, Floor);
1248 /// assert_eq!(quotient.to_string(), "1.12");
1249 /// assert_eq!(o, Less);
1250 ///
1251 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 5, Ceiling);
1252 /// assert_eq!(quotient.to_string(), "1.19");
1253 /// assert_eq!(o, Greater);
1254 ///
1255 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 5, Nearest);
1256 /// assert_eq!(quotient.to_string(), "1.12");
1257 /// assert_eq!(o, Less);
1258 ///
1259 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 20, Floor);
1260 /// assert_eq!(quotient.to_string(), "1.155725");
1261 /// assert_eq!(o, Less);
1262 ///
1263 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 20, Ceiling);
1264 /// assert_eq!(quotient.to_string(), "1.155727");
1265 /// assert_eq!(o, Greater);
1266 ///
1267 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 20, Nearest);
1268 /// assert_eq!(quotient.to_string(), "1.155727");
1269 /// assert_eq!(o, Greater);
1270 /// ```
1271 #[inline]
1272 pub fn div_prec_round_val_ref(
1273 mut self,
1274 other: &Self,
1275 prec: u64,
1276 rm: RoundingMode,
1277 ) -> (Self, Ordering) {
1278 let o = self.div_prec_round_assign_ref(other, prec, rm);
1279 (self, o)
1280 }
1281
1282 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
1283 /// specified rounding mode. The first [`Float`] is are taken by reference and the second by
1284 /// value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
1285 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
1286 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1287 ///
1288 /// See [`RoundingMode`] for a description of the possible rounding modes.
1289 ///
1290 /// $$
1291 /// f(x,y,p,m) = x/y+\varepsilon.
1292 /// $$
1293 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1294 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1295 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
1296 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
1297 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1298 ///
1299 /// If the output has a precision, it is `prec`.
1300 ///
1301 /// Special cases:
1302 /// - $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) =
1303 /// \text{NaN}$
1304 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
1305 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
1306 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
1307 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
1308 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
1309 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
1310 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
1311 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
1312 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
1313 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1314 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1315 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1316 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1317 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $x<0.0$
1318 /// - $f(x,-\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1319 /// - $f(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1320 ///
1321 /// Overflow and underflow:
1322 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1323 /// returned instead.
1324 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
1325 /// is returned instead, where `p` is the precision of the input.
1326 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
1327 /// returned instead.
1328 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
1329 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
1330 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1331 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1332 /// instead.
1333 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1334 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1335 /// instead.
1336 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
1337 /// instead.
1338 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
1339 /// instead.
1340 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1341 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
1342 /// returned instead.
1343 ///
1344 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_ref_val`] instead.
1345 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1346 /// consider using [`Float::div_round_ref_val`] instead. If both of these things are true,
1347 /// consider using `/` instead.
1348 ///
1349 /// # Worst-case complexity
1350 /// $T(n) = O(n \log n \log\log n)$
1351 ///
1352 /// $M(n) = O(n \log n)$
1353 ///
1354 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1355 /// other.significant_bits(), `prec`)`.
1356 ///
1357 /// # Panics
1358 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
1359 ///
1360 /// # Examples
1361 /// ```
1362 /// use core::f64::consts::{E, PI};
1363 /// use malachite_base::rounding_modes::RoundingMode::*;
1364 /// use malachite_float::Float;
1365 /// use std::cmp::Ordering::*;
1366 ///
1367 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 5, Floor);
1368 /// assert_eq!(quotient.to_string(), "1.12");
1369 /// assert_eq!(o, Less);
1370 ///
1371 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 5, Ceiling);
1372 /// assert_eq!(quotient.to_string(), "1.19");
1373 /// assert_eq!(o, Greater);
1374 ///
1375 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 5, Nearest);
1376 /// assert_eq!(quotient.to_string(), "1.12");
1377 /// assert_eq!(o, Less);
1378 ///
1379 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 20, Floor);
1380 /// assert_eq!(quotient.to_string(), "1.155725");
1381 /// assert_eq!(o, Less);
1382 ///
1383 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 20, Ceiling);
1384 /// assert_eq!(quotient.to_string(), "1.155727");
1385 /// assert_eq!(o, Greater);
1386 ///
1387 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 20, Nearest);
1388 /// assert_eq!(quotient.to_string(), "1.155727");
1389 /// assert_eq!(o, Greater);
1390 /// ```
1391 #[inline]
1392 pub fn div_prec_round_ref_val(
1393 &self,
1394 other: Self,
1395 prec: u64,
1396 rm: RoundingMode,
1397 ) -> (Self, Ordering) {
1398 assert_ne!(prec, 0);
1399 match (self, other) {
1400 (float_nan!(), _)
1401 | (_, float_nan!())
1402 | (float_either_infinity!(), float_either_infinity!())
1403 | (float_either_zero!(), float_either_zero!()) => (float_nan!(), Equal),
1404 (
1405 Self(Infinity { sign: x_sign }),
1406 Self(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
1407 )
1408 | (Self(Finite { sign: x_sign, .. }), Self(Zero { sign: y_sign })) => (
1409 Self(Infinity {
1410 sign: *x_sign == y_sign,
1411 }),
1412 Equal,
1413 ),
1414 (
1415 Self(Zero { sign: x_sign }),
1416 Self(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
1417 )
1418 | (Self(Finite { sign: x_sign, .. }), Self(Infinity { sign: y_sign })) => (
1419 Self(Zero {
1420 sign: *x_sign == y_sign,
1421 }),
1422 Equal,
1423 ),
1424 (
1425 Self(Finite {
1426 sign: x_sign,
1427 exponent: x_exp,
1428 precision: x_prec,
1429 significand: x,
1430 }),
1431 Self(Finite {
1432 sign: y_sign,
1433 exponent: y_exp,
1434 precision: y_prec,
1435 significand: mut y,
1436 }),
1437 ) => {
1438 if y.is_power_of_2() {
1439 let (mut quotient, mut o) =
1440 self.shr_prec_round_ref(y_exp - 1, prec, if y_sign { rm } else { -rm });
1441 if !y_sign {
1442 quotient.neg_assign();
1443 o = o.reverse();
1444 }
1445 return (quotient, o);
1446 }
1447 let sign = *x_sign == y_sign;
1448 let exp_diff = *x_exp - y_exp;
1449 if exp_diff > Self::MAX_EXPONENT {
1450 return match (sign, rm) {
1451 (_, Exact) => panic!("Inexact Float division"),
1452 (true, Ceiling | Up | Nearest) => (float_infinity!(), Greater),
1453 (true, _) => (Self::max_finite_value_with_prec(prec), Less),
1454 (false, Floor | Up | Nearest) => (float_negative_infinity!(), Less),
1455 (false, _) => (-Self::max_finite_value_with_prec(prec), Greater),
1456 };
1457 } else if exp_diff + 2 < Self::MIN_EXPONENT {
1458 return match (sign, rm) {
1459 (_, Exact) => panic!("Inexact Float division"),
1460 (true, Ceiling | Up) => (Self::min_positive_value_prec(prec), Greater),
1461 (true, _) => (float_zero!(), Less),
1462 (false, Floor | Up) => (-Self::min_positive_value_prec(prec), Less),
1463 (false, _) => (float_negative_zero!(), Greater),
1464 };
1465 }
1466 let (quotient, exp_offset, o) = div_float_significands_ref_val(
1467 x,
1468 *x_prec,
1469 &mut y,
1470 y_prec,
1471 prec,
1472 if sign { rm } else { -rm },
1473 );
1474 let exp = exp_diff.checked_add(i32::exact_from(exp_offset)).unwrap();
1475 if exp > Self::MAX_EXPONENT {
1476 return match (sign, rm) {
1477 (_, Exact) => panic!("Inexact Float division"),
1478 (true, Ceiling | Up | Nearest) => (float_infinity!(), Greater),
1479 (true, _) => (Self::max_finite_value_with_prec(prec), Less),
1480 (false, Floor | Up | Nearest) => (float_negative_infinity!(), Less),
1481 (false, _) => (-Self::max_finite_value_with_prec(prec), Greater),
1482 };
1483 } else if exp < Self::MIN_EXPONENT {
1484 return if rm == Nearest
1485 && exp == Self::MIN_EXPONENT - 1
1486 && (o == Less || !quotient.is_power_of_2())
1487 {
1488 if sign {
1489 (Self::min_positive_value_prec(prec), Greater)
1490 } else {
1491 (-Self::min_positive_value_prec(prec), Less)
1492 }
1493 } else {
1494 match (sign, rm) {
1495 (_, Exact) => panic!("Inexact Float division"),
1496 (true, Ceiling | Up) => (Self::min_positive_value_prec(prec), Greater),
1497 (true, _) => (float_zero!(), Less),
1498 (false, Floor | Up) => (-Self::min_positive_value_prec(prec), Less),
1499 (false, _) => (float_negative_zero!(), Greater),
1500 }
1501 };
1502 }
1503 (
1504 Self(Finite {
1505 sign,
1506 exponent: exp,
1507 precision: prec,
1508 significand: quotient,
1509 }),
1510 if sign { o } else { o.reverse() },
1511 )
1512 }
1513 }
1514 }
1515
1516 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
1517 /// specified rounding mode. Both [`Float`]s are taken by reference. An [`Ordering`] is also
1518 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
1519 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
1520 /// function returns a `NaN` it also returns `Equal`.
1521 ///
1522 /// See [`RoundingMode`] for a description of the possible rounding modes.
1523 ///
1524 /// $$
1525 /// f(x,y,p,m) = x/y+\varepsilon.
1526 /// $$
1527 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1528 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1529 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
1530 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
1531 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1532 ///
1533 /// If the output has a precision, it is `prec`.
1534 ///
1535 /// Special cases:
1536 /// - $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) =
1537 /// \text{NaN}$
1538 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
1539 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
1540 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
1541 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
1542 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
1543 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
1544 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
1545 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
1546 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
1547 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1548 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1549 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1550 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1551 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $x<0.0$
1552 /// - $f(x,-\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1553 /// - $f(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1554 ///
1555 /// Overflow and underflow:
1556 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1557 /// returned instead.
1558 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
1559 /// is returned instead, where `p` is the precision of the input.
1560 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
1561 /// returned instead.
1562 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
1563 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
1564 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1565 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1566 /// instead.
1567 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1568 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1569 /// instead.
1570 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
1571 /// instead.
1572 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
1573 /// instead.
1574 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1575 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
1576 /// returned instead.
1577 ///
1578 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_ref_ref`] instead.
1579 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1580 /// consider using [`Float::div_round_ref_ref`] instead. If both of these things are true,
1581 /// consider using `/` instead.
1582 ///
1583 /// # Worst-case complexity
1584 /// $T(n) = O(n \log n \log\log n)$
1585 ///
1586 /// $M(n) = O(n \log n)$
1587 ///
1588 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1589 /// other.significant_bits(), `prec`)`.
1590 ///
1591 /// # Panics
1592 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
1593 ///
1594 /// # Examples
1595 /// ```
1596 /// use core::f64::consts::{E, PI};
1597 /// use malachite_base::rounding_modes::RoundingMode::*;
1598 /// use malachite_float::Float;
1599 /// use std::cmp::Ordering::*;
1600 ///
1601 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 5, Floor);
1602 /// assert_eq!(quotient.to_string(), "1.12");
1603 /// assert_eq!(o, Less);
1604 ///
1605 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 5, Ceiling);
1606 /// assert_eq!(quotient.to_string(), "1.19");
1607 /// assert_eq!(o, Greater);
1608 ///
1609 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 5, Nearest);
1610 /// assert_eq!(quotient.to_string(), "1.12");
1611 /// assert_eq!(o, Less);
1612 ///
1613 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 20, Floor);
1614 /// assert_eq!(quotient.to_string(), "1.155725");
1615 /// assert_eq!(o, Less);
1616 ///
1617 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 20, Ceiling);
1618 /// assert_eq!(quotient.to_string(), "1.155727");
1619 /// assert_eq!(o, Greater);
1620 ///
1621 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 20, Nearest);
1622 /// assert_eq!(quotient.to_string(), "1.155727");
1623 /// assert_eq!(o, Greater);
1624 /// ```
1625 #[inline]
1626 pub fn div_prec_round_ref_ref(
1627 &self,
1628 other: &Self,
1629 prec: u64,
1630 rm: RoundingMode,
1631 ) -> (Self, Ordering) {
1632 assert_ne!(prec, 0);
1633 match (self, other) {
1634 (float_nan!(), _)
1635 | (_, float_nan!())
1636 | (float_either_infinity!(), float_either_infinity!())
1637 | (float_either_zero!(), float_either_zero!()) => (float_nan!(), Equal),
1638 (
1639 Self(Infinity { sign: x_sign }),
1640 Self(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
1641 )
1642 | (Self(Finite { sign: x_sign, .. }), Self(Zero { sign: y_sign })) => (
1643 Self(Infinity {
1644 sign: x_sign == y_sign,
1645 }),
1646 Equal,
1647 ),
1648 (
1649 Self(Zero { sign: x_sign }),
1650 Self(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
1651 )
1652 | (Self(Finite { sign: x_sign, .. }), Self(Infinity { sign: y_sign })) => (
1653 Self(Zero {
1654 sign: x_sign == y_sign,
1655 }),
1656 Equal,
1657 ),
1658 (
1659 Self(Finite {
1660 sign: x_sign,
1661 exponent: x_exp,
1662 precision: x_prec,
1663 significand: x,
1664 }),
1665 Self(Finite {
1666 sign: y_sign,
1667 exponent: y_exp,
1668 precision: y_prec,
1669 significand: y,
1670 }),
1671 ) => {
1672 if y.is_power_of_2() {
1673 let (mut quotient, mut o) =
1674 self.shr_prec_round_ref(y_exp - 1, prec, if *y_sign { rm } else { -rm });
1675 if !*y_sign {
1676 quotient.neg_assign();
1677 o = o.reverse();
1678 }
1679 return (quotient, o);
1680 }
1681 let sign = x_sign == y_sign;
1682 let exp_diff = *x_exp - y_exp;
1683 if exp_diff > Self::MAX_EXPONENT {
1684 return match (sign, rm) {
1685 (_, Exact) => panic!("Inexact Float division"),
1686 (true, Ceiling | Up | Nearest) => (float_infinity!(), Greater),
1687 (true, _) => (Self::max_finite_value_with_prec(prec), Less),
1688 (false, Floor | Up | Nearest) => (float_negative_infinity!(), Less),
1689 (false, _) => (-Self::max_finite_value_with_prec(prec), Greater),
1690 };
1691 } else if exp_diff + 2 < Self::MIN_EXPONENT {
1692 return match (sign, rm) {
1693 (_, Exact) => panic!("Inexact Float division"),
1694 (true, Ceiling | Up) => (Self::min_positive_value_prec(prec), Greater),
1695 (true, _) => (float_zero!(), Less),
1696 (false, Floor | Up) => (-Self::min_positive_value_prec(prec), Less),
1697 (false, _) => (float_negative_zero!(), Greater),
1698 };
1699 }
1700 let (quotient, exp_offset, o) = div_float_significands_ref_ref(
1701 x,
1702 *x_prec,
1703 y,
1704 *y_prec,
1705 prec,
1706 if sign { rm } else { -rm },
1707 );
1708 let exp = exp_diff.checked_add(i32::exact_from(exp_offset)).unwrap();
1709 if exp > Self::MAX_EXPONENT {
1710 return match (sign, rm) {
1711 (_, Exact) => panic!("Inexact Float division"),
1712 (true, Ceiling | Up | Nearest) => (float_infinity!(), Greater),
1713 (true, _) => (Self::max_finite_value_with_prec(prec), Less),
1714 (false, Floor | Up | Nearest) => (float_negative_infinity!(), Less),
1715 (false, _) => (-Self::max_finite_value_with_prec(prec), Greater),
1716 };
1717 } else if exp < Self::MIN_EXPONENT {
1718 return if rm == Nearest
1719 && exp == Self::MIN_EXPONENT - 1
1720 && (o == Less || !quotient.is_power_of_2())
1721 {
1722 if sign {
1723 (Self::min_positive_value_prec(prec), Greater)
1724 } else {
1725 (-Self::min_positive_value_prec(prec), Less)
1726 }
1727 } else {
1728 match (sign, rm) {
1729 (_, Exact) => panic!("Inexact Float division"),
1730 (true, Ceiling | Up) => (Self::min_positive_value_prec(prec), Greater),
1731 (true, _) => (float_zero!(), Less),
1732 (false, Floor | Up) => (-Self::min_positive_value_prec(prec), Less),
1733 (false, _) => (float_negative_zero!(), Greater),
1734 }
1735 };
1736 }
1737 (
1738 Self(Finite {
1739 sign,
1740 exponent: exp,
1741 precision: prec,
1742 significand: quotient,
1743 }),
1744 if sign { o } else { o.reverse() },
1745 )
1746 }
1747 }
1748 }
1749
1750 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1751 /// Both [`Float`]s are taken by value. An [`Ordering`] is also returned, indicating whether the
1752 /// rounded quotient is less than, equal to, or greater than the exact quotient. Although `NaN`s
1753 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
1754 /// `Equal`.
1755 ///
1756 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1757 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1758 /// description of the `Nearest` rounding mode.
1759 ///
1760 /// $$
1761 /// f(x,y,p) = x/y+\varepsilon.
1762 /// $$
1763 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1764 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1765 ///
1766 /// If the output has a precision, it is `prec`.
1767 ///
1768 /// Special cases:
1769 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1770 /// \text{NaN}$
1771 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
1772 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
1773 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
1774 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
1775 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
1776 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
1777 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
1778 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
1779 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
1780 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
1781 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1782 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1783 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
1784 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
1785 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1786 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1787 ///
1788 /// Overflow and underflow:
1789 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
1790 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
1791 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1792 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1793 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1794 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1795 ///
1796 /// If you want to use a rounding mode other than `Nearest`, consider using
1797 /// [`Float::div_prec_round`] instead. If you know that your target precision is the maximum of
1798 /// the precisions of the two inputs, consider using `/` instead.
1799 ///
1800 /// # Worst-case complexity
1801 /// $T(n) = O(n \log n \log\log n)$
1802 ///
1803 /// $M(n) = O(n \log n)$
1804 ///
1805 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1806 /// other.significant_bits(), `prec`)`.
1807 ///
1808 /// # Examples
1809 /// ```
1810 /// use core::f64::consts::{E, PI};
1811 /// use malachite_float::Float;
1812 /// use std::cmp::Ordering::*;
1813 ///
1814 /// let (quotient, o) = Float::from(PI).div_prec(Float::from(E), 5);
1815 /// assert_eq!(quotient.to_string(), "1.12");
1816 /// assert_eq!(o, Less);
1817 ///
1818 /// let (quotient, o) = Float::from(PI).div_prec(Float::from(E), 20);
1819 /// assert_eq!(quotient.to_string(), "1.155727");
1820 /// assert_eq!(o, Greater);
1821 /// ```
1822 #[inline]
1823 pub fn div_prec(self, other: Self, prec: u64) -> (Self, Ordering) {
1824 self.div_prec_round(other, prec, Nearest)
1825 }
1826
1827 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1828 /// The first [`Float`] is taken by value and the second by reference. An [`Ordering`] is also
1829 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
1830 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
1831 /// function returns a `NaN` it also returns `Equal`.
1832 ///
1833 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1834 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1835 /// description of the `Nearest` rounding mode.
1836 ///
1837 /// $$
1838 /// f(x,y,p) = 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, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1842 ///
1843 /// If the output has a precision, it is `prec`.
1844 ///
1845 /// Special cases:
1846 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1847 /// \text{NaN}$
1848 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
1849 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
1850 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
1851 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
1852 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
1853 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
1854 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
1855 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
1856 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
1857 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
1858 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1859 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1860 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
1861 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
1862 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1863 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1864 ///
1865 /// Overflow and underflow:
1866 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
1867 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
1868 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1869 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1870 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1871 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1872 ///
1873 /// If you want to use a rounding mode other than `Nearest`, consider using
1874 /// [`Float::div_prec_round_val_ref`] instead. If you know that your target precision is the
1875 /// maximum of the precisions of the two inputs, consider using `/` instead.
1876 ///
1877 /// # Worst-case complexity
1878 /// $T(n) = O(n \log n \log\log n)$
1879 ///
1880 /// $M(n) = O(n \log n)$
1881 ///
1882 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1883 /// other.significant_bits(), `prec`)`.
1884 ///
1885 /// # Examples
1886 /// ```
1887 /// use core::f64::consts::{E, PI};
1888 /// use malachite_float::Float;
1889 /// use std::cmp::Ordering::*;
1890 ///
1891 /// let (quotient, o) = Float::from(PI).div_prec_val_ref(&Float::from(E), 5);
1892 /// assert_eq!(quotient.to_string(), "1.12");
1893 /// assert_eq!(o, Less);
1894 ///
1895 /// let (quotient, o) = Float::from(PI).div_prec_val_ref(&Float::from(E), 20);
1896 /// assert_eq!(quotient.to_string(), "1.155727");
1897 /// assert_eq!(o, Greater);
1898 /// ```
1899 #[inline]
1900 pub fn div_prec_val_ref(self, other: &Self, prec: u64) -> (Self, Ordering) {
1901 self.div_prec_round_val_ref(other, prec, Nearest)
1902 }
1903
1904 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1905 /// The first [`Float`] is taken by reference and the second by value. An [`Ordering`] is also
1906 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
1907 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
1908 /// function returns a `NaN` it also returns `Equal`.
1909 ///
1910 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1911 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1912 /// description of the `Nearest` rounding mode.
1913 ///
1914 /// $$
1915 /// f(x,y,p) = x/y+\varepsilon.
1916 /// $$
1917 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1918 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1919 ///
1920 /// If the output has a precision, it is `prec`.
1921 ///
1922 /// Special cases:
1923 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1924 /// \text{NaN}$
1925 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
1926 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
1927 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
1928 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
1929 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
1930 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
1931 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
1932 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
1933 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
1934 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
1935 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1936 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1937 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
1938 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
1939 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1940 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1941 ///
1942 /// Overflow and underflow:
1943 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
1944 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
1945 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1946 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1947 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1948 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1949 ///
1950 /// If you want to use a rounding mode other than `Nearest`, consider using
1951 /// [`Float::div_prec_round_ref_val`] instead. If you know that your target precision is the
1952 /// maximum of the precisions of the two inputs, consider using `/` instead.
1953 ///
1954 /// # Worst-case complexity
1955 /// $T(n) = O(n \log n \log\log n)$
1956 ///
1957 /// $M(n) = O(n \log n)$
1958 ///
1959 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1960 /// other.significant_bits(), `prec`)`.
1961 ///
1962 /// # Examples
1963 /// ```
1964 /// use core::f64::consts::{E, PI};
1965 /// use malachite_float::Float;
1966 /// use std::cmp::Ordering::*;
1967 ///
1968 /// let (quotient, o) = Float::from(PI).div_prec_ref_val(Float::from(E), 5);
1969 /// assert_eq!(quotient.to_string(), "1.12");
1970 /// assert_eq!(o, Less);
1971 ///
1972 /// let (quotient, o) = Float::from(PI).div_prec_ref_val(Float::from(E), 20);
1973 /// assert_eq!(quotient.to_string(), "1.155727");
1974 /// assert_eq!(o, Greater);
1975 /// ```
1976 #[inline]
1977 pub fn div_prec_ref_val(&self, other: Self, prec: u64) -> (Self, Ordering) {
1978 self.div_prec_round_ref_val(other, prec, Nearest)
1979 }
1980
1981 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1982 /// Both [`Float`]s are taken by reference. An [`Ordering`] is also returned, indicating whether
1983 /// the rounded quotient is less than, equal to, or greater than the exact quotient. Although
1984 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
1985 /// returns `Equal`.
1986 ///
1987 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1988 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1989 /// description of the `Nearest` rounding mode.
1990 ///
1991 /// $$
1992 /// f(x,y,p) = x/y+\varepsilon.
1993 /// $$
1994 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1995 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1996 ///
1997 /// If the output has a precision, it is `prec`.
1998 ///
1999 /// Special cases:
2000 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
2001 /// \text{NaN}$
2002 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
2003 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
2004 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
2005 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
2006 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
2007 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
2008 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
2009 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
2010 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
2011 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
2012 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2013 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2014 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
2015 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
2016 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2017 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2018 ///
2019 /// Overflow and underflow:
2020 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
2021 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
2022 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2023 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2024 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2025 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2026 ///
2027 /// If you want to use a rounding mode other than `Nearest`, consider using
2028 /// [`Float::div_prec_round_ref_ref`] instead. If you know that your target precision is the
2029 /// maximum of the precisions of the two inputs, consider using `/` instead.
2030 ///
2031 /// # Worst-case complexity
2032 /// $T(n) = O(n \log n \log\log n)$
2033 ///
2034 /// $M(n) = O(n \log n)$
2035 ///
2036 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2037 /// other.significant_bits(), `prec`)`.
2038 ///
2039 /// # Examples
2040 /// ```
2041 /// use core::f64::consts::{E, PI};
2042 /// use malachite_float::Float;
2043 /// use std::cmp::Ordering::*;
2044 ///
2045 /// let (quotient, o) = Float::from(PI).div_prec_ref_ref(&Float::from(E), 5);
2046 /// assert_eq!(quotient.to_string(), "1.12");
2047 /// assert_eq!(o, Less);
2048 ///
2049 /// let (quotient, o) = Float::from(PI).div_prec_ref_ref(&Float::from(E), 20);
2050 /// assert_eq!(quotient.to_string(), "1.155727");
2051 /// assert_eq!(o, Greater);
2052 /// ```
2053 #[inline]
2054 pub fn div_prec_ref_ref(&self, other: &Self, prec: u64) -> (Self, Ordering) {
2055 self.div_prec_round_ref_ref(other, prec, Nearest)
2056 }
2057
2058 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. Both
2059 /// [`Float`]s are taken by value. An [`Ordering`] is also returned, indicating whether the
2060 /// rounded quotient is less than, equal to, or greater than the exact quotient. Although `NaN`s
2061 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
2062 /// `Equal`.
2063 ///
2064 /// The precision of the output is the maximum of the precision of the inputs. See
2065 /// [`RoundingMode`] for a description of the possible rounding modes.
2066 ///
2067 /// $$
2068 /// f(x,y,m) = x/y+\varepsilon.
2069 /// $$
2070 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2071 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2072 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2073 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2074 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2075 ///
2076 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2077 ///
2078 /// Special cases:
2079 /// - $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) =
2080 /// \text{NaN}$
2081 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
2082 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
2083 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
2084 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
2085 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
2086 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
2087 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
2088 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
2089 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
2090 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
2091 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2092 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2093 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
2094 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
2095 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2096 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2097 ///
2098 /// Overflow and underflow:
2099 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
2100 /// returned instead.
2101 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
2102 /// returned instead, where `p` is the precision of the input.
2103 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
2104 /// returned instead.
2105 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
2106 /// is returned instead, where `p` is the precision of the input.
2107 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
2108 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
2109 /// instead.
2110 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
2111 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
2112 /// instead.
2113 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
2114 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
2115 /// instead.
2116 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
2117 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
2118 /// returned instead.
2119 ///
2120 /// If you want to specify an output precision, consider using [`Float::div_prec_round`]
2121 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
2122 /// instead.
2123 ///
2124 /// # Worst-case complexity
2125 /// $T(n) = O(n \log n \log\log n)$
2126 ///
2127 /// $M(n) = O(n \log n)$
2128 ///
2129 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2130 /// other.significant_bits())`.
2131 ///
2132 /// # Panics
2133 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
2134 /// represent the output.
2135 ///
2136 /// # Examples
2137 /// ```
2138 /// use core::f64::consts::{E, PI};
2139 /// use malachite_base::rounding_modes::RoundingMode::*;
2140 /// use malachite_float::Float;
2141 /// use std::cmp::Ordering::*;
2142 ///
2143 /// let (quotient, o) = Float::from(PI).div_round(Float::from(E), Floor);
2144 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2145 /// assert_eq!(o, Less);
2146 ///
2147 /// let (quotient, o) = Float::from(PI).div_round(Float::from(E), Ceiling);
2148 /// assert_eq!(quotient.to_string(), "1.155727349790922");
2149 /// assert_eq!(o, Greater);
2150 ///
2151 /// let (quotient, o) = Float::from(PI).div_round(Float::from(E), Nearest);
2152 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2153 /// assert_eq!(o, Less);
2154 /// ```
2155 #[inline]
2156 pub fn div_round(self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
2157 let prec = max(self.significant_bits(), other.significant_bits());
2158 self.div_prec_round(other, prec, rm)
2159 }
2160
2161 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. The first
2162 /// [`Float`] is taken by value and the second by reference. An [`Ordering`] is also returned,
2163 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
2164 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function
2165 /// returns a `NaN` it also returns `Equal`.
2166 ///
2167 /// The precision of the output is the maximum of the precision of the inputs. See
2168 /// [`RoundingMode`] for a description of the possible rounding modes.
2169 ///
2170 /// $$
2171 /// f(x,y,m) = x/y+\varepsilon.
2172 /// $$
2173 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2174 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2175 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2176 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2177 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2178 ///
2179 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2180 ///
2181 /// Special cases:
2182 /// - $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) =
2183 /// \text{NaN}$
2184 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
2185 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
2186 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
2187 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
2188 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
2189 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
2190 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
2191 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
2192 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
2193 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
2194 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2195 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2196 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
2197 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
2198 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2199 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2200 ///
2201 /// Overflow and underflow:
2202 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
2203 /// returned instead.
2204 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
2205 /// returned instead, where `p` is the precision of the input.
2206 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
2207 /// returned instead.
2208 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
2209 /// is returned instead, where `p` is the precision of the input.
2210 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
2211 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
2212 /// instead.
2213 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
2214 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
2215 /// instead.
2216 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
2217 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
2218 /// instead.
2219 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
2220 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
2221 /// returned instead.
2222 ///
2223 /// If you want to specify an output precision, consider using [`Float::div_prec_round_val_ref`]
2224 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
2225 /// instead.
2226 ///
2227 /// # Worst-case complexity
2228 /// $T(n) = O(n \log n \log\log n)$
2229 ///
2230 /// $M(n) = O(n \log n)$
2231 ///
2232 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2233 /// other.significant_bits())`.
2234 ///
2235 /// # Panics
2236 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
2237 /// represent the output.
2238 ///
2239 /// # Examples
2240 /// ```
2241 /// use core::f64::consts::{E, PI};
2242 /// use malachite_base::rounding_modes::RoundingMode::*;
2243 /// use malachite_float::Float;
2244 /// use std::cmp::Ordering::*;
2245 ///
2246 /// let (quotient, o) = Float::from(PI).div_round_val_ref(&Float::from(E), Floor);
2247 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2248 /// assert_eq!(o, Less);
2249 ///
2250 /// let (quotient, o) = Float::from(PI).div_round_val_ref(&Float::from(E), Ceiling);
2251 /// assert_eq!(quotient.to_string(), "1.155727349790922");
2252 /// assert_eq!(o, Greater);
2253 ///
2254 /// let (quotient, o) = Float::from(PI).div_round_val_ref(&Float::from(E), Nearest);
2255 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2256 /// assert_eq!(o, Less);
2257 /// ```
2258 #[inline]
2259 pub fn div_round_val_ref(self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
2260 let prec = max(self.significant_bits(), other.significant_bits());
2261 self.div_prec_round_val_ref(other, prec, rm)
2262 }
2263
2264 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. The first
2265 /// [`Float`] is taken by reference and the second by value. An [`Ordering`] is also returned,
2266 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
2267 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function
2268 /// returns a `NaN` it also returns `Equal`.
2269 ///
2270 /// The precision of the output is the maximum of the precision of the inputs. See
2271 /// [`RoundingMode`] for a description of the possible rounding modes.
2272 ///
2273 /// $$
2274 /// f(x,y,m) = x/y+\varepsilon.
2275 /// $$
2276 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2277 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2278 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2279 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2280 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2281 ///
2282 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2283 ///
2284 /// Special cases:
2285 /// - $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) =
2286 /// \text{NaN}$
2287 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
2288 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
2289 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
2290 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
2291 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
2292 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
2293 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
2294 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
2295 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
2296 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
2297 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2298 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2299 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
2300 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
2301 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2302 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2303 ///
2304 /// Overflow and underflow:
2305 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
2306 /// returned instead.
2307 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
2308 /// returned instead, where `p` is the precision of the input.
2309 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
2310 /// returned instead.
2311 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
2312 /// is returned instead, where `p` is the precision of the input.
2313 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
2314 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
2315 /// instead.
2316 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
2317 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
2318 /// instead.
2319 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
2320 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
2321 /// instead.
2322 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
2323 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
2324 /// returned instead.
2325 ///
2326 /// If you want to specify an output precision, consider using [`Float::div_prec_round_ref_val`]
2327 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
2328 /// instead.
2329 ///
2330 /// # Worst-case complexity
2331 /// $T(n) = O(n \log n \log\log n)$
2332 ///
2333 /// $M(n) = O(n \log n)$
2334 ///
2335 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2336 /// other.significant_bits())`.
2337 ///
2338 /// # Panics
2339 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
2340 /// represent the output.
2341 ///
2342 /// # Examples
2343 /// ```
2344 /// use core::f64::consts::{E, PI};
2345 /// use malachite_base::rounding_modes::RoundingMode::*;
2346 /// use malachite_float::Float;
2347 /// use std::cmp::Ordering::*;
2348 ///
2349 /// let (quotient, o) = Float::from(PI).div_round_ref_val(Float::from(E), Floor);
2350 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2351 /// assert_eq!(o, Less);
2352 ///
2353 /// let (quotient, o) = Float::from(PI).div_round_ref_val(Float::from(E), Ceiling);
2354 /// assert_eq!(quotient.to_string(), "1.155727349790922");
2355 /// assert_eq!(o, Greater);
2356 ///
2357 /// let (quotient, o) = Float::from(PI).div_round_ref_val(Float::from(E), Nearest);
2358 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2359 /// assert_eq!(o, Less);
2360 /// ```
2361 #[inline]
2362 pub fn div_round_ref_val(&self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
2363 let prec = max(self.significant_bits(), other.significant_bits());
2364 self.div_prec_round_ref_val(other, prec, rm)
2365 }
2366
2367 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. Both
2368 /// [`Float`]s are taken by reference. An [`Ordering`] is also returned, indicating whether the
2369 /// rounded quotient is less than, equal to, or greater than the exact quotient. Although `NaN`s
2370 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
2371 /// `Equal`.
2372 ///
2373 /// The precision of the output is the maximum of the precision of the inputs. See
2374 /// [`RoundingMode`] for a description of the possible rounding modes.
2375 ///
2376 /// $$
2377 /// f(x,y,m) = x/y+\varepsilon.
2378 /// $$
2379 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2380 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2381 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2382 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2383 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2384 ///
2385 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2386 ///
2387 /// Special cases:
2388 /// - $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) =
2389 /// \text{NaN}$
2390 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
2391 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
2392 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
2393 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
2394 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
2395 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
2396 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
2397 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
2398 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
2399 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
2400 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2401 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2402 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
2403 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
2404 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2405 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2406 ///
2407 /// Overflow and underflow:
2408 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
2409 /// returned instead.
2410 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
2411 /// returned instead, where `p` is the precision of the input.
2412 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
2413 /// returned instead.
2414 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
2415 /// is returned instead, where `p` is the precision of the input.
2416 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
2417 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
2418 /// instead.
2419 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
2420 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
2421 /// instead.
2422 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
2423 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
2424 /// instead.
2425 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
2426 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
2427 /// returned instead.
2428 ///
2429 /// If you want to specify an output precision, consider using [`Float::div_prec_round_ref_ref`]
2430 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
2431 /// 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 (quotient, o) = Float::from(PI).div_round_ref_ref(&Float::from(E), Floor);
2453 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2454 /// assert_eq!(o, Less);
2455 ///
2456 /// let (quotient, o) = Float::from(PI).div_round_ref_ref(&Float::from(E), Ceiling);
2457 /// assert_eq!(quotient.to_string(), "1.155727349790922");
2458 /// assert_eq!(o, Greater);
2459 ///
2460 /// let (quotient, o) = Float::from(PI).div_round_ref_ref(&Float::from(E), Nearest);
2461 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2462 /// assert_eq!(o, Less);
2463 /// ```
2464 #[inline]
2465 pub fn div_round_ref_ref(&self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
2466 let prec = max(self.significant_bits(), other.significant_bits());
2467 self.div_prec_round_ref_ref(other, prec, rm)
2468 }
2469
2470 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the specified precision
2471 /// and with the specified rounding mode. The [`Float`] on the right-hand side is taken by
2472 /// value. An [`Ordering`] is returned, indicating whether the rounded quotient is less than,
2473 /// equal to, or greater than the exact quotient. Although `NaN`s are not comparable to any
2474 /// [`Float`], whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
2475 ///
2476 /// See [`RoundingMode`] for a description of the possible rounding modes.
2477 ///
2478 /// $$
2479 /// x \gets 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 /// See the [`Float::div_prec_round`] documentation for information on special cases, overflow,
2490 /// and underflow.
2491 ///
2492 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_assign`] instead. If
2493 /// you know that your target precision is the maximum of the precisions of the two inputs,
2494 /// consider using [`Float::div_round_assign`] instead. If both of these things are true,
2495 /// consider using `/=` instead.
2496 ///
2497 /// # Worst-case complexity
2498 /// $T(n) = O(n \log n \log\log n)$
2499 ///
2500 /// $M(n) = O(n \log n)$
2501 ///
2502 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2503 /// other.significant_bits(), `prec`)`.
2504 ///
2505 /// # Panics
2506 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2507 ///
2508 /// # Examples
2509 /// ```
2510 /// use core::f64::consts::{E, PI};
2511 /// use malachite_base::rounding_modes::RoundingMode::*;
2512 /// use malachite_float::Float;
2513 /// use std::cmp::Ordering::*;
2514 ///
2515 /// let mut quotient = Float::from(PI);
2516 /// assert_eq!(
2517 /// quotient.div_prec_round_assign(Float::from(E), 5, Floor),
2518 /// Less
2519 /// );
2520 /// assert_eq!(quotient.to_string(), "1.12");
2521 ///
2522 /// let mut quotient = Float::from(PI);
2523 /// assert_eq!(
2524 /// quotient.div_prec_round_assign(Float::from(E), 5, Ceiling),
2525 /// Greater
2526 /// );
2527 /// assert_eq!(quotient.to_string(), "1.19");
2528 ///
2529 /// let mut quotient = Float::from(PI);
2530 /// assert_eq!(
2531 /// quotient.div_prec_round_assign(Float::from(E), 5, Nearest),
2532 /// Less
2533 /// );
2534 /// assert_eq!(quotient.to_string(), "1.12");
2535 ///
2536 /// let mut quotient = Float::from(PI);
2537 /// assert_eq!(
2538 /// quotient.div_prec_round_assign(Float::from(E), 20, Floor),
2539 /// Less
2540 /// );
2541 /// assert_eq!(quotient.to_string(), "1.155725");
2542 ///
2543 /// let mut quotient = Float::from(PI);
2544 /// assert_eq!(
2545 /// quotient.div_prec_round_assign(Float::from(E), 20, Ceiling),
2546 /// Greater
2547 /// );
2548 /// assert_eq!(quotient.to_string(), "1.155727");
2549 ///
2550 /// let mut quotient = Float::from(PI);
2551 /// assert_eq!(
2552 /// quotient.div_prec_round_assign(Float::from(E), 20, Nearest),
2553 /// Greater
2554 /// );
2555 /// assert_eq!(quotient.to_string(), "1.155727");
2556 /// ```
2557 #[inline]
2558 pub fn div_prec_round_assign(&mut self, other: Self, prec: u64, rm: RoundingMode) -> Ordering {
2559 assert_ne!(prec, 0);
2560 match (&mut *self, other) {
2561 (float_nan!(), _)
2562 | (_, float_nan!())
2563 | (float_either_infinity!(), float_either_infinity!())
2564 | (float_either_zero!(), float_either_zero!()) => {
2565 *self = float_nan!();
2566 Equal
2567 }
2568 (
2569 Self(Infinity { sign: x_sign }),
2570 Self(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
2571 )
2572 | (Self(Finite { sign: x_sign, .. }), Self(Zero { sign: y_sign })) => {
2573 *self = Self(Infinity {
2574 sign: *x_sign == y_sign,
2575 });
2576 Equal
2577 }
2578 (
2579 Self(Zero { sign: x_sign }),
2580 Self(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
2581 )
2582 | (Self(Finite { sign: x_sign, .. }), Self(Infinity { sign: y_sign })) => {
2583 *self = Self(Zero {
2584 sign: *x_sign == y_sign,
2585 });
2586 Equal
2587 }
2588 (_, y) if abs_is_power_of_2(&y) => {
2589 let sign = y >= 0;
2590 let mut o = self.shr_prec_round_assign(
2591 y.get_exponent().unwrap() - 1,
2592 prec,
2593 if sign { rm } else { -rm },
2594 );
2595 if !sign {
2596 self.neg_assign();
2597 o = o.reverse();
2598 }
2599 o
2600 }
2601 (
2602 Self(Finite {
2603 sign: x_sign,
2604 exponent: x_exp,
2605 precision: x_prec,
2606 significand: x,
2607 }),
2608 Self(Finite {
2609 sign: y_sign,
2610 exponent: y_exp,
2611 precision: y_prec,
2612 significand: mut y,
2613 }),
2614 ) => {
2615 let sign = *x_sign == y_sign;
2616 let exp_diff = *x_exp - y_exp;
2617 if exp_diff > Self::MAX_EXPONENT {
2618 return match (sign, rm) {
2619 (_, Exact) => panic!("Inexact Float division"),
2620 (true, Ceiling | Up | Nearest) => {
2621 *self = float_infinity!();
2622 Greater
2623 }
2624 (true, _) => {
2625 *self = Self::max_finite_value_with_prec(prec);
2626 Less
2627 }
2628 (false, Floor | Up | Nearest) => {
2629 *self = float_negative_infinity!();
2630 Less
2631 }
2632 (false, _) => {
2633 *self = -Self::max_finite_value_with_prec(prec);
2634 Greater
2635 }
2636 };
2637 } else if exp_diff + 2 < Self::MIN_EXPONENT {
2638 return match (sign, rm) {
2639 (_, Exact) => panic!("Inexact Float division"),
2640 (true, Ceiling | Up) => {
2641 *self = Self::min_positive_value_prec(prec);
2642 Greater
2643 }
2644 (true, _) => {
2645 *self = float_zero!();
2646 Less
2647 }
2648 (false, Floor | Up) => {
2649 *self = -Self::min_positive_value_prec(prec);
2650 Less
2651 }
2652 (false, _) => {
2653 *self = float_negative_zero!();
2654 Greater
2655 }
2656 };
2657 }
2658 let (exp_offset, o) = div_float_significands_in_place(
2659 x,
2660 *x_prec,
2661 &mut y,
2662 y_prec,
2663 prec,
2664 if sign { rm } else { -rm },
2665 );
2666 *x_exp = exp_diff.checked_add(i32::exact_from(exp_offset)).unwrap();
2667 if *x_exp > Self::MAX_EXPONENT {
2668 return match (sign, rm) {
2669 (_, Exact) => panic!("Inexact Float division"),
2670 (true, Ceiling | Up | Nearest) => {
2671 *self = float_infinity!();
2672 Greater
2673 }
2674 (true, _) => {
2675 *self = Self::max_finite_value_with_prec(prec);
2676 Less
2677 }
2678 (false, Floor | Up | Nearest) => {
2679 *self = float_negative_infinity!();
2680 Less
2681 }
2682 (false, _) => {
2683 *self = -Self::max_finite_value_with_prec(prec);
2684 Greater
2685 }
2686 };
2687 } else if *x_exp < Self::MIN_EXPONENT {
2688 return if rm == Nearest
2689 && *x_exp == Self::MIN_EXPONENT - 1
2690 && (o == Less || !x.is_power_of_2())
2691 {
2692 if sign {
2693 *self = Self::min_positive_value_prec(prec);
2694 Greater
2695 } else {
2696 *self = -Self::min_positive_value_prec(prec);
2697 Less
2698 }
2699 } else {
2700 match (sign, rm) {
2701 (_, Exact) => panic!("Inexact Float division"),
2702 (true, Ceiling | Up) => {
2703 *self = Self::min_positive_value_prec(prec);
2704 Greater
2705 }
2706 (true, _) => {
2707 *self = float_zero!();
2708 Less
2709 }
2710 (false, Floor | Up) => {
2711 *self = -Self::min_positive_value_prec(prec);
2712 Less
2713 }
2714 (false, _) => {
2715 *self = float_negative_zero!();
2716 Greater
2717 }
2718 }
2719 };
2720 }
2721 *x_sign = sign;
2722 *x_prec = prec;
2723 if sign { o } else { o.reverse() }
2724 }
2725 }
2726 }
2727
2728 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the specified precision
2729 /// and with the specified rounding mode. The [`Float`] on the right-hand side is taken by
2730 /// reference. An [`Ordering`] is returned, indicating whether the rounded quotient is less
2731 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
2732 /// any [`Float`], whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
2733 ///
2734 /// See [`RoundingMode`] for a description of the possible rounding modes.
2735 ///
2736 /// $$
2737 /// x \gets x/y+\varepsilon.
2738 /// $$
2739 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2740 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2741 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2742 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
2743 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2744 ///
2745 /// If the output has a precision, it is `prec`.
2746 ///
2747 /// See the [`Float::div_prec_round`] documentation for information on special cases, overflow,
2748 /// and underflow.
2749 ///
2750 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_assign_ref`]
2751 /// instead. If you know that your target precision is the maximum of the precisions of the two
2752 /// inputs, consider using [`Float::div_round_assign_ref`] instead. If both of these things are
2753 /// true, consider using `/=` instead.
2754 ///
2755 /// # Worst-case complexity
2756 /// $T(n) = O(n \log n \log\log n)$
2757 ///
2758 /// $M(n) = O(n \log n)$
2759 ///
2760 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2761 /// other.significant_bits(), `prec`)`.
2762 ///
2763 /// # Panics
2764 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2765 ///
2766 /// # Examples
2767 /// ```
2768 /// use core::f64::consts::{E, PI};
2769 /// use malachite_base::rounding_modes::RoundingMode::*;
2770 /// use malachite_float::Float;
2771 /// use std::cmp::Ordering::*;
2772 ///
2773 /// let mut quotient = Float::from(PI);
2774 /// assert_eq!(
2775 /// quotient.div_prec_round_assign_ref(&Float::from(E), 5, Floor),
2776 /// Less
2777 /// );
2778 /// assert_eq!(quotient.to_string(), "1.12");
2779 ///
2780 /// let mut quotient = Float::from(PI);
2781 /// assert_eq!(
2782 /// quotient.div_prec_round_assign_ref(&Float::from(E), 5, Ceiling),
2783 /// Greater
2784 /// );
2785 /// assert_eq!(quotient.to_string(), "1.19");
2786 ///
2787 /// let mut quotient = Float::from(PI);
2788 /// assert_eq!(
2789 /// quotient.div_prec_round_assign_ref(&Float::from(E), 5, Nearest),
2790 /// Less
2791 /// );
2792 /// assert_eq!(quotient.to_string(), "1.12");
2793 ///
2794 /// let mut quotient = Float::from(PI);
2795 /// assert_eq!(
2796 /// quotient.div_prec_round_assign_ref(&Float::from(E), 20, Floor),
2797 /// Less
2798 /// );
2799 /// assert_eq!(quotient.to_string(), "1.155725");
2800 ///
2801 /// let mut quotient = Float::from(PI);
2802 /// assert_eq!(
2803 /// quotient.div_prec_round_assign_ref(&Float::from(E), 20, Ceiling),
2804 /// Greater
2805 /// );
2806 /// assert_eq!(quotient.to_string(), "1.155727");
2807 ///
2808 /// let mut quotient = Float::from(PI);
2809 /// assert_eq!(
2810 /// quotient.div_prec_round_assign_ref(&Float::from(E), 20, Nearest),
2811 /// Greater
2812 /// );
2813 /// assert_eq!(quotient.to_string(), "1.155727");
2814 /// ```
2815 #[inline]
2816 pub fn div_prec_round_assign_ref(
2817 &mut self,
2818 other: &Self,
2819 prec: u64,
2820 rm: RoundingMode,
2821 ) -> Ordering {
2822 assert_ne!(prec, 0);
2823 match (&mut *self, other) {
2824 (float_nan!(), _)
2825 | (_, float_nan!())
2826 | (float_either_infinity!(), float_either_infinity!())
2827 | (float_either_zero!(), float_either_zero!()) => {
2828 *self = float_nan!();
2829 Equal
2830 }
2831 (
2832 Self(Infinity { sign: x_sign }),
2833 Self(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
2834 )
2835 | (Self(Finite { sign: x_sign, .. }), Self(Zero { sign: y_sign })) => {
2836 *self = Self(Infinity {
2837 sign: x_sign == y_sign,
2838 });
2839 Equal
2840 }
2841 (
2842 Self(Zero { sign: x_sign }),
2843 Self(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
2844 )
2845 | (Self(Finite { sign: x_sign, .. }), Self(Infinity { sign: y_sign })) => {
2846 *self = Self(Zero {
2847 sign: x_sign == y_sign,
2848 });
2849 Equal
2850 }
2851 (_, y) if abs_is_power_of_2(y) => {
2852 let sign = *y >= 0;
2853 let mut o = self.shr_prec_round_assign(
2854 y.get_exponent().unwrap() - 1,
2855 prec,
2856 if sign { rm } else { -rm },
2857 );
2858 if !sign {
2859 self.neg_assign();
2860 o = o.reverse();
2861 }
2862 o
2863 }
2864 (
2865 Self(Finite {
2866 sign: x_sign,
2867 exponent: x_exp,
2868 precision: x_prec,
2869 significand: x,
2870 }),
2871 Self(Finite {
2872 sign: y_sign,
2873 exponent: y_exp,
2874 precision: y_prec,
2875 significand: y,
2876 }),
2877 ) => {
2878 let sign = x_sign == y_sign;
2879 let exp_diff = *x_exp - y_exp;
2880 if exp_diff > Self::MAX_EXPONENT {
2881 return match (sign, rm) {
2882 (_, Exact) => panic!("Inexact Float division"),
2883 (true, Ceiling | Up | Nearest) => {
2884 *self = float_infinity!();
2885 Greater
2886 }
2887 (true, _) => {
2888 *self = Self::max_finite_value_with_prec(prec);
2889 Less
2890 }
2891 (false, Floor | Up | Nearest) => {
2892 *self = float_negative_infinity!();
2893 Less
2894 }
2895 (false, _) => {
2896 *self = -Self::max_finite_value_with_prec(prec);
2897 Greater
2898 }
2899 };
2900 } else if exp_diff + 2 < Self::MIN_EXPONENT {
2901 return match (sign, rm) {
2902 (_, Exact) => panic!("Inexact Float division"),
2903 (true, Ceiling | Up) => {
2904 *self = Self::min_positive_value_prec(prec);
2905 Greater
2906 }
2907 (true, _) => {
2908 *self = float_zero!();
2909 Less
2910 }
2911 (false, Floor | Up) => {
2912 *self = -Self::min_positive_value_prec(prec);
2913 Less
2914 }
2915 (false, _) => {
2916 *self = float_negative_zero!();
2917 Greater
2918 }
2919 };
2920 }
2921 let (exp_offset, o) = div_float_significands_in_place_ref(
2922 x,
2923 *x_prec,
2924 y,
2925 *y_prec,
2926 prec,
2927 if sign { rm } else { -rm },
2928 );
2929 *x_exp = exp_diff.checked_add(i32::exact_from(exp_offset)).unwrap();
2930 if *x_exp > Self::MAX_EXPONENT {
2931 return match (sign, rm) {
2932 (_, Exact) => panic!("Inexact Float division"),
2933 (true, Ceiling | Up | Nearest) => {
2934 *self = float_infinity!();
2935 Greater
2936 }
2937 (true, _) => {
2938 *self = Self::max_finite_value_with_prec(prec);
2939 Less
2940 }
2941 (false, Floor | Up | Nearest) => {
2942 *self = float_negative_infinity!();
2943 Less
2944 }
2945 (false, _) => {
2946 *self = -Self::max_finite_value_with_prec(prec);
2947 Greater
2948 }
2949 };
2950 } else if *x_exp < Self::MIN_EXPONENT {
2951 return if rm == Nearest
2952 && *x_exp == Self::MIN_EXPONENT - 1
2953 && (o == Less || !x.is_power_of_2())
2954 {
2955 if sign {
2956 *self = Self::min_positive_value_prec(prec);
2957 Greater
2958 } else {
2959 *self = -Self::min_positive_value_prec(prec);
2960 Less
2961 }
2962 } else {
2963 match (sign, rm) {
2964 (_, Exact) => panic!("Inexact Float division"),
2965 (true, Ceiling | Up) => {
2966 *self = Self::min_positive_value_prec(prec);
2967 Greater
2968 }
2969 (true, _) => {
2970 *self = float_zero!();
2971 Less
2972 }
2973 (false, Floor | Up) => {
2974 *self = -Self::min_positive_value_prec(prec);
2975 Less
2976 }
2977 (false, _) => {
2978 *self = float_negative_zero!();
2979 Greater
2980 }
2981 }
2982 };
2983 }
2984 *x_sign = sign;
2985 *x_prec = prec;
2986 if sign { o } else { o.reverse() }
2987 }
2988 }
2989 }
2990
2991 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the nearest value of the
2992 /// specified precision. The [`Float`] on the right-hand side is taken by value. An [`Ordering`]
2993 /// is returned, indicating whether the rounded quotient is less than, equal to, or greater than
2994 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
2995 /// function sets the [`Float`] to `NaN` it also returns `Equal`.
2996 ///
2997 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
2998 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2999 /// description of the `Nearest` rounding mode.
3000 ///
3001 /// $$
3002 /// x \gets x/y+\varepsilon.
3003 /// $$
3004 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3005 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3006 ///
3007 /// If the output has a precision, it is `prec`.
3008 ///
3009 /// See the [`Float::div_prec`] documentation for information on special cases, overflow, and
3010 /// underflow.
3011 ///
3012 /// If you want to use a rounding mode other than `Nearest`, consider using
3013 /// [`Float::div_prec_round_assign`] instead. If you know that your target precision is the
3014 /// maximum of the precisions of the two inputs, consider using `/=` instead.
3015 ///
3016 /// # Worst-case complexity
3017 /// $T(n) = O(n \log n \log\log n)$
3018 ///
3019 /// $M(n) = O(n \log n)$
3020 ///
3021 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3022 /// other.significant_bits(), `prec`)`.
3023 ///
3024 /// # Examples
3025 /// ```
3026 /// use core::f64::consts::{E, PI};
3027 /// use malachite_float::Float;
3028 /// use std::cmp::Ordering::*;
3029 ///
3030 /// let mut x = Float::from(PI);
3031 /// assert_eq!(x.div_prec_assign(Float::from(E), 5), Less);
3032 /// assert_eq!(x.to_string(), "1.12");
3033 ///
3034 /// let mut x = Float::from(PI);
3035 /// assert_eq!(x.div_prec_assign(Float::from(E), 20), Greater);
3036 /// assert_eq!(x.to_string(), "1.155727");
3037 /// ```
3038 #[inline]
3039 pub fn div_prec_assign(&mut self, other: Self, prec: u64) -> Ordering {
3040 self.div_prec_round_assign(other, prec, Nearest)
3041 }
3042
3043 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the nearest value of the
3044 /// specified precision. The [`Float`] on the right-hand side is taken by reference. An
3045 /// [`Ordering`] is returned, indicating whether the rounded quotient is less than, equal to, or
3046 /// greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3047 /// whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
3048 ///
3049 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3050 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3051 /// description of the `Nearest` rounding mode.
3052 ///
3053 /// $$
3054 /// x \gets x/y+\varepsilon.
3055 /// $$
3056 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3057 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3058 ///
3059 /// If the output has a precision, it is `prec`.
3060 ///
3061 /// See the [`Float::div_prec`] documentation for information on special cases, overflow, and
3062 /// underflow.
3063 ///
3064 /// If you want to use a rounding mode other than `Nearest`, consider using
3065 /// [`Float::div_prec_round_assign_ref`] instead. If you know that your target precision is the
3066 /// maximum of the precisions of the two inputs, consider using `/=` instead.
3067 ///
3068 /// # Worst-case complexity
3069 /// $T(n) = O(n \log n \log\log n)$
3070 ///
3071 /// $M(n) = O(n \log n)$
3072 ///
3073 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3074 /// other.significant_bits(), `prec`)`.
3075 ///
3076 /// # Examples
3077 /// ```
3078 /// use core::f64::consts::{E, PI};
3079 /// use malachite_float::Float;
3080 /// use std::cmp::Ordering::*;
3081 ///
3082 /// let mut x = Float::from(PI);
3083 /// assert_eq!(x.div_prec_assign_ref(&Float::from(E), 5), Less);
3084 /// assert_eq!(x.to_string(), "1.12");
3085 ///
3086 /// let mut x = Float::from(PI);
3087 /// assert_eq!(x.div_prec_assign_ref(&Float::from(E), 20), Greater);
3088 /// assert_eq!(x.to_string(), "1.155727");
3089 /// ```
3090 #[inline]
3091 pub fn div_prec_assign_ref(&mut self, other: &Self, prec: u64) -> Ordering {
3092 self.div_prec_round_assign_ref(other, prec, Nearest)
3093 }
3094
3095 /// Divides a [`Float`] by a [`Float`] in place, rounding the result with the specified rounding
3096 /// mode. The [`Float`] on the right-hand side is taken by value. An [`Ordering`] is returned,
3097 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
3098 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
3099 /// the [`Float`] to `NaN` it also returns `Equal`.
3100 ///
3101 /// The precision of the output is the maximum of the precision of the inputs. See
3102 /// [`RoundingMode`] for a description of the possible rounding modes.
3103 ///
3104 /// $$
3105 /// x \gets x/y+\varepsilon.
3106 /// $$
3107 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3108 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3109 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
3110 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3111 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
3112 ///
3113 /// If the output has a precision, it is the maximum of the precisions of the inputs.
3114 ///
3115 /// See the [`Float::div_round`] documentation for information on special cases, overflow, and
3116 /// underflow.
3117 ///
3118 /// If you want to specify an output precision, consider using [`Float::div_prec_round_assign`]
3119 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/=`
3120 /// instead.
3121 ///
3122 /// # Worst-case complexity
3123 /// $T(n) = O(n \log n \log\log n)$
3124 ///
3125 /// $M(n) = O(n \log n)$
3126 ///
3127 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3128 /// other.significant_bits())`.
3129 ///
3130 /// # Panics
3131 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
3132 /// represent the output.
3133 ///
3134 /// # Examples
3135 /// ```
3136 /// use core::f64::consts::{E, PI};
3137 /// use malachite_base::rounding_modes::RoundingMode::*;
3138 /// use malachite_float::Float;
3139 /// use std::cmp::Ordering::*;
3140 ///
3141 /// let mut x = Float::from(PI);
3142 /// assert_eq!(x.div_round_assign(Float::from(E), Floor), Less);
3143 /// assert_eq!(x.to_string(), "1.1557273497909217");
3144 ///
3145 /// let mut x = Float::from(PI);
3146 /// assert_eq!(x.div_round_assign(Float::from(E), Ceiling), Greater);
3147 /// assert_eq!(x.to_string(), "1.155727349790922");
3148 ///
3149 /// let mut x = Float::from(PI);
3150 /// assert_eq!(x.div_round_assign(Float::from(E), Nearest), Less);
3151 /// assert_eq!(x.to_string(), "1.1557273497909217");
3152 /// ```
3153 #[inline]
3154 pub fn div_round_assign(&mut self, other: Self, rm: RoundingMode) -> Ordering {
3155 let prec = max(self.significant_bits(), other.significant_bits());
3156 self.div_prec_round_assign(other, prec, rm)
3157 }
3158
3159 /// Divides a [`Float`] by a [`Float`] in place, rounding the result with the specified rounding
3160 /// mode. The [`Float`] on the right-hand side is taken by reference. An [`Ordering`] is
3161 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3162 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3163 /// function sets the [`Float`] to `NaN` it also returns `Equal`.
3164 ///
3165 /// The precision of the output is the maximum of the precision of the inputs. See
3166 /// [`RoundingMode`] for a description of the possible rounding modes.
3167 ///
3168 /// $$
3169 /// x \gets x/y+\varepsilon.
3170 /// $$
3171 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3172 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3173 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
3174 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3175 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
3176 ///
3177 /// If the output has a precision, it is the maximum of the precisions of the inputs.
3178 ///
3179 /// See the [`Float::div_round`] documentation for information on special cases, overflow, and
3180 /// underflow.
3181 ///
3182 /// If you want to specify an output precision, consider using
3183 /// [`Float::div_prec_round_assign_ref`] instead. If you know you'll be using the `Nearest`
3184 /// rounding mode, consider using `/=` instead.
3185 ///
3186 /// # Worst-case complexity
3187 /// $T(n) = O(n \log n \log\log n)$
3188 ///
3189 /// $M(n) = O(n \log n)$
3190 ///
3191 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3192 /// other.significant_bits())`.
3193 ///
3194 /// # Panics
3195 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
3196 /// represent the output.
3197 ///
3198 /// # Examples
3199 /// ```
3200 /// use core::f64::consts::{E, PI};
3201 /// use malachite_base::rounding_modes::RoundingMode::*;
3202 /// use malachite_float::Float;
3203 /// use std::cmp::Ordering::*;
3204 ///
3205 /// let mut x = Float::from(PI);
3206 /// assert_eq!(x.div_round_assign_ref(&Float::from(E), Floor), Less);
3207 /// assert_eq!(x.to_string(), "1.1557273497909217");
3208 ///
3209 /// let mut x = Float::from(PI);
3210 /// assert_eq!(x.div_round_assign_ref(&Float::from(E), Ceiling), Greater);
3211 /// assert_eq!(x.to_string(), "1.155727349790922");
3212 ///
3213 /// let mut x = Float::from(PI);
3214 /// assert_eq!(x.div_round_assign_ref(&Float::from(E), Nearest), Less);
3215 /// assert_eq!(x.to_string(), "1.1557273497909217");
3216 /// ```
3217 #[inline]
3218 pub fn div_round_assign_ref(&mut self, other: &Self, rm: RoundingMode) -> Ordering {
3219 let prec = max(self.significant_bits(), other.significant_bits());
3220 self.div_prec_round_assign_ref(other, prec, rm)
3221 }
3222
3223 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
3224 /// with the specified rounding mode. The [`Float`] and the [`Rational`] are both taken by
3225 /// value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
3226 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
3227 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3228 ///
3229 /// See [`RoundingMode`] for a description of the possible rounding modes.
3230 ///
3231 /// $$
3232 /// f(x,y,p,m) = x/y+\varepsilon.
3233 /// $$
3234 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3235 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3236 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3237 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3238 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3239 ///
3240 /// If the output has a precision, it is `prec`.
3241 ///
3242 /// Special cases:
3243 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
3244 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
3245 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
3246 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
3247 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
3248 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
3249 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
3250 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
3251 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
3252 ///
3253 /// Overflow and underflow:
3254 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
3255 /// returned instead.
3256 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
3257 /// is returned instead, where `p` is the precision of the input.
3258 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
3259 /// returned instead.
3260 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
3261 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
3262 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
3263 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
3264 /// instead.
3265 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
3266 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
3267 /// instead.
3268 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
3269 /// instead.
3270 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
3271 /// instead.
3272 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
3273 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
3274 /// returned instead.
3275 ///
3276 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec`] instead.
3277 /// If you know that your target precision is the precision of the [`Float`] input, consider
3278 /// using [`Float::div_rational_round`] instead. If both of these things are true, consider
3279 /// using `/` instead.
3280 ///
3281 /// # Worst-case complexity
3282 /// $T(n) = O(n \log n \log\log n)$
3283 ///
3284 /// $M(n) = O(n \log n)$
3285 ///
3286 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3287 /// other.significant_bits(), prec)`.
3288 ///
3289 /// # Panics
3290 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3291 ///
3292 /// # Examples
3293 /// ```
3294 /// use core::f64::consts::PI;
3295 /// use malachite_base::rounding_modes::RoundingMode::*;
3296 /// use malachite_float::Float;
3297 /// use malachite_q::Rational;
3298 /// use std::cmp::Ordering::*;
3299 ///
3300 /// let (quotient, o) =
3301 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 5, Floor);
3302 /// assert_eq!(quotient.to_string(), "9.0");
3303 /// assert_eq!(o, Less);
3304 ///
3305 /// let (quotient, o) =
3306 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 5, Ceiling);
3307 /// assert_eq!(quotient.to_string(), "9.5");
3308 /// assert_eq!(o, Greater);
3309 ///
3310 /// let (quotient, o) =
3311 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 5, Nearest);
3312 /// assert_eq!(quotient.to_string(), "9.5");
3313 /// assert_eq!(o, Greater);
3314 ///
3315 /// let (quotient, o) =
3316 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 20, Floor);
3317 /// assert_eq!(quotient.to_string(), "9.42477");
3318 /// assert_eq!(o, Less);
3319 ///
3320 /// let (quotient, o) =
3321 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 20, Ceiling);
3322 /// assert_eq!(quotient.to_string(), "9.42479");
3323 /// assert_eq!(o, Greater);
3324 ///
3325 /// let (quotient, o) =
3326 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 20, Nearest);
3327 /// assert_eq!(quotient.to_string(), "9.42477");
3328 /// assert_eq!(o, Less);
3329 /// ```
3330 #[inline]
3331 pub fn div_rational_prec_round(
3332 mut self,
3333 other: Rational,
3334 prec: u64,
3335 rm: RoundingMode,
3336 ) -> (Self, Ordering) {
3337 let o = self.div_rational_prec_round_assign(other, prec, rm);
3338 (self, o)
3339 }
3340
3341 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
3342 /// with the specified rounding mode. The [`Float`] is taken by value and the [`Rational`] by
3343 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
3344 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
3345 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3346 ///
3347 /// See [`RoundingMode`] for a description of the possible rounding modes.
3348 ///
3349 /// $$
3350 /// f(x,y,p,m) = x/y+\varepsilon.
3351 /// $$
3352 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3353 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3354 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3355 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3356 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3357 ///
3358 /// If the output has a precision, it is `prec`.
3359 ///
3360 /// Special cases:
3361 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
3362 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
3363 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
3364 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
3365 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
3366 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
3367 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
3368 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
3369 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
3370 ///
3371 /// Overflow and underflow:
3372 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
3373 /// returned instead.
3374 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
3375 /// is returned instead, where `p` is the precision of the input.
3376 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
3377 /// returned instead.
3378 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
3379 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
3380 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
3381 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
3382 /// instead.
3383 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
3384 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
3385 /// instead.
3386 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
3387 /// instead.
3388 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
3389 /// instead.
3390 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
3391 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
3392 /// returned instead.
3393 ///
3394 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_val_ref`]
3395 /// instead. If you know that your target precision is the precision of the [`Float`] input,
3396 /// consider using [`Float::div_rational_round_val_ref`] instead. If both of these things are
3397 /// true, consider using `/` instead.
3398 ///
3399 /// # Worst-case complexity
3400 /// $T(n) = O(n \log n \log\log n)$
3401 ///
3402 /// $M(n) = O(n \log n)$
3403 ///
3404 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3405 /// other.significant_bits(), prec)`.
3406 ///
3407 /// # Panics
3408 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3409 ///
3410 /// # Examples
3411 /// ```
3412 /// use core::f64::consts::PI;
3413 /// use malachite_base::rounding_modes::RoundingMode::*;
3414 /// use malachite_float::Float;
3415 /// use malachite_q::Rational;
3416 /// use std::cmp::Ordering::*;
3417 ///
3418 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3419 /// &Rational::from_unsigneds(1u8, 3),
3420 /// 5,
3421 /// Floor,
3422 /// );
3423 /// assert_eq!(quotient.to_string(), "9.0");
3424 /// assert_eq!(o, Less);
3425 ///
3426 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3427 /// &Rational::from_unsigneds(1u8, 3),
3428 /// 5,
3429 /// Ceiling,
3430 /// );
3431 /// assert_eq!(quotient.to_string(), "9.5");
3432 /// assert_eq!(o, Greater);
3433 ///
3434 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3435 /// &Rational::from_unsigneds(1u8, 3),
3436 /// 5,
3437 /// Nearest,
3438 /// );
3439 /// assert_eq!(quotient.to_string(), "9.5");
3440 /// assert_eq!(o, Greater);
3441 ///
3442 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3443 /// &Rational::from_unsigneds(1u8, 3),
3444 /// 20,
3445 /// Floor,
3446 /// );
3447 /// assert_eq!(quotient.to_string(), "9.42477");
3448 /// assert_eq!(o, Less);
3449 ///
3450 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3451 /// &Rational::from_unsigneds(1u8, 3),
3452 /// 20,
3453 /// Ceiling,
3454 /// );
3455 /// assert_eq!(quotient.to_string(), "9.42479");
3456 /// assert_eq!(o, Greater);
3457 ///
3458 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3459 /// &Rational::from_unsigneds(1u8, 3),
3460 /// 20,
3461 /// Nearest,
3462 /// );
3463 /// assert_eq!(quotient.to_string(), "9.42477");
3464 /// assert_eq!(o, Less);
3465 /// ```
3466 #[inline]
3467 pub fn div_rational_prec_round_val_ref(
3468 mut self,
3469 other: &Rational,
3470 prec: u64,
3471 rm: RoundingMode,
3472 ) -> (Self, Ordering) {
3473 let o = self.div_rational_prec_round_assign_ref(other, prec, rm);
3474 (self, o)
3475 }
3476
3477 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
3478 /// with the specified rounding mode. The [`Float`] is taken by reference and the [`Rational`]
3479 /// by value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
3480 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
3481 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3482 ///
3483 /// See [`RoundingMode`] for a description of the possible rounding modes.
3484 ///
3485 /// $$
3486 /// f(x,y,p,m) = x/y+\varepsilon.
3487 /// $$
3488 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3489 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3490 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3491 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3492 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3493 ///
3494 /// If the output has a precision, it is `prec`.
3495 ///
3496 /// Special cases:
3497 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
3498 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
3499 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
3500 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
3501 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
3502 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
3503 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
3504 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
3505 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
3506 ///
3507 /// Overflow and underflow:
3508 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
3509 /// returned instead.
3510 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
3511 /// is returned instead, where `p` is the precision of the input.
3512 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
3513 /// returned instead.
3514 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
3515 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
3516 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
3517 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
3518 /// instead.
3519 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
3520 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
3521 /// instead.
3522 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
3523 /// instead.
3524 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
3525 /// instead.
3526 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
3527 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
3528 /// returned instead.
3529 ///
3530 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_ref_val`]
3531 /// instead. If you know that your target precision is the precision of the [`Float`] input,
3532 /// consider using [`Float::div_rational_round_ref_val`] instead. If both of these things are
3533 /// true, consider using `/` instead.
3534 ///
3535 /// # Worst-case complexity
3536 /// $T(n) = O(n \log n \log\log n)$
3537 ///
3538 /// $M(n) = O(n \log n)$
3539 ///
3540 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3541 /// other.significant_bits(), prec)`.
3542 ///
3543 /// # Panics
3544 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3545 ///
3546 /// # Examples
3547 /// ```
3548 /// use core::f64::consts::PI;
3549 /// use malachite_base::rounding_modes::RoundingMode::*;
3550 /// use malachite_float::Float;
3551 /// use malachite_q::Rational;
3552 /// use std::cmp::Ordering::*;
3553 ///
3554 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3555 /// Rational::from_unsigneds(1u8, 3),
3556 /// 5,
3557 /// Floor,
3558 /// );
3559 /// assert_eq!(quotient.to_string(), "9.0");
3560 /// assert_eq!(o, Less);
3561 ///
3562 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3563 /// Rational::from_unsigneds(1u8, 3),
3564 /// 5,
3565 /// Ceiling,
3566 /// );
3567 /// assert_eq!(quotient.to_string(), "9.5");
3568 /// assert_eq!(o, Greater);
3569 ///
3570 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3571 /// Rational::from_unsigneds(1u8, 3),
3572 /// 5,
3573 /// Nearest,
3574 /// );
3575 /// assert_eq!(quotient.to_string(), "9.5");
3576 /// assert_eq!(o, Greater);
3577 ///
3578 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3579 /// Rational::from_unsigneds(1u8, 3),
3580 /// 20,
3581 /// Floor,
3582 /// );
3583 /// assert_eq!(quotient.to_string(), "9.42477");
3584 /// assert_eq!(o, Less);
3585 ///
3586 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3587 /// Rational::from_unsigneds(1u8, 3),
3588 /// 20,
3589 /// Ceiling,
3590 /// );
3591 /// assert_eq!(quotient.to_string(), "9.42479");
3592 /// assert_eq!(o, Greater);
3593 ///
3594 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3595 /// Rational::from_unsigneds(1u8, 3),
3596 /// 20,
3597 /// Nearest,
3598 /// );
3599 /// assert_eq!(quotient.to_string(), "9.42477");
3600 /// assert_eq!(o, Less);
3601 /// ```
3602 #[inline]
3603 pub fn div_rational_prec_round_ref_val(
3604 &self,
3605 other: Rational,
3606 prec: u64,
3607 rm: RoundingMode,
3608 ) -> (Self, Ordering) {
3609 if !self.is_normal()
3610 || max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD
3611 {
3612 div_rational_prec_round_naive_ref_val(self, other, prec, rm)
3613 } else {
3614 div_rational_prec_round_direct_ref_val(self, other, prec, rm)
3615 }
3616 }
3617
3618 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
3619 /// with the specified rounding mode. The [`Float`] and the [`Rational`] are both taken by
3620 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
3621 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
3622 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3623 ///
3624 /// See [`RoundingMode`] for a description of the possible rounding modes.
3625 ///
3626 /// $$
3627 /// f(x,y,p,m) = x/y+\varepsilon.
3628 /// $$
3629 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3630 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3631 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3632 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
3633 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3634 ///
3635 /// If the output has a precision, it is `prec`.
3636 ///
3637 /// Special cases:
3638 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
3639 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
3640 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
3641 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
3642 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
3643 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
3644 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
3645 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
3646 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
3647 ///
3648 /// Overflow and underflow:
3649 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
3650 /// returned instead.
3651 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
3652 /// is returned instead, where `p` is the precision of the input.
3653 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
3654 /// returned instead.
3655 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
3656 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
3657 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
3658 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
3659 /// instead.
3660 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
3661 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
3662 /// instead.
3663 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
3664 /// instead.
3665 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
3666 /// instead.
3667 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
3668 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
3669 /// returned instead.
3670 ///
3671 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_ref_ref`]
3672 /// instead. If you know that your target precision is the precision of the [`Float`] input,
3673 /// consider using [`Float::div_rational_round_ref_ref`] instead. If both of these things are
3674 /// true, consider using `/` instead.
3675 ///
3676 /// # Worst-case complexity
3677 /// $T(n) = O(n \log n \log\log n)$
3678 ///
3679 /// $M(n) = O(n \log n)$
3680 ///
3681 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3682 /// other.significant_bits(), prec)`.
3683 ///
3684 /// # Panics
3685 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3686 ///
3687 /// # Examples
3688 /// ```
3689 /// use core::f64::consts::PI;
3690 /// use malachite_base::rounding_modes::RoundingMode::*;
3691 /// use malachite_float::Float;
3692 /// use malachite_q::Rational;
3693 /// use std::cmp::Ordering::*;
3694 ///
3695 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3696 /// &Rational::from_unsigneds(1u8, 3),
3697 /// 5,
3698 /// Floor,
3699 /// );
3700 /// assert_eq!(quotient.to_string(), "9.0");
3701 /// assert_eq!(o, Less);
3702 ///
3703 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3704 /// &Rational::from_unsigneds(1u8, 3),
3705 /// 5,
3706 /// Ceiling,
3707 /// );
3708 /// assert_eq!(quotient.to_string(), "9.5");
3709 /// assert_eq!(o, Greater);
3710 ///
3711 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3712 /// &Rational::from_unsigneds(1u8, 3),
3713 /// 5,
3714 /// Nearest,
3715 /// );
3716 /// assert_eq!(quotient.to_string(), "9.5");
3717 /// assert_eq!(o, Greater);
3718 ///
3719 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3720 /// &Rational::from_unsigneds(1u8, 3),
3721 /// 20,
3722 /// Floor,
3723 /// );
3724 /// assert_eq!(quotient.to_string(), "9.42477");
3725 /// assert_eq!(o, Less);
3726 ///
3727 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3728 /// &Rational::from_unsigneds(1u8, 3),
3729 /// 20,
3730 /// Ceiling,
3731 /// );
3732 /// assert_eq!(quotient.to_string(), "9.42479");
3733 /// assert_eq!(o, Greater);
3734 ///
3735 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3736 /// &Rational::from_unsigneds(1u8, 3),
3737 /// 20,
3738 /// Nearest,
3739 /// );
3740 /// assert_eq!(quotient.to_string(), "9.42477");
3741 /// assert_eq!(o, Less);
3742 /// ```
3743 #[inline]
3744 pub fn div_rational_prec_round_ref_ref(
3745 &self,
3746 other: &Rational,
3747 prec: u64,
3748 rm: RoundingMode,
3749 ) -> (Self, Ordering) {
3750 if !self.is_normal()
3751 || max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD
3752 {
3753 div_rational_prec_round_naive_ref_ref(self, other, prec, rm)
3754 } else {
3755 div_rational_prec_round_direct_ref_ref(self, other, prec, rm)
3756 }
3757 }
3758
3759 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3760 /// specified precision. The [`Float`] and the [`Rational`] are both are taken by value. An
3761 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3762 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3763 /// whenever this function returns a `NaN` it also returns `Equal`.
3764 ///
3765 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3766 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3767 /// description of the `Nearest` rounding mode.
3768 ///
3769 /// $$
3770 /// f(x,y,p) = x/y+\varepsilon.
3771 /// $$
3772 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3773 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3774 ///
3775 /// If the output has a precision, it is `prec`.
3776 ///
3777 /// Special cases:
3778 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
3779 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
3780 /// - $f(\infty,x,p)=-\infty$ if $x<0$
3781 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
3782 /// - $f(-\infty,x,p)=\infty$ if $x<0$
3783 /// - $f(0.0,x,p)=0.0$ if $x>0$
3784 /// - $f(0.0,x,p)=-0.0$ if $x<0$
3785 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
3786 /// - $f(-0.0,x,p)=0.0$ if $x<0$
3787 ///
3788 /// Overflow and underflow:
3789 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
3790 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
3791 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3792 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3793 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3794 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3795 ///
3796 /// If you want to use a rounding mode other than `Nearest`, consider using
3797 /// [`Float::div_rational_prec_round`] instead. If you know that your target precision is the
3798 /// precision of the [`Float`] input, consider using `/` instead.
3799 ///
3800 /// # Worst-case complexity
3801 /// $T(n) = O(n \log n \log\log n)$
3802 ///
3803 /// $M(n) = O(n \log n)$
3804 ///
3805 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3806 /// other.significant_bits(), prec)`.
3807 ///
3808 /// # Examples
3809 /// ```
3810 /// use core::f64::consts::PI;
3811 /// use malachite_base::num::conversion::traits::ExactFrom;
3812 /// use malachite_float::Float;
3813 /// use malachite_q::Rational;
3814 /// use std::cmp::Ordering::*;
3815 ///
3816 /// let (quotient, o) = Float::from(PI).div_rational_prec(Rational::exact_from(1.5), 5);
3817 /// assert_eq!(quotient.to_string(), "2.1");
3818 /// assert_eq!(o, Greater);
3819 ///
3820 /// let (quotient, o) = Float::from(PI).div_rational_prec(Rational::exact_from(1.5), 20);
3821 /// assert_eq!(quotient.to_string(), "2.094395");
3822 /// assert_eq!(o, Less);
3823 /// ```
3824 #[inline]
3825 pub fn div_rational_prec(self, other: Rational, prec: u64) -> (Self, Ordering) {
3826 self.div_rational_prec_round(other, prec, Nearest)
3827 }
3828
3829 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3830 /// specified precision. The [`Float`] is taken by value and the [`Rational`] by reference. An
3831 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3832 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3833 /// whenever this function returns a `NaN` it also returns `Equal`.
3834 ///
3835 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3836 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3837 /// description of the `Nearest` rounding mode.
3838 ///
3839 /// $$
3840 /// f(x,y,p) = x/y+\varepsilon.
3841 /// $$
3842 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3843 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3844 ///
3845 /// If the output has a precision, it is `prec`.
3846 ///
3847 /// Special cases:
3848 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
3849 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
3850 /// - $f(\infty,x,p)=-\infty$ if $x<0$
3851 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
3852 /// - $f(-\infty,x,p)=\infty$ if $x<0$
3853 /// - $f(0.0,x,p)=0.0$ if $x>0$
3854 /// - $f(0.0,x,p)=-0.0$ if $x<0$
3855 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
3856 /// - $f(-0.0,x,p)=0.0$ if $x<0$
3857 ///
3858 /// Overflow and underflow:
3859 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
3860 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
3861 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3862 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3863 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3864 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3865 ///
3866 /// If you want to use a rounding mode other than `Nearest`, consider using
3867 /// [`Float::div_rational_prec_round_val_ref`] instead. If you know that your target precision
3868 /// is the precision of the [`Float`] input, consider using `/` instead.
3869 ///
3870 /// # Worst-case complexity
3871 /// $T(n) = O(n \log n \log\log n)$
3872 ///
3873 /// $M(n) = O(n \log n)$
3874 ///
3875 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3876 /// other.significant_bits(), prec)`.
3877 ///
3878 /// # Examples
3879 /// ```
3880 /// use core::f64::consts::PI;
3881 /// use malachite_base::num::conversion::traits::ExactFrom;
3882 /// use malachite_float::Float;
3883 /// use malachite_q::Rational;
3884 /// use std::cmp::Ordering::*;
3885 ///
3886 /// let (quotient, o) =
3887 /// Float::from(PI).div_rational_prec_val_ref(&Rational::exact_from(1.5), 5);
3888 /// assert_eq!(quotient.to_string(), "2.1");
3889 /// assert_eq!(o, Greater);
3890 ///
3891 /// let (quotient, o) =
3892 /// Float::from(PI).div_rational_prec_val_ref(&Rational::exact_from(1.5), 20);
3893 /// assert_eq!(quotient.to_string(), "2.094395");
3894 /// assert_eq!(o, Less);
3895 /// ```
3896 #[inline]
3897 pub fn div_rational_prec_val_ref(self, other: &Rational, prec: u64) -> (Self, Ordering) {
3898 self.div_rational_prec_round_val_ref(other, prec, Nearest)
3899 }
3900
3901 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3902 /// specified precision. The [`Float`] is taken by reference and the [`Rational`] by value. An
3903 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3904 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3905 /// whenever this function returns a `NaN` it also returns `Equal`.
3906 ///
3907 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3908 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3909 /// description of the `Nearest` rounding mode.
3910 ///
3911 /// $$
3912 /// f(x,y,p) = x/y+\varepsilon.
3913 /// $$
3914 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3915 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3916 ///
3917 /// If the output has a precision, it is `prec`.
3918 ///
3919 /// Special cases:
3920 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
3921 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
3922 /// - $f(\infty,x,p)=-\infty$ if $x<0$
3923 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
3924 /// - $f(-\infty,x,p)=\infty$ if $x<0$
3925 /// - $f(0.0,x,p)=0.0$ if $x>0$
3926 /// - $f(0.0,x,p)=-0.0$ if $x<0$
3927 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
3928 /// - $f(-0.0,x,p)=0.0$ if $x<0$
3929 ///
3930 /// Overflow and underflow:
3931 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
3932 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
3933 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3934 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3935 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3936 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3937 ///
3938 /// If you want to use a rounding mode other than `Nearest`, consider using
3939 /// [`Float::div_rational_prec_round_ref_val`] instead. If you know that your target precision
3940 /// is the precision of the [`Float`] input, consider using `/` instead.
3941 ///
3942 /// # Worst-case complexity
3943 /// $T(n) = O(n \log n \log\log n)$
3944 ///
3945 /// $M(n) = O(n \log n)$
3946 ///
3947 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3948 /// other.significant_bits(), prec)`.
3949 ///
3950 /// # Examples
3951 /// ```
3952 /// use core::f64::consts::PI;
3953 /// use malachite_base::num::conversion::traits::ExactFrom;
3954 /// use malachite_float::Float;
3955 /// use malachite_q::Rational;
3956 /// use std::cmp::Ordering::*;
3957 ///
3958 /// let (quotient, o) = Float::from(PI).div_rational_prec_ref_val(Rational::exact_from(1.5), 5);
3959 /// assert_eq!(quotient.to_string(), "2.1");
3960 /// assert_eq!(o, Greater);
3961 ///
3962 /// let (quotient, o) =
3963 /// Float::from(PI).div_rational_prec_ref_val(Rational::exact_from(1.5), 20);
3964 /// assert_eq!(quotient.to_string(), "2.094395");
3965 /// assert_eq!(o, Less);
3966 /// ```
3967 #[inline]
3968 pub fn div_rational_prec_ref_val(&self, other: Rational, prec: u64) -> (Self, Ordering) {
3969 self.div_rational_prec_round_ref_val(other, prec, Nearest)
3970 }
3971
3972 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3973 /// specified precision. The [`Float`] and the [`Rational`] are both are taken by reference. An
3974 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3975 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3976 /// whenever this function returns a `NaN` it also returns `Equal`.
3977 ///
3978 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3979 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3980 /// description of the `Nearest` rounding mode.
3981 ///
3982 /// $$
3983 /// f(x,y,p) = x/y+\varepsilon.
3984 /// $$
3985 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3986 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3987 ///
3988 /// If the output has a precision, it is `prec`.
3989 ///
3990 /// Special cases:
3991 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
3992 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
3993 /// - $f(\infty,x,p)=-\infty$ if $x<0$
3994 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
3995 /// - $f(-\infty,x,p)=\infty$ if $x<0$
3996 /// - $f(0.0,x,p)=0.0$ if $x>0$
3997 /// - $f(0.0,x,p)=-0.0$ if $x<0$
3998 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
3999 /// - $f(-0.0,x,p)=0.0$ if $x<0$
4000 ///
4001 /// Overflow and underflow:
4002 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
4003 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
4004 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4005 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4006 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4007 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4008 ///
4009 /// If you want to use a rounding mode other than `Nearest`, consider using
4010 /// [`Float::div_rational_prec_round_ref_ref`] instead. If you know that your target precision
4011 /// is the precision of the [`Float`] input, consider using `/` instead.
4012 ///
4013 /// # Worst-case complexity
4014 /// $T(n) = O(n \log n \log\log n)$
4015 ///
4016 /// $M(n) = O(n \log n)$
4017 ///
4018 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4019 /// other.significant_bits(), prec)`.
4020 ///
4021 /// # Examples
4022 /// ```
4023 /// use core::f64::consts::PI;
4024 /// use malachite_base::num::conversion::traits::ExactFrom;
4025 /// use malachite_float::Float;
4026 /// use malachite_q::Rational;
4027 /// use std::cmp::Ordering::*;
4028 ///
4029 /// let (quotient, o) =
4030 /// Float::from(PI).div_rational_prec_ref_ref(&Rational::exact_from(1.5), 5);
4031 /// assert_eq!(quotient.to_string(), "2.1");
4032 /// assert_eq!(o, Greater);
4033 ///
4034 /// let (quotient, o) =
4035 /// Float::from(PI).div_rational_prec_ref_ref(&Rational::exact_from(1.5), 20);
4036 /// assert_eq!(quotient.to_string(), "2.094395");
4037 /// assert_eq!(o, Less);
4038 /// ```
4039 #[inline]
4040 pub fn div_rational_prec_ref_ref(&self, other: &Rational, prec: u64) -> (Self, Ordering) {
4041 self.div_rational_prec_round_ref_ref(other, prec, Nearest)
4042 }
4043
4044 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
4045 /// The [`Float`] and the [`Rational`] are both are taken by value. An [`Ordering`] is also
4046 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4047 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4048 /// function returns a `NaN` it also returns `Equal`.
4049 ///
4050 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4051 /// for a description of the possible rounding modes.
4052 ///
4053 /// $$
4054 /// f(x,y,m) = x/y+\varepsilon.
4055 /// $$
4056 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4057 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4058 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4059 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4060 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4061 ///
4062 /// If the output has a precision, it is the precision of the [`Float`] input.
4063 ///
4064 /// Special cases:
4065 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
4066 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
4067 /// - $f(\infty,x,m)=-\infty$ if $x<0$
4068 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
4069 /// - $f(-\infty,x,m)=\infty$ if $x<0$
4070 /// - $f(0.0,x,m)=0.0$ if $x>0$
4071 /// - $f(0.0,x,m)=-0.0$ if $x<0$
4072 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
4073 /// - $f(-0.0,x,m)=0.0$ if $x<0$
4074 ///
4075 /// Overflow and underflow:
4076 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
4077 /// returned instead.
4078 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
4079 /// returned instead, where `p` is the precision of the input.
4080 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
4081 /// returned instead.
4082 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
4083 /// is returned instead, where `p` is the precision of the input.
4084 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
4085 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
4086 /// instead.
4087 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
4088 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
4089 /// instead.
4090 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
4091 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
4092 /// instead.
4093 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
4094 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
4095 /// returned instead.
4096 ///
4097 /// If you want to specify an output precision, consider using
4098 /// [`Float::div_rational_prec_round`] instead. If you know you'll be using the `Nearest`
4099 /// rounding mode, consider using `/` instead.
4100 ///
4101 /// # Worst-case complexity
4102 /// $T(n) = O(n \log n \log\log n)$
4103 ///
4104 /// $M(n) = O(n \log n)$
4105 ///
4106 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4107 /// other.significant_bits())`.
4108 ///
4109 /// # Panics
4110 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4111 /// represent the output.
4112 ///
4113 /// # Examples
4114 /// ```
4115 /// use core::f64::consts::PI;
4116 /// use malachite_base::rounding_modes::RoundingMode::*;
4117 /// use malachite_float::Float;
4118 /// use malachite_q::Rational;
4119 /// use std::cmp::Ordering::*;
4120 ///
4121 /// let (quotient, o) =
4122 /// Float::from(PI).div_rational_round(Rational::from_unsigneds(1u8, 3), Floor);
4123 /// assert_eq!(quotient.to_string(), "9.42477796076938");
4124 /// assert_eq!(o, Less);
4125 ///
4126 /// let (quotient, o) =
4127 /// Float::from(PI).div_rational_round(Rational::from_unsigneds(1u8, 3), Ceiling);
4128 /// assert_eq!(quotient.to_string(), "9.42477796076939");
4129 /// assert_eq!(o, Greater);
4130 ///
4131 /// let (quotient, o) =
4132 /// Float::from(PI).div_rational_round(Rational::from_unsigneds(1u8, 3), Nearest);
4133 /// assert_eq!(quotient.to_string(), "9.42477796076938");
4134 /// assert_eq!(o, Less);
4135 /// ```
4136 #[inline]
4137 pub fn div_rational_round(self, other: Rational, rm: RoundingMode) -> (Self, Ordering) {
4138 let prec = self.significant_bits();
4139 self.div_rational_prec_round(other, prec, rm)
4140 }
4141
4142 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
4143 /// The [`Float`] is taken by value and the [`Rational`] by reference. An [`Ordering`] is also
4144 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4145 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4146 /// function returns a `NaN` it also returns `Equal`.
4147 ///
4148 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4149 /// for a description of the possible rounding modes.
4150 ///
4151 /// $$
4152 /// f(x,y,m) = x/y+\varepsilon.
4153 /// $$
4154 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4155 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4156 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4157 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4158 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4159 ///
4160 /// If the output has a precision, it is the precision of the [`Float`] input.
4161 ///
4162 /// Special cases:
4163 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
4164 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
4165 /// - $f(\infty,x,m)=-\infty$ if $x<0$
4166 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
4167 /// - $f(-\infty,x,m)=\infty$ if $x<0$
4168 /// - $f(0.0,x,m)=0.0$ if $x>0$
4169 /// - $f(0.0,x,m)=-0.0$ if $x<0$
4170 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
4171 /// - $f(-0.0,x,m)=0.0$ if $x<0$
4172 ///
4173 /// Overflow and underflow:
4174 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
4175 /// returned instead.
4176 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
4177 /// returned instead, where `p` is the precision of the input.
4178 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
4179 /// returned instead.
4180 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
4181 /// is returned instead, where `p` is the precision of the input.
4182 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
4183 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
4184 /// instead.
4185 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
4186 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
4187 /// instead.
4188 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
4189 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
4190 /// instead.
4191 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
4192 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
4193 /// returned instead.
4194 ///
4195 /// If you want to specify an output precision, consider using
4196 /// [`Float::div_rational_prec_round_val_ref`] instead. If you know you'll be using the
4197 /// `Nearest` rounding mode, consider using `/` instead.
4198 ///
4199 /// # Worst-case complexity
4200 /// $T(n) = O(n \log n \log\log n)$
4201 ///
4202 /// $M(n) = O(n \log n)$
4203 ///
4204 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4205 /// other.significant_bits())`.
4206 ///
4207 /// # Panics
4208 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4209 /// represent the output.
4210 ///
4211 /// # Examples
4212 /// ```
4213 /// use core::f64::consts::PI;
4214 /// use malachite_base::rounding_modes::RoundingMode::*;
4215 /// use malachite_float::Float;
4216 /// use malachite_q::Rational;
4217 /// use std::cmp::Ordering::*;
4218 ///
4219 /// let (quotient, o) =
4220 /// Float::from(PI).div_rational_round_val_ref(&Rational::from_unsigneds(1u8, 3), Floor);
4221 /// assert_eq!(quotient.to_string(), "9.42477796076938");
4222 /// assert_eq!(o, Less);
4223 ///
4224 /// let (quotient, o) =
4225 /// Float::from(PI).div_rational_round_val_ref(&Rational::from_unsigneds(1u8, 3), Ceiling);
4226 /// assert_eq!(quotient.to_string(), "9.42477796076939");
4227 /// assert_eq!(o, Greater);
4228 ///
4229 /// let (quotient, o) =
4230 /// Float::from(PI).div_rational_round_val_ref(&Rational::from_unsigneds(1u8, 3), Nearest);
4231 /// assert_eq!(quotient.to_string(), "9.42477796076938");
4232 /// assert_eq!(o, Less);
4233 /// ```
4234 #[inline]
4235 pub fn div_rational_round_val_ref(
4236 self,
4237 other: &Rational,
4238 rm: RoundingMode,
4239 ) -> (Self, Ordering) {
4240 let prec = self.significant_bits();
4241 self.div_rational_prec_round_val_ref(other, prec, rm)
4242 }
4243
4244 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
4245 /// The [`Float`] is taken by reference and the [`Rational`] by value. An [`Ordering`] is also
4246 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4247 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4248 /// function returns a `NaN` it also returns `Equal`.
4249 ///
4250 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4251 /// for a description of the possible rounding modes.
4252 ///
4253 /// $$
4254 /// f(x,y,m) = x/y+\varepsilon.
4255 /// $$
4256 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4257 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4258 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4259 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4260 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4261 ///
4262 /// If the output has a precision, it is the precision of the [`Float`] input.
4263 ///
4264 /// Special cases:
4265 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
4266 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
4267 /// - $f(\infty,x,m)=-\infty$ if $x<0$
4268 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
4269 /// - $f(-\infty,x,m)=\infty$ if $x<0$
4270 /// - $f(0.0,x,m)=0.0$ if $x>0$
4271 /// - $f(0.0,x,m)=-0.0$ if $x<0$
4272 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
4273 /// - $f(-0.0,x,m)=0.0$ if $x<0$
4274 ///
4275 /// Overflow and underflow:
4276 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
4277 /// returned instead.
4278 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
4279 /// returned instead, where `p` is the precision of the input.
4280 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
4281 /// returned instead.
4282 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
4283 /// is returned instead, where `p` is the precision of the input.
4284 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
4285 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
4286 /// instead.
4287 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
4288 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
4289 /// instead.
4290 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
4291 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
4292 /// instead.
4293 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
4294 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
4295 /// returned instead.
4296 ///
4297 /// If you want to specify an output precision, consider using
4298 /// [`Float::div_rational_prec_round_ref_val`] instead. If you know you'll be using the
4299 /// `Nearest` rounding mode, consider using `/` instead.
4300 ///
4301 /// # Worst-case complexity
4302 /// $T(n) = O(n \log n \log\log n)$
4303 ///
4304 /// $M(n) = O(n \log n)$
4305 ///
4306 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4307 /// other.significant_bits())`.
4308 ///
4309 /// # Panics
4310 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4311 /// represent the output.
4312 ///
4313 /// # Examples
4314 /// ```
4315 /// use core::f64::consts::PI;
4316 /// use malachite_base::rounding_modes::RoundingMode::*;
4317 /// use malachite_float::Float;
4318 /// use malachite_q::Rational;
4319 /// use std::cmp::Ordering::*;
4320 ///
4321 /// let (quotient, o) =
4322 /// Float::from(PI).div_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Floor);
4323 /// assert_eq!(quotient.to_string(), "9.42477796076938");
4324 /// assert_eq!(o, Less);
4325 ///
4326 /// let (quotient, o) =
4327 /// Float::from(PI).div_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Ceiling);
4328 /// assert_eq!(quotient.to_string(), "9.42477796076939");
4329 /// assert_eq!(o, Greater);
4330 ///
4331 /// let (quotient, o) =
4332 /// Float::from(PI).div_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Nearest);
4333 /// assert_eq!(quotient.to_string(), "9.42477796076938");
4334 /// assert_eq!(o, Less);
4335 /// ```
4336 #[inline]
4337 pub fn div_rational_round_ref_val(
4338 &self,
4339 other: Rational,
4340 rm: RoundingMode,
4341 ) -> (Self, Ordering) {
4342 let prec = self.significant_bits();
4343 self.div_rational_prec_round_ref_val(other, prec, rm)
4344 }
4345
4346 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
4347 /// The [`Float`] and the [`Rational`] are both are taken by reference. An [`Ordering`] is also
4348 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4349 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4350 /// function returns a `NaN` it also returns `Equal`.
4351 ///
4352 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4353 /// for a description of the possible rounding modes.
4354 ///
4355 /// $$
4356 /// f(x,y,m) = x/y+\varepsilon.
4357 /// $$
4358 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4359 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4360 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4361 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4362 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4363 ///
4364 /// If the output has a precision, it is the precision of the [`Float`] input.
4365 ///
4366 /// Special cases:
4367 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
4368 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
4369 /// - $f(\infty,x,m)=-\infty$ if $x<0$
4370 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
4371 /// - $f(-\infty,x,m)=\infty$ if $x<0$
4372 /// - $f(0.0,x,m)=0.0$ if $x>0$
4373 /// - $f(0.0,x,m)=-0.0$ if $x<0$
4374 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
4375 /// - $f(-0.0,x,m)=0.0$ if $x<0$
4376 ///
4377 /// Overflow and underflow:
4378 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
4379 /// returned instead.
4380 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
4381 /// returned instead, where `p` is the precision of the input.
4382 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
4383 /// returned instead.
4384 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
4385 /// is returned instead, where `p` is the precision of the input.
4386 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
4387 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
4388 /// instead.
4389 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
4390 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
4391 /// instead.
4392 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
4393 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
4394 /// instead.
4395 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
4396 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
4397 /// returned instead.
4398 ///
4399 /// If you want to specify an output precision, consider using
4400 /// [`Float::div_rational_prec_round_ref_ref`] instead. If you know you'll be using the
4401 /// `Nearest` rounding mode, consider using `/` instead.
4402 ///
4403 /// # Worst-case complexity
4404 /// $T(n) = O(n \log n \log\log n)$
4405 ///
4406 /// $M(n) = O(n \log n)$
4407 ///
4408 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4409 /// other.significant_bits())`.
4410 ///
4411 /// # Panics
4412 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4413 /// represent the output.
4414 ///
4415 /// # Examples
4416 /// ```
4417 /// use core::f64::consts::PI;
4418 /// use malachite_base::rounding_modes::RoundingMode::*;
4419 /// use malachite_float::Float;
4420 /// use malachite_q::Rational;
4421 /// use std::cmp::Ordering::*;
4422 ///
4423 /// let (quotient, o) =
4424 /// Float::from(PI).div_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Floor);
4425 /// assert_eq!(quotient.to_string(), "9.42477796076938");
4426 /// assert_eq!(o, Less);
4427 ///
4428 /// let (quotient, o) =
4429 /// Float::from(PI).div_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Ceiling);
4430 /// assert_eq!(quotient.to_string(), "9.42477796076939");
4431 /// assert_eq!(o, Greater);
4432 ///
4433 /// let (quotient, o) =
4434 /// Float::from(PI).div_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Nearest);
4435 /// assert_eq!(quotient.to_string(), "9.42477796076938");
4436 /// assert_eq!(o, Less);
4437 /// ```
4438 #[inline]
4439 pub fn div_rational_round_ref_ref(
4440 &self,
4441 other: &Rational,
4442 rm: RoundingMode,
4443 ) -> (Self, Ordering) {
4444 let prec = self.significant_bits();
4445 self.div_rational_prec_round_ref_ref(other, prec, rm)
4446 }
4447
4448 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the specified
4449 /// precision and with the specified rounding mode. The [`Rational`] is taken by value. An
4450 /// [`Ordering`] is returned, indicating whether the rounded quotient is less than, equal to, or
4451 /// greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
4452 /// whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
4453 ///
4454 /// See [`RoundingMode`] for a description of the possible rounding modes.
4455 ///
4456 /// $$
4457 /// x \gets x/y+\varepsilon.
4458 /// $$
4459 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4460 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4461 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4462 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4463 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4464 ///
4465 /// If the output has a precision, it is `prec`.
4466 ///
4467 /// See the [`Float::div_rational_prec_round`] documentation for information on special cases,
4468 /// overflow, and underflow.
4469 ///
4470 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_assign`]
4471 /// instead. If you know that your target precision is the precision of the [`Float`] input,
4472 /// consider using [`Float::div_rational_round_assign`] instead. If both of these things are
4473 /// true, consider using `/=` instead.
4474 ///
4475 /// # Worst-case complexity
4476 /// $T(n) = O(n \log n \log\log n)$
4477 ///
4478 /// $M(n) = O(n \log n)$
4479 ///
4480 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4481 /// other.significant_bits(), prec)`.
4482 ///
4483 /// # Panics
4484 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4485 ///
4486 /// # Examples
4487 /// ```
4488 /// use core::f64::consts::PI;
4489 /// use malachite_base::rounding_modes::RoundingMode::*;
4490 /// use malachite_float::Float;
4491 /// use malachite_q::Rational;
4492 /// use std::cmp::Ordering::*;
4493 ///
4494 /// let mut x = Float::from(PI);
4495 /// assert_eq!(
4496 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 5, Floor),
4497 /// Less
4498 /// );
4499 /// assert_eq!(x.to_string(), "9.0");
4500 ///
4501 /// let mut x = Float::from(PI);
4502 /// assert_eq!(
4503 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 5, Ceiling),
4504 /// Greater
4505 /// );
4506 /// assert_eq!(x.to_string(), "9.5");
4507 ///
4508 /// let mut x = Float::from(PI);
4509 /// assert_eq!(
4510 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 5, Nearest),
4511 /// Greater
4512 /// );
4513 /// assert_eq!(x.to_string(), "9.5");
4514 ///
4515 /// let mut x = Float::from(PI);
4516 /// assert_eq!(
4517 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 20, Floor),
4518 /// Less
4519 /// );
4520 /// assert_eq!(x.to_string(), "9.42477");
4521 ///
4522 /// let mut x = Float::from(PI);
4523 /// assert_eq!(
4524 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 20, Ceiling),
4525 /// Greater
4526 /// );
4527 /// assert_eq!(x.to_string(), "9.42479");
4528 ///
4529 /// let mut x = Float::from(PI);
4530 /// assert_eq!(
4531 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 20, Nearest),
4532 /// Less
4533 /// );
4534 /// assert_eq!(x.to_string(), "9.42477");
4535 /// ```
4536 #[inline]
4537 pub fn div_rational_prec_round_assign(
4538 &mut self,
4539 other: Rational,
4540 prec: u64,
4541 rm: RoundingMode,
4542 ) -> Ordering {
4543 if !self.is_normal()
4544 || max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD
4545 {
4546 div_rational_prec_round_assign_naive(self, other, prec, rm)
4547 } else {
4548 div_rational_prec_round_assign_direct(self, other, prec, rm)
4549 }
4550 }
4551
4552 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the specified
4553 /// precision and with the specified rounding mode. The [`Rational`] is taken by reference. An
4554 /// [`Ordering`] is returned, indicating whether the rounded quotient is less than, equal to, or
4555 /// greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
4556 /// whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
4557 ///
4558 /// See [`RoundingMode`] for a description of the possible rounding modes.
4559 ///
4560 /// $$
4561 /// x \gets x/y+\varepsilon.
4562 /// $$
4563 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4564 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4565 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4566 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4567 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4568 ///
4569 /// If the output has a precision, it is `prec`.
4570 ///
4571 /// See the [`Float::div_rational_prec_round`] documentation for information on special cases,
4572 /// overflow, and underflow.
4573 ///
4574 /// If you know you'll be using `Nearest`, consider using
4575 /// [`Float::div_rational_prec_assign_ref`] instead. If you know that your target precision is
4576 /// the precision of the [`Float`] input, consider using
4577 /// [`Float::div_rational_round_assign_ref`] instead. If both of these things are true, consider
4578 /// using `/=` instead.
4579 ///
4580 /// # Worst-case complexity
4581 /// $T(n) = O(n \log n \log\log n)$
4582 ///
4583 /// $M(n) = O(n \log n)$
4584 ///
4585 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4586 /// other.significant_bits(), prec)`.
4587 ///
4588 /// # Panics
4589 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4590 ///
4591 /// # Examples
4592 /// ```
4593 /// use core::f64::consts::PI;
4594 /// use malachite_base::rounding_modes::RoundingMode::*;
4595 /// use malachite_float::Float;
4596 /// use malachite_q::Rational;
4597 /// use std::cmp::Ordering::*;
4598 ///
4599 /// let mut x = Float::from(PI);
4600 /// assert_eq!(
4601 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 5, Floor),
4602 /// Less
4603 /// );
4604 /// assert_eq!(x.to_string(), "9.0");
4605 ///
4606 /// let mut x = Float::from(PI);
4607 /// assert_eq!(
4608 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 5, Ceiling),
4609 /// Greater
4610 /// );
4611 /// assert_eq!(x.to_string(), "9.5");
4612 ///
4613 /// let mut x = Float::from(PI);
4614 /// assert_eq!(
4615 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 5, Nearest),
4616 /// Greater
4617 /// );
4618 /// assert_eq!(x.to_string(), "9.5");
4619 ///
4620 /// let mut x = Float::from(PI);
4621 /// assert_eq!(
4622 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 20, Floor),
4623 /// Less
4624 /// );
4625 /// assert_eq!(x.to_string(), "9.42477");
4626 ///
4627 /// let mut x = Float::from(PI);
4628 /// assert_eq!(
4629 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 20, Ceiling),
4630 /// Greater
4631 /// );
4632 /// assert_eq!(x.to_string(), "9.42479");
4633 ///
4634 /// let mut x = Float::from(PI);
4635 /// assert_eq!(
4636 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 20, Nearest),
4637 /// Less
4638 /// );
4639 /// assert_eq!(x.to_string(), "9.42477");
4640 /// ```
4641 #[inline]
4642 pub fn div_rational_prec_round_assign_ref(
4643 &mut self,
4644 other: &Rational,
4645 prec: u64,
4646 rm: RoundingMode,
4647 ) -> Ordering {
4648 if !self.is_normal()
4649 || max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD
4650 {
4651 div_rational_prec_round_assign_naive_ref(self, other, prec, rm)
4652 } else {
4653 div_rational_prec_round_assign_direct_ref(self, other, prec, rm)
4654 }
4655 }
4656
4657 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the nearest value of
4658 /// the specified precision. The [`Rational`] is taken by value. An [`Ordering`] is returned,
4659 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
4660 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
4661 /// the [`Float`] to `NaN` it also returns `Equal`.
4662 ///
4663 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4664 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4665 /// description of the `Nearest` rounding mode.
4666 ///
4667 /// $$
4668 /// x \gets x/y+\varepsilon.
4669 /// $$
4670 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4671 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4672 ///
4673 /// If the output has a precision, it is `prec`.
4674 ///
4675 /// See the [`Float::div_rational_prec`] documentation for information on special cases,
4676 /// overflow, and underflow.
4677 ///
4678 /// If you want to use a rounding mode other than `Nearest`, consider using
4679 /// [`Float::div_rational_prec_round_assign`] instead. If you know that your target precision is
4680 /// the maximum of the precisions of the two inputs, consider using `/=` instead.
4681 ///
4682 /// # Worst-case complexity
4683 /// $T(n) = O(n \log n \log\log n)$
4684 ///
4685 /// $M(n) = O(n \log n)$
4686 ///
4687 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4688 /// other.significant_bits(), prec)`.
4689 ///
4690 /// # Examples
4691 /// ```
4692 /// use core::f64::consts::PI;
4693 /// use malachite_base::num::conversion::traits::ExactFrom;
4694 /// use malachite_float::Float;
4695 /// use malachite_q::Rational;
4696 /// use std::cmp::Ordering::*;
4697 ///
4698 /// let mut x = Float::from(PI);
4699 /// assert_eq!(
4700 /// x.div_rational_prec_assign(Rational::exact_from(1.5), 5),
4701 /// Greater
4702 /// );
4703 /// assert_eq!(x.to_string(), "2.1");
4704 ///
4705 /// let mut x = Float::from(PI);
4706 /// assert_eq!(
4707 /// x.div_rational_prec_assign(Rational::exact_from(1.5), 20),
4708 /// Less
4709 /// );
4710 /// assert_eq!(x.to_string(), "2.094395");
4711 /// ```
4712 #[inline]
4713 pub fn div_rational_prec_assign(&mut self, other: Rational, prec: u64) -> Ordering {
4714 self.div_rational_prec_round_assign(other, prec, Nearest)
4715 }
4716
4717 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the nearest value of
4718 /// the specified precision. The [`Rational`] is taken by reference. An [`Ordering`] is
4719 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4720 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4721 /// function sets the [`Float`] to `NaN` it also returns `Equal`.
4722 ///
4723 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4724 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4725 /// description of the `Nearest` rounding mode.
4726 ///
4727 /// $$
4728 /// x \gets x/y+\varepsilon.
4729 /// $$
4730 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4731 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4732 ///
4733 /// If the output has a precision, it is `prec`.
4734 ///
4735 /// See the [`Float::div_rational_prec`] documentation for information on special cases,
4736 /// overflow, and underflow.
4737 ///
4738 /// If you want to use a rounding mode other than `Nearest`, consider using
4739 /// [`Float::div_rational_prec_round_assign`] instead. If you know that your target precision is
4740 /// the maximum of the precisions of the two inputs, consider using `/=` instead.
4741 ///
4742 /// # Worst-case complexity
4743 /// $T(n) = O(n \log n \log\log n)$
4744 ///
4745 /// $M(n) = O(n \log n)$
4746 ///
4747 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4748 /// other.significant_bits(), prec)`.
4749 ///
4750 /// # Examples
4751 /// ```
4752 /// use core::f64::consts::PI;
4753 /// use malachite_base::num::conversion::traits::ExactFrom;
4754 /// use malachite_float::Float;
4755 /// use malachite_q::Rational;
4756 /// use std::cmp::Ordering::*;
4757 ///
4758 /// let mut x = Float::from(PI);
4759 /// assert_eq!(
4760 /// x.div_rational_prec_assign_ref(&Rational::exact_from(1.5), 5),
4761 /// Greater
4762 /// );
4763 /// assert_eq!(x.to_string(), "2.1");
4764 ///
4765 /// let mut x = Float::from(PI);
4766 /// assert_eq!(
4767 /// x.div_rational_prec_assign_ref(&Rational::exact_from(1.5), 20),
4768 /// Less
4769 /// );
4770 /// assert_eq!(x.to_string(), "2.094395");
4771 /// ```
4772 #[inline]
4773 pub fn div_rational_prec_assign_ref(&mut self, other: &Rational, prec: u64) -> Ordering {
4774 self.div_rational_prec_round_assign_ref(other, prec, Nearest)
4775 }
4776
4777 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result with the specified
4778 /// rounding mode. The [`Rational`] is taken by value. An [`Ordering`] is returned, indicating
4779 /// whether the rounded quotient is less than, equal to, or greater than the exact quotient.
4780 /// Although `NaN`s are not comparable to any [`Float`], whenever this function sets the
4781 /// [`Float`] to `NaN` it also returns `Equal`.
4782 ///
4783 /// The precision of the output is the precision of the input [`Float`]. See [`RoundingMode`]
4784 /// for a description of the possible rounding modes.
4785 ///
4786 /// $$
4787 /// x \gets x/y+\varepsilon.
4788 /// $$
4789 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4790 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4791 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4792 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4793 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4794 ///
4795 /// If the output has a precision, it is the precision of the input [`Float`].
4796 ///
4797 /// See the [`Float::div_rational_round`] documentation for information on special cases,
4798 /// overflow, and underflow.
4799 ///
4800 /// If you want to specify an output precision, consider using
4801 /// [`Float::div_rational_prec_round_assign`] instead. If you know you'll be using the `Nearest`
4802 /// rounding mode, consider using `/=` instead.
4803 ///
4804 /// # Worst-case complexity
4805 /// $T(n) = O(n \log n \log\log n)$
4806 ///
4807 /// $M(n) = O(n \log n)$
4808 ///
4809 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4810 /// other.significant_bits())`.
4811 ///
4812 /// # Panics
4813 /// Panics if `rm` is `Exact` but the precision of the input [`Float`] is not high enough to
4814 /// represent the output.
4815 ///
4816 /// # Examples
4817 /// ```
4818 /// use core::f64::consts::PI;
4819 /// use malachite_base::rounding_modes::RoundingMode::*;
4820 /// use malachite_float::Float;
4821 /// use malachite_q::Rational;
4822 /// use std::cmp::Ordering::*;
4823 ///
4824 /// let mut x = Float::from(PI);
4825 /// assert_eq!(
4826 /// x.div_rational_round_assign(Rational::from_unsigneds(1u8, 3), Floor),
4827 /// Less
4828 /// );
4829 /// assert_eq!(x.to_string(), "9.42477796076938");
4830 ///
4831 /// let mut x = Float::from(PI);
4832 /// assert_eq!(
4833 /// x.div_rational_round_assign(Rational::from_unsigneds(1u8, 3), Ceiling),
4834 /// Greater
4835 /// );
4836 /// assert_eq!(x.to_string(), "9.42477796076939");
4837 ///
4838 /// let mut x = Float::from(PI);
4839 /// assert_eq!(
4840 /// x.div_rational_round_assign(Rational::from_unsigneds(1u8, 3), Nearest),
4841 /// Less
4842 /// );
4843 /// assert_eq!(x.to_string(), "9.42477796076938");
4844 /// ```
4845 #[inline]
4846 pub fn div_rational_round_assign(&mut self, other: Rational, rm: RoundingMode) -> Ordering {
4847 let prec = self.significant_bits();
4848 self.div_rational_prec_round_assign(other, prec, rm)
4849 }
4850
4851 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result with the specified
4852 /// rounding mode. The [`Rational`] is taken by reference. An [`Ordering`] is returned,
4853 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
4854 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
4855 /// the [`Float`] to `NaN` it also returns `Equal`.
4856 ///
4857 /// The precision of the output is the precision of the input [`Float`]. See [`RoundingMode`]
4858 /// for a description of the possible rounding modes.
4859 ///
4860 /// $$
4861 /// x \gets x/y+\varepsilon.
4862 /// $$
4863 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4864 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4865 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4866 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4867 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4868 ///
4869 /// If the output has a precision, it is the precision of the input [`Float`].
4870 ///
4871 /// See the [`Float::div_rational_round`] documentation for information on special cases,
4872 /// overflow, and underflow.
4873 ///
4874 /// If you want to specify an output precision, consider using
4875 /// [`Float::div_rational_prec_round_assign`] instead. If you know you'll be using the `Nearest`
4876 /// rounding mode, consider using `/=` instead.
4877 ///
4878 /// # Worst-case complexity
4879 /// $T(n) = O(n \log n \log\log n)$
4880 ///
4881 /// $M(n) = O(n \log n)$
4882 ///
4883 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4884 /// other.significant_bits())`.
4885 ///
4886 /// # Panics
4887 /// Panics if `rm` is `Exact` but the precision of the input [`Float`] is not high enough to
4888 /// represent the output.
4889 ///
4890 /// # Examples
4891 /// ```
4892 /// use core::f64::consts::PI;
4893 /// use malachite_base::rounding_modes::RoundingMode::*;
4894 /// use malachite_float::Float;
4895 /// use malachite_q::Rational;
4896 /// use std::cmp::Ordering::*;
4897 ///
4898 /// let mut x = Float::from(PI);
4899 /// assert_eq!(
4900 /// x.div_rational_round_assign_ref(&Rational::from_unsigneds(1u8, 3), Floor),
4901 /// Less
4902 /// );
4903 /// assert_eq!(x.to_string(), "9.42477796076938");
4904 ///
4905 /// let mut x = Float::from(PI);
4906 /// assert_eq!(
4907 /// x.div_rational_round_assign_ref(&Rational::from_unsigneds(1u8, 3), Ceiling),
4908 /// Greater
4909 /// );
4910 /// assert_eq!(x.to_string(), "9.42477796076939");
4911 ///
4912 /// let mut x = Float::from(PI);
4913 /// assert_eq!(
4914 /// x.div_rational_round_assign_ref(&Rational::from_unsigneds(1u8, 3), Nearest),
4915 /// Less
4916 /// );
4917 /// assert_eq!(x.to_string(), "9.42477796076938");
4918 /// ```
4919 #[inline]
4920 pub fn div_rational_round_assign_ref(
4921 &mut self,
4922 other: &Rational,
4923 rm: RoundingMode,
4924 ) -> Ordering {
4925 let prec = self.significant_bits();
4926 self.div_rational_prec_round_assign_ref(other, prec, rm)
4927 }
4928
4929 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
4930 /// with the specified rounding mode. The [`Rational`] and the [`Float`] are both taken by
4931 /// value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
4932 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
4933 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4934 ///
4935 /// See [`RoundingMode`] for a description of the possible rounding modes.
4936 ///
4937 /// $$
4938 /// f(x,y,p,m) = x/y+\varepsilon.
4939 /// $$
4940 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4941 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4942 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4943 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
4944 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4945 ///
4946 /// If the output has a precision, it is `prec`.
4947 ///
4948 /// Special cases:
4949 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
4950 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
4951 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
4952 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
4953 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
4954 /// - $f(0,x,p,m)=0.0$ if $x>0$
4955 /// - $f(0,x,p,m)=-0.0$ if $x<0$
4956 ///
4957 /// Overflow and underflow:
4958 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
4959 /// returned instead.
4960 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
4961 /// is returned instead, where `p` is the precision of the input.
4962 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
4963 /// returned instead.
4964 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
4965 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
4966 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
4967 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
4968 /// instead.
4969 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
4970 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
4971 /// instead.
4972 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
4973 /// instead.
4974 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
4975 /// instead.
4976 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
4977 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
4978 /// returned instead.
4979 ///
4980 /// If you know you'll be using `Nearest`, consider using [`Float::rational_div_float_prec`]
4981 /// instead. If you know that your target precision is the precision of the [`Float`] input,
4982 /// consider using [`Float::rational_div_float_round`] instead. If both of these things are
4983 /// true, consider using `/` instead.
4984 ///
4985 /// # Worst-case complexity
4986 /// $T(n) = O(n \log n \log\log n)$
4987 ///
4988 /// $M(n) = O(n \log n)$
4989 ///
4990 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
4991 /// y.significant_bits(), prec)`.
4992 ///
4993 /// # Panics
4994 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4995 ///
4996 /// # Examples
4997 /// ```
4998 /// use core::f64::consts::PI;
4999 /// use malachite_base::rounding_modes::RoundingMode::*;
5000 /// use malachite_float::Float;
5001 /// use malachite_q::Rational;
5002 /// use std::cmp::Ordering::*;
5003 ///
5004 /// let (quotient, o) =
5005 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 5, Floor);
5006 /// assert_eq!(quotient.to_string(), "0.94");
5007 /// assert_eq!(o, Less);
5008 ///
5009 /// let (quotient, o) =
5010 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 5, Ceiling);
5011 /// assert_eq!(quotient.to_string(), "0.97");
5012 /// assert_eq!(o, Greater);
5013 ///
5014 /// let (quotient, o) =
5015 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 5, Nearest);
5016 /// assert_eq!(quotient.to_string(), "0.97");
5017 /// assert_eq!(o, Greater);
5018 ///
5019 /// let (quotient, o) =
5020 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 20, Floor);
5021 /// assert_eq!(quotient.to_string(), "0.954929");
5022 /// assert_eq!(o, Less);
5023 ///
5024 /// let (quotient, o) =
5025 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 20, Ceiling);
5026 /// assert_eq!(quotient.to_string(), "0.95493");
5027 /// assert_eq!(o, Greater);
5028 ///
5029 /// let (quotient, o) =
5030 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 20, Nearest);
5031 /// assert_eq!(quotient.to_string(), "0.954929");
5032 /// assert_eq!(o, Less);
5033 /// ```
5034 #[inline]
5035 pub fn rational_div_float_prec_round(
5036 x: Rational,
5037 y: Self,
5038 prec: u64,
5039 rm: RoundingMode,
5040 ) -> (Self, Ordering) {
5041 if !y.is_normal() || max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
5042 rational_div_float_prec_round_naive(x, y, prec, rm)
5043 } else {
5044 rational_div_float_prec_round_direct(x, y, prec, rm)
5045 }
5046 }
5047
5048 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
5049 /// with the specified rounding mode. The [`Rational`] is taken by value and the [`Float`] by
5050 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
5051 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
5052 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
5053 ///
5054 /// See [`RoundingMode`] for a description of the possible rounding modes.
5055 ///
5056 /// $$
5057 /// f(x,y,p,m) = x/y+\varepsilon.
5058 /// $$
5059 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5060 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5061 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
5062 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
5063 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5064 ///
5065 /// If the output has a precision, it is `prec`.
5066 ///
5067 /// Special cases:
5068 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
5069 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
5070 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
5071 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
5072 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
5073 /// - $f(0,x,p,m)=0.0$ if $x>0$
5074 /// - $f(0,x,p,m)=-0.0$ if $x<0$
5075 ///
5076 /// Overflow and underflow:
5077 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5078 /// returned instead.
5079 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
5080 /// is returned instead, where `p` is the precision of the input.
5081 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
5082 /// returned instead.
5083 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
5084 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
5085 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5086 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
5087 /// instead.
5088 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
5089 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
5090 /// instead.
5091 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
5092 /// instead.
5093 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
5094 /// instead.
5095 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
5096 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
5097 /// returned instead.
5098 ///
5099 /// If you know you'll be using `Nearest`, consider using
5100 /// [`Float::rational_div_float_prec_val_ref`] instead. If you know that your target precision
5101 /// is the precision of the [`Float`] input, consider using
5102 /// [`Float::rational_div_float_round_val_ref`] instead. If both of these things are true,
5103 /// consider using `/` instead.
5104 ///
5105 /// # Worst-case complexity
5106 /// $T(n) = O(n \log n \log\log n)$
5107 ///
5108 /// $M(n) = O(n \log n)$
5109 ///
5110 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5111 /// y.significant_bits(), prec)`.
5112 ///
5113 /// # Panics
5114 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
5115 ///
5116 /// # Examples
5117 /// ```
5118 /// use core::f64::consts::PI;
5119 /// use malachite_base::rounding_modes::RoundingMode::*;
5120 /// use malachite_float::Float;
5121 /// use malachite_q::Rational;
5122 /// use std::cmp::Ordering::*;
5123 ///
5124 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
5125 /// Rational::from(3),
5126 /// &Float::from(PI),
5127 /// 5,
5128 /// Floor,
5129 /// );
5130 /// assert_eq!(quotient.to_string(), "0.94");
5131 /// assert_eq!(o, Less);
5132 ///
5133 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
5134 /// Rational::from(3),
5135 /// &Float::from(PI),
5136 /// 5,
5137 /// Ceiling,
5138 /// );
5139 /// assert_eq!(quotient.to_string(), "0.97");
5140 /// assert_eq!(o, Greater);
5141 ///
5142 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
5143 /// Rational::from(3),
5144 /// &Float::from(PI),
5145 /// 5,
5146 /// Nearest,
5147 /// );
5148 /// assert_eq!(quotient.to_string(), "0.97");
5149 /// assert_eq!(o, Greater);
5150 ///
5151 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
5152 /// Rational::from(3),
5153 /// &Float::from(PI),
5154 /// 20,
5155 /// Floor,
5156 /// );
5157 /// assert_eq!(quotient.to_string(), "0.954929");
5158 /// assert_eq!(o, Less);
5159 ///
5160 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
5161 /// Rational::from(3),
5162 /// &Float::from(PI),
5163 /// 20,
5164 /// Ceiling,
5165 /// );
5166 /// assert_eq!(quotient.to_string(), "0.95493");
5167 /// assert_eq!(o, Greater);
5168 ///
5169 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
5170 /// Rational::from(3),
5171 /// &Float::from(PI),
5172 /// 20,
5173 /// Nearest,
5174 /// );
5175 /// assert_eq!(quotient.to_string(), "0.954929");
5176 /// assert_eq!(o, Less);
5177 /// ```
5178 #[inline]
5179 pub fn rational_div_float_prec_round_val_ref(
5180 x: Rational,
5181 y: &Self,
5182 prec: u64,
5183 rm: RoundingMode,
5184 ) -> (Self, Ordering) {
5185 if !y.is_normal() || max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
5186 rational_div_float_prec_round_naive_val_ref(x, y, prec, rm)
5187 } else {
5188 rational_div_float_prec_round_direct_val_ref(x, y, prec, rm)
5189 }
5190 }
5191
5192 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
5193 /// with the specified rounding mode. The [`Rational`] is taken by reference and the [`Float`]
5194 /// by value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
5195 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
5196 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
5197 ///
5198 /// See [`RoundingMode`] for a description of the possible rounding modes.
5199 ///
5200 /// $$
5201 /// f(x,y,p,m) = x/y+\varepsilon.
5202 /// $$
5203 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5204 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5205 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
5206 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
5207 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5208 ///
5209 /// If the output has a precision, it is `prec`.
5210 ///
5211 /// Special cases:
5212 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
5213 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
5214 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
5215 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
5216 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
5217 /// - $f(0,x,p,m)=0.0$ if $x>0$
5218 /// - $f(0,x,p,m)=-0.0$ if $x<0$
5219 ///
5220 /// Overflow and underflow:
5221 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5222 /// returned instead.
5223 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
5224 /// is returned instead, where `p` is the precision of the input.
5225 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
5226 /// returned instead.
5227 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
5228 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
5229 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5230 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
5231 /// instead.
5232 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
5233 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
5234 /// instead.
5235 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
5236 /// instead.
5237 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
5238 /// instead.
5239 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
5240 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
5241 /// returned instead.
5242 ///
5243 /// If you know you'll be using `Nearest`, consider using
5244 /// [`Float::rational_div_float_prec_ref_val`] instead. If you know that your target precision
5245 /// is the precision of the [`Float`] input, consider using
5246 /// [`Float::rational_div_float_round_ref_val`] instead. If both of these things are true,
5247 /// consider using `/` instead.
5248 ///
5249 /// # Worst-case complexity
5250 /// $T(n) = O(n \log n \log\log n)$
5251 ///
5252 /// $M(n) = O(n \log n)$
5253 ///
5254 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5255 /// y.significant_bits(), prec)`.
5256 ///
5257 /// # Panics
5258 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
5259 ///
5260 /// # Examples
5261 /// ```
5262 /// use core::f64::consts::PI;
5263 /// use malachite_base::rounding_modes::RoundingMode::*;
5264 /// use malachite_float::Float;
5265 /// use malachite_q::Rational;
5266 /// use std::cmp::Ordering::*;
5267 ///
5268 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
5269 /// &Rational::from(3),
5270 /// Float::from(PI),
5271 /// 5,
5272 /// Floor,
5273 /// );
5274 /// assert_eq!(quotient.to_string(), "0.94");
5275 /// assert_eq!(o, Less);
5276 ///
5277 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
5278 /// &Rational::from(3),
5279 /// Float::from(PI),
5280 /// 5,
5281 /// Ceiling,
5282 /// );
5283 /// assert_eq!(quotient.to_string(), "0.97");
5284 /// assert_eq!(o, Greater);
5285 ///
5286 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
5287 /// &Rational::from(3),
5288 /// Float::from(PI),
5289 /// 5,
5290 /// Nearest,
5291 /// );
5292 /// assert_eq!(quotient.to_string(), "0.97");
5293 /// assert_eq!(o, Greater);
5294 ///
5295 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
5296 /// &Rational::from(3),
5297 /// Float::from(PI),
5298 /// 20,
5299 /// Floor,
5300 /// );
5301 /// assert_eq!(quotient.to_string(), "0.954929");
5302 /// assert_eq!(o, Less);
5303 ///
5304 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
5305 /// &Rational::from(3),
5306 /// Float::from(PI),
5307 /// 20,
5308 /// Ceiling,
5309 /// );
5310 /// assert_eq!(quotient.to_string(), "0.95493");
5311 /// assert_eq!(o, Greater);
5312 ///
5313 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
5314 /// &Rational::from(3),
5315 /// Float::from(PI),
5316 /// 20,
5317 /// Nearest,
5318 /// );
5319 /// assert_eq!(quotient.to_string(), "0.954929");
5320 /// assert_eq!(o, Less);
5321 /// ```
5322 #[inline]
5323 pub fn rational_div_float_prec_round_ref_val(
5324 x: &Rational,
5325 y: Self,
5326 prec: u64,
5327 rm: RoundingMode,
5328 ) -> (Self, Ordering) {
5329 if !y.is_normal() || max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
5330 rational_div_float_prec_round_naive_ref_val(x, y, prec, rm)
5331 } else {
5332 rational_div_float_prec_round_direct_ref_val(x, y, prec, rm)
5333 }
5334 }
5335
5336 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
5337 /// with the specified rounding mode. The [`Rational`] and the [`Float`] are both taken by
5338 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
5339 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
5340 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
5341 ///
5342 /// See [`RoundingMode`] for a description of the possible rounding modes.
5343 ///
5344 /// $$
5345 /// f(x,y,p,m) = x/y+\varepsilon.
5346 /// $$
5347 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5348 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5349 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
5350 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
5351 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5352 ///
5353 /// If the output has a precision, it is `prec`.
5354 ///
5355 /// Special cases:
5356 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
5357 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
5358 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
5359 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
5360 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
5361 /// - $f(0,x,p,m)=0.0$ if $x>0$
5362 /// - $f(0,x,p,m)=-0.0$ if $x<0$
5363 ///
5364 /// Overflow and underflow:
5365 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5366 /// returned instead.
5367 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
5368 /// is returned instead, where `p` is the precision of the input.
5369 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
5370 /// returned instead.
5371 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
5372 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
5373 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5374 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
5375 /// instead.
5376 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
5377 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
5378 /// instead.
5379 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
5380 /// instead.
5381 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
5382 /// instead.
5383 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
5384 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
5385 /// returned instead.
5386 ///
5387 /// If you know you'll be using `Nearest`, consider using
5388 /// [`Float::rational_div_float_prec_ref_ref`] instead. If you know that your target precision
5389 /// is the precision of the [`Float`] input, consider using
5390 /// [`Float::rational_div_float_round_ref_ref`] instead. If both of these things are true,
5391 /// consider using `/` instead.
5392 ///
5393 /// # Worst-case complexity
5394 /// $T(n) = O(n \log n \log\log n)$
5395 ///
5396 /// $M(n) = O(n \log n)$
5397 ///
5398 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5399 /// y.significant_bits(), prec)`.
5400 ///
5401 /// # Panics
5402 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
5403 ///
5404 /// # Examples
5405 /// ```
5406 /// use core::f64::consts::PI;
5407 /// use malachite_base::rounding_modes::RoundingMode::*;
5408 /// use malachite_float::Float;
5409 /// use malachite_q::Rational;
5410 /// use std::cmp::Ordering::*;
5411 ///
5412 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
5413 /// &Rational::from(3),
5414 /// &Float::from(PI),
5415 /// 5,
5416 /// Floor,
5417 /// );
5418 /// assert_eq!(quotient.to_string(), "0.94");
5419 /// assert_eq!(o, Less);
5420 ///
5421 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
5422 /// &Rational::from(3),
5423 /// &Float::from(PI),
5424 /// 5,
5425 /// Ceiling,
5426 /// );
5427 /// assert_eq!(quotient.to_string(), "0.97");
5428 /// assert_eq!(o, Greater);
5429 ///
5430 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
5431 /// &Rational::from(3),
5432 /// &Float::from(PI),
5433 /// 5,
5434 /// Nearest,
5435 /// );
5436 /// assert_eq!(quotient.to_string(), "0.97");
5437 /// assert_eq!(o, Greater);
5438 ///
5439 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
5440 /// &Rational::from(3),
5441 /// &Float::from(PI),
5442 /// 20,
5443 /// Floor,
5444 /// );
5445 /// assert_eq!(quotient.to_string(), "0.954929");
5446 /// assert_eq!(o, Less);
5447 ///
5448 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
5449 /// &Rational::from(3),
5450 /// &Float::from(PI),
5451 /// 20,
5452 /// Ceiling,
5453 /// );
5454 /// assert_eq!(quotient.to_string(), "0.95493");
5455 /// assert_eq!(o, Greater);
5456 ///
5457 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
5458 /// &Rational::from(3),
5459 /// &Float::from(PI),
5460 /// 20,
5461 /// Nearest,
5462 /// );
5463 /// assert_eq!(quotient.to_string(), "0.954929");
5464 /// assert_eq!(o, Less);
5465 /// ```
5466 #[inline]
5467 pub fn rational_div_float_prec_round_ref_ref(
5468 x: &Rational,
5469 y: &Self,
5470 prec: u64,
5471 rm: RoundingMode,
5472 ) -> (Self, Ordering) {
5473 if !y.is_normal() || max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
5474 rational_div_float_prec_round_naive_ref_ref(x, y, prec, rm)
5475 } else {
5476 rational_div_float_prec_round_direct_ref_ref(x, y, prec, rm)
5477 }
5478 }
5479
5480 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
5481 /// specified precision. The [`Rational`] and the [`Float`] are both are taken by value. An
5482 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
5483 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
5484 /// whenever this function returns a `NaN` it also returns `Equal`.
5485 ///
5486 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
5487 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
5488 /// description of the `Nearest` rounding mode.
5489 ///
5490 /// $$
5491 /// f(x,y,p) = x/y+\varepsilon.
5492 /// $$
5493 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5494 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5495 ///
5496 /// If the output has a precision, it is `prec`.
5497 ///
5498 /// Special cases:
5499 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
5500 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
5501 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
5502 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
5503 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
5504 /// - $f(0,x,p)=0.0$ if $x>0$
5505 /// - $f(0,x,p)=-0.0$ if $x<0$
5506 ///
5507 /// Overflow and underflow:
5508 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
5509 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
5510 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5511 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5512 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5513 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5514 ///
5515 /// If you want to use a rounding mode other than `Nearest`, consider using
5516 /// [`Float::rational_div_float_prec_round`] instead. If you know that your target precision is
5517 /// the precision of the [`Float`] input, consider using `/` instead.
5518 ///
5519 /// # Worst-case complexity
5520 /// $T(n) = O(n \log n \log\log n)$
5521 ///
5522 /// $M(n) = O(n \log n)$
5523 ///
5524 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5525 /// y.significant_bits(), prec)`.
5526 ///
5527 /// # Examples
5528 /// ```
5529 /// use core::f64::consts::PI;
5530 /// use malachite_float::Float;
5531 /// use malachite_q::Rational;
5532 /// use std::cmp::Ordering::*;
5533 ///
5534 /// let (quotient, o) = Float::rational_div_float_prec(Rational::from(3), Float::from(PI), 5);
5535 /// assert_eq!(quotient.to_string(), "0.97");
5536 /// assert_eq!(o, Greater);
5537 ///
5538 /// let (quotient, o) = Float::rational_div_float_prec(Rational::from(3), Float::from(PI), 20);
5539 /// assert_eq!(quotient.to_string(), "0.954929");
5540 /// assert_eq!(o, Less);
5541 /// ```
5542 #[inline]
5543 pub fn rational_div_float_prec(x: Rational, y: Self, prec: u64) -> (Self, Ordering) {
5544 Self::rational_div_float_prec_round(x, y, prec, Nearest)
5545 }
5546
5547 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
5548 /// specified precision. The [`Rational`] is taken by value and the [`Float`] by reference. An
5549 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
5550 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
5551 /// whenever this function returns a `NaN` it also returns `Equal`.
5552 ///
5553 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
5554 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
5555 /// description of the `Nearest` rounding mode.
5556 ///
5557 /// $$
5558 /// f(x,y,p) = x/y+\varepsilon.
5559 /// $$
5560 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5561 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5562 ///
5563 /// If the output has a precision, it is `prec`.
5564 ///
5565 /// Special cases:
5566 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
5567 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
5568 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
5569 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
5570 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
5571 /// - $f(0,x,p)=0.0$ if $x>0$
5572 /// - $f(0,x,p)=-0.0$ if $x<0$
5573 ///
5574 /// Overflow and underflow:
5575 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
5576 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
5577 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5578 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5579 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5580 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5581 ///
5582 /// If you want to use a rounding mode other than `Nearest`, consider using
5583 /// [`Float::rational_div_float_prec_round_val_ref`] instead. If you know that your target
5584 /// precision is the precision of the [`Float`] input, consider using `/` instead.
5585 ///
5586 /// # Worst-case complexity
5587 /// $T(n) = O(n \log n \log\log n)$
5588 ///
5589 /// $M(n) = O(n \log n)$
5590 ///
5591 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5592 /// y.significant_bits(), prec)`.
5593 ///
5594 /// # Examples
5595 /// ```
5596 /// use core::f64::consts::PI;
5597 /// use malachite_float::Float;
5598 /// use malachite_q::Rational;
5599 /// use std::cmp::Ordering::*;
5600 ///
5601 /// let (quotient, o) =
5602 /// Float::rational_div_float_prec_val_ref(Rational::from(3), &Float::from(PI), 5);
5603 /// assert_eq!(quotient.to_string(), "0.97");
5604 /// assert_eq!(o, Greater);
5605 ///
5606 /// let (quotient, o) =
5607 /// Float::rational_div_float_prec_val_ref(Rational::from(3), &Float::from(PI), 20);
5608 /// assert_eq!(quotient.to_string(), "0.954929");
5609 /// assert_eq!(o, Less);
5610 /// ```
5611 #[inline]
5612 pub fn rational_div_float_prec_val_ref(x: Rational, y: &Self, prec: u64) -> (Self, Ordering) {
5613 Self::rational_div_float_prec_round_val_ref(x, y, prec, Nearest)
5614 }
5615
5616 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
5617 /// specified precision. The [`Rational`] is taken by reference and the [`Float`] by value. An
5618 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
5619 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
5620 /// whenever this function returns a `NaN` it also returns `Equal`.
5621 ///
5622 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
5623 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
5624 /// description of the `Nearest` rounding mode.
5625 ///
5626 /// $$
5627 /// f(x,y,p) = x/y+\varepsilon.
5628 /// $$
5629 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5630 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5631 ///
5632 /// If the output has a precision, it is `prec`.
5633 ///
5634 /// Special cases:
5635 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
5636 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
5637 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
5638 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
5639 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
5640 /// - $f(0,x,p)=0.0$ if $x>0$
5641 /// - $f(0,x,p)=-0.0$ if $x<0$
5642 ///
5643 /// Overflow and underflow:
5644 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
5645 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
5646 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5647 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5648 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5649 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5650 ///
5651 /// If you want to use a rounding mode other than `Nearest`, consider using
5652 /// [`Float::rational_div_float_prec_round_ref_val`] instead. If you know that your target
5653 /// precision is the precision of the [`Float`] input, consider using `/` instead.
5654 ///
5655 /// # Worst-case complexity
5656 /// $T(n) = O(n \log n \log\log n)$
5657 ///
5658 /// $M(n) = O(n \log n)$
5659 ///
5660 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5661 /// y.significant_bits(), prec)`.
5662 ///
5663 /// # Examples
5664 /// ```
5665 /// use core::f64::consts::PI;
5666 /// use malachite_float::Float;
5667 /// use malachite_q::Rational;
5668 /// use std::cmp::Ordering::*;
5669 ///
5670 /// let (quotient, o) =
5671 /// Float::rational_div_float_prec_ref_val(&Rational::from(3), Float::from(PI), 5);
5672 /// assert_eq!(quotient.to_string(), "0.97");
5673 /// assert_eq!(o, Greater);
5674 ///
5675 /// let (quotient, o) =
5676 /// Float::rational_div_float_prec_ref_val(&Rational::from(3), Float::from(PI), 20);
5677 /// assert_eq!(quotient.to_string(), "0.954929");
5678 /// assert_eq!(o, Less);
5679 /// ```
5680 #[inline]
5681 pub fn rational_div_float_prec_ref_val(x: &Rational, y: Self, prec: u64) -> (Self, Ordering) {
5682 Self::rational_div_float_prec_round_ref_val(x, y, prec, Nearest)
5683 }
5684
5685 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
5686 /// specified precision. The [`Rational`] and the [`Float`] are both are taken by reference. An
5687 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
5688 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
5689 /// whenever this function returns a `NaN` it also returns `Equal`.
5690 ///
5691 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
5692 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
5693 /// description of the `Nearest` rounding mode.
5694 ///
5695 /// $$
5696 /// f(x,y,p) = x/y+\varepsilon.
5697 /// $$
5698 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5699 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5700 ///
5701 /// If the output has a precision, it is `prec`.
5702 ///
5703 /// Special cases:
5704 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
5705 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
5706 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or #x=-0.0$
5707 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
5708 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or #x=-0.0$
5709 /// - $f(0,x,p)=0.0$ if $x>0$
5710 /// - $f(0,x,p)=-0.0$ if $x<0$
5711 ///
5712 /// Overflow and underflow:
5713 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
5714 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
5715 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5716 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5717 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5718 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5719 ///
5720 /// If you want to use a rounding mode other than `Nearest`, consider using
5721 /// [`Float::rational_div_float_prec_round_ref_ref`] instead. If you know that your target
5722 /// precision is the precision of the [`Float`] input, consider using `/` instead.
5723 ///
5724 /// # Worst-case complexity
5725 /// $T(n) = O(n \log n \log\log n)$
5726 ///
5727 /// $M(n) = O(n \log n)$
5728 ///
5729 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5730 /// y.significant_bits(), prec)`.
5731 ///
5732 /// # Examples
5733 /// ```
5734 /// use core::f64::consts::PI;
5735 /// use malachite_float::Float;
5736 /// use malachite_q::Rational;
5737 /// use std::cmp::Ordering::*;
5738 ///
5739 /// let (quotient, o) =
5740 /// Float::rational_div_float_prec_ref_ref(&Rational::from(3), &Float::from(PI), 5);
5741 /// assert_eq!(quotient.to_string(), "0.97");
5742 /// assert_eq!(o, Greater);
5743 ///
5744 /// let (quotient, o) =
5745 /// Float::rational_div_float_prec_ref_ref(&Rational::from(3), &Float::from(PI), 20);
5746 /// assert_eq!(quotient.to_string(), "0.954929");
5747 /// assert_eq!(o, Less);
5748 /// ```
5749 #[inline]
5750 pub fn rational_div_float_prec_ref_ref(x: &Rational, y: &Self, prec: u64) -> (Self, Ordering) {
5751 Self::rational_div_float_prec_round_ref_ref(x, y, prec, Nearest)
5752 }
5753
5754 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
5755 /// The [`Rational`] and the [`Float`] are both are taken by value. An [`Ordering`] is also
5756 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
5757 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
5758 /// function returns a `NaN` it also returns `Equal`.
5759 ///
5760 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
5761 /// for a description of the possible rounding modes.
5762 ///
5763 /// $$
5764 /// f(x,y,m) = x/y+\varepsilon.
5765 /// $$
5766 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5767 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5768 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
5769 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
5770 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
5771 ///
5772 /// If the output has a precision, it is the precision of the [`Float`] input.
5773 ///
5774 /// Special cases:
5775 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
5776 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
5777 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
5778 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
5779 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
5780 /// - $f(0,x,m)=0.0$ if $x>0$
5781 /// - $f(0,x,m)=-0.0$ if $x<0$
5782 ///
5783 /// Overflow and underflow:
5784 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5785 /// returned instead.
5786 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
5787 /// returned instead, where `p` is the precision of the input.
5788 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
5789 /// returned instead.
5790 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
5791 /// is returned instead, where `p` is the precision of the input.
5792 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5793 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
5794 /// instead.
5795 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
5796 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
5797 /// instead.
5798 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
5799 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
5800 /// instead.
5801 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
5802 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
5803 /// returned instead.
5804 ///
5805 /// If you want to specify an output precision, consider using
5806 /// [`Float::rational_div_float_prec_round`] instead. If you know you'll be using the `Nearest`
5807 /// rounding mode, consider using `/` instead.
5808 ///
5809 /// # Worst-case complexity
5810 /// $T(n) = O(n \log n \log\log n)$
5811 ///
5812 /// $M(n) = O(n \log n)$
5813 ///
5814 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5815 /// y.significant_bits())`.
5816 ///
5817 /// # Panics
5818 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
5819 /// represent the output.
5820 ///
5821 /// # Examples
5822 /// ```
5823 /// use core::f64::consts::PI;
5824 /// use malachite_base::rounding_modes::RoundingMode::*;
5825 /// use malachite_float::Float;
5826 /// use malachite_q::Rational;
5827 /// use std::cmp::Ordering::*;
5828 ///
5829 /// let (quotient, o) =
5830 /// Float::rational_div_float_round(Rational::from(3), Float::from(PI), Floor);
5831 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
5832 /// assert_eq!(o, Less);
5833 ///
5834 /// let (quotient, o) =
5835 /// Float::rational_div_float_round(Rational::from(3), Float::from(PI), Ceiling);
5836 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
5837 /// assert_eq!(o, Greater);
5838 ///
5839 /// let (quotient, o) =
5840 /// Float::rational_div_float_round(Rational::from(3), Float::from(PI), Nearest);
5841 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
5842 /// assert_eq!(o, Greater);
5843 /// ```
5844 #[inline]
5845 pub fn rational_div_float_round(x: Rational, y: Self, rm: RoundingMode) -> (Self, Ordering) {
5846 let prec = y.significant_bits();
5847 Self::rational_div_float_prec_round(x, y, prec, rm)
5848 }
5849
5850 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
5851 /// The [`Rational`] is taken by value and the [`Float`] by reference. An [`Ordering`] is also
5852 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
5853 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
5854 /// function returns a `NaN` it also returns `Equal`.
5855 ///
5856 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
5857 /// for a description of the possible rounding modes.
5858 ///
5859 /// $$
5860 /// f(x,y,m) = x/y+\varepsilon.
5861 /// $$
5862 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5863 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5864 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
5865 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
5866 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
5867 ///
5868 /// If the output has a precision, it is the precision of the [`Float`] input.
5869 ///
5870 /// Special cases:
5871 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
5872 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
5873 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
5874 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
5875 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
5876 /// - $f(0,x,m)=0.0$ if $x>0$
5877 /// - $f(0,x,m)=-0.0$ if $x<0$
5878 ///
5879 /// Overflow and underflow:
5880 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5881 /// returned instead.
5882 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
5883 /// returned instead, where `p` is the precision of the input.
5884 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
5885 /// returned instead.
5886 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
5887 /// is returned instead, where `p` is the precision of the input.
5888 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5889 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
5890 /// instead.
5891 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
5892 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
5893 /// instead.
5894 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
5895 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
5896 /// instead.
5897 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
5898 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
5899 /// returned instead.
5900 ///
5901 /// If you want to specify an output precision, consider using
5902 /// [`Float::rational_div_float_prec_round_val_ref`] instead. If you know you'll be using the
5903 /// `Nearest` rounding mode, consider using `/` instead.
5904 ///
5905 /// # Worst-case complexity
5906 /// $T(n) = O(n \log n \log\log n)$
5907 ///
5908 /// $M(n) = O(n \log n)$
5909 ///
5910 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5911 /// y.significant_bits())`.
5912 ///
5913 /// # Panics
5914 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
5915 /// represent the output.
5916 ///
5917 /// # Examples
5918 /// ```
5919 /// use core::f64::consts::PI;
5920 /// use malachite_base::rounding_modes::RoundingMode::*;
5921 /// use malachite_float::Float;
5922 /// use malachite_q::Rational;
5923 /// use std::cmp::Ordering::*;
5924 ///
5925 /// let (quotient, o) =
5926 /// Float::rational_div_float_round_val_ref(Rational::from(3), &Float::from(PI), Floor);
5927 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
5928 /// assert_eq!(o, Less);
5929 ///
5930 /// let (quotient, o) =
5931 /// Float::rational_div_float_round_val_ref(Rational::from(3), &Float::from(PI), Ceiling);
5932 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
5933 /// assert_eq!(o, Greater);
5934 ///
5935 /// let (quotient, o) =
5936 /// Float::rational_div_float_round_val_ref(Rational::from(3), &Float::from(PI), Nearest);
5937 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
5938 /// assert_eq!(o, Greater);
5939 /// ```
5940 #[inline]
5941 pub fn rational_div_float_round_val_ref(
5942 x: Rational,
5943 y: &Self,
5944 rm: RoundingMode,
5945 ) -> (Self, Ordering) {
5946 let prec = y.significant_bits();
5947 Self::rational_div_float_prec_round_val_ref(x, y, prec, rm)
5948 }
5949
5950 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
5951 /// The [`Rational`] is taken by reference and the [`Float`] by value. An [`Ordering`] is also
5952 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
5953 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
5954 /// function returns a `NaN` it also returns `Equal`.
5955 ///
5956 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
5957 /// for a description of the possible rounding modes.
5958 ///
5959 /// $$
5960 /// f(x,y,m) = x/y+\varepsilon.
5961 /// $$
5962 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5963 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5964 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
5965 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
5966 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
5967 ///
5968 /// If the output has a precision, it is the precision of the [`Float`] input.
5969 ///
5970 /// Special cases:
5971 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
5972 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
5973 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
5974 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
5975 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
5976 /// - $f(0,x,m)=0.0$ if $x>0$
5977 /// - $f(0,x,m)=-0.0$ if $x<0$
5978 ///
5979 /// Overflow and underflow:
5980 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5981 /// returned instead.
5982 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
5983 /// returned instead, where `p` is the precision of the input.
5984 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
5985 /// returned instead.
5986 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
5987 /// is returned instead, where `p` is the precision of the input.
5988 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5989 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
5990 /// instead.
5991 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
5992 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
5993 /// instead.
5994 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
5995 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
5996 /// instead.
5997 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
5998 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
5999 /// returned instead.
6000 ///
6001 /// If you want to specify an output precision, consider using
6002 /// [`Float::rational_div_float_prec_round_ref_val`] instead. If you know you'll be using the
6003 /// `Nearest` rounding mode, consider using `/` instead.
6004 ///
6005 /// # Worst-case complexity
6006 /// $T(n) = O(n \log n \log\log n)$
6007 ///
6008 /// $M(n) = O(n \log n)$
6009 ///
6010 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
6011 /// y.significant_bits())`.
6012 ///
6013 /// # Panics
6014 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
6015 /// represent the output.
6016 ///
6017 /// # Examples
6018 /// ```
6019 /// use core::f64::consts::PI;
6020 /// use malachite_base::rounding_modes::RoundingMode::*;
6021 /// use malachite_float::Float;
6022 /// use malachite_q::Rational;
6023 /// use std::cmp::Ordering::*;
6024 ///
6025 /// let (quotient, o) =
6026 /// Float::rational_div_float_round_ref_val(&Rational::from(3), Float::from(PI), Floor);
6027 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
6028 /// assert_eq!(o, Less);
6029 ///
6030 /// let (quotient, o) =
6031 /// Float::rational_div_float_round_ref_val(&Rational::from(3), Float::from(PI), Ceiling);
6032 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
6033 /// assert_eq!(o, Greater);
6034 ///
6035 /// let (quotient, o) =
6036 /// Float::rational_div_float_round_ref_val(&Rational::from(3), Float::from(PI), Nearest);
6037 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
6038 /// assert_eq!(o, Greater);
6039 /// ```
6040 #[inline]
6041 pub fn rational_div_float_round_ref_val(
6042 x: &Rational,
6043 y: Self,
6044 rm: RoundingMode,
6045 ) -> (Self, Ordering) {
6046 let prec = y.significant_bits();
6047 Self::rational_div_float_prec_round_ref_val(x, y, prec, rm)
6048 }
6049
6050 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
6051 /// The [`Rational`] and the [`Float`] are both are taken by reference. An [`Ordering`] is also
6052 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
6053 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
6054 /// function returns a `NaN` it also returns `Equal`.
6055 ///
6056 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
6057 /// for a description of the possible rounding modes.
6058 ///
6059 /// $$
6060 /// f(x,y,m) = x/y+\varepsilon.
6061 /// $$
6062 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6063 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
6064 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
6065 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
6066 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
6067 ///
6068 /// If the output has a precision, it is the precision of the [`Float`] input.
6069 ///
6070 /// Special cases:
6071 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
6072 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
6073 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or #x=-0.0$
6074 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
6075 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or #x=-0.0$
6076 /// - $f(0,x,m)=0.0$ if $x>0$
6077 /// - $f(0,x,m)=-0.0$ if $x<0$
6078 ///
6079 /// Overflow and underflow:
6080 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
6081 /// returned instead.
6082 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
6083 /// returned instead, where `p` is the precision of the input.
6084 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
6085 /// returned instead.
6086 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
6087 /// is returned instead, where `p` is the precision of the input.
6088 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
6089 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
6090 /// instead.
6091 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
6092 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
6093 /// instead.
6094 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
6095 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
6096 /// instead.
6097 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
6098 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
6099 /// returned instead.
6100 ///
6101 /// If you want to specify an output precision, consider using
6102 /// [`Float::rational_div_float_prec_round_ref_ref`] instead. If you know you'll be using the
6103 /// `Nearest` rounding mode, consider using `/` instead.
6104 ///
6105 /// # Worst-case complexity
6106 /// $T(n) = O(n \log n \log\log n)$
6107 ///
6108 /// $M(n) = O(n \log n)$
6109 ///
6110 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
6111 /// y.significant_bits())`.
6112 ///
6113 /// # Panics
6114 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
6115 /// represent the output.
6116 ///
6117 /// # Examples
6118 /// ```
6119 /// use core::f64::consts::PI;
6120 /// use malachite_base::rounding_modes::RoundingMode::*;
6121 /// use malachite_float::Float;
6122 /// use malachite_q::Rational;
6123 /// use std::cmp::Ordering::*;
6124 ///
6125 /// let (quotient, o) =
6126 /// Float::rational_div_float_round_ref_ref(&Rational::from(3), &Float::from(PI), Floor);
6127 /// assert_eq!(quotient.to_string(), "0.9549296585513716");
6128 /// assert_eq!(o, Less);
6129 ///
6130 /// let (quotient, o) =
6131 /// Float::rational_div_float_round_ref_ref(&Rational::from(3), &Float::from(PI), Ceiling);
6132 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
6133 /// assert_eq!(o, Greater);
6134 ///
6135 /// let (quotient, o) =
6136 /// Float::rational_div_float_round_ref_ref(&Rational::from(3), &Float::from(PI), Nearest);
6137 /// assert_eq!(quotient.to_string(), "0.9549296585513725");
6138 /// assert_eq!(o, Greater);
6139 /// ```
6140 #[inline]
6141 pub fn rational_div_float_round_ref_ref(
6142 x: &Rational,
6143 y: &Self,
6144 rm: RoundingMode,
6145 ) -> (Self, Ordering) {
6146 let prec = y.significant_bits();
6147 Self::rational_div_float_prec_round_ref_ref(x, y, prec, rm)
6148 }
6149}
6150
6151impl Div<Self> for Float {
6152 type Output = Self;
6153
6154 /// Divides two [`Float`]s, taking both by value.
6155 ///
6156 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
6157 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
6158 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
6159 /// `Nearest` rounding mode.
6160 ///
6161 /// $$
6162 /// f(x,y) = x/y+\varepsilon.
6163 /// $$
6164 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6165 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6166 /// where $p$ is the maximum precision of the inputs.
6167 ///
6168 /// Special cases:
6169 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
6170 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
6171 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
6172 /// - $f(x,0.0)=\infty$ if $x>0.0$
6173 /// - $f(x,0.0)=-\infty$ if $x<0.0$
6174 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
6175 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
6176 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
6177 /// - $f(x,-0.0)=\infty$ if $x<0.0$
6178 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
6179 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
6180 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6181 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6182 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
6183 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
6184 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6185 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6186 ///
6187 /// Overflow and underflow:
6188 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
6189 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
6190 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6191 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6192 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
6193 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6194 ///
6195 /// If you want to use a rounding mode other than `Nearest`, consider using [`Float::div_prec`]
6196 /// instead. If you want to specify the output precision, consider using [`Float::div_round`].
6197 /// If you want both of these things, consider using [`Float::div_prec_round`].
6198 ///
6199 /// # Worst-case complexity
6200 /// $T(n) = O(n \log n \log\log n)$
6201 ///
6202 /// $M(n) = O(n \log n)$
6203 ///
6204 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6205 /// other.significant_bits())`.
6206 ///
6207 /// # Examples
6208 /// ```
6209 /// use malachite_base::num::basic::traits::{
6210 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6211 /// };
6212 /// use malachite_float::Float;
6213 ///
6214 /// assert!((Float::from(1.5) / Float::NAN).is_nan());
6215 /// assert_eq!(Float::from(1.5) / Float::ZERO, Float::INFINITY);
6216 /// assert_eq!(
6217 /// Float::from(1.5) / Float::NEGATIVE_ZERO,
6218 /// Float::NEGATIVE_INFINITY
6219 /// );
6220 /// assert_eq!(Float::from(-1.5) / Float::ZERO, Float::NEGATIVE_INFINITY);
6221 /// assert_eq!(Float::from(-1.5) / Float::NEGATIVE_ZERO, Float::INFINITY);
6222 /// assert!((Float::ZERO / Float::ZERO).is_nan());
6223 ///
6224 /// assert_eq!((Float::from(1.5) / Float::from(2.5)).to_string(), "0.6");
6225 /// assert_eq!((Float::from(1.5) / Float::from(-2.5)).to_string(), "-0.6");
6226 /// assert_eq!((Float::from(-1.5) / Float::from(2.5)).to_string(), "-0.6");
6227 /// assert_eq!((Float::from(-1.5) / Float::from(-2.5)).to_string(), "0.6");
6228 /// ```
6229 #[inline]
6230 fn div(self, other: Self) -> Self {
6231 let prec = max(self.significant_bits(), other.significant_bits());
6232 self.div_prec_round(other, prec, Nearest).0
6233 }
6234}
6235
6236impl Div<&Self> for Float {
6237 type Output = Self;
6238
6239 /// Divides two [`Float`]s, taking the first by value and the second by reference.
6240 ///
6241 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
6242 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
6243 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
6244 /// `Nearest` rounding mode.
6245 ///
6246 /// $$
6247 /// f(x,y) = x/y+\varepsilon.
6248 /// $$
6249 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6250 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6251 /// where $p$ is the maximum precision of the inputs.
6252 ///
6253 /// Special cases:
6254 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
6255 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
6256 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
6257 /// - $f(x,0.0)=\infty$ if $x>0.0$
6258 /// - $f(x,0.0)=-\infty$ if $x<0.0$
6259 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
6260 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
6261 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
6262 /// - $f(x,-0.0)=\infty$ if $x<0.0$
6263 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
6264 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
6265 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6266 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6267 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
6268 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
6269 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6270 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6271 ///
6272 /// Overflow and underflow:
6273 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
6274 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
6275 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6276 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6277 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
6278 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6279 ///
6280 /// If you want to use a rounding mode other than `Nearest`, consider using
6281 /// [`Float::div_prec_val_ref`] instead. If you want to specify the output precision, consider
6282 /// using [`Float::div_round_val_ref`]. If you want both of these things, consider using
6283 /// [`Float::div_prec_round_val_ref`].
6284 ///
6285 /// # Worst-case complexity
6286 /// $T(n) = O(n \log n \log\log n)$
6287 ///
6288 /// $M(n) = O(n \log n)$
6289 ///
6290 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6291 /// other.significant_bits())`.
6292 ///
6293 /// # Examples
6294 /// ```
6295 /// use malachite_base::num::basic::traits::{
6296 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6297 /// };
6298 /// use malachite_float::Float;
6299 ///
6300 /// assert!((Float::from(1.5) / &Float::NAN).is_nan());
6301 /// assert_eq!(Float::from(1.5) / &Float::ZERO, Float::INFINITY);
6302 /// assert_eq!(
6303 /// Float::from(1.5) / &Float::NEGATIVE_ZERO,
6304 /// Float::NEGATIVE_INFINITY
6305 /// );
6306 /// assert_eq!(Float::from(-1.5) / &Float::ZERO, Float::NEGATIVE_INFINITY);
6307 /// assert_eq!(Float::from(-1.5) / &Float::NEGATIVE_ZERO, Float::INFINITY);
6308 /// assert!((Float::ZERO / &Float::ZERO).is_nan());
6309 ///
6310 /// assert_eq!((Float::from(1.5) / &Float::from(2.5)).to_string(), "0.6");
6311 /// assert_eq!((Float::from(1.5) / &Float::from(-2.5)).to_string(), "-0.6");
6312 /// assert_eq!((Float::from(-1.5) / &Float::from(2.5)).to_string(), "-0.6");
6313 /// assert_eq!((Float::from(-1.5) / &Float::from(-2.5)).to_string(), "0.6");
6314 /// ```
6315 #[inline]
6316 fn div(self, other: &Self) -> Self {
6317 let prec = max(self.significant_bits(), other.significant_bits());
6318 self.div_prec_round_val_ref(other, prec, Nearest).0
6319 }
6320}
6321
6322impl Div<Float> for &Float {
6323 type Output = Float;
6324
6325 /// Divides two [`Float`]s, taking the first by reference and the second by value.
6326 ///
6327 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
6328 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
6329 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
6330 /// `Nearest` rounding mode.
6331 ///
6332 /// $$
6333 /// f(x,y) = x/y+\varepsilon.
6334 /// $$
6335 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6336 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6337 /// where $p$ is the maximum precision of the inputs.
6338 ///
6339 /// Special cases:
6340 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
6341 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
6342 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
6343 /// - $f(x,0.0)=\infty$ if $x>0.0$
6344 /// - $f(x,0.0)=-\infty$ if $x<0.0$
6345 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
6346 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
6347 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
6348 /// - $f(x,-0.0)=\infty$ if $x<0.0$
6349 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
6350 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
6351 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6352 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6353 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
6354 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
6355 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6356 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6357 ///
6358 /// Overflow and underflow:
6359 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
6360 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
6361 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6362 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6363 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
6364 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6365 ///
6366 /// If you want to use a rounding mode other than `Nearest`, consider using
6367 /// [`Float::div_prec_ref_val`] instead. If you want to specify the output precision, consider
6368 /// using [`Float::div_round_ref_val`]. If you want both of these things, consider using
6369 /// [`Float::div_prec_round_ref_val`].
6370 ///
6371 /// # Worst-case complexity
6372 /// $T(n) = O(n \log n \log\log n)$
6373 ///
6374 /// $M(n) = O(n \log n)$
6375 ///
6376 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6377 /// other.significant_bits())`.
6378 ///
6379 /// # Examples
6380 /// ```
6381 /// use malachite_base::num::basic::traits::{
6382 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6383 /// };
6384 /// use malachite_float::Float;
6385 ///
6386 /// assert!((&Float::from(1.5) / Float::NAN).is_nan());
6387 /// assert_eq!(&Float::from(1.5) / Float::ZERO, Float::INFINITY);
6388 /// assert_eq!(
6389 /// &Float::from(1.5) / Float::NEGATIVE_ZERO,
6390 /// Float::NEGATIVE_INFINITY
6391 /// );
6392 /// assert_eq!(&Float::from(-1.5) / Float::ZERO, Float::NEGATIVE_INFINITY);
6393 /// assert_eq!(&Float::from(-1.5) / Float::NEGATIVE_ZERO, Float::INFINITY);
6394 /// assert!((&Float::ZERO / Float::ZERO).is_nan());
6395 ///
6396 /// assert_eq!((&Float::from(1.5) / Float::from(2.5)).to_string(), "0.6");
6397 /// assert_eq!((&Float::from(1.5) / Float::from(-2.5)).to_string(), "-0.6");
6398 /// assert_eq!((&Float::from(-1.5) / Float::from(2.5)).to_string(), "-0.6");
6399 /// assert_eq!((&Float::from(-1.5) / Float::from(-2.5)).to_string(), "0.6");
6400 /// ```
6401 #[inline]
6402 fn div(self, other: Float) -> Float {
6403 let prec = max(self.significant_bits(), other.significant_bits());
6404 self.div_prec_round_ref_val(other, prec, Nearest).0
6405 }
6406}
6407
6408impl Div<&Float> for &Float {
6409 type Output = Float;
6410
6411 /// Divides two [`Float`]s, taking both by reference.
6412 ///
6413 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
6414 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
6415 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
6416 /// `Nearest` rounding mode.
6417 ///
6418 /// $$
6419 /// f(x,y) = x/y+\varepsilon.
6420 /// $$
6421 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6422 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6423 /// where $p$ is the maximum precision of the inputs.
6424 ///
6425 /// Special cases:
6426 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
6427 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
6428 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
6429 /// - $f(x,0.0)=\infty$ if $x>0.0$
6430 /// - $f(x,0.0)=-\infty$ if $x<0.0$
6431 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
6432 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
6433 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
6434 /// - $f(x,-0.0)=\infty$ if $x<0.0$
6435 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
6436 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
6437 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6438 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6439 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
6440 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
6441 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6442 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6443 ///
6444 /// Overflow and underflow:
6445 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
6446 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
6447 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6448 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6449 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
6450 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6451 ///
6452 /// If you want to use a rounding mode other than `Nearest`, consider using
6453 /// [`Float::div_prec_ref_ref`] instead. If you want to specify the output precision, consider
6454 /// using [`Float::div_round_ref_ref`]. If you want both of these things, consider using
6455 /// [`Float::div_prec_round_ref_ref`].
6456 ///
6457 /// # Worst-case complexity
6458 /// $T(n) = O(n \log n \log\log n)$
6459 ///
6460 /// $M(n) = O(n \log n)$
6461 ///
6462 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6463 /// other.significant_bits())`.
6464 ///
6465 /// # Examples
6466 /// ```
6467 /// use malachite_base::num::basic::traits::{
6468 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6469 /// };
6470 /// use malachite_float::Float;
6471 ///
6472 /// assert!((&Float::from(1.5) / &Float::NAN).is_nan());
6473 /// assert_eq!(&Float::from(1.5) / &Float::ZERO, Float::INFINITY);
6474 /// assert_eq!(
6475 /// &Float::from(1.5) / &Float::NEGATIVE_ZERO,
6476 /// Float::NEGATIVE_INFINITY
6477 /// );
6478 /// assert_eq!(&Float::from(-1.5) / &Float::ZERO, Float::NEGATIVE_INFINITY);
6479 /// assert_eq!(&Float::from(-1.5) / &Float::NEGATIVE_ZERO, Float::INFINITY);
6480 /// assert!((&Float::ZERO / &Float::ZERO).is_nan());
6481 ///
6482 /// assert_eq!((&Float::from(1.5) / &Float::from(2.5)).to_string(), "0.6");
6483 /// assert_eq!((&Float::from(1.5) / &Float::from(-2.5)).to_string(), "-0.6");
6484 /// assert_eq!((&Float::from(-1.5) / &Float::from(2.5)).to_string(), "-0.6");
6485 /// assert_eq!((&Float::from(-1.5) / &Float::from(-2.5)).to_string(), "0.6");
6486 /// ```
6487 #[inline]
6488 fn div(self, other: &Float) -> Float {
6489 let prec = max(self.significant_bits(), other.significant_bits());
6490 self.div_prec_round_ref_ref(other, prec, Nearest).0
6491 }
6492}
6493
6494impl DivAssign<Self> for Float {
6495 /// Divides a [`Float`] by a [`Float`] in place, taking the [`Float`] on the right-hand side by
6496 /// value.
6497 ///
6498 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
6499 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
6500 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
6501 /// `Nearest` rounding mode.
6502 ///
6503 /// $$
6504 /// x\gets = x/y+\varepsilon.
6505 /// $$
6506 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6507 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6508 /// where $p$ is the maximum precision of the inputs.
6509 ///
6510 /// See the `/` documentation for information on special cases, overflow, and underflow.
6511 ///
6512 /// If you want to use a rounding mode other than `Nearest`, consider using
6513 /// [`Float::div_prec_assign`] instead. If you want to specify the output precision, consider
6514 /// using [`Float::div_round_assign`]. If you want both of these things, consider using
6515 /// [`Float::div_prec_round_assign`].
6516 ///
6517 /// # Worst-case complexity
6518 /// $T(n) = O(n \log n \log\log n)$
6519 ///
6520 /// $M(n) = O(n \log n)$
6521 ///
6522 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6523 /// other.significant_bits())`.
6524 ///
6525 /// # Examples
6526 /// ```
6527 /// use malachite_base::num::basic::traits::{
6528 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6529 /// };
6530 /// use malachite_float::Float;
6531 ///
6532 /// let mut x = Float::from(1.5);
6533 /// x /= Float::NAN;
6534 /// assert!(x.is_nan());
6535 ///
6536 /// let mut x = Float::from(1.5);
6537 /// x /= Float::ZERO;
6538 /// assert_eq!(x, Float::INFINITY);
6539 ///
6540 /// let mut x = Float::from(1.5);
6541 /// x /= Float::NEGATIVE_ZERO;
6542 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
6543 ///
6544 /// let mut x = Float::from(-1.5);
6545 /// x /= Float::ZERO;
6546 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
6547 ///
6548 /// let mut x = Float::from(-1.5);
6549 /// x /= Float::NEGATIVE_ZERO;
6550 /// assert_eq!(x, Float::INFINITY);
6551 ///
6552 /// let mut x = Float::INFINITY;
6553 /// x /= Float::INFINITY;
6554 /// assert!(x.is_nan());
6555 ///
6556 /// let mut x = Float::from(1.5);
6557 /// x /= Float::from(2.5);
6558 /// assert_eq!(x.to_string(), "0.6");
6559 ///
6560 /// let mut x = Float::from(1.5);
6561 /// x /= Float::from(-2.5);
6562 /// assert_eq!(x.to_string(), "-0.6");
6563 ///
6564 /// let mut x = Float::from(-1.5);
6565 /// x /= Float::from(2.5);
6566 /// assert_eq!(x.to_string(), "-0.6");
6567 ///
6568 /// let mut x = Float::from(-1.5);
6569 /// x /= Float::from(-2.5);
6570 /// assert_eq!(x.to_string(), "0.6");
6571 /// ```
6572 #[inline]
6573 fn div_assign(&mut self, other: Self) {
6574 let prec = max(self.significant_bits(), other.significant_bits());
6575 self.div_prec_round_assign(other, prec, Nearest);
6576 }
6577}
6578
6579impl DivAssign<&Self> for Float {
6580 /// Divides a [`Float`] by a [`Float`] in place, taking the [`Float`] on the right-hand side by
6581 /// reference.
6582 ///
6583 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
6584 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
6585 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
6586 /// `Nearest` rounding mode.
6587 ///
6588 /// $$
6589 /// x\gets = x/y+\varepsilon.
6590 /// $$
6591 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6592 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6593 /// where $p$ is the maximum precision of the inputs.
6594 ///
6595 /// See the `/` documentation for information on special cases, overflow, and underflow.
6596 ///
6597 /// If you want to use a rounding mode other than `Nearest`, consider using
6598 /// [`Float::div_prec_assign`] instead. If you want to specify the output precision, consider
6599 /// using [`Float::div_round_assign`]. If you want both of these things, consider using
6600 /// [`Float::div_prec_round_assign`].
6601 ///
6602 /// # Worst-case complexity
6603 /// $T(n) = O(n \log n \log\log n)$
6604 ///
6605 /// $M(n) = O(n \log n)$
6606 ///
6607 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6608 /// other.significant_bits())`.
6609 ///
6610 /// # Examples
6611 /// ```
6612 /// use malachite_base::num::basic::traits::{
6613 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6614 /// };
6615 /// use malachite_float::Float;
6616 ///
6617 /// let mut x = Float::from(1.5);
6618 /// x /= &Float::NAN;
6619 /// assert!(x.is_nan());
6620 ///
6621 /// let mut x = Float::from(1.5);
6622 /// x /= &Float::ZERO;
6623 /// assert_eq!(x, Float::INFINITY);
6624 ///
6625 /// let mut x = Float::from(1.5);
6626 /// x /= &Float::NEGATIVE_ZERO;
6627 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
6628 ///
6629 /// let mut x = Float::from(-1.5);
6630 /// x /= &Float::ZERO;
6631 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
6632 ///
6633 /// let mut x = Float::from(-1.5);
6634 /// x /= &Float::NEGATIVE_ZERO;
6635 /// assert_eq!(x, Float::INFINITY);
6636 ///
6637 /// let mut x = Float::INFINITY;
6638 /// x /= &Float::INFINITY;
6639 /// assert!(x.is_nan());
6640 ///
6641 /// let mut x = Float::from(1.5);
6642 /// x /= &Float::from(2.5);
6643 /// assert_eq!(x.to_string(), "0.6");
6644 ///
6645 /// let mut x = Float::from(1.5);
6646 /// x /= &Float::from(-2.5);
6647 /// assert_eq!(x.to_string(), "-0.6");
6648 ///
6649 /// let mut x = Float::from(-1.5);
6650 /// x /= &Float::from(2.5);
6651 /// assert_eq!(x.to_string(), "-0.6");
6652 ///
6653 /// let mut x = Float::from(-1.5);
6654 /// x /= &Float::from(-2.5);
6655 /// assert_eq!(x.to_string(), "0.6");
6656 /// ```
6657 #[inline]
6658 fn div_assign(&mut self, other: &Self) {
6659 let prec = max(self.significant_bits(), other.significant_bits());
6660 self.div_prec_round_assign_ref(other, prec, Nearest);
6661 }
6662}
6663
6664impl Div<Rational> for Float {
6665 type Output = Self;
6666
6667 /// Divides a [`Float`] by a [`Rational`], taking both by value.
6668 ///
6669 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6670 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6671 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6672 /// rounding mode.
6673 ///
6674 /// $$
6675 /// f(x,y) = x/y+\varepsilon.
6676 /// $$
6677 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6678 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6679 /// where $p$ is the precision of the input [`Float`].
6680 ///
6681 /// Special cases:
6682 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
6683 /// - $f(\infty,x)=\infty$ if $x\geq 0$
6684 /// - $f(\infty,x)=-\infty$ if $x<0$
6685 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
6686 /// - $f(-\infty,x)=\infty$ if $x<0$
6687 /// - $f(0.0,x)=0.0$ if $x>0$
6688 /// - $f(0.0,x)=-0.0$ if $x<0$
6689 /// - $f(-0.0,x)=-0.0$ if $x>0$
6690 /// - $f(-0.0,x)=0.0$ if $x<0$
6691 ///
6692 /// Overflow and underflow:
6693 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
6694 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
6695 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6696 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6697 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
6698 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6699 ///
6700 /// If you want to use a rounding mode other than `Nearest`, consider using
6701 /// [`Float::div_rational_prec`] instead. If you want to specify the output precision, consider
6702 /// using [`Float::div_rational_round`]. If you want both of these things, consider using
6703 /// [`Float::div_rational_prec_round`].
6704 ///
6705 /// # Worst-case complexity
6706 /// $T(n) = O(n \log n \log\log n)$
6707 ///
6708 /// $M(n) = O(n \log n)$
6709 ///
6710 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6711 /// other.significant_bits())`.
6712 ///
6713 /// # Examples
6714 /// ```
6715 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
6716 /// use malachite_base::num::conversion::traits::ExactFrom;
6717 /// use malachite_float::Float;
6718 /// use malachite_q::Rational;
6719 ///
6720 /// assert!((Float::NAN / Rational::exact_from(1.5)).is_nan());
6721 /// assert_eq!(Float::INFINITY / Rational::exact_from(1.5), Float::INFINITY);
6722 /// assert_eq!(
6723 /// Float::NEGATIVE_INFINITY / Rational::exact_from(1.5),
6724 /// Float::NEGATIVE_INFINITY
6725 /// );
6726 /// assert_eq!(
6727 /// Float::INFINITY / Rational::exact_from(-1.5),
6728 /// Float::NEGATIVE_INFINITY
6729 /// );
6730 /// assert_eq!(
6731 /// Float::NEGATIVE_INFINITY / Rational::exact_from(-1.5),
6732 /// Float::INFINITY
6733 /// );
6734 ///
6735 /// assert_eq!(
6736 /// (Float::from(2.5) / Rational::exact_from(1.5)).to_string(),
6737 /// "1.8"
6738 /// );
6739 /// assert_eq!(
6740 /// (Float::from(2.5) / Rational::exact_from(-1.5)).to_string(),
6741 /// "-1.8"
6742 /// );
6743 /// assert_eq!(
6744 /// (Float::from(-2.5) / Rational::exact_from(1.5)).to_string(),
6745 /// "-1.8"
6746 /// );
6747 /// assert_eq!(
6748 /// (Float::from(-2.5) / Rational::exact_from(-1.5)).to_string(),
6749 /// "1.8"
6750 /// );
6751 /// ```
6752 #[inline]
6753 fn div(self, other: Rational) -> Self {
6754 let prec = self.significant_bits();
6755 self.div_rational_prec_round(other, prec, Nearest).0
6756 }
6757}
6758
6759impl Div<&Rational> for Float {
6760 type Output = Self;
6761
6762 /// Divides a [`Float`] by a [`Rational`], taking the first by value and the second by
6763 /// reference.
6764 ///
6765 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6766 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6767 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6768 /// rounding mode.
6769 ///
6770 /// $$
6771 /// f(x,y) = x/y+\varepsilon.
6772 /// $$
6773 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6774 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6775 /// where $p$ is the precision of the input [`Float`].
6776 ///
6777 /// Special cases:
6778 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
6779 /// - $f(\infty,x)=\infty$ if $x\geq 0$
6780 /// - $f(\infty,x)=-\infty$ if $x<0$
6781 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
6782 /// - $f(-\infty,x)=\infty$ if $x<0$
6783 /// - $f(0.0,x)=0.0$ if $x>0$
6784 /// - $f(0.0,x)=-0.0$ if $x<0$
6785 /// - $f(-0.0,x)=-0.0$ if $x>0$
6786 /// - $f(-0.0,x)=0.0$ if $x<0$
6787 ///
6788 /// Overflow and underflow:
6789 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
6790 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
6791 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6792 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6793 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
6794 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6795 ///
6796 /// If you want to use a rounding mode other than `Nearest`, consider using
6797 /// [`Float::div_rational_prec_val_ref`] instead. If you want to specify the output precision,
6798 /// consider using [`Float::div_rational_round_val_ref`]. If you want both of these things,
6799 /// consider using [`Float::div_rational_prec_round_val_ref`].
6800 ///
6801 /// # Worst-case complexity
6802 /// $T(n) = O(n \log n \log\log n)$
6803 ///
6804 /// $M(n) = O(n \log n)$
6805 ///
6806 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6807 /// other.significant_bits())`.
6808 ///
6809 /// # Examples
6810 /// ```
6811 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
6812 /// use malachite_base::num::conversion::traits::ExactFrom;
6813 /// use malachite_float::Float;
6814 /// use malachite_q::Rational;
6815 ///
6816 /// assert!((Float::NAN / &Rational::exact_from(1.5)).is_nan());
6817 /// assert_eq!(
6818 /// Float::INFINITY / &Rational::exact_from(1.5),
6819 /// Float::INFINITY
6820 /// );
6821 /// assert_eq!(
6822 /// Float::NEGATIVE_INFINITY / &Rational::exact_from(1.5),
6823 /// Float::NEGATIVE_INFINITY
6824 /// );
6825 /// assert_eq!(
6826 /// Float::INFINITY / &Rational::exact_from(-1.5),
6827 /// Float::NEGATIVE_INFINITY
6828 /// );
6829 /// assert_eq!(
6830 /// Float::NEGATIVE_INFINITY / &Rational::exact_from(-1.5),
6831 /// Float::INFINITY
6832 /// );
6833 ///
6834 /// assert_eq!(
6835 /// (Float::from(2.5) / &Rational::exact_from(1.5)).to_string(),
6836 /// "1.8"
6837 /// );
6838 /// assert_eq!(
6839 /// (Float::from(2.5) / &Rational::exact_from(-1.5)).to_string(),
6840 /// "-1.8"
6841 /// );
6842 /// assert_eq!(
6843 /// (Float::from(-2.5) / &Rational::exact_from(1.5)).to_string(),
6844 /// "-1.8"
6845 /// );
6846 /// assert_eq!(
6847 /// (Float::from(-2.5) / &Rational::exact_from(-1.5)).to_string(),
6848 /// "1.8"
6849 /// );
6850 /// ```
6851 #[inline]
6852 fn div(self, other: &Rational) -> Self {
6853 let prec = self.significant_bits();
6854 self.div_rational_prec_round_val_ref(other, prec, Nearest).0
6855 }
6856}
6857
6858impl Div<Rational> for &Float {
6859 type Output = Float;
6860
6861 /// Divides a [`Float`] by a [`Rational`], taking the first by reference and the second by
6862 /// value.
6863 ///
6864 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6865 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6866 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6867 /// rounding mode.
6868 ///
6869 /// $$
6870 /// f(x,y) = x/y+\varepsilon.
6871 /// $$
6872 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6873 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6874 /// where $p$ is the precision of the input [`Float`].
6875 ///
6876 /// Special cases:
6877 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
6878 /// - $f(\infty,x)=\infty$ if $x\geq 0$
6879 /// - $f(\infty,x)=-\infty$ if $x<0$
6880 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
6881 /// - $f(-\infty,x)=\infty$ if $x<0$
6882 /// - $f(0.0,x)=0.0$ if $x>0$
6883 /// - $f(0.0,x)=-0.0$ if $x<0$
6884 /// - $f(-0.0,x)=-0.0$ if $x>0$
6885 /// - $f(-0.0,x)=0.0$ if $x<0$
6886 ///
6887 /// Overflow and underflow:
6888 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
6889 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
6890 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6891 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6892 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
6893 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6894 ///
6895 /// If you want to use a rounding mode other than `Nearest`, consider using
6896 /// [`Float::div_rational_prec_ref_val`] instead. If you want to specify the output precision,
6897 /// consider using [`Float::div_rational_round_ref_val`]. If you want both of these things,
6898 /// consider using [`Float::div_rational_prec_round_ref_val`].
6899 ///
6900 /// # Worst-case complexity
6901 /// $T(n) = O(n \log n \log\log n)$
6902 ///
6903 /// $M(n) = O(n \log n)$
6904 ///
6905 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6906 /// other.significant_bits())`.
6907 ///
6908 /// # Examples
6909 /// ```
6910 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
6911 /// use malachite_base::num::conversion::traits::ExactFrom;
6912 /// use malachite_float::Float;
6913 /// use malachite_q::Rational;
6914 ///
6915 /// assert!((&Float::NAN / Rational::exact_from(1.5)).is_nan());
6916 /// assert_eq!(
6917 /// &Float::INFINITY / Rational::exact_from(1.5),
6918 /// Float::INFINITY
6919 /// );
6920 /// assert_eq!(
6921 /// &Float::NEGATIVE_INFINITY / Rational::exact_from(1.5),
6922 /// Float::NEGATIVE_INFINITY
6923 /// );
6924 /// assert_eq!(
6925 /// &Float::INFINITY / Rational::exact_from(-1.5),
6926 /// Float::NEGATIVE_INFINITY
6927 /// );
6928 /// assert_eq!(
6929 /// &Float::NEGATIVE_INFINITY / Rational::exact_from(-1.5),
6930 /// Float::INFINITY
6931 /// );
6932 ///
6933 /// assert_eq!(
6934 /// (&Float::from(2.5) / Rational::exact_from(1.5)).to_string(),
6935 /// "1.8"
6936 /// );
6937 /// assert_eq!(
6938 /// (&Float::from(2.5) / Rational::exact_from(-1.5)).to_string(),
6939 /// "-1.8"
6940 /// );
6941 /// assert_eq!(
6942 /// (&Float::from(-2.5) / Rational::exact_from(1.5)).to_string(),
6943 /// "-1.8"
6944 /// );
6945 /// assert_eq!(
6946 /// (&Float::from(-2.5) / Rational::exact_from(-1.5)).to_string(),
6947 /// "1.8"
6948 /// );
6949 /// ```
6950 #[inline]
6951 fn div(self, other: Rational) -> Float {
6952 let prec = self.significant_bits();
6953 self.div_rational_prec_round_ref_val(other, prec, Nearest).0
6954 }
6955}
6956
6957impl Div<&Rational> for &Float {
6958 type Output = Float;
6959
6960 /// Divides a [`Float`] by a [`Rational`], taking both by reference.
6961 ///
6962 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6963 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6964 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6965 /// rounding mode.
6966 ///
6967 /// $$
6968 /// f(x,y) = x/y+\varepsilon.
6969 /// $$
6970 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6971 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6972 /// where $p$ is the precision of the input [`Float`].
6973 ///
6974 /// Special cases:
6975 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
6976 /// - $f(\infty,x)=\infty$ if $x\geq 0$
6977 /// - $f(\infty,x)=-\infty$ if $x<0$
6978 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
6979 /// - $f(-\infty,x)=\infty$ if $x<0$
6980 /// - $f(0.0,x)=0.0$ if $x>0$
6981 /// - $f(0.0,x)=-0.0$ if $x<0$
6982 /// - $f(-0.0,x)=-0.0$ if $x>0$
6983 /// - $f(-0.0,x)=0.0$ if $x<0$
6984 ///
6985 /// Overflow and underflow:
6986 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
6987 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
6988 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6989 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6990 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
6991 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6992 ///
6993 /// If you want to use a rounding mode other than `Nearest`, consider using
6994 /// [`Float::div_rational_prec_ref_ref`] instead. If you want to specify the output precision,
6995 /// consider using [`Float::div_rational_round_ref_ref`]. If you want both of these things,
6996 /// consider using [`Float::div_rational_prec_round_ref_ref`].
6997 ///
6998 /// # Worst-case complexity
6999 /// $T(n) = O(n \log n \log\log n)$
7000 ///
7001 /// $M(n) = O(n \log n)$
7002 ///
7003 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
7004 /// other.significant_bits())`.
7005 ///
7006 /// # Examples
7007 /// ```
7008 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
7009 /// use malachite_base::num::conversion::traits::ExactFrom;
7010 /// use malachite_float::Float;
7011 /// use malachite_q::Rational;
7012 ///
7013 /// assert!((&Float::NAN / &Rational::exact_from(1.5)).is_nan());
7014 /// assert_eq!(
7015 /// &Float::INFINITY / &Rational::exact_from(1.5),
7016 /// Float::INFINITY
7017 /// );
7018 /// assert_eq!(
7019 /// &Float::NEGATIVE_INFINITY / &Rational::exact_from(1.5),
7020 /// Float::NEGATIVE_INFINITY
7021 /// );
7022 /// assert_eq!(
7023 /// &Float::INFINITY / &Rational::exact_from(-1.5),
7024 /// Float::NEGATIVE_INFINITY
7025 /// );
7026 /// assert_eq!(
7027 /// &Float::NEGATIVE_INFINITY / &Rational::exact_from(-1.5),
7028 /// Float::INFINITY
7029 /// );
7030 ///
7031 /// assert_eq!(
7032 /// (&Float::from(2.5) / &Rational::exact_from(1.5)).to_string(),
7033 /// "1.8"
7034 /// );
7035 /// assert_eq!(
7036 /// (&Float::from(2.5) / &Rational::exact_from(-1.5)).to_string(),
7037 /// "-1.8"
7038 /// );
7039 /// assert_eq!(
7040 /// (&Float::from(-2.5) / &Rational::exact_from(1.5)).to_string(),
7041 /// "-1.8"
7042 /// );
7043 /// assert_eq!(
7044 /// (&Float::from(-2.5) / &Rational::exact_from(-1.5)).to_string(),
7045 /// "1.8"
7046 /// );
7047 /// ```
7048 #[inline]
7049 fn div(self, other: &Rational) -> Float {
7050 let prec = self.significant_bits();
7051 self.div_rational_prec_round_ref_ref(other, prec, Nearest).0
7052 }
7053}
7054
7055impl DivAssign<Rational> for Float {
7056 /// Divides a [`Float`] by a [`Rational`] in place, taking the [`Rational`] by value.
7057 ///
7058 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
7059 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
7060 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
7061 /// rounding mode.
7062 ///
7063 /// $$
7064 /// x\gets = x/y+\varepsilon.
7065 /// $$
7066 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7067 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
7068 /// where $p$ is the precision of the input [`Float`].
7069 ///
7070 /// See the `/` documentation for information on special cases, overflow, and underflow.
7071 ///
7072 /// If you want to use a rounding mode other than `Nearest`, consider using
7073 /// [`Float::div_rational_prec_assign`] instead. If you want to specify the output precision,
7074 /// consider using [`Float::div_rational_round_assign`]. If you want both of these things,
7075 /// consider using [`Float::div_rational_prec_round_assign`].
7076 ///
7077 /// # Worst-case complexity
7078 /// $T(n) = O(n \log n \log\log n)$
7079 ///
7080 /// $M(n) = O(n \log n)$
7081 ///
7082 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
7083 /// other.significant_bits())`.
7084 ///
7085 /// # Examples
7086 /// ```
7087 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
7088 /// use malachite_base::num::conversion::traits::ExactFrom;
7089 /// use malachite_float::Float;
7090 /// use malachite_q::Rational;
7091 ///
7092 /// let mut x = Float::NAN;
7093 /// x /= Rational::exact_from(1.5);
7094 /// assert!(x.is_nan());
7095 ///
7096 /// let mut x = Float::INFINITY;
7097 /// x /= Rational::exact_from(1.5);
7098 /// assert_eq!(x, Float::INFINITY);
7099 ///
7100 /// let mut x = Float::NEGATIVE_INFINITY;
7101 /// x /= Rational::exact_from(1.5);
7102 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
7103 ///
7104 /// let mut x = Float::INFINITY;
7105 /// x /= Rational::exact_from(-1.5);
7106 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
7107 ///
7108 /// let mut x = Float::NEGATIVE_INFINITY;
7109 /// x /= Rational::exact_from(-1.5);
7110 /// assert_eq!(x, Float::INFINITY);
7111 ///
7112 /// let mut x = Float::from(2.5);
7113 /// x /= Rational::exact_from(1.5);
7114 /// assert_eq!(x.to_string(), "1.8");
7115 /// ```
7116 #[inline]
7117 fn div_assign(&mut self, other: Rational) {
7118 let prec = self.significant_bits();
7119 self.div_rational_prec_round_assign(other, prec, Nearest);
7120 }
7121}
7122
7123impl DivAssign<&Rational> for Float {
7124 /// Divides a [`Float`] by a [`Rational`] in place, taking the [`Rational`] by reference.
7125 ///
7126 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
7127 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
7128 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
7129 /// rounding mode.
7130 ///
7131 /// $$
7132 /// x\gets = x/y+\varepsilon.
7133 /// $$
7134 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7135 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
7136 /// where $p$ is the precision of the input [`Float`].
7137 ///
7138 /// See the `/` documentation for information on special cases, overflow, and underflow.
7139 ///
7140 /// If you want to use a rounding mode other than `Nearest`, consider using
7141 /// [`Float::div_rational_prec_assign_ref`] instead. If you want to specify the output
7142 /// precision, consider using [`Float::div_rational_round_assign_ref`]. If you want both of
7143 /// these things, consider using [`Float::div_rational_prec_round_assign_ref`].
7144 ///
7145 /// # Worst-case complexity
7146 /// $T(n) = O(n \log n \log\log n)$
7147 ///
7148 /// $M(n) = O(n \log n)$
7149 ///
7150 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
7151 /// other.significant_bits())`.
7152 ///
7153 /// # Examples
7154 /// ```
7155 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
7156 /// use malachite_base::num::conversion::traits::ExactFrom;
7157 /// use malachite_float::Float;
7158 /// use malachite_q::Rational;
7159 ///
7160 /// let mut x = Float::NAN;
7161 /// x /= &Rational::exact_from(1.5);
7162 /// assert!(x.is_nan());
7163 ///
7164 /// let mut x = Float::INFINITY;
7165 /// x /= &Rational::exact_from(1.5);
7166 /// assert_eq!(x, Float::INFINITY);
7167 ///
7168 /// let mut x = Float::NEGATIVE_INFINITY;
7169 /// x /= &Rational::exact_from(1.5);
7170 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
7171 ///
7172 /// let mut x = Float::INFINITY;
7173 /// x /= &Rational::exact_from(-1.5);
7174 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
7175 ///
7176 /// let mut x = Float::NEGATIVE_INFINITY;
7177 /// x /= &Rational::exact_from(-1.5);
7178 /// assert_eq!(x, Float::INFINITY);
7179 ///
7180 /// let mut x = Float::from(2.5);
7181 /// x /= &Rational::exact_from(1.5);
7182 /// assert_eq!(x.to_string(), "1.8");
7183 /// ```
7184 #[inline]
7185 fn div_assign(&mut self, other: &Rational) {
7186 let prec = self.significant_bits();
7187 self.div_rational_prec_round_assign_ref(other, prec, Nearest);
7188 }
7189}
7190
7191impl Div<Float> for Rational {
7192 type Output = Float;
7193
7194 /// Divides a [`Rational`] by a [`Float`], taking both by value.
7195 ///
7196 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
7197 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
7198 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
7199 /// rounding mode.
7200 ///
7201 /// $$
7202 /// f(x,y) = x/y+\varepsilon.
7203 /// $$
7204 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7205 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
7206 /// where $p$ is the precision of the input [`Float`].
7207 ///
7208 /// Special cases:
7209 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
7210 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
7211 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
7212 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
7213 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
7214 /// - $f(0,x,p,m)=0.0$ if $x>0$
7215 /// - $f(0,x,p,m)=-0.0$ if $x<0$
7216 ///
7217 /// Overflow and underflow:
7218 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7219 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
7220 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7221 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7222 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
7223 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7224 ///
7225 /// # Worst-case complexity
7226 /// $T(n) = O(n \log n \log\log n)$
7227 ///
7228 /// $M(n) = O(n \log n)$
7229 ///
7230 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
7231 /// other.significant_bits())`.
7232 ///
7233 /// # Examples
7234 /// ```
7235 /// use malachite_base::num::basic::traits::{
7236 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
7237 /// };
7238 /// use malachite_base::num::conversion::traits::ExactFrom;
7239 /// use malachite_float::Float;
7240 /// use malachite_q::Rational;
7241 ///
7242 /// assert!((Rational::exact_from(1.5) / Float::NAN).is_nan());
7243 /// assert_eq!(Rational::exact_from(1.5) / Float::ZERO, Float::INFINITY);
7244 /// assert_eq!(
7245 /// Rational::exact_from(1.5) / Float::NEGATIVE_ZERO,
7246 /// Float::NEGATIVE_INFINITY
7247 /// );
7248 /// assert_eq!(
7249 /// Rational::exact_from(-1.5) / Float::ZERO,
7250 /// Float::NEGATIVE_INFINITY
7251 /// );
7252 /// assert_eq!(
7253 /// Rational::exact_from(-1.5) / Float::NEGATIVE_ZERO,
7254 /// Float::INFINITY
7255 /// );
7256 ///
7257 /// assert_eq!(
7258 /// (Rational::exact_from(1.5) / Float::from(2.5)).to_string(),
7259 /// "0.6"
7260 /// );
7261 /// assert_eq!(
7262 /// (Rational::exact_from(-1.5) / Float::from(2.5)).to_string(),
7263 /// "-0.6"
7264 /// );
7265 /// assert_eq!(
7266 /// (Rational::exact_from(1.5) / Float::from(-2.5)).to_string(),
7267 /// "-0.6"
7268 /// );
7269 /// assert_eq!(
7270 /// (Rational::exact_from(-1.5) / Float::from(-2.5)).to_string(),
7271 /// "0.6"
7272 /// );
7273 /// ```
7274 #[inline]
7275 fn div(self, other: Float) -> Float {
7276 let prec = other.significant_bits();
7277 Float::rational_div_float_prec_round(self, other, prec, Nearest).0
7278 }
7279}
7280
7281impl Div<&Float> for Rational {
7282 type Output = Float;
7283
7284 /// Divides a [`Rational`] by a [`Float`], taking the [`Rational`] by value and the [`Float`] by
7285 /// reference.
7286 ///
7287 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
7288 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
7289 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
7290 /// rounding mode.
7291 ///
7292 /// $$
7293 /// f(x,y) = x/y+\varepsilon.
7294 /// $$
7295 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7296 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
7297 /// where $p$ is the precision of the input [`Float`].
7298 ///
7299 /// Special cases:
7300 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
7301 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
7302 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
7303 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
7304 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
7305 /// - $f(0,x,p,m)=0.0$ if $x>0$
7306 /// - $f(0,x,p,m)=-0.0$ if $x<0$
7307 ///
7308 /// Overflow and underflow:
7309 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7310 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
7311 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7312 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7313 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
7314 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7315 ///
7316 /// # Worst-case complexity
7317 /// $T(n) = O(n \log n \log\log n)$
7318 ///
7319 /// $M(n) = O(n \log n)$
7320 ///
7321 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
7322 /// other.significant_bits())`.
7323 ///
7324 /// # Examples
7325 /// ```
7326 /// use malachite_base::num::basic::traits::{
7327 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
7328 /// };
7329 /// use malachite_base::num::conversion::traits::ExactFrom;
7330 /// use malachite_float::Float;
7331 /// use malachite_q::Rational;
7332 ///
7333 /// assert!((Rational::exact_from(1.5) / &Float::NAN).is_nan());
7334 /// assert_eq!(Rational::exact_from(1.5) / &Float::ZERO, Float::INFINITY);
7335 /// assert_eq!(
7336 /// Rational::exact_from(1.5) / &Float::NEGATIVE_ZERO,
7337 /// Float::NEGATIVE_INFINITY
7338 /// );
7339 /// assert_eq!(
7340 /// Rational::exact_from(-1.5) / &Float::ZERO,
7341 /// Float::NEGATIVE_INFINITY
7342 /// );
7343 /// assert_eq!(
7344 /// Rational::exact_from(-1.5) / &Float::NEGATIVE_ZERO,
7345 /// Float::INFINITY
7346 /// );
7347 ///
7348 /// assert_eq!(
7349 /// (Rational::exact_from(1.5) / &Float::from(2.5)).to_string(),
7350 /// "0.6"
7351 /// );
7352 /// assert_eq!(
7353 /// (Rational::exact_from(-1.5) / &Float::from(2.5)).to_string(),
7354 /// "-0.6"
7355 /// );
7356 /// assert_eq!(
7357 /// (Rational::exact_from(1.5) / &Float::from(-2.5)).to_string(),
7358 /// "-0.6"
7359 /// );
7360 /// assert_eq!(
7361 /// (Rational::exact_from(-1.5) / &Float::from(-2.5)).to_string(),
7362 /// "0.6"
7363 /// );
7364 /// ```
7365 #[inline]
7366 fn div(self, other: &Float) -> Float {
7367 let prec = other.significant_bits();
7368 Float::rational_div_float_prec_round_val_ref(self, other, prec, Nearest).0
7369 }
7370}
7371
7372impl Div<Float> for &Rational {
7373 type Output = Float;
7374
7375 /// Divides a [`Rational`] by a [`Float`], taking the [`Rational`] by reference and the
7376 /// [`Float`] by value.
7377 ///
7378 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
7379 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
7380 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
7381 /// rounding mode.
7382 ///
7383 /// $$
7384 /// f(x,y) = x/y+\varepsilon.
7385 /// $$
7386 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7387 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
7388 /// where $p$ is the precision of the input [`Float`].
7389 ///
7390 /// Special cases:
7391 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
7392 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
7393 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
7394 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
7395 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
7396 /// - $f(0,x,p,m)=0.0$ if $x>0$
7397 /// - $f(0,x,p,m)=-0.0$ if $x<0$
7398 ///
7399 /// Overflow and underflow:
7400 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7401 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
7402 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7403 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7404 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
7405 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7406 ///
7407 /// # Worst-case complexity
7408 /// $T(n) = O(n \log n \log\log n)$
7409 ///
7410 /// $M(n) = O(n \log n)$
7411 ///
7412 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
7413 /// other.significant_bits())`.
7414 ///
7415 /// # Examples
7416 /// ```
7417 /// use malachite_base::num::basic::traits::{
7418 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
7419 /// };
7420 /// use malachite_base::num::conversion::traits::ExactFrom;
7421 /// use malachite_float::Float;
7422 /// use malachite_q::Rational;
7423 ///
7424 /// assert!((&Rational::exact_from(1.5) / Float::NAN).is_nan());
7425 /// assert_eq!(&Rational::exact_from(1.5) / Float::ZERO, Float::INFINITY);
7426 /// assert_eq!(
7427 /// &Rational::exact_from(1.5) / Float::NEGATIVE_ZERO,
7428 /// Float::NEGATIVE_INFINITY
7429 /// );
7430 /// assert_eq!(
7431 /// &Rational::exact_from(-1.5) / Float::ZERO,
7432 /// Float::NEGATIVE_INFINITY
7433 /// );
7434 /// assert_eq!(
7435 /// &Rational::exact_from(-1.5) / Float::NEGATIVE_ZERO,
7436 /// Float::INFINITY
7437 /// );
7438 ///
7439 /// assert_eq!(
7440 /// (&Rational::exact_from(1.5) / Float::from(2.5)).to_string(),
7441 /// "0.6"
7442 /// );
7443 /// assert_eq!(
7444 /// (&Rational::exact_from(-1.5) / Float::from(2.5)).to_string(),
7445 /// "-0.6"
7446 /// );
7447 /// assert_eq!(
7448 /// (&Rational::exact_from(1.5) / Float::from(-2.5)).to_string(),
7449 /// "-0.6"
7450 /// );
7451 /// assert_eq!(
7452 /// (&Rational::exact_from(-1.5) / Float::from(-2.5)).to_string(),
7453 /// "0.6"
7454 /// );
7455 /// ```
7456 #[inline]
7457 fn div(self, other: Float) -> Float {
7458 let prec = other.significant_bits();
7459 Float::rational_div_float_prec_round_ref_val(self, other, prec, Nearest).0
7460 }
7461}
7462
7463impl Div<&Float> for &Rational {
7464 type Output = Float;
7465
7466 /// Divides a [`Rational`] by a [`Float`], taking both by reference.
7467 ///
7468 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
7469 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
7470 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
7471 /// rounding mode.
7472 ///
7473 /// $$
7474 /// f(x,y) = x/y+\varepsilon.
7475 /// $$
7476 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7477 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
7478 /// where $p$ is the precision of the input [`Float`].
7479 ///
7480 /// Special cases:
7481 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
7482 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
7483 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or #x=-0.0$
7484 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
7485 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or #x=-0.0$
7486 /// - $f(0,x,p,m)=0.0$ if $x>0$
7487 /// - $f(0,x,p,m)=-0.0$ if $x<0$
7488 ///
7489 /// Overflow and underflow:
7490 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7491 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
7492 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7493 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7494 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
7495 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7496 ///
7497 /// # Worst-case complexity
7498 /// $T(n) = O(n \log n \log\log n)$
7499 ///
7500 /// $M(n) = O(n \log n)$
7501 ///
7502 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
7503 /// other.significant_bits())`.
7504 ///
7505 /// # Examples
7506 /// ```
7507 /// use malachite_base::num::basic::traits::{
7508 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
7509 /// };
7510 /// use malachite_base::num::conversion::traits::ExactFrom;
7511 /// use malachite_float::Float;
7512 /// use malachite_q::Rational;
7513 ///
7514 /// assert!((&Rational::exact_from(1.5) / &Float::NAN).is_nan());
7515 /// assert_eq!(&Rational::exact_from(1.5) / &Float::ZERO, Float::INFINITY);
7516 /// assert_eq!(
7517 /// &Rational::exact_from(1.5) / &Float::NEGATIVE_ZERO,
7518 /// Float::NEGATIVE_INFINITY
7519 /// );
7520 /// assert_eq!(
7521 /// &Rational::exact_from(-1.5) / &Float::ZERO,
7522 /// Float::NEGATIVE_INFINITY
7523 /// );
7524 /// assert_eq!(
7525 /// &Rational::exact_from(-1.5) / &Float::NEGATIVE_ZERO,
7526 /// Float::INFINITY
7527 /// );
7528 ///
7529 /// assert_eq!(
7530 /// (&Rational::exact_from(1.5) / &Float::from(2.5)).to_string(),
7531 /// "0.6"
7532 /// );
7533 /// assert_eq!(
7534 /// (&Rational::exact_from(-1.5) / &Float::from(2.5)).to_string(),
7535 /// "-0.6"
7536 /// );
7537 /// assert_eq!(
7538 /// (&Rational::exact_from(1.5) / &Float::from(-2.5)).to_string(),
7539 /// "-0.6"
7540 /// );
7541 /// assert_eq!(
7542 /// (&Rational::exact_from(-1.5) / &Float::from(-2.5)).to_string(),
7543 /// "0.6"
7544 /// );
7545 /// ```
7546 #[inline]
7547 fn div(self, other: &Float) -> Float {
7548 let prec = other.significant_bits();
7549 Float::rational_div_float_prec_round_ref_ref(self, other, prec, Nearest).0
7550 }
7551}