malachite_float/float/arithmetic/div.rs
1// Copyright © 2026 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::float::arithmetic::is_power_of_2::abs_is_power_of_2;
11use crate::float::conversion::from_natural::{
12 from_natural_zero_exponent, from_natural_zero_exponent_ref,
13};
14use crate::{
15 Float, float_either_infinity, float_either_zero, float_infinity, float_nan,
16 float_negative_infinity, float_negative_zero, float_zero,
17};
18use core::cmp::Ordering::{self, *};
19use core::cmp::max;
20use core::mem::swap;
21use core::ops::{Div, DivAssign};
22use malachite_base::num::arithmetic::traits::{
23 CheckedLogBase2, FloorLogBase2, IsPowerOf2, NegAssign, Sign,
24};
25use malachite_base::num::basic::traits::{
26 Infinity as InfinityTrait, NaN as NaNTrait, NegativeInfinity, NegativeZero, Zero as ZeroTrait,
27};
28use malachite_base::num::conversion::traits::ExactFrom;
29use malachite_base::num::logic::traits::{NotAssign, SignificantBits};
30use malachite_base::rounding_modes::RoundingMode::{self, *};
31use malachite_nz::natural::arithmetic::float_div::{
32 div_float_significands_in_place, div_float_significands_in_place_ref,
33 div_float_significands_ref_ref, div_float_significands_ref_val,
34};
35use malachite_q::Rational;
36
37const DIV_RATIONAL_THRESHOLD: u64 = 50;
38const RATIONAL_DIV_THRESHOLD: u64 = 50;
39
40fn div_rational_prec_round_assign_naive(
41 x: &mut Float,
42 y: Rational,
43 prec: u64,
44 rm: RoundingMode,
45) -> Ordering {
46 assert_ne!(prec, 0);
47 match (&mut *x, y) {
48 (float_nan!(), _) => Equal,
49 (Float(Infinity { sign }), y) => {
50 if y < 0 {
51 sign.not_assign();
52 };
53 Equal
54 }
55 (Float(Zero { sign }), y) => {
56 match y.sign() {
57 Equal => *x = float_nan!(),
58 Greater => {}
59 Less => sign.not_assign(),
60 }
61 Equal
62 }
63 (x, y) => {
64 if y == 0 {
65 *x = Float(Infinity { sign: *x > 0u32 });
66 Equal
67 } else {
68 let not_sign = *x < 0;
69 let mut z = Float::ZERO;
70 swap(x, &mut z);
71 let (mut quotient, o) =
72 Float::from_rational_prec_round(Rational::exact_from(z) / y, prec, rm);
73 if quotient == 0u32 && not_sign {
74 quotient.neg_assign();
75 }
76 *x = quotient;
77 o
78 }
79 }
80 }
81}
82
83fn div_rational_prec_round_assign_naive_ref(
84 x: &mut Float,
85 y: &Rational,
86 prec: u64,
87 rm: RoundingMode,
88) -> Ordering {
89 assert_ne!(prec, 0);
90 match (&mut *x, y) {
91 (float_nan!(), _) => Equal,
92 (Float(Infinity { sign }), y) => {
93 if *y < 0 {
94 sign.not_assign();
95 };
96 Equal
97 }
98 (Float(Zero { sign }), y) => {
99 match y.sign() {
100 Equal => *x = float_nan!(),
101 Greater => {}
102 Less => sign.not_assign(),
103 }
104 Equal
105 }
106 (x, y) => {
107 if *y == 0 {
108 *x = Float(Infinity { sign: *x > 0u32 });
109 Equal
110 } else {
111 let not_sign = *x < 0;
112 let mut z = Float::ZERO;
113 swap(x, &mut z);
114 let (mut quotient, o) =
115 Float::from_rational_prec_round(Rational::exact_from(z) / y, prec, rm);
116 if quotient == 0u32 && not_sign {
117 quotient.neg_assign();
118 }
119 *x = quotient;
120 o
121 }
122 }
123 }
124}
125
126pub_test! {div_rational_prec_round_naive(
127 mut x: Float,
128 y: Rational,
129 prec: u64,
130 rm: RoundingMode,
131) -> (Float, Ordering) {
132 let o = div_rational_prec_round_assign_naive(&mut x, y, prec, rm);
133 (x, o)
134}}
135
136pub_test! {div_rational_prec_round_naive_val_ref(
137 mut x: Float,
138 y: &Rational,
139 prec: u64,
140 rm: RoundingMode,
141) -> (Float, Ordering) {
142 let o = div_rational_prec_round_assign_naive_ref(&mut x, y, prec, rm);
143 (x, o)
144}}
145
146pub_test! {div_rational_prec_round_naive_ref_val(
147 x: &Float,
148 y: Rational,
149 prec: u64,
150 rm: RoundingMode,
151) -> (Float, Ordering) {
152 assert_ne!(prec, 0);
153 match (x, y) {
154 (float_nan!(), _) => (float_nan!(), Equal),
155 (Float(Infinity { sign }), y) => (
156 if y >= 0u32 {
157 Float(Infinity { sign: *sign })
158 } else {
159 Float(Infinity { sign: !*sign })
160 },
161 Equal,
162 ),
163 (Float(Zero { sign }), y) => (
164 match y.sign() {
165 Equal => float_nan!(),
166 Greater => Float(Zero { sign: *sign }),
167 Less => Float(Zero { sign: !*sign }),
168 },
169 Equal,
170 ),
171 (x, y) => {
172 if y == 0 {
173 (Float(Infinity { sign: *x > 0u32 }), Equal)
174 } else {
175 let (mut quotient, o) =
176 Float::from_rational_prec_round(Rational::exact_from(x) / y, prec, rm);
177 if quotient == 0u32 && *x < 0 {
178 quotient.neg_assign();
179 }
180 (quotient, o)
181 }
182 }
183 }
184}}
185
186pub_test! {div_rational_prec_round_naive_ref_ref(
187 x: &Float,
188 y: &Rational,
189 prec: u64,
190 rm: RoundingMode,
191) -> (Float, Ordering) {
192 assert_ne!(prec, 0);
193 match (x, y) {
194 (float_nan!(), _) => (float_nan!(), Equal),
195 (Float(Infinity { sign }), y) => (
196 if *y >= 0u32 {
197 Float(Infinity { sign: *sign })
198 } else {
199 Float(Infinity { sign: !*sign })
200 },
201 Equal,
202 ),
203 (Float(Zero { sign }), y) => (
204 match y.sign() {
205 Equal => float_nan!(),
206 Greater => Float(Zero { sign: *sign }),
207 Less => Float(Zero { sign: !*sign }),
208 },
209 Equal,
210 ),
211 (x, y) => {
212 if *y == 0 {
213 (Float(Infinity { sign: *x > 0u32 }), Equal)
214 } else {
215 let (mut quotient, o) =
216 Float::from_rational_prec_round(Rational::exact_from(x) / y, prec, rm);
217 if quotient == 0u32 && *x < 0 {
218 quotient.neg_assign();
219 }
220 (quotient, o)
221 }
222 }
223 }
224}}
225
226fn div_rational_prec_round_assign_direct(
227 x: &mut Float,
228 y: Rational,
229 prec: u64,
230 mut rm: RoundingMode,
231) -> Ordering {
232 assert_ne!(prec, 0);
233 if y == 0u32 {
234 *x = match (*x).partial_cmp(&0u32) {
235 Some(Greater) => Float::INFINITY,
236 Some(Less) => Float::NEGATIVE_INFINITY,
237 _ => Float::NAN,
238 };
239 return Equal;
240 }
241 let sign = y >= 0;
242 let (n, d) = y.into_numerator_and_denominator();
243 if !sign {
244 rm.neg_assign();
245 }
246 let o = match (n.checked_log_base_2(), d.checked_log_base_2()) {
247 (Some(log_n), Some(log_d)) => {
248 x.shl_prec_round_assign(i128::from(log_d) - i128::from(log_n), prec, rm)
249 }
250 (None, Some(log_d)) => {
251 let x_exp = x.get_exponent().unwrap();
252 let n_exp = n.floor_log_base_2();
253 *x >>= x_exp;
254 let o = x.div_prec_round_assign(from_natural_zero_exponent(n), prec, rm);
255 x.shl_prec_round_assign_helper(
256 i128::from(x_exp) - i128::from(n_exp) + i128::from(log_d) - 1,
257 prec,
258 rm,
259 o,
260 )
261 }
262 (Some(log_n), None) => {
263 let x_exp = x.get_exponent().unwrap();
264 let d_exp = d.floor_log_base_2();
265 *x >>= x_exp;
266 let o = x.mul_prec_round_assign(from_natural_zero_exponent(d), prec, rm);
267 x.shl_prec_round_assign_helper(
268 i128::from(x_exp) - i128::from(log_n) + i128::from(d_exp) + 1,
269 prec,
270 rm,
271 o,
272 )
273 }
274 (None, None) => {
275 let x_exp = x.get_exponent().unwrap();
276 let n_exp = n.floor_log_base_2();
277 let d_exp = d.floor_log_base_2();
278 let n = from_natural_zero_exponent(n);
279 let d = from_natural_zero_exponent(d);
280 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
281 *x >>= x_exp;
282 x.mul_prec_round_assign(d, mul_prec, Floor);
283 let o = x.div_prec_round_assign(n, prec, rm);
284 x.shl_prec_round_assign_helper(
285 i128::from(x_exp) - i128::from(n_exp) + i128::from(d_exp),
286 prec,
287 rm,
288 o,
289 )
290 }
291 };
292 if sign {
293 o
294 } else {
295 x.neg_assign();
296 o.reverse()
297 }
298}
299
300fn div_rational_prec_round_assign_direct_ref(
301 x: &mut Float,
302 y: &Rational,
303 prec: u64,
304 mut rm: RoundingMode,
305) -> Ordering {
306 assert_ne!(prec, 0);
307 if *y == 0u32 {
308 *x = match (*x).partial_cmp(&0u32) {
309 Some(Greater) => Float::INFINITY,
310 Some(Less) => Float::NEGATIVE_INFINITY,
311 _ => Float::NAN,
312 };
313 return Equal;
314 }
315 let sign = *y >= 0;
316 let (n, d) = y.numerator_and_denominator_ref();
317 if !sign {
318 rm.neg_assign();
319 }
320 let o = match (n.checked_log_base_2(), d.checked_log_base_2()) {
321 (Some(log_n), Some(log_d)) => {
322 x.shl_prec_round_assign(i128::from(log_d) - i128::from(log_n), prec, rm)
323 }
324 (None, Some(log_d)) => {
325 let x_exp = x.get_exponent().unwrap();
326 let n_exp = n.floor_log_base_2();
327 *x >>= x_exp;
328 let o = x.div_prec_round_assign(from_natural_zero_exponent_ref(n), prec, rm);
329 x.shl_prec_round_assign_helper(
330 i128::from(x_exp) - i128::from(n_exp) + i128::from(log_d) - 1,
331 prec,
332 rm,
333 o,
334 )
335 }
336 (Some(log_n), None) => {
337 let x_exp = x.get_exponent().unwrap();
338 let d_exp = d.floor_log_base_2();
339 *x >>= x_exp;
340 let o = x.mul_prec_round_assign(from_natural_zero_exponent_ref(d), prec, rm);
341 x.shl_prec_round_assign_helper(
342 i128::from(x_exp) - i128::from(log_n) + i128::from(d_exp) + 1,
343 prec,
344 rm,
345 o,
346 )
347 }
348 (None, None) => {
349 let x_exp = x.get_exponent().unwrap();
350 let n_exp = n.floor_log_base_2();
351 let d_exp = d.floor_log_base_2();
352 let n = from_natural_zero_exponent_ref(n);
353 let d = from_natural_zero_exponent_ref(d);
354 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
355 *x >>= x_exp;
356 x.mul_prec_round_assign(d, mul_prec, Floor);
357 let o = x.div_prec_round_assign(n, prec, rm);
358 x.shl_prec_round_assign_helper(
359 i128::from(x_exp) - i128::from(n_exp) + i128::from(d_exp),
360 prec,
361 rm,
362 o,
363 )
364 }
365 };
366 if sign {
367 o
368 } else {
369 x.neg_assign();
370 o.reverse()
371 }
372}
373
374pub_test! {div_rational_prec_round_direct(
375 mut x: Float,
376 y: Rational,
377 prec: u64,
378 rm: RoundingMode,
379) -> (Float, Ordering) {
380 let o = div_rational_prec_round_assign_direct(&mut x, y, prec, rm);
381 (x, o)
382}}
383
384pub_test! {div_rational_prec_round_direct_val_ref(
385 mut x: Float,
386 y: &Rational,
387 prec: u64,
388 rm: RoundingMode,
389) -> (Float, Ordering) {
390 let o = div_rational_prec_round_assign_direct_ref(&mut x, y, prec, rm);
391 (x, o)
392}}
393
394pub_test! {div_rational_prec_round_direct_ref_val(
395 x: &Float,
396 y: Rational,
397 prec: u64,
398 mut rm: RoundingMode,
399) -> (Float, Ordering) {
400 assert_ne!(prec, 0);
401 let sign = y >= 0;
402 if y == 0u32 {
403 return (
404 match x.partial_cmp(&0u32) {
405 Some(Greater) => Float::INFINITY,
406 Some(Less) => Float::NEGATIVE_INFINITY,
407 _ => Float::NAN,
408 },
409 Equal,
410 );
411 }
412 let (n, d) = y.into_numerator_and_denominator();
413 if !sign {
414 rm.neg_assign();
415 }
416 let (quotient, o) = match (n.checked_log_base_2(), d.checked_log_base_2()) {
417 (Some(log_n), Some(log_d)) => {
418 x.shl_prec_round_ref(i128::from(log_d) - i128::from(log_n), prec, rm)
419 }
420 (None, Some(log_d)) => {
421 let x_exp = x.get_exponent().unwrap();
422 let n_exp = n.floor_log_base_2();
423 let mut x = x >> x_exp;
424 let o = x.div_prec_round_assign(from_natural_zero_exponent(n), prec, rm);
425 let o = x.shl_prec_round_assign_helper(
426 i128::from(x_exp) - i128::from(n_exp) + i128::from(log_d) - 1,
427 prec,
428 rm,
429 o,
430 );
431 (x, o)
432 }
433 (Some(log_n), None) => {
434 let x_exp = x.get_exponent().unwrap();
435 let d_exp = d.floor_log_base_2();
436 let mut x = x >> x_exp;
437 let o = x.mul_prec_round_assign(from_natural_zero_exponent(d), prec, rm);
438 let o = x.shl_prec_round_assign_helper(
439 i128::from(x_exp) - i128::from(log_n) + i128::from(d_exp) + 1,
440 prec,
441 rm,
442 o,
443 );
444 (x, o)
445 }
446 (None, None) => {
447 let x_exp = x.get_exponent().unwrap();
448 let n_exp = n.floor_log_base_2();
449 let d_exp = d.floor_log_base_2();
450 let n = from_natural_zero_exponent(n);
451 let d = from_natural_zero_exponent(d);
452 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
453 let mut x = x >> x_exp;
454 x.mul_prec_round_assign(d, mul_prec, Floor);
455 let o = x.div_prec_round_assign(n, prec, rm);
456 let o = x.shl_prec_round_assign_helper(
457 i128::from(x_exp) - i128::from(n_exp) + i128::from(d_exp),
458 prec,
459 rm,
460 o,
461 );
462 (x, o)
463 }
464 };
465 if sign {
466 (quotient, o)
467 } else {
468 (-quotient, o.reverse())
469 }
470}}
471
472pub_test! {div_rational_prec_round_direct_ref_ref(
473 x: &Float,
474 y: &Rational,
475 prec: u64,
476 mut rm: RoundingMode,
477) -> (Float, Ordering) {
478 assert_ne!(prec, 0);
479 if *y == 0u32 {
480 return (
481 match x.partial_cmp(&0u32) {
482 Some(Greater) => Float::INFINITY,
483 Some(Less) => Float::NEGATIVE_INFINITY,
484 _ => Float::NAN,
485 },
486 Equal,
487 );
488 }
489 let sign = *y >= 0;
490 let (n, d) = y.numerator_and_denominator_ref();
491 if !sign {
492 rm.neg_assign();
493 }
494 let (quotient, o) = match (n.checked_log_base_2(), d.checked_log_base_2()) {
495 (Some(log_n), Some(log_d)) => {
496 x.shl_prec_round_ref(i128::from(log_d) - i128::from(log_n), prec, rm)
497 }
498 (None, Some(log_d)) => {
499 let x_exp = x.get_exponent().unwrap();
500 let n_exp = n.floor_log_base_2();
501 let mut x = x >> x_exp;
502 let o = x.div_prec_round_assign(from_natural_zero_exponent_ref(n), prec, rm);
503 let o = x.shl_prec_round_assign_helper(
504 i128::from(x_exp) - i128::from(n_exp) + i128::from(log_d) - 1,
505 prec,
506 rm,
507 o,
508 );
509 (x, o)
510 }
511 (Some(log_n), None) => {
512 let x_exp = x.get_exponent().unwrap();
513 let d_exp = d.floor_log_base_2();
514 let mut x = x >> x_exp;
515 let o = x.mul_prec_round_assign(from_natural_zero_exponent_ref(d), prec, rm);
516 let o = x.shl_prec_round_assign_helper(
517 i128::from(x_exp) - i128::from(log_n) + i128::from(d_exp) + 1,
518 prec,
519 rm,
520 o,
521 );
522 (x, o)
523 }
524 (None, None) => {
525 let x_exp = x.get_exponent().unwrap();
526 let n_exp = n.floor_log_base_2();
527 let d_exp = d.floor_log_base_2();
528 let n = from_natural_zero_exponent_ref(n);
529 let d = from_natural_zero_exponent_ref(d);
530 let mul_prec = x.get_min_prec().unwrap_or(1) + d.significant_bits();
531 let mut x = x >> x_exp;
532 x.mul_prec_round_assign(d, mul_prec, Floor);
533 let o = x.div_prec_round_assign(n, prec, rm);
534 let o = x.shl_prec_round_assign_helper(
535 i128::from(x_exp) - i128::from(n_exp) + i128::from(d_exp),
536 prec,
537 rm,
538 o,
539 );
540 (x, o)
541 }
542 };
543 if sign {
544 (quotient, o)
545 } else {
546 (-quotient, o.reverse())
547 }
548}}
549
550pub_test! {rational_div_float_prec_round_naive(
551 x: Rational,
552 y: Float,
553 prec: u64,
554 rm: RoundingMode,
555) -> (Float, Ordering) {
556 assert_ne!(prec, 0);
557 match (x, y) {
558 (_, float_nan!()) => (float_nan!(), Equal),
559 (x, Float(Infinity { sign })) => (
560 if x >= 0u32 {
561 Float(Zero { sign })
562 } else {
563 Float(Zero { sign: !sign })
564 },
565 Equal,
566 ),
567 (x, Float(Zero { sign })) => (
568 match x.sign() {
569 Equal => float_nan!(),
570 Greater => Float(Infinity { sign }),
571 Less => Float(Infinity { sign: !sign }),
572 },
573 Equal,
574 ),
575 (x, y) => {
576 let not_sign = y < 0;
577 let (mut quotient, o) =
578 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
579 if quotient == 0u32 && not_sign {
580 quotient.neg_assign();
581 }
582 (quotient, o)
583 }
584 }
585}}
586
587pub_test! {rational_div_float_prec_round_naive_val_ref(
588 x: Rational,
589 y: &Float,
590 prec: u64,
591 rm: RoundingMode,
592) -> (Float, Ordering) {
593 assert_ne!(prec, 0);
594 match (x, y) {
595 (_, float_nan!()) => (float_nan!(), Equal),
596 (x, Float(Infinity { sign })) => (
597 if x >= 0u32 {
598 Float(Zero { sign: *sign })
599 } else {
600 Float(Zero { sign: !*sign })
601 },
602 Equal,
603 ),
604 (x, Float(Zero { sign })) => (
605 match x.sign() {
606 Equal => float_nan!(),
607 Greater => Float(Infinity { sign: *sign }),
608 Less => Float(Infinity { sign: !*sign }),
609 },
610 Equal,
611 ),
612 (x, y) => {
613 let (mut quotient, o) =
614 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
615 if quotient == 0u32 && *y < 0 {
616 quotient.neg_assign();
617 }
618 (quotient, o)
619 }
620 }
621}}
622
623pub_test! {rational_div_float_prec_round_naive_ref_val(
624 x: &Rational,
625 y: Float,
626 prec: u64,
627 rm: RoundingMode,
628) -> (Float, Ordering) {
629 assert_ne!(prec, 0);
630 match (x, y) {
631 (_, float_nan!()) => (float_nan!(), Equal),
632 (x, Float(Infinity { sign })) => (
633 if *x >= 0u32 {
634 Float(Zero { sign })
635 } else {
636 Float(Zero { sign: !sign })
637 },
638 Equal,
639 ),
640 (x, Float(Zero { sign })) => (
641 match x.sign() {
642 Equal => float_nan!(),
643 Greater => Float(Infinity { sign }),
644 Less => Float(Infinity { sign: !sign }),
645 },
646 Equal,
647 ),
648 (x, y) => {
649 let not_sign = y < 0;
650 let (mut quotient, o) =
651 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
652 if quotient == 0u32 && not_sign {
653 quotient.neg_assign();
654 }
655 (quotient, o)
656 }
657 }
658}}
659
660pub_test! {rational_div_float_prec_round_naive_ref_ref(
661 x: &Rational,
662 y: &Float,
663 prec: u64,
664 rm: RoundingMode,
665) -> (Float, Ordering) {
666 assert_ne!(prec, 0);
667 match (x, y) {
668 (_, float_nan!()) => (float_nan!(), Equal),
669 (x, Float(Infinity { sign })) => (
670 if *x >= 0u32 {
671 Float(Zero { sign: *sign })
672 } else {
673 Float(Zero { sign: !*sign })
674 },
675 Equal,
676 ),
677 (x, Float(Zero { sign })) => (
678 match x.sign() {
679 Equal => float_nan!(),
680 Greater => Float(Infinity { sign: *sign }),
681 Less => Float(Infinity { sign: !*sign }),
682 },
683 Equal,
684 ),
685 (x, y) => {
686 let (mut quotient, o) =
687 Float::from_rational_prec_round(x / Rational::exact_from(y), prec, rm);
688 if quotient == 0u32 && *y < 0 {
689 quotient.neg_assign();
690 }
691 (quotient, o)
692 }
693 }
694}}
695
696pub_test! {rational_div_float_prec_round_direct(
697 x: Rational,
698 y: Float,
699 prec: u64,
700 mut rm: RoundingMode,
701) -> (Float, Ordering) {
702 assert_ne!(prec, 0);
703 if x == 0u32 {
704 return (
705 if y > 0u32 {
706 Float::ZERO
707 } else {
708 Float::NEGATIVE_ZERO
709 },
710 Equal,
711 );
712 }
713 let sign = x >= 0;
714 let (n, d) = x.into_numerator_and_denominator();
715 if !sign {
716 rm.neg_assign();
717 }
718 let (quotient, o) = match (n.checked_log_base_2(), d.checked_log_base_2()) {
719 (Some(log_n), Some(log_d)) => {
720 let y_exp = y.get_exponent().unwrap();
721 let (mut quotient, o) = (y >> y_exp).reciprocal_prec_round(prec, rm);
722 let o = quotient.shl_prec_round_assign_helper(
723 i128::from(log_n) - i128::from(log_d) - i128::from(y_exp),
724 prec,
725 rm,
726 o,
727 );
728 (quotient, o)
729 }
730 (None, Some(log_d)) => {
731 let y_exp = y.get_exponent().unwrap();
732 let n_exp = n.floor_log_base_2();
733 let mut quotient = from_natural_zero_exponent(n);
734 let o = quotient.div_prec_round_assign(y >> y_exp, prec, rm);
735 let o = quotient.shl_prec_round_assign_helper(
736 i128::from(n_exp) - i128::from(log_d) - i128::from(y_exp) + 1,
737 prec,
738 rm,
739 o,
740 );
741 (quotient, o)
742 }
743 (Some(log_n), None) => {
744 let y_exp = y.get_exponent().unwrap();
745 let d_exp = d.floor_log_base_2();
746 let mut y = y >> y_exp;
747 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
748 y.mul_prec_round_assign(from_natural_zero_exponent(d), mul_prec, Floor);
749 let (mut quotient, o) = y.reciprocal_prec_round(prec, rm);
750 let o = quotient.shl_prec_round_assign_helper(
751 i128::from(log_n) - i128::from(d_exp) - i128::from(y_exp) - 1,
752 prec,
753 rm,
754 o,
755 );
756 (quotient, o)
757 }
758 (None, None) => {
759 let y_exp = y.get_exponent().unwrap();
760 let n_exp = n.floor_log_base_2();
761 let d_exp = d.floor_log_base_2();
762 let mut quotient = from_natural_zero_exponent(n);
763 let d = from_natural_zero_exponent(d);
764 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
765 let o = quotient.div_prec_round_assign(
766 (y >> y_exp).mul_prec_round(d, mul_prec, Floor).0,
767 prec,
768 rm,
769 );
770 let o = quotient.shl_prec_round_assign_helper(
771 -i128::from(y_exp) + i128::from(n_exp) - i128::from(d_exp),
772 prec,
773 rm,
774 o,
775 );
776 (quotient, o)
777 }
778 };
779 if sign {
780 (quotient, o)
781 } else {
782 (-quotient, o.reverse())
783 }
784}}
785
786pub_test! {rational_div_float_prec_round_direct_val_ref(
787 x: Rational,
788 y: &Float,
789 prec: u64,
790 mut rm: RoundingMode,
791) -> (Float, Ordering) {
792 assert_ne!(prec, 0);
793 if x == 0u32 {
794 return (
795 if *y > 0u32 {
796 Float::ZERO
797 } else {
798 Float::NEGATIVE_ZERO
799 },
800 Equal,
801 );
802 }
803 let sign = x >= 0;
804 let (n, d) = x.into_numerator_and_denominator();
805 if !sign {
806 rm.neg_assign();
807 }
808 let (quotient, o) = match (n.checked_log_base_2(), d.checked_log_base_2()) {
809 (Some(log_n), Some(log_d)) => {
810 let y_exp = y.get_exponent().unwrap();
811 let (mut quotient, o) = (y >> y_exp).reciprocal_prec_round(prec, rm);
812 let o = quotient.shl_prec_round_assign_helper(
813 i128::from(log_n) - i128::from(log_d) - i128::from(y_exp),
814 prec,
815 rm,
816 o,
817 );
818 (quotient, o)
819 }
820 (None, Some(log_d)) => {
821 let y_exp = y.get_exponent().unwrap();
822 let n_exp = n.floor_log_base_2();
823 let mut quotient = from_natural_zero_exponent(n);
824 let o = quotient.div_prec_round_assign(y >> y_exp, prec, rm);
825 let o = quotient.shl_prec_round_assign_helper(
826 i128::from(n_exp) - i128::from(log_d) - i128::from(y_exp) + 1,
827 prec,
828 rm,
829 o,
830 );
831 (quotient, o)
832 }
833 (Some(log_n), None) => {
834 let y_exp = y.get_exponent().unwrap();
835 let d_exp = d.floor_log_base_2();
836 let mut y = y >> y_exp;
837 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
838 y.mul_prec_round_assign(from_natural_zero_exponent(d), mul_prec, Floor);
839 let (mut quotient, o) = y.reciprocal_prec_round(prec, rm);
840 let o = quotient.shl_prec_round_assign_helper(
841 i128::from(log_n) - i128::from(d_exp) - i128::from(y_exp) - 1,
842 prec,
843 rm,
844 o,
845 );
846 (quotient, o)
847 }
848 (None, None) => {
849 let y_exp = y.get_exponent().unwrap();
850 let n_exp = n.floor_log_base_2();
851 let d_exp = d.floor_log_base_2();
852 let mut quotient = from_natural_zero_exponent(n);
853 let d = from_natural_zero_exponent(d);
854 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
855 let o = quotient.div_prec_round_assign(
856 (y >> y_exp).mul_prec_round(d, mul_prec, Floor).0,
857 prec,
858 rm,
859 );
860 let o = quotient.shl_prec_round_assign_helper(
861 -i128::from(y_exp) + i128::from(n_exp) - i128::from(d_exp),
862 prec,
863 rm,
864 o,
865 );
866 (quotient, o)
867 }
868 };
869 if sign {
870 (quotient, o)
871 } else {
872 (-quotient, o.reverse())
873 }
874}}
875
876pub_test! {rational_div_float_prec_round_direct_ref_val(
877 x: &Rational,
878 y: Float,
879 prec: u64,
880 mut rm: RoundingMode,
881) -> (Float, Ordering) {
882 assert_ne!(prec, 0);
883 if *x == 0u32 {
884 return (
885 if y > 0u32 {
886 Float::ZERO
887 } else {
888 Float::NEGATIVE_ZERO
889 },
890 Equal,
891 );
892 }
893 let sign = *x >= 0;
894 let (n, d) = x.numerator_and_denominator_ref();
895 if !sign {
896 rm.neg_assign();
897 }
898 let (quotient, o) = match (n.checked_log_base_2(), d.checked_log_base_2()) {
899 (Some(log_n), Some(log_d)) => {
900 let y_exp = y.get_exponent().unwrap();
901 let (mut quotient, o) = (y >> y_exp).reciprocal_prec_round(prec, rm);
902 let o = quotient.shl_prec_round_assign_helper(
903 i128::from(log_n) - i128::from(log_d) - i128::from(y_exp),
904 prec,
905 rm,
906 o,
907 );
908 (quotient, o)
909 }
910 (None, Some(log_d)) => {
911 let y_exp = y.get_exponent().unwrap();
912 let n_exp = n.floor_log_base_2();
913 let mut quotient = from_natural_zero_exponent_ref(n);
914 let o = quotient.div_prec_round_assign(y >> y_exp, prec, rm);
915 let o = quotient.shl_prec_round_assign_helper(
916 i128::from(n_exp) - i128::from(log_d) - i128::from(y_exp) + 1,
917 prec,
918 rm,
919 o,
920 );
921 (quotient, o)
922 }
923 (Some(log_n), None) => {
924 let y_exp = y.get_exponent().unwrap();
925 let d_exp = d.floor_log_base_2();
926 let mut y = y >> y_exp;
927 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
928 y.mul_prec_round_assign(from_natural_zero_exponent_ref(d), mul_prec, Floor);
929 let (mut quotient, o) = y.reciprocal_prec_round(prec, rm);
930 let o = quotient.shl_prec_round_assign_helper(
931 i128::from(log_n) - i128::from(d_exp) - i128::from(y_exp) - 1,
932 prec,
933 rm,
934 o,
935 );
936 (quotient, o)
937 }
938 (None, None) => {
939 let y_exp = y.get_exponent().unwrap();
940 let n_exp = n.floor_log_base_2();
941 let d_exp = d.floor_log_base_2();
942 let mut quotient = from_natural_zero_exponent_ref(n);
943 let d = from_natural_zero_exponent_ref(d);
944 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
945 let o = quotient.div_prec_round_assign(
946 (y >> y_exp).mul_prec_round(d, mul_prec, Floor).0,
947 prec,
948 rm,
949 );
950 let o = quotient.shl_prec_round_assign_helper(
951 -i128::from(y_exp) + i128::from(n_exp) - i128::from(d_exp),
952 prec,
953 rm,
954 o,
955 );
956 (quotient, o)
957 }
958 };
959 if sign {
960 (quotient, o)
961 } else {
962 (-quotient, o.reverse())
963 }
964}}
965
966pub_test! {rational_div_float_prec_round_direct_ref_ref(
967 x: &Rational,
968 y: &Float,
969 prec: u64,
970 mut rm: RoundingMode,
971) -> (Float, Ordering) {
972 assert_ne!(prec, 0);
973 if *x == 0u32 {
974 return (
975 if *y > 0u32 {
976 Float::ZERO
977 } else {
978 Float::NEGATIVE_ZERO
979 },
980 Equal,
981 );
982 }
983 let sign = *x >= 0;
984 let (n, d) = x.numerator_and_denominator_ref();
985 if !sign {
986 rm.neg_assign();
987 }
988 let (quotient, o) = match (n.checked_log_base_2(), d.checked_log_base_2()) {
989 (Some(log_n), Some(log_d)) => {
990 let y_exp = y.get_exponent().unwrap();
991 let (mut quotient, o) = (y >> y_exp).reciprocal_prec_round(prec, rm);
992 let o = quotient.shl_prec_round_assign_helper(
993 i128::from(log_n) - i128::from(log_d) - i128::from(y_exp),
994 prec,
995 rm,
996 o,
997 );
998 (quotient, o)
999 }
1000 (None, Some(log_d)) => {
1001 let y_exp = y.get_exponent().unwrap();
1002 let n_exp = n.floor_log_base_2();
1003 let mut quotient = from_natural_zero_exponent_ref(n);
1004 let o = quotient.div_prec_round_assign(y >> y_exp, prec, rm);
1005 let o = quotient.shl_prec_round_assign_helper(
1006 i128::from(n_exp) - i128::from(log_d) - i128::from(y_exp) + 1,
1007 prec,
1008 rm,
1009 o,
1010 );
1011 (quotient, o)
1012 }
1013 (Some(log_n), None) => {
1014 let y_exp = y.get_exponent().unwrap();
1015 let d_exp = d.floor_log_base_2();
1016 let mut y = y >> y_exp;
1017 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
1018 y.mul_prec_round_assign(from_natural_zero_exponent_ref(d), mul_prec, Floor);
1019 let (mut quotient, o) = y.reciprocal_prec_round(prec, rm);
1020 let o = quotient.shl_prec_round_assign_helper(
1021 i128::from(log_n) - i128::from(d_exp) - i128::from(y_exp) - 1,
1022 prec,
1023 rm,
1024 o,
1025 );
1026 (quotient, o)
1027 }
1028 (None, None) => {
1029 let y_exp = y.get_exponent().unwrap();
1030 let n_exp = n.floor_log_base_2();
1031 let d_exp = d.floor_log_base_2();
1032 let mut quotient = from_natural_zero_exponent_ref(n);
1033 let d = from_natural_zero_exponent_ref(d);
1034 let mul_prec = y.get_min_prec().unwrap_or(1) + d.significant_bits();
1035 let o = quotient.div_prec_round_assign(
1036 (y >> y_exp).mul_prec_round(d, mul_prec, Floor).0,
1037 prec,
1038 rm,
1039 );
1040 let o = quotient.shl_prec_round_assign_helper(
1041 -i128::from(y_exp) + i128::from(n_exp) - i128::from(d_exp),
1042 prec,
1043 rm,
1044 o,
1045 );
1046 (quotient, o)
1047 }
1048 };
1049 if sign {
1050 (quotient, o)
1051 } else {
1052 (-quotient, o.reverse())
1053 }
1054}}
1055
1056impl Float {
1057 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
1058 /// specified rounding mode. Both [`Float`]s are taken by value. An [`Ordering`] is also
1059 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
1060 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
1061 /// function returns a `NaN` it also returns `Equal`.
1062 ///
1063 /// See [`RoundingMode`] for a description of the possible rounding modes.
1064 ///
1065 /// $$
1066 /// f(x,y,p,m) = x/y+\varepsilon.
1067 /// $$
1068 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1069 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1070 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
1071 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
1072 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1073 ///
1074 /// If the output has a precision, it is `prec`.
1075 ///
1076 /// Special cases:
1077 /// - $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) =
1078 /// \text{NaN}$
1079 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
1080 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
1081 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
1082 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
1083 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
1084 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
1085 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
1086 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
1087 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
1088 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1089 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1090 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1091 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1092 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $x<0.0$
1093 /// - $f(x,-\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1094 /// - $f(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1095 ///
1096 /// Overflow and underflow:
1097 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1098 /// returned instead.
1099 /// - 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}$
1100 /// is returned instead, where `p` is the precision of the input.
1101 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
1102 /// returned instead.
1103 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
1104 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
1105 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1106 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1107 /// instead.
1108 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1109 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1110 /// instead.
1111 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
1112 /// instead.
1113 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
1114 /// instead.
1115 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1116 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
1117 /// returned instead.
1118 ///
1119 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec`] instead. If you
1120 /// know that your target precision is the maximum of the precisions of the two inputs, consider
1121 /// using [`Float::div_round`] instead. If both of these things are true, consider using `/`
1122 /// instead.
1123 ///
1124 /// # Worst-case complexity
1125 /// $T(n) = O(n \log n \log\log n)$
1126 ///
1127 /// $M(n) = O(n \log n)$
1128 ///
1129 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1130 /// other.significant_bits(), `prec`)`.
1131 ///
1132 /// # Panics
1133 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
1134 ///
1135 /// # Examples
1136 /// ```
1137 /// use core::f64::consts::{E, PI};
1138 /// use malachite_base::rounding_modes::RoundingMode::*;
1139 /// use malachite_float::Float;
1140 /// use std::cmp::Ordering::*;
1141 ///
1142 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 5, Floor);
1143 /// assert_eq!(quotient.to_string(), "1.12");
1144 /// assert_eq!(o, Less);
1145 ///
1146 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 5, Ceiling);
1147 /// assert_eq!(quotient.to_string(), "1.19");
1148 /// assert_eq!(o, Greater);
1149 ///
1150 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 5, Nearest);
1151 /// assert_eq!(quotient.to_string(), "1.12");
1152 /// assert_eq!(o, Less);
1153 ///
1154 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 20, Floor);
1155 /// assert_eq!(quotient.to_string(), "1.1557255");
1156 /// assert_eq!(o, Less);
1157 ///
1158 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 20, Ceiling);
1159 /// assert_eq!(quotient.to_string(), "1.1557274");
1160 /// assert_eq!(o, Greater);
1161 ///
1162 /// let (quotient, o) = Float::from(PI).div_prec_round(Float::from(E), 20, Nearest);
1163 /// assert_eq!(quotient.to_string(), "1.1557274");
1164 /// assert_eq!(o, Greater);
1165 /// ```
1166 #[inline]
1167 pub fn div_prec_round(mut self, other: Self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
1168 let o = self.div_prec_round_assign(other, prec, rm);
1169 (self, o)
1170 }
1171
1172 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
1173 /// specified rounding mode. The first [`Float`] is are taken by value and the second by
1174 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
1175 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
1176 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1177 ///
1178 /// See [`RoundingMode`] for a description of the possible rounding modes.
1179 ///
1180 /// $$
1181 /// f(x,y,p,m) = x/y+\varepsilon.
1182 /// $$
1183 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1184 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1185 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
1186 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
1187 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1188 ///
1189 /// If the output has a precision, it is `prec`.
1190 ///
1191 /// Special cases:
1192 /// - $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) =
1193 /// \text{NaN}$
1194 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
1195 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
1196 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
1197 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
1198 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
1199 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
1200 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
1201 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
1202 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
1203 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1204 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1205 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1206 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1207 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $x<0.0$
1208 /// - $f(x,-\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1209 /// - $f(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1210 ///
1211 /// Overflow and underflow:
1212 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1213 /// returned instead.
1214 /// - 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}$
1215 /// is returned instead, where `p` is the precision of the input.
1216 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
1217 /// returned instead.
1218 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
1219 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
1220 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1221 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1222 /// instead.
1223 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1224 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1225 /// instead.
1226 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
1227 /// instead.
1228 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
1229 /// instead.
1230 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1231 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
1232 /// returned instead.
1233 ///
1234 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_val_ref`] instead.
1235 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1236 /// consider using [`Float::div_round_val_ref`] instead. If both of these things are true,
1237 /// consider using `/` instead.
1238 ///
1239 /// # Worst-case complexity
1240 /// $T(n) = O(n \log n \log\log n)$
1241 ///
1242 /// $M(n) = O(n \log n)$
1243 ///
1244 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1245 /// other.significant_bits(), `prec`)`.
1246 ///
1247 /// # Panics
1248 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
1249 ///
1250 /// # Examples
1251 /// ```
1252 /// use core::f64::consts::{E, PI};
1253 /// use malachite_base::rounding_modes::RoundingMode::*;
1254 /// use malachite_float::Float;
1255 /// use std::cmp::Ordering::*;
1256 ///
1257 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 5, Floor);
1258 /// assert_eq!(quotient.to_string(), "1.12");
1259 /// assert_eq!(o, Less);
1260 ///
1261 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 5, Ceiling);
1262 /// assert_eq!(quotient.to_string(), "1.19");
1263 /// assert_eq!(o, Greater);
1264 ///
1265 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 5, Nearest);
1266 /// assert_eq!(quotient.to_string(), "1.12");
1267 /// assert_eq!(o, Less);
1268 ///
1269 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 20, Floor);
1270 /// assert_eq!(quotient.to_string(), "1.1557255");
1271 /// assert_eq!(o, Less);
1272 ///
1273 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 20, Ceiling);
1274 /// assert_eq!(quotient.to_string(), "1.1557274");
1275 /// assert_eq!(o, Greater);
1276 ///
1277 /// let (quotient, o) = Float::from(PI).div_prec_round_val_ref(&Float::from(E), 20, Nearest);
1278 /// assert_eq!(quotient.to_string(), "1.1557274");
1279 /// assert_eq!(o, Greater);
1280 /// ```
1281 #[inline]
1282 pub fn div_prec_round_val_ref(
1283 mut self,
1284 other: &Self,
1285 prec: u64,
1286 rm: RoundingMode,
1287 ) -> (Self, Ordering) {
1288 let o = self.div_prec_round_assign_ref(other, prec, rm);
1289 (self, o)
1290 }
1291
1292 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
1293 /// specified rounding mode. The first [`Float`] is are taken by reference and the second by
1294 /// value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
1295 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
1296 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1297 ///
1298 /// See [`RoundingMode`] for a description of the possible rounding modes.
1299 ///
1300 /// $$
1301 /// f(x,y,p,m) = x/y+\varepsilon.
1302 /// $$
1303 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1304 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1305 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
1306 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
1307 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1308 ///
1309 /// If the output has a precision, it is `prec`.
1310 ///
1311 /// Special cases:
1312 /// - $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) =
1313 /// \text{NaN}$
1314 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
1315 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
1316 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
1317 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
1318 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
1319 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
1320 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
1321 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
1322 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
1323 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1324 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1325 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1326 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1327 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $x<0.0$
1328 /// - $f(x,-\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1329 /// - $f(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1330 ///
1331 /// Overflow and underflow:
1332 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1333 /// returned instead.
1334 /// - 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}$
1335 /// is returned instead, where `p` is the precision of the input.
1336 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
1337 /// returned instead.
1338 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
1339 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
1340 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1341 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1342 /// instead.
1343 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1344 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1345 /// instead.
1346 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
1347 /// instead.
1348 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
1349 /// instead.
1350 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1351 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
1352 /// returned instead.
1353 ///
1354 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_ref_val`] instead.
1355 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1356 /// consider using [`Float::div_round_ref_val`] instead. If both of these things are true,
1357 /// consider using `/` instead.
1358 ///
1359 /// # Worst-case complexity
1360 /// $T(n) = O(n \log n \log\log n)$
1361 ///
1362 /// $M(n) = O(n \log n)$
1363 ///
1364 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1365 /// other.significant_bits(), `prec`)`.
1366 ///
1367 /// # Panics
1368 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
1369 ///
1370 /// # Examples
1371 /// ```
1372 /// use core::f64::consts::{E, PI};
1373 /// use malachite_base::rounding_modes::RoundingMode::*;
1374 /// use malachite_float::Float;
1375 /// use std::cmp::Ordering::*;
1376 ///
1377 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 5, Floor);
1378 /// assert_eq!(quotient.to_string(), "1.12");
1379 /// assert_eq!(o, Less);
1380 ///
1381 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 5, Ceiling);
1382 /// assert_eq!(quotient.to_string(), "1.19");
1383 /// assert_eq!(o, Greater);
1384 ///
1385 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 5, Nearest);
1386 /// assert_eq!(quotient.to_string(), "1.12");
1387 /// assert_eq!(o, Less);
1388 ///
1389 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 20, Floor);
1390 /// assert_eq!(quotient.to_string(), "1.1557255");
1391 /// assert_eq!(o, Less);
1392 ///
1393 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 20, Ceiling);
1394 /// assert_eq!(quotient.to_string(), "1.1557274");
1395 /// assert_eq!(o, Greater);
1396 ///
1397 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_val(Float::from(E), 20, Nearest);
1398 /// assert_eq!(quotient.to_string(), "1.1557274");
1399 /// assert_eq!(o, Greater);
1400 /// ```
1401 #[inline]
1402 pub fn div_prec_round_ref_val(
1403 &self,
1404 other: Self,
1405 prec: u64,
1406 rm: RoundingMode,
1407 ) -> (Self, Ordering) {
1408 assert_ne!(prec, 0);
1409 match (self, other) {
1410 (float_nan!(), _)
1411 | (_, float_nan!())
1412 | (float_either_infinity!(), float_either_infinity!())
1413 | (float_either_zero!(), float_either_zero!()) => (float_nan!(), Equal),
1414 (
1415 Self(Infinity { sign: x_sign }),
1416 Self(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
1417 )
1418 | (Self(Finite { sign: x_sign, .. }), Self(Zero { sign: y_sign })) => (
1419 Self(Infinity {
1420 sign: *x_sign == y_sign,
1421 }),
1422 Equal,
1423 ),
1424 (
1425 Self(Zero { sign: x_sign }),
1426 Self(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
1427 )
1428 | (Self(Finite { sign: x_sign, .. }), Self(Infinity { sign: y_sign })) => (
1429 Self(Zero {
1430 sign: *x_sign == y_sign,
1431 }),
1432 Equal,
1433 ),
1434 (
1435 Self(Finite {
1436 sign: x_sign,
1437 exponent: x_exp,
1438 precision: x_prec,
1439 significand: x,
1440 }),
1441 Self(Finite {
1442 sign: y_sign,
1443 exponent: y_exp,
1444 precision: y_prec,
1445 significand: mut y,
1446 }),
1447 ) => {
1448 if y.is_power_of_2() {
1449 let (mut quotient, mut o) =
1450 self.shr_prec_round_ref(y_exp - 1, prec, if y_sign { rm } else { -rm });
1451 if !y_sign {
1452 quotient.neg_assign();
1453 o = o.reverse();
1454 }
1455 return (quotient, o);
1456 }
1457 let sign = *x_sign == y_sign;
1458 let exp_diff = *x_exp - y_exp;
1459 if exp_diff > Self::MAX_EXPONENT {
1460 return match (sign, rm) {
1461 (_, Exact) => panic!("Inexact Float division"),
1462 (true, Ceiling | Up | Nearest) => (float_infinity!(), Greater),
1463 (true, _) => (Self::max_finite_value_with_prec(prec), Less),
1464 (false, Floor | Up | Nearest) => (float_negative_infinity!(), Less),
1465 (false, _) => (-Self::max_finite_value_with_prec(prec), Greater),
1466 };
1467 } else if exp_diff + 2 < Self::MIN_EXPONENT {
1468 return match (sign, rm) {
1469 (_, Exact) => panic!("Inexact Float division"),
1470 (true, Ceiling | Up) => (Self::min_positive_value_prec(prec), Greater),
1471 (true, _) => (float_zero!(), Less),
1472 (false, Floor | Up) => (-Self::min_positive_value_prec(prec), Less),
1473 (false, _) => (float_negative_zero!(), Greater),
1474 };
1475 }
1476 let (quotient, exp_offset, o) = div_float_significands_ref_val(
1477 x,
1478 *x_prec,
1479 &mut y,
1480 y_prec,
1481 prec,
1482 if sign { rm } else { -rm },
1483 );
1484 let exp = exp_diff.checked_add(i32::exact_from(exp_offset)).unwrap();
1485 if exp > Self::MAX_EXPONENT {
1486 return match (sign, rm) {
1487 (_, Exact) => panic!("Inexact Float division"),
1488 (true, Ceiling | Up | Nearest) => (float_infinity!(), Greater),
1489 (true, _) => (Self::max_finite_value_with_prec(prec), Less),
1490 (false, Floor | Up | Nearest) => (float_negative_infinity!(), Less),
1491 (false, _) => (-Self::max_finite_value_with_prec(prec), Greater),
1492 };
1493 } else if exp < Self::MIN_EXPONENT {
1494 return if rm == Nearest
1495 && exp == Self::MIN_EXPONENT_MINUS_1
1496 && (o == Less || !quotient.is_power_of_2())
1497 {
1498 if sign {
1499 (Self::min_positive_value_prec(prec), Greater)
1500 } else {
1501 (-Self::min_positive_value_prec(prec), Less)
1502 }
1503 } else {
1504 match (sign, rm) {
1505 (_, Exact) => panic!("Inexact Float division"),
1506 (true, Ceiling | Up) => (Self::min_positive_value_prec(prec), Greater),
1507 (true, _) => (float_zero!(), Less),
1508 (false, Floor | Up) => (-Self::min_positive_value_prec(prec), Less),
1509 (false, _) => (float_negative_zero!(), Greater),
1510 }
1511 };
1512 }
1513 (
1514 Self(Finite {
1515 sign,
1516 exponent: exp,
1517 precision: prec,
1518 significand: quotient,
1519 }),
1520 if sign { o } else { o.reverse() },
1521 )
1522 }
1523 }
1524 }
1525
1526 /// Divides two [`Float`]s, rounding the result to the specified precision and with the
1527 /// specified rounding mode. Both [`Float`]s are taken by reference. An [`Ordering`] is also
1528 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
1529 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
1530 /// function returns a `NaN` it also returns `Equal`.
1531 ///
1532 /// See [`RoundingMode`] for a description of the possible rounding modes.
1533 ///
1534 /// $$
1535 /// f(x,y,p,m) = x/y+\varepsilon.
1536 /// $$
1537 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1538 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1539 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
1540 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
1541 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1542 ///
1543 /// If the output has a precision, it is `prec`.
1544 ///
1545 /// Special cases:
1546 /// - $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) =
1547 /// \text{NaN}$
1548 /// - $f(\infty,x,p,m)=\infty$ if $0.0<x<\infty$
1549 /// - $f(\infty,x,p,m)=-\infty$ if $-\infty<x<0.0$
1550 /// - $f(x,0.0,p,m)=\infty$ if $x>0.0$
1551 /// - $f(x,0.0,p,m)=-\infty$ if $x<0.0$
1552 /// - $f(-\infty,x,p,m)=-\infty$ if $0.0<x<\infty$
1553 /// - $f(-\infty,x,p,m)=\infty$ if $-\infty<x<0.0$
1554 /// - $f(x,-0.0,p,m)=-\infty$ if $x>0.0$
1555 /// - $f(x,-0.0,p,m)=\infty$ if $x<0.0$
1556 /// - $f(0.0,x,p,m)=0.0$ if $x$ is not NaN and $x>0.0$
1557 /// - $f(0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x<0.0$
1558 /// - $f(x,\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1559 /// - $f(x,\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1560 /// - $f(-0.0,x,p,m)=-0.0$ if $x$ is not NaN and $x>0.0$
1561 /// - $f(-0.0,x,p,m)=0.0$ if $x$ is not NaN and $x<0.0$
1562 /// - $f(x,-\infty,p,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1563 /// - $f(x,-\infty,p,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1564 ///
1565 /// Overflow and underflow:
1566 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1567 /// returned instead.
1568 /// - 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}$
1569 /// is returned instead, where `p` is the precision of the input.
1570 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
1571 /// returned instead.
1572 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
1573 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
1574 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1575 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1576 /// instead.
1577 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1578 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1579 /// instead.
1580 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
1581 /// instead.
1582 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
1583 /// instead.
1584 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1585 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
1586 /// returned instead.
1587 ///
1588 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_ref_ref`] instead.
1589 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1590 /// consider using [`Float::div_round_ref_ref`] instead. If both of these things are true,
1591 /// consider using `/` instead.
1592 ///
1593 /// # Worst-case complexity
1594 /// $T(n) = O(n \log n \log\log n)$
1595 ///
1596 /// $M(n) = O(n \log n)$
1597 ///
1598 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1599 /// other.significant_bits(), `prec`)`.
1600 ///
1601 /// # Panics
1602 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
1603 ///
1604 /// # Examples
1605 /// ```
1606 /// use core::f64::consts::{E, PI};
1607 /// use malachite_base::rounding_modes::RoundingMode::*;
1608 /// use malachite_float::Float;
1609 /// use std::cmp::Ordering::*;
1610 ///
1611 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 5, Floor);
1612 /// assert_eq!(quotient.to_string(), "1.12");
1613 /// assert_eq!(o, Less);
1614 ///
1615 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 5, Ceiling);
1616 /// assert_eq!(quotient.to_string(), "1.19");
1617 /// assert_eq!(o, Greater);
1618 ///
1619 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 5, Nearest);
1620 /// assert_eq!(quotient.to_string(), "1.12");
1621 /// assert_eq!(o, Less);
1622 ///
1623 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 20, Floor);
1624 /// assert_eq!(quotient.to_string(), "1.1557255");
1625 /// assert_eq!(o, Less);
1626 ///
1627 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 20, Ceiling);
1628 /// assert_eq!(quotient.to_string(), "1.1557274");
1629 /// assert_eq!(o, Greater);
1630 ///
1631 /// let (quotient, o) = Float::from(PI).div_prec_round_ref_ref(&Float::from(E), 20, Nearest);
1632 /// assert_eq!(quotient.to_string(), "1.1557274");
1633 /// assert_eq!(o, Greater);
1634 /// ```
1635 #[inline]
1636 pub fn div_prec_round_ref_ref(
1637 &self,
1638 other: &Self,
1639 prec: u64,
1640 rm: RoundingMode,
1641 ) -> (Self, Ordering) {
1642 assert_ne!(prec, 0);
1643 match (self, other) {
1644 (float_nan!(), _)
1645 | (_, float_nan!())
1646 | (float_either_infinity!(), float_either_infinity!())
1647 | (float_either_zero!(), float_either_zero!()) => (float_nan!(), Equal),
1648 (
1649 Self(Infinity { sign: x_sign }),
1650 Self(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
1651 )
1652 | (Self(Finite { sign: x_sign, .. }), Self(Zero { sign: y_sign })) => (
1653 Self(Infinity {
1654 sign: x_sign == y_sign,
1655 }),
1656 Equal,
1657 ),
1658 (
1659 Self(Zero { sign: x_sign }),
1660 Self(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
1661 )
1662 | (Self(Finite { sign: x_sign, .. }), Self(Infinity { sign: y_sign })) => (
1663 Self(Zero {
1664 sign: x_sign == y_sign,
1665 }),
1666 Equal,
1667 ),
1668 (
1669 Self(Finite {
1670 sign: x_sign,
1671 exponent: x_exp,
1672 precision: x_prec,
1673 significand: x,
1674 }),
1675 Self(Finite {
1676 sign: y_sign,
1677 exponent: y_exp,
1678 precision: y_prec,
1679 significand: y,
1680 }),
1681 ) => {
1682 if y.is_power_of_2() {
1683 let (mut quotient, mut o) =
1684 self.shr_prec_round_ref(y_exp - 1, prec, if *y_sign { rm } else { -rm });
1685 if !*y_sign {
1686 quotient.neg_assign();
1687 o = o.reverse();
1688 }
1689 return (quotient, o);
1690 }
1691 let sign = x_sign == y_sign;
1692 let exp_diff = *x_exp - y_exp;
1693 if exp_diff > Self::MAX_EXPONENT {
1694 return match (sign, rm) {
1695 (_, Exact) => panic!("Inexact Float division"),
1696 (true, Ceiling | Up | Nearest) => (float_infinity!(), Greater),
1697 (true, _) => (Self::max_finite_value_with_prec(prec), Less),
1698 (false, Floor | Up | Nearest) => (float_negative_infinity!(), Less),
1699 (false, _) => (-Self::max_finite_value_with_prec(prec), Greater),
1700 };
1701 } else if exp_diff + 2 < Self::MIN_EXPONENT {
1702 return match (sign, rm) {
1703 (_, Exact) => panic!("Inexact Float division"),
1704 (true, Ceiling | Up) => (Self::min_positive_value_prec(prec), Greater),
1705 (true, _) => (float_zero!(), Less),
1706 (false, Floor | Up) => (-Self::min_positive_value_prec(prec), Less),
1707 (false, _) => (float_negative_zero!(), Greater),
1708 };
1709 }
1710 let (quotient, exp_offset, o) = div_float_significands_ref_ref(
1711 x,
1712 *x_prec,
1713 y,
1714 *y_prec,
1715 prec,
1716 if sign { rm } else { -rm },
1717 );
1718 let exp = exp_diff.checked_add(i32::exact_from(exp_offset)).unwrap();
1719 if exp > Self::MAX_EXPONENT {
1720 return match (sign, rm) {
1721 (_, Exact) => panic!("Inexact Float division"),
1722 (true, Ceiling | Up | Nearest) => (float_infinity!(), Greater),
1723 (true, _) => (Self::max_finite_value_with_prec(prec), Less),
1724 (false, Floor | Up | Nearest) => (float_negative_infinity!(), Less),
1725 (false, _) => (-Self::max_finite_value_with_prec(prec), Greater),
1726 };
1727 } else if exp < Self::MIN_EXPONENT {
1728 return if rm == Nearest
1729 && exp == Self::MIN_EXPONENT_MINUS_1
1730 && (o == Less || !quotient.is_power_of_2())
1731 {
1732 if sign {
1733 (Self::min_positive_value_prec(prec), Greater)
1734 } else {
1735 (-Self::min_positive_value_prec(prec), Less)
1736 }
1737 } else {
1738 match (sign, rm) {
1739 (_, Exact) => panic!("Inexact Float division"),
1740 (true, Ceiling | Up) => (Self::min_positive_value_prec(prec), Greater),
1741 (true, _) => (float_zero!(), Less),
1742 (false, Floor | Up) => (-Self::min_positive_value_prec(prec), Less),
1743 (false, _) => (float_negative_zero!(), Greater),
1744 }
1745 };
1746 }
1747 (
1748 Self(Finite {
1749 sign,
1750 exponent: exp,
1751 precision: prec,
1752 significand: quotient,
1753 }),
1754 if sign { o } else { o.reverse() },
1755 )
1756 }
1757 }
1758 }
1759
1760 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1761 /// Both [`Float`]s are taken by value. An [`Ordering`] is also returned, indicating whether the
1762 /// rounded quotient is less than, equal to, or greater than the exact quotient. Although `NaN`s
1763 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
1764 /// `Equal`.
1765 ///
1766 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1767 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1768 /// description of the `Nearest` rounding mode.
1769 ///
1770 /// $$
1771 /// f(x,y,p) = x/y+\varepsilon.
1772 /// $$
1773 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1774 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1775 ///
1776 /// If the output has a precision, it is `prec`.
1777 ///
1778 /// Special cases:
1779 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1780 /// \text{NaN}$
1781 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
1782 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
1783 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
1784 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
1785 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
1786 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
1787 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
1788 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
1789 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
1790 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
1791 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1792 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1793 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
1794 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
1795 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1796 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1797 ///
1798 /// Overflow and underflow:
1799 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
1800 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
1801 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1802 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1803 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1804 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1805 ///
1806 /// If you want to use a rounding mode other than `Nearest`, consider using
1807 /// [`Float::div_prec_round`] instead. If you know that your target precision is the maximum of
1808 /// the precisions of the two inputs, consider using `/` instead.
1809 ///
1810 /// # Worst-case complexity
1811 /// $T(n) = O(n \log n \log\log n)$
1812 ///
1813 /// $M(n) = O(n \log n)$
1814 ///
1815 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1816 /// other.significant_bits(), `prec`)`.
1817 ///
1818 /// # Examples
1819 /// ```
1820 /// use core::f64::consts::{E, PI};
1821 /// use malachite_float::Float;
1822 /// use std::cmp::Ordering::*;
1823 ///
1824 /// let (quotient, o) = Float::from(PI).div_prec(Float::from(E), 5);
1825 /// assert_eq!(quotient.to_string(), "1.12");
1826 /// assert_eq!(o, Less);
1827 ///
1828 /// let (quotient, o) = Float::from(PI).div_prec(Float::from(E), 20);
1829 /// assert_eq!(quotient.to_string(), "1.1557274");
1830 /// assert_eq!(o, Greater);
1831 /// ```
1832 #[inline]
1833 pub fn div_prec(self, other: Self, prec: u64) -> (Self, Ordering) {
1834 self.div_prec_round(other, prec, Nearest)
1835 }
1836
1837 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1838 /// The first [`Float`] is taken by value and the second by reference. An [`Ordering`] is also
1839 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
1840 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
1841 /// function returns a `NaN` it also returns `Equal`.
1842 ///
1843 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1844 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1845 /// description of the `Nearest` rounding mode.
1846 ///
1847 /// $$
1848 /// f(x,y,p) = x/y+\varepsilon.
1849 /// $$
1850 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1851 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1852 ///
1853 /// If the output has a precision, it is `prec`.
1854 ///
1855 /// Special cases:
1856 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1857 /// \text{NaN}$
1858 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
1859 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
1860 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
1861 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
1862 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
1863 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
1864 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
1865 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
1866 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
1867 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
1868 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1869 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1870 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
1871 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
1872 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1873 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1874 ///
1875 /// Overflow and underflow:
1876 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
1877 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
1878 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1879 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1880 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1881 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1882 ///
1883 /// If you want to use a rounding mode other than `Nearest`, consider using
1884 /// [`Float::div_prec_round_val_ref`] instead. If you know that your target precision is the
1885 /// maximum of the precisions of the two inputs, consider using `/` instead.
1886 ///
1887 /// # Worst-case complexity
1888 /// $T(n) = O(n \log n \log\log n)$
1889 ///
1890 /// $M(n) = O(n \log n)$
1891 ///
1892 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1893 /// other.significant_bits(), `prec`)`.
1894 ///
1895 /// # Examples
1896 /// ```
1897 /// use core::f64::consts::{E, PI};
1898 /// use malachite_float::Float;
1899 /// use std::cmp::Ordering::*;
1900 ///
1901 /// let (quotient, o) = Float::from(PI).div_prec_val_ref(&Float::from(E), 5);
1902 /// assert_eq!(quotient.to_string(), "1.12");
1903 /// assert_eq!(o, Less);
1904 ///
1905 /// let (quotient, o) = Float::from(PI).div_prec_val_ref(&Float::from(E), 20);
1906 /// assert_eq!(quotient.to_string(), "1.1557274");
1907 /// assert_eq!(o, Greater);
1908 /// ```
1909 #[inline]
1910 pub fn div_prec_val_ref(self, other: &Self, prec: u64) -> (Self, Ordering) {
1911 self.div_prec_round_val_ref(other, prec, Nearest)
1912 }
1913
1914 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1915 /// The first [`Float`] is taken by reference and the second by value. An [`Ordering`] is also
1916 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
1917 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
1918 /// function returns a `NaN` it also returns `Equal`.
1919 ///
1920 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1921 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1922 /// description of the `Nearest` rounding mode.
1923 ///
1924 /// $$
1925 /// f(x,y,p) = x/y+\varepsilon.
1926 /// $$
1927 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1928 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
1929 ///
1930 /// If the output has a precision, it is `prec`.
1931 ///
1932 /// Special cases:
1933 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
1934 /// \text{NaN}$
1935 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
1936 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
1937 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
1938 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
1939 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
1940 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
1941 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
1942 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
1943 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
1944 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
1945 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1946 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1947 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
1948 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
1949 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
1950 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
1951 ///
1952 /// Overflow and underflow:
1953 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
1954 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
1955 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1956 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1957 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
1958 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1959 ///
1960 /// If you want to use a rounding mode other than `Nearest`, consider using
1961 /// [`Float::div_prec_round_ref_val`] instead. If you know that your target precision is the
1962 /// maximum of the precisions of the two inputs, consider using `/` instead.
1963 ///
1964 /// # Worst-case complexity
1965 /// $T(n) = O(n \log n \log\log n)$
1966 ///
1967 /// $M(n) = O(n \log n)$
1968 ///
1969 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1970 /// other.significant_bits(), `prec`)`.
1971 ///
1972 /// # Examples
1973 /// ```
1974 /// use core::f64::consts::{E, PI};
1975 /// use malachite_float::Float;
1976 /// use std::cmp::Ordering::*;
1977 ///
1978 /// let (quotient, o) = Float::from(PI).div_prec_ref_val(Float::from(E), 5);
1979 /// assert_eq!(quotient.to_string(), "1.12");
1980 /// assert_eq!(o, Less);
1981 ///
1982 /// let (quotient, o) = Float::from(PI).div_prec_ref_val(Float::from(E), 20);
1983 /// assert_eq!(quotient.to_string(), "1.1557274");
1984 /// assert_eq!(o, Greater);
1985 /// ```
1986 #[inline]
1987 pub fn div_prec_ref_val(&self, other: Self, prec: u64) -> (Self, Ordering) {
1988 self.div_prec_round_ref_val(other, prec, Nearest)
1989 }
1990
1991 /// Divides two [`Float`]s, rounding the result to the nearest value of the specified precision.
1992 /// Both [`Float`]s are taken by reference. An [`Ordering`] is also returned, indicating whether
1993 /// the rounded quotient is less than, equal to, or greater than the exact quotient. Although
1994 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
1995 /// returns `Equal`.
1996 ///
1997 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
1998 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1999 /// description of the `Nearest` rounding mode.
2000 ///
2001 /// $$
2002 /// f(x,y,p) = x/y+\varepsilon.
2003 /// $$
2004 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2005 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2006 ///
2007 /// If the output has a precision, it is `prec`.
2008 ///
2009 /// Special cases:
2010 /// - $f(\text{NaN},x,p)=f(x,\text{NaN},p)=f(\pm\infty,\pm\infty,p,m)=f(\pm0.0,\pm0.0,p,m) =
2011 /// \text{NaN}$
2012 /// - $f(\infty,x,p)=\infty$ if $0.0<x<\infty$
2013 /// - $f(\infty,x,p)=-\infty$ if $-\infty<x<0.0$
2014 /// - $f(x,0.0,p)=\infty$ if $x>0.0$
2015 /// - $f(x,0.0,p)=-\infty$ if $x<0.0$
2016 /// - $f(-\infty,x,p)=-\infty$ if $0.0<x<\infty$
2017 /// - $f(-\infty,x,p)=\infty$ if $-\infty<x<0.0$
2018 /// - $f(x,-0.0,p)=-\infty$ if $x>0.0$
2019 /// - $f(x,-0.0,p)=\infty$ if $x<0.0$
2020 /// - $f(0.0,x,p)=0.0$ if $x$ is not NaN and $x>0.0$
2021 /// - $f(0.0,x,p)=-0.0$ if $x$ is not NaN and $x<0.0$
2022 /// - $f(x,\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2023 /// - $f(x,\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2024 /// - $f(-0.0,x,p)=-0.0$ if $x$ is not NaN and $x>0.0$
2025 /// - $f(-0.0,x,p)=0.0$ if $x$ is not NaN and $x<0.0$
2026 /// - $f(x,-\infty,p)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2027 /// - $f(x,-\infty,p)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2028 ///
2029 /// Overflow and underflow:
2030 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
2031 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
2032 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2033 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2034 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
2035 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
2036 ///
2037 /// If you want to use a rounding mode other than `Nearest`, consider using
2038 /// [`Float::div_prec_round_ref_ref`] instead. If you know that your target precision is the
2039 /// maximum of the precisions of the two inputs, consider using `/` instead.
2040 ///
2041 /// # Worst-case complexity
2042 /// $T(n) = O(n \log n \log\log n)$
2043 ///
2044 /// $M(n) = O(n \log n)$
2045 ///
2046 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2047 /// other.significant_bits(), `prec`)`.
2048 ///
2049 /// # Examples
2050 /// ```
2051 /// use core::f64::consts::{E, PI};
2052 /// use malachite_float::Float;
2053 /// use std::cmp::Ordering::*;
2054 ///
2055 /// let (quotient, o) = Float::from(PI).div_prec_ref_ref(&Float::from(E), 5);
2056 /// assert_eq!(quotient.to_string(), "1.12");
2057 /// assert_eq!(o, Less);
2058 ///
2059 /// let (quotient, o) = Float::from(PI).div_prec_ref_ref(&Float::from(E), 20);
2060 /// assert_eq!(quotient.to_string(), "1.1557274");
2061 /// assert_eq!(o, Greater);
2062 /// ```
2063 #[inline]
2064 pub fn div_prec_ref_ref(&self, other: &Self, prec: u64) -> (Self, Ordering) {
2065 self.div_prec_round_ref_ref(other, prec, Nearest)
2066 }
2067
2068 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. Both
2069 /// [`Float`]s are taken by value. An [`Ordering`] is also returned, indicating whether the
2070 /// rounded quotient is less than, equal to, or greater than the exact quotient. Although `NaN`s
2071 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
2072 /// `Equal`.
2073 ///
2074 /// The precision of the output is the maximum of the precision of the inputs. See
2075 /// [`RoundingMode`] for a description of the possible rounding modes.
2076 ///
2077 /// $$
2078 /// f(x,y,m) = x/y+\varepsilon.
2079 /// $$
2080 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2081 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2082 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2083 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2084 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2085 ///
2086 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2087 ///
2088 /// Special cases:
2089 /// - $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) =
2090 /// \text{NaN}$
2091 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
2092 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
2093 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
2094 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
2095 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
2096 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
2097 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
2098 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
2099 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
2100 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
2101 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2102 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2103 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
2104 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
2105 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2106 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2107 ///
2108 /// Overflow and underflow:
2109 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
2110 /// returned instead.
2111 /// - 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
2112 /// returned instead, where `p` is the precision of the input.
2113 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
2114 /// returned instead.
2115 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
2116 /// is returned instead, where `p` is the precision of the input.
2117 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
2118 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
2119 /// instead.
2120 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
2121 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
2122 /// instead.
2123 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
2124 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
2125 /// instead.
2126 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
2127 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
2128 /// returned instead.
2129 ///
2130 /// If you want to specify an output precision, consider using [`Float::div_prec_round`]
2131 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
2132 /// instead.
2133 ///
2134 /// # Worst-case complexity
2135 /// $T(n) = O(n \log n \log\log n)$
2136 ///
2137 /// $M(n) = O(n \log n)$
2138 ///
2139 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2140 /// other.significant_bits())`.
2141 ///
2142 /// # Panics
2143 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
2144 /// represent the output.
2145 ///
2146 /// # Examples
2147 /// ```
2148 /// use core::f64::consts::{E, PI};
2149 /// use malachite_base::rounding_modes::RoundingMode::*;
2150 /// use malachite_float::Float;
2151 /// use std::cmp::Ordering::*;
2152 ///
2153 /// let (quotient, o) = Float::from(PI).div_round(Float::from(E), Floor);
2154 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2155 /// assert_eq!(o, Less);
2156 ///
2157 /// let (quotient, o) = Float::from(PI).div_round(Float::from(E), Ceiling);
2158 /// assert_eq!(quotient.to_string(), "1.1557273497909220");
2159 /// assert_eq!(o, Greater);
2160 ///
2161 /// let (quotient, o) = Float::from(PI).div_round(Float::from(E), Nearest);
2162 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2163 /// assert_eq!(o, Less);
2164 /// ```
2165 #[inline]
2166 pub fn div_round(self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
2167 let prec = max(self.significant_bits(), other.significant_bits());
2168 self.div_prec_round(other, prec, rm)
2169 }
2170
2171 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. The first
2172 /// [`Float`] is taken by value and the second by reference. An [`Ordering`] is also returned,
2173 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
2174 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function
2175 /// returns a `NaN` it also returns `Equal`.
2176 ///
2177 /// The precision of the output is the maximum of the precision of the inputs. See
2178 /// [`RoundingMode`] for a description of the possible rounding modes.
2179 ///
2180 /// $$
2181 /// f(x,y,m) = x/y+\varepsilon.
2182 /// $$
2183 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2184 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2185 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2186 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2187 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2188 ///
2189 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2190 ///
2191 /// Special cases:
2192 /// - $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) =
2193 /// \text{NaN}$
2194 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
2195 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
2196 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
2197 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
2198 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
2199 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
2200 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
2201 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
2202 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
2203 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
2204 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2205 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2206 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
2207 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
2208 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2209 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2210 ///
2211 /// Overflow and underflow:
2212 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
2213 /// returned instead.
2214 /// - 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
2215 /// returned instead, where `p` is the precision of the input.
2216 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
2217 /// returned instead.
2218 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
2219 /// is returned instead, where `p` is the precision of the input.
2220 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
2221 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
2222 /// instead.
2223 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
2224 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
2225 /// instead.
2226 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
2227 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
2228 /// instead.
2229 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
2230 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
2231 /// returned instead.
2232 ///
2233 /// If you want to specify an output precision, consider using [`Float::div_prec_round_val_ref`]
2234 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
2235 /// instead.
2236 ///
2237 /// # Worst-case complexity
2238 /// $T(n) = O(n \log n \log\log n)$
2239 ///
2240 /// $M(n) = O(n \log n)$
2241 ///
2242 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2243 /// other.significant_bits())`.
2244 ///
2245 /// # Panics
2246 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
2247 /// represent the output.
2248 ///
2249 /// # Examples
2250 /// ```
2251 /// use core::f64::consts::{E, PI};
2252 /// use malachite_base::rounding_modes::RoundingMode::*;
2253 /// use malachite_float::Float;
2254 /// use std::cmp::Ordering::*;
2255 ///
2256 /// let (quotient, o) = Float::from(PI).div_round_val_ref(&Float::from(E), Floor);
2257 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2258 /// assert_eq!(o, Less);
2259 ///
2260 /// let (quotient, o) = Float::from(PI).div_round_val_ref(&Float::from(E), Ceiling);
2261 /// assert_eq!(quotient.to_string(), "1.1557273497909220");
2262 /// assert_eq!(o, Greater);
2263 ///
2264 /// let (quotient, o) = Float::from(PI).div_round_val_ref(&Float::from(E), Nearest);
2265 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2266 /// assert_eq!(o, Less);
2267 /// ```
2268 #[inline]
2269 pub fn div_round_val_ref(self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
2270 let prec = max(self.significant_bits(), other.significant_bits());
2271 self.div_prec_round_val_ref(other, prec, rm)
2272 }
2273
2274 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. The first
2275 /// [`Float`] is taken by reference and the second by value. An [`Ordering`] is also returned,
2276 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
2277 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function
2278 /// returns a `NaN` it also returns `Equal`.
2279 ///
2280 /// The precision of the output is the maximum of the precision of the inputs. See
2281 /// [`RoundingMode`] for a description of the possible rounding modes.
2282 ///
2283 /// $$
2284 /// f(x,y,m) = x/y+\varepsilon.
2285 /// $$
2286 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2287 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2288 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2289 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2290 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2291 ///
2292 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2293 ///
2294 /// Special cases:
2295 /// - $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) =
2296 /// \text{NaN}$
2297 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
2298 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
2299 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
2300 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
2301 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
2302 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
2303 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
2304 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
2305 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
2306 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
2307 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2308 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2309 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
2310 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
2311 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2312 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2313 ///
2314 /// Overflow and underflow:
2315 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
2316 /// returned instead.
2317 /// - 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
2318 /// returned instead, where `p` is the precision of the input.
2319 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
2320 /// returned instead.
2321 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
2322 /// is returned instead, where `p` is the precision of the input.
2323 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
2324 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
2325 /// instead.
2326 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
2327 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
2328 /// instead.
2329 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
2330 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
2331 /// instead.
2332 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
2333 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
2334 /// returned instead.
2335 ///
2336 /// If you want to specify an output precision, consider using [`Float::div_prec_round_ref_val`]
2337 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
2338 /// instead.
2339 ///
2340 /// # Worst-case complexity
2341 /// $T(n) = O(n \log n \log\log n)$
2342 ///
2343 /// $M(n) = O(n \log n)$
2344 ///
2345 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2346 /// other.significant_bits())`.
2347 ///
2348 /// # Panics
2349 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
2350 /// represent the output.
2351 ///
2352 /// # Examples
2353 /// ```
2354 /// use core::f64::consts::{E, PI};
2355 /// use malachite_base::rounding_modes::RoundingMode::*;
2356 /// use malachite_float::Float;
2357 /// use std::cmp::Ordering::*;
2358 ///
2359 /// let (quotient, o) = Float::from(PI).div_round_ref_val(Float::from(E), Floor);
2360 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2361 /// assert_eq!(o, Less);
2362 ///
2363 /// let (quotient, o) = Float::from(PI).div_round_ref_val(Float::from(E), Ceiling);
2364 /// assert_eq!(quotient.to_string(), "1.1557273497909220");
2365 /// assert_eq!(o, Greater);
2366 ///
2367 /// let (quotient, o) = Float::from(PI).div_round_ref_val(Float::from(E), Nearest);
2368 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2369 /// assert_eq!(o, Less);
2370 /// ```
2371 #[inline]
2372 pub fn div_round_ref_val(&self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
2373 let prec = max(self.significant_bits(), other.significant_bits());
2374 self.div_prec_round_ref_val(other, prec, rm)
2375 }
2376
2377 /// Divides two [`Float`]s, rounding the result with the specified rounding mode. Both
2378 /// [`Float`]s are taken by reference. An [`Ordering`] is also returned, indicating whether the
2379 /// rounded quotient is less than, equal to, or greater than the exact quotient. Although `NaN`s
2380 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
2381 /// `Equal`.
2382 ///
2383 /// The precision of the output is the maximum of the precision of the inputs. See
2384 /// [`RoundingMode`] for a description of the possible rounding modes.
2385 ///
2386 /// $$
2387 /// f(x,y,m) = x/y+\varepsilon.
2388 /// $$
2389 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2390 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2391 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
2392 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2393 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2394 ///
2395 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2396 ///
2397 /// Special cases:
2398 /// - $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) =
2399 /// \text{NaN}$
2400 /// - $f(\infty,x,m)=\infty$ if $0.0<x<\infty$
2401 /// - $f(\infty,x,m)=-\infty$ if $-\infty<x<0.0$
2402 /// - $f(x,0.0,m)=\infty$ if $x>0.0$
2403 /// - $f(x,0.0,m)=-\infty$ if $x<0.0$
2404 /// - $f(-\infty,x,m)=-\infty$ if $0.0<x<\infty$
2405 /// - $f(-\infty,x,m)=\infty$ if $-\infty<x<0.0$
2406 /// - $f(x,-0.0,m)=-\infty$ if $x>0.0$
2407 /// - $f(x,-0.0,m)=\infty$ if $x<0.0$
2408 /// - $f(0.0,x,m)=0.0$ if $x$ is not NaN and $x>0.0$
2409 /// - $f(0.0,x,m)=-0.0$ if $x$ is not NaN and $x<0.0$
2410 /// - $f(x,\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2411 /// - $f(x,\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2412 /// - $f(-0.0,x,m)=-0.0$ if $x$ is not NaN and $x>0.0$
2413 /// - $f(-0.0,x,m)=0.0$ if $x$ is not NaN and $x<0.0$
2414 /// - $f(x,-\infty,m)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
2415 /// - $f(x,-\infty,m)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
2416 ///
2417 /// Overflow and underflow:
2418 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
2419 /// returned instead.
2420 /// - 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
2421 /// returned instead, where `p` is the precision of the input.
2422 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
2423 /// returned instead.
2424 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
2425 /// is returned instead, where `p` is the precision of the input.
2426 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
2427 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
2428 /// instead.
2429 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
2430 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
2431 /// instead.
2432 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
2433 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
2434 /// instead.
2435 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
2436 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
2437 /// returned instead.
2438 ///
2439 /// If you want to specify an output precision, consider using [`Float::div_prec_round_ref_ref`]
2440 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/`
2441 /// instead.
2442 ///
2443 /// # Worst-case complexity
2444 /// $T(n) = O(n \log n \log\log n)$
2445 ///
2446 /// $M(n) = O(n \log n)$
2447 ///
2448 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2449 /// other.significant_bits())`.
2450 ///
2451 /// # Panics
2452 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
2453 /// represent the output.
2454 ///
2455 /// # Examples
2456 /// ```
2457 /// use core::f64::consts::{E, PI};
2458 /// use malachite_base::rounding_modes::RoundingMode::*;
2459 /// use malachite_float::Float;
2460 /// use std::cmp::Ordering::*;
2461 ///
2462 /// let (quotient, o) = Float::from(PI).div_round_ref_ref(&Float::from(E), Floor);
2463 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2464 /// assert_eq!(o, Less);
2465 ///
2466 /// let (quotient, o) = Float::from(PI).div_round_ref_ref(&Float::from(E), Ceiling);
2467 /// assert_eq!(quotient.to_string(), "1.1557273497909220");
2468 /// assert_eq!(o, Greater);
2469 ///
2470 /// let (quotient, o) = Float::from(PI).div_round_ref_ref(&Float::from(E), Nearest);
2471 /// assert_eq!(quotient.to_string(), "1.1557273497909217");
2472 /// assert_eq!(o, Less);
2473 /// ```
2474 #[inline]
2475 pub fn div_round_ref_ref(&self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
2476 let prec = max(self.significant_bits(), other.significant_bits());
2477 self.div_prec_round_ref_ref(other, prec, rm)
2478 }
2479
2480 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the specified precision
2481 /// and with the specified rounding mode. The [`Float`] on the right-hand side is taken by
2482 /// value. An [`Ordering`] is returned, indicating whether the rounded quotient is less than,
2483 /// equal to, or greater than the exact quotient. Although `NaN`s are not comparable to any
2484 /// [`Float`], whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
2485 ///
2486 /// See [`RoundingMode`] for a description of the possible rounding modes.
2487 ///
2488 /// $$
2489 /// x \gets x/y+\varepsilon.
2490 /// $$
2491 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2492 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2493 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2494 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2495 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2496 ///
2497 /// If the output has a precision, it is `prec`.
2498 ///
2499 /// See the [`Float::div_prec_round`] documentation for information on special cases, overflow,
2500 /// and underflow.
2501 ///
2502 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_assign`] instead. If
2503 /// you know that your target precision is the maximum of the precisions of the two inputs,
2504 /// consider using [`Float::div_round_assign`] instead. If both of these things are true,
2505 /// consider using `/=` instead.
2506 ///
2507 /// # Worst-case complexity
2508 /// $T(n) = O(n \log n \log\log n)$
2509 ///
2510 /// $M(n) = O(n \log n)$
2511 ///
2512 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2513 /// other.significant_bits(), `prec`)`.
2514 ///
2515 /// # Panics
2516 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2517 ///
2518 /// # Examples
2519 /// ```
2520 /// use core::f64::consts::{E, PI};
2521 /// use malachite_base::rounding_modes::RoundingMode::*;
2522 /// use malachite_float::Float;
2523 /// use std::cmp::Ordering::*;
2524 ///
2525 /// let mut quotient = Float::from(PI);
2526 /// assert_eq!(
2527 /// quotient.div_prec_round_assign(Float::from(E), 5, Floor),
2528 /// Less
2529 /// );
2530 /// assert_eq!(quotient.to_string(), "1.12");
2531 ///
2532 /// let mut quotient = Float::from(PI);
2533 /// assert_eq!(
2534 /// quotient.div_prec_round_assign(Float::from(E), 5, Ceiling),
2535 /// Greater
2536 /// );
2537 /// assert_eq!(quotient.to_string(), "1.19");
2538 ///
2539 /// let mut quotient = Float::from(PI);
2540 /// assert_eq!(
2541 /// quotient.div_prec_round_assign(Float::from(E), 5, Nearest),
2542 /// Less
2543 /// );
2544 /// assert_eq!(quotient.to_string(), "1.12");
2545 ///
2546 /// let mut quotient = Float::from(PI);
2547 /// assert_eq!(
2548 /// quotient.div_prec_round_assign(Float::from(E), 20, Floor),
2549 /// Less
2550 /// );
2551 /// assert_eq!(quotient.to_string(), "1.1557255");
2552 ///
2553 /// let mut quotient = Float::from(PI);
2554 /// assert_eq!(
2555 /// quotient.div_prec_round_assign(Float::from(E), 20, Ceiling),
2556 /// Greater
2557 /// );
2558 /// assert_eq!(quotient.to_string(), "1.1557274");
2559 ///
2560 /// let mut quotient = Float::from(PI);
2561 /// assert_eq!(
2562 /// quotient.div_prec_round_assign(Float::from(E), 20, Nearest),
2563 /// Greater
2564 /// );
2565 /// assert_eq!(quotient.to_string(), "1.1557274");
2566 /// ```
2567 #[inline]
2568 pub fn div_prec_round_assign(&mut self, other: Self, prec: u64, rm: RoundingMode) -> Ordering {
2569 assert_ne!(prec, 0);
2570 match (&mut *self, other) {
2571 (float_nan!(), _)
2572 | (_, float_nan!())
2573 | (float_either_infinity!(), float_either_infinity!())
2574 | (float_either_zero!(), float_either_zero!()) => {
2575 *self = float_nan!();
2576 Equal
2577 }
2578 (
2579 Self(Infinity { sign: x_sign }),
2580 Self(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
2581 )
2582 | (Self(Finite { sign: x_sign, .. }), Self(Zero { sign: y_sign })) => {
2583 *self = Self(Infinity {
2584 sign: *x_sign == y_sign,
2585 });
2586 Equal
2587 }
2588 (
2589 Self(Zero { sign: x_sign }),
2590 Self(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
2591 )
2592 | (Self(Finite { sign: x_sign, .. }), Self(Infinity { sign: y_sign })) => {
2593 *self = Self(Zero {
2594 sign: *x_sign == y_sign,
2595 });
2596 Equal
2597 }
2598 (_, y) if abs_is_power_of_2(&y) => {
2599 let sign = y >= 0;
2600 let mut o = self.shr_prec_round_assign(
2601 y.get_exponent().unwrap() - 1,
2602 prec,
2603 if sign { rm } else { -rm },
2604 );
2605 if !sign {
2606 self.neg_assign();
2607 o = o.reverse();
2608 }
2609 o
2610 }
2611 (
2612 Self(Finite {
2613 sign: x_sign,
2614 exponent: x_exp,
2615 precision: x_prec,
2616 significand: x,
2617 }),
2618 Self(Finite {
2619 sign: y_sign,
2620 exponent: y_exp,
2621 precision: y_prec,
2622 significand: mut y,
2623 }),
2624 ) => {
2625 let sign = *x_sign == y_sign;
2626 let exp_diff = *x_exp - y_exp;
2627 if exp_diff > Self::MAX_EXPONENT {
2628 return match (sign, rm) {
2629 (_, Exact) => panic!("Inexact Float division"),
2630 (true, Ceiling | Up | Nearest) => {
2631 *self = float_infinity!();
2632 Greater
2633 }
2634 (true, _) => {
2635 *self = Self::max_finite_value_with_prec(prec);
2636 Less
2637 }
2638 (false, Floor | Up | Nearest) => {
2639 *self = float_negative_infinity!();
2640 Less
2641 }
2642 (false, _) => {
2643 *self = -Self::max_finite_value_with_prec(prec);
2644 Greater
2645 }
2646 };
2647 } else if exp_diff + 2 < Self::MIN_EXPONENT {
2648 return match (sign, rm) {
2649 (_, Exact) => panic!("Inexact Float division"),
2650 (true, Ceiling | Up) => {
2651 *self = Self::min_positive_value_prec(prec);
2652 Greater
2653 }
2654 (true, _) => {
2655 *self = float_zero!();
2656 Less
2657 }
2658 (false, Floor | Up) => {
2659 *self = -Self::min_positive_value_prec(prec);
2660 Less
2661 }
2662 (false, _) => {
2663 *self = float_negative_zero!();
2664 Greater
2665 }
2666 };
2667 }
2668 let (exp_offset, o) = div_float_significands_in_place(
2669 x,
2670 *x_prec,
2671 &mut y,
2672 y_prec,
2673 prec,
2674 if sign { rm } else { -rm },
2675 );
2676 *x_exp = exp_diff.checked_add(i32::exact_from(exp_offset)).unwrap();
2677 if *x_exp > Self::MAX_EXPONENT {
2678 return match (sign, rm) {
2679 (_, Exact) => panic!("Inexact Float division"),
2680 (true, Ceiling | Up | Nearest) => {
2681 *self = float_infinity!();
2682 Greater
2683 }
2684 (true, _) => {
2685 *self = Self::max_finite_value_with_prec(prec);
2686 Less
2687 }
2688 (false, Floor | Up | Nearest) => {
2689 *self = float_negative_infinity!();
2690 Less
2691 }
2692 (false, _) => {
2693 *self = -Self::max_finite_value_with_prec(prec);
2694 Greater
2695 }
2696 };
2697 } else if *x_exp < Self::MIN_EXPONENT {
2698 return if rm == Nearest
2699 && *x_exp == Self::MIN_EXPONENT_MINUS_1
2700 && (o == Less || !x.is_power_of_2())
2701 {
2702 if sign {
2703 *self = Self::min_positive_value_prec(prec);
2704 Greater
2705 } else {
2706 *self = -Self::min_positive_value_prec(prec);
2707 Less
2708 }
2709 } else {
2710 match (sign, rm) {
2711 (_, Exact) => panic!("Inexact Float division"),
2712 (true, Ceiling | Up) => {
2713 *self = Self::min_positive_value_prec(prec);
2714 Greater
2715 }
2716 (true, _) => {
2717 *self = float_zero!();
2718 Less
2719 }
2720 (false, Floor | Up) => {
2721 *self = -Self::min_positive_value_prec(prec);
2722 Less
2723 }
2724 (false, _) => {
2725 *self = float_negative_zero!();
2726 Greater
2727 }
2728 }
2729 };
2730 }
2731 *x_sign = sign;
2732 *x_prec = prec;
2733 if sign { o } else { o.reverse() }
2734 }
2735 }
2736 }
2737
2738 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the specified precision
2739 /// and with the specified rounding mode. The [`Float`] on the right-hand side is taken by
2740 /// reference. An [`Ordering`] is returned, indicating whether the rounded quotient is less
2741 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
2742 /// any [`Float`], whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
2743 ///
2744 /// See [`RoundingMode`] for a description of the possible rounding modes.
2745 ///
2746 /// $$
2747 /// x \gets x/y+\varepsilon.
2748 /// $$
2749 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2750 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2751 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
2752 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2753 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
2754 ///
2755 /// If the output has a precision, it is `prec`.
2756 ///
2757 /// See the [`Float::div_prec_round`] documentation for information on special cases, overflow,
2758 /// and underflow.
2759 ///
2760 /// If you know you'll be using `Nearest`, consider using [`Float::div_prec_assign_ref`]
2761 /// instead. If you know that your target precision is the maximum of the precisions of the two
2762 /// inputs, consider using [`Float::div_round_assign_ref`] instead. If both of these things are
2763 /// true, consider using `/=` instead.
2764 ///
2765 /// # Worst-case complexity
2766 /// $T(n) = O(n \log n \log\log n)$
2767 ///
2768 /// $M(n) = O(n \log n)$
2769 ///
2770 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2771 /// other.significant_bits(), `prec`)`.
2772 ///
2773 /// # Panics
2774 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
2775 ///
2776 /// # Examples
2777 /// ```
2778 /// use core::f64::consts::{E, PI};
2779 /// use malachite_base::rounding_modes::RoundingMode::*;
2780 /// use malachite_float::Float;
2781 /// use std::cmp::Ordering::*;
2782 ///
2783 /// let mut quotient = Float::from(PI);
2784 /// assert_eq!(
2785 /// quotient.div_prec_round_assign_ref(&Float::from(E), 5, Floor),
2786 /// Less
2787 /// );
2788 /// assert_eq!(quotient.to_string(), "1.12");
2789 ///
2790 /// let mut quotient = Float::from(PI);
2791 /// assert_eq!(
2792 /// quotient.div_prec_round_assign_ref(&Float::from(E), 5, Ceiling),
2793 /// Greater
2794 /// );
2795 /// assert_eq!(quotient.to_string(), "1.19");
2796 ///
2797 /// let mut quotient = Float::from(PI);
2798 /// assert_eq!(
2799 /// quotient.div_prec_round_assign_ref(&Float::from(E), 5, Nearest),
2800 /// Less
2801 /// );
2802 /// assert_eq!(quotient.to_string(), "1.12");
2803 ///
2804 /// let mut quotient = Float::from(PI);
2805 /// assert_eq!(
2806 /// quotient.div_prec_round_assign_ref(&Float::from(E), 20, Floor),
2807 /// Less
2808 /// );
2809 /// assert_eq!(quotient.to_string(), "1.1557255");
2810 ///
2811 /// let mut quotient = Float::from(PI);
2812 /// assert_eq!(
2813 /// quotient.div_prec_round_assign_ref(&Float::from(E), 20, Ceiling),
2814 /// Greater
2815 /// );
2816 /// assert_eq!(quotient.to_string(), "1.1557274");
2817 ///
2818 /// let mut quotient = Float::from(PI);
2819 /// assert_eq!(
2820 /// quotient.div_prec_round_assign_ref(&Float::from(E), 20, Nearest),
2821 /// Greater
2822 /// );
2823 /// assert_eq!(quotient.to_string(), "1.1557274");
2824 /// ```
2825 #[inline]
2826 pub fn div_prec_round_assign_ref(
2827 &mut self,
2828 other: &Self,
2829 prec: u64,
2830 rm: RoundingMode,
2831 ) -> Ordering {
2832 assert_ne!(prec, 0);
2833 match (&mut *self, other) {
2834 (float_nan!(), _)
2835 | (_, float_nan!())
2836 | (float_either_infinity!(), float_either_infinity!())
2837 | (float_either_zero!(), float_either_zero!()) => {
2838 *self = float_nan!();
2839 Equal
2840 }
2841 (
2842 Self(Infinity { sign: x_sign }),
2843 Self(Finite { sign: y_sign, .. } | Zero { sign: y_sign }),
2844 )
2845 | (Self(Finite { sign: x_sign, .. }), Self(Zero { sign: y_sign })) => {
2846 *self = Self(Infinity {
2847 sign: x_sign == y_sign,
2848 });
2849 Equal
2850 }
2851 (
2852 Self(Zero { sign: x_sign }),
2853 Self(Finite { sign: y_sign, .. } | Infinity { sign: y_sign }),
2854 )
2855 | (Self(Finite { sign: x_sign, .. }), Self(Infinity { sign: y_sign })) => {
2856 *self = Self(Zero {
2857 sign: x_sign == y_sign,
2858 });
2859 Equal
2860 }
2861 (_, y) if abs_is_power_of_2(y) => {
2862 let sign = *y >= 0;
2863 let mut o = self.shr_prec_round_assign(
2864 y.get_exponent().unwrap() - 1,
2865 prec,
2866 if sign { rm } else { -rm },
2867 );
2868 if !sign {
2869 self.neg_assign();
2870 o = o.reverse();
2871 }
2872 o
2873 }
2874 (
2875 Self(Finite {
2876 sign: x_sign,
2877 exponent: x_exp,
2878 precision: x_prec,
2879 significand: x,
2880 }),
2881 Self(Finite {
2882 sign: y_sign,
2883 exponent: y_exp,
2884 precision: y_prec,
2885 significand: y,
2886 }),
2887 ) => {
2888 let sign = x_sign == y_sign;
2889 let exp_diff = *x_exp - y_exp;
2890 if exp_diff > Self::MAX_EXPONENT {
2891 return match (sign, rm) {
2892 (_, Exact) => panic!("Inexact Float division"),
2893 (true, Ceiling | Up | Nearest) => {
2894 *self = float_infinity!();
2895 Greater
2896 }
2897 (true, _) => {
2898 *self = Self::max_finite_value_with_prec(prec);
2899 Less
2900 }
2901 (false, Floor | Up | Nearest) => {
2902 *self = float_negative_infinity!();
2903 Less
2904 }
2905 (false, _) => {
2906 *self = -Self::max_finite_value_with_prec(prec);
2907 Greater
2908 }
2909 };
2910 } else if exp_diff + 2 < Self::MIN_EXPONENT {
2911 return match (sign, rm) {
2912 (_, Exact) => panic!("Inexact Float division"),
2913 (true, Ceiling | Up) => {
2914 *self = Self::min_positive_value_prec(prec);
2915 Greater
2916 }
2917 (true, _) => {
2918 *self = float_zero!();
2919 Less
2920 }
2921 (false, Floor | Up) => {
2922 *self = -Self::min_positive_value_prec(prec);
2923 Less
2924 }
2925 (false, _) => {
2926 *self = float_negative_zero!();
2927 Greater
2928 }
2929 };
2930 }
2931 let (exp_offset, o) = div_float_significands_in_place_ref(
2932 x,
2933 *x_prec,
2934 y,
2935 *y_prec,
2936 prec,
2937 if sign { rm } else { -rm },
2938 );
2939 *x_exp = exp_diff.checked_add(i32::exact_from(exp_offset)).unwrap();
2940 if *x_exp > Self::MAX_EXPONENT {
2941 return match (sign, rm) {
2942 (_, Exact) => panic!("Inexact Float division"),
2943 (true, Ceiling | Up | Nearest) => {
2944 *self = float_infinity!();
2945 Greater
2946 }
2947 (true, _) => {
2948 *self = Self::max_finite_value_with_prec(prec);
2949 Less
2950 }
2951 (false, Floor | Up | Nearest) => {
2952 *self = float_negative_infinity!();
2953 Less
2954 }
2955 (false, _) => {
2956 *self = -Self::max_finite_value_with_prec(prec);
2957 Greater
2958 }
2959 };
2960 } else if *x_exp < Self::MIN_EXPONENT {
2961 return if rm == Nearest
2962 && *x_exp == Self::MIN_EXPONENT_MINUS_1
2963 && (o == Less || !x.is_power_of_2())
2964 {
2965 if sign {
2966 *self = Self::min_positive_value_prec(prec);
2967 Greater
2968 } else {
2969 *self = -Self::min_positive_value_prec(prec);
2970 Less
2971 }
2972 } else {
2973 match (sign, rm) {
2974 (_, Exact) => panic!("Inexact Float division"),
2975 (true, Ceiling | Up) => {
2976 *self = Self::min_positive_value_prec(prec);
2977 Greater
2978 }
2979 (true, _) => {
2980 *self = float_zero!();
2981 Less
2982 }
2983 (false, Floor | Up) => {
2984 *self = -Self::min_positive_value_prec(prec);
2985 Less
2986 }
2987 (false, _) => {
2988 *self = float_negative_zero!();
2989 Greater
2990 }
2991 }
2992 };
2993 }
2994 *x_sign = sign;
2995 *x_prec = prec;
2996 if sign { o } else { o.reverse() }
2997 }
2998 }
2999 }
3000
3001 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the nearest value of the
3002 /// specified precision. The [`Float`] on the right-hand side is taken by value. An [`Ordering`]
3003 /// is returned, indicating whether the rounded quotient is less than, equal to, or greater than
3004 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3005 /// function sets the [`Float`] to `NaN` it also returns `Equal`.
3006 ///
3007 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3008 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3009 /// description of the `Nearest` rounding mode.
3010 ///
3011 /// $$
3012 /// x \gets x/y+\varepsilon.
3013 /// $$
3014 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3015 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3016 ///
3017 /// If the output has a precision, it is `prec`.
3018 ///
3019 /// See the [`Float::div_prec`] documentation for information on special cases, overflow, and
3020 /// underflow.
3021 ///
3022 /// If you want to use a rounding mode other than `Nearest`, consider using
3023 /// [`Float::div_prec_round_assign`] instead. If you know that your target precision is the
3024 /// maximum of the precisions of the two inputs, consider using `/=` instead.
3025 ///
3026 /// # Worst-case complexity
3027 /// $T(n) = O(n \log n \log\log n)$
3028 ///
3029 /// $M(n) = O(n \log n)$
3030 ///
3031 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3032 /// other.significant_bits(), `prec`)`.
3033 ///
3034 /// # Examples
3035 /// ```
3036 /// use core::f64::consts::{E, PI};
3037 /// use malachite_float::Float;
3038 /// use std::cmp::Ordering::*;
3039 ///
3040 /// let mut x = Float::from(PI);
3041 /// assert_eq!(x.div_prec_assign(Float::from(E), 5), Less);
3042 /// assert_eq!(x.to_string(), "1.12");
3043 ///
3044 /// let mut x = Float::from(PI);
3045 /// assert_eq!(x.div_prec_assign(Float::from(E), 20), Greater);
3046 /// assert_eq!(x.to_string(), "1.1557274");
3047 /// ```
3048 #[inline]
3049 pub fn div_prec_assign(&mut self, other: Self, prec: u64) -> Ordering {
3050 self.div_prec_round_assign(other, prec, Nearest)
3051 }
3052
3053 /// Divides a [`Float`] by a [`Float`] in place, rounding the result to the nearest value of the
3054 /// specified precision. The [`Float`] on the right-hand side is taken by reference. An
3055 /// [`Ordering`] is returned, indicating whether the rounded quotient is less than, equal to, or
3056 /// greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3057 /// whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
3058 ///
3059 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3060 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3061 /// description of the `Nearest` rounding mode.
3062 ///
3063 /// $$
3064 /// x \gets x/y+\varepsilon.
3065 /// $$
3066 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3067 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3068 ///
3069 /// If the output has a precision, it is `prec`.
3070 ///
3071 /// See the [`Float::div_prec`] documentation for information on special cases, overflow, and
3072 /// underflow.
3073 ///
3074 /// If you want to use a rounding mode other than `Nearest`, consider using
3075 /// [`Float::div_prec_round_assign_ref`] instead. If you know that your target precision is the
3076 /// maximum of the precisions of the two inputs, consider using `/=` instead.
3077 ///
3078 /// # Worst-case complexity
3079 /// $T(n) = O(n \log n \log\log n)$
3080 ///
3081 /// $M(n) = O(n \log n)$
3082 ///
3083 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3084 /// other.significant_bits(), `prec`)`.
3085 ///
3086 /// # Examples
3087 /// ```
3088 /// use core::f64::consts::{E, PI};
3089 /// use malachite_float::Float;
3090 /// use std::cmp::Ordering::*;
3091 ///
3092 /// let mut x = Float::from(PI);
3093 /// assert_eq!(x.div_prec_assign_ref(&Float::from(E), 5), Less);
3094 /// assert_eq!(x.to_string(), "1.12");
3095 ///
3096 /// let mut x = Float::from(PI);
3097 /// assert_eq!(x.div_prec_assign_ref(&Float::from(E), 20), Greater);
3098 /// assert_eq!(x.to_string(), "1.1557274");
3099 /// ```
3100 #[inline]
3101 pub fn div_prec_assign_ref(&mut self, other: &Self, prec: u64) -> Ordering {
3102 self.div_prec_round_assign_ref(other, prec, Nearest)
3103 }
3104
3105 /// Divides a [`Float`] by a [`Float`] in place, rounding the result with the specified rounding
3106 /// mode. The [`Float`] on the right-hand side is taken by value. An [`Ordering`] is returned,
3107 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
3108 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
3109 /// the [`Float`] to `NaN` it also returns `Equal`.
3110 ///
3111 /// The precision of the output is the maximum of the precision of the inputs. See
3112 /// [`RoundingMode`] for a description of the possible rounding modes.
3113 ///
3114 /// $$
3115 /// x \gets x/y+\varepsilon.
3116 /// $$
3117 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3118 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3119 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
3120 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3121 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
3122 ///
3123 /// If the output has a precision, it is the maximum of the precisions of the inputs.
3124 ///
3125 /// See the [`Float::div_round`] documentation for information on special cases, overflow, and
3126 /// underflow.
3127 ///
3128 /// If you want to specify an output precision, consider using [`Float::div_prec_round_assign`]
3129 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using `/=`
3130 /// instead.
3131 ///
3132 /// # Worst-case complexity
3133 /// $T(n) = O(n \log n \log\log n)$
3134 ///
3135 /// $M(n) = O(n \log n)$
3136 ///
3137 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3138 /// other.significant_bits())`.
3139 ///
3140 /// # Panics
3141 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
3142 /// represent the output.
3143 ///
3144 /// # Examples
3145 /// ```
3146 /// use core::f64::consts::{E, PI};
3147 /// use malachite_base::rounding_modes::RoundingMode::*;
3148 /// use malachite_float::Float;
3149 /// use std::cmp::Ordering::*;
3150 ///
3151 /// let mut x = Float::from(PI);
3152 /// assert_eq!(x.div_round_assign(Float::from(E), Floor), Less);
3153 /// assert_eq!(x.to_string(), "1.1557273497909217");
3154 ///
3155 /// let mut x = Float::from(PI);
3156 /// assert_eq!(x.div_round_assign(Float::from(E), Ceiling), Greater);
3157 /// assert_eq!(x.to_string(), "1.1557273497909220");
3158 ///
3159 /// let mut x = Float::from(PI);
3160 /// assert_eq!(x.div_round_assign(Float::from(E), Nearest), Less);
3161 /// assert_eq!(x.to_string(), "1.1557273497909217");
3162 /// ```
3163 #[inline]
3164 pub fn div_round_assign(&mut self, other: Self, rm: RoundingMode) -> Ordering {
3165 let prec = max(self.significant_bits(), other.significant_bits());
3166 self.div_prec_round_assign(other, prec, rm)
3167 }
3168
3169 /// Divides a [`Float`] by a [`Float`] in place, rounding the result with the specified rounding
3170 /// mode. The [`Float`] on the right-hand side is taken by reference. An [`Ordering`] is
3171 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
3172 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
3173 /// function sets the [`Float`] to `NaN` it also returns `Equal`.
3174 ///
3175 /// The precision of the output is the maximum of the precision of the inputs. See
3176 /// [`RoundingMode`] for a description of the possible rounding modes.
3177 ///
3178 /// $$
3179 /// x \gets x/y+\varepsilon.
3180 /// $$
3181 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3182 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3183 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
3184 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3185 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
3186 ///
3187 /// If the output has a precision, it is the maximum of the precisions of the inputs.
3188 ///
3189 /// See the [`Float::div_round`] documentation for information on special cases, overflow, and
3190 /// underflow.
3191 ///
3192 /// If you want to specify an output precision, consider using
3193 /// [`Float::div_prec_round_assign_ref`] instead. If you know you'll be using the `Nearest`
3194 /// rounding mode, consider using `/=` instead.
3195 ///
3196 /// # Worst-case complexity
3197 /// $T(n) = O(n \log n \log\log n)$
3198 ///
3199 /// $M(n) = O(n \log n)$
3200 ///
3201 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3202 /// other.significant_bits())`.
3203 ///
3204 /// # Panics
3205 /// Panics if `rm` is `Exact` but the maximum precision of the inputs is not high enough to
3206 /// represent the output.
3207 ///
3208 /// # Examples
3209 /// ```
3210 /// use core::f64::consts::{E, PI};
3211 /// use malachite_base::rounding_modes::RoundingMode::*;
3212 /// use malachite_float::Float;
3213 /// use std::cmp::Ordering::*;
3214 ///
3215 /// let mut x = Float::from(PI);
3216 /// assert_eq!(x.div_round_assign_ref(&Float::from(E), Floor), Less);
3217 /// assert_eq!(x.to_string(), "1.1557273497909217");
3218 ///
3219 /// let mut x = Float::from(PI);
3220 /// assert_eq!(x.div_round_assign_ref(&Float::from(E), Ceiling), Greater);
3221 /// assert_eq!(x.to_string(), "1.1557273497909220");
3222 ///
3223 /// let mut x = Float::from(PI);
3224 /// assert_eq!(x.div_round_assign_ref(&Float::from(E), Nearest), Less);
3225 /// assert_eq!(x.to_string(), "1.1557273497909217");
3226 /// ```
3227 #[inline]
3228 pub fn div_round_assign_ref(&mut self, other: &Self, rm: RoundingMode) -> Ordering {
3229 let prec = max(self.significant_bits(), other.significant_bits());
3230 self.div_prec_round_assign_ref(other, prec, rm)
3231 }
3232
3233 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
3234 /// with the specified rounding mode. The [`Float`] and the [`Rational`] are both taken by
3235 /// value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
3236 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
3237 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3238 ///
3239 /// See [`RoundingMode`] for a description of the possible rounding modes.
3240 ///
3241 /// $$
3242 /// f(x,y,p,m) = x/y+\varepsilon.
3243 /// $$
3244 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3245 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3246 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3247 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3248 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3249 ///
3250 /// If the output has a precision, it is `prec`.
3251 ///
3252 /// Special cases:
3253 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
3254 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
3255 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
3256 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
3257 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
3258 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
3259 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
3260 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
3261 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
3262 ///
3263 /// Overflow and underflow:
3264 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
3265 /// returned instead.
3266 /// - 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}$
3267 /// is returned instead, where `p` is the precision of the input.
3268 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
3269 /// returned instead.
3270 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
3271 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
3272 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
3273 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
3274 /// instead.
3275 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
3276 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
3277 /// instead.
3278 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
3279 /// instead.
3280 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
3281 /// instead.
3282 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
3283 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
3284 /// returned instead.
3285 ///
3286 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec`] instead.
3287 /// If you know that your target precision is the precision of the [`Float`] input, consider
3288 /// using [`Float::div_rational_round`] instead. If both of these things are true, consider
3289 /// using `/` instead.
3290 ///
3291 /// # Worst-case complexity
3292 /// $T(n) = O(n \log n \log\log n)$
3293 ///
3294 /// $M(n) = O(n \log n)$
3295 ///
3296 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3297 /// other.significant_bits(), prec)`.
3298 ///
3299 /// # Panics
3300 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3301 ///
3302 /// # Examples
3303 /// ```
3304 /// use core::f64::consts::PI;
3305 /// use malachite_base::rounding_modes::RoundingMode::*;
3306 /// use malachite_float::Float;
3307 /// use malachite_q::Rational;
3308 /// use std::cmp::Ordering::*;
3309 ///
3310 /// let (quotient, o) =
3311 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 5, Floor);
3312 /// assert_eq!(quotient.to_string(), "9.00");
3313 /// assert_eq!(o, Less);
3314 ///
3315 /// let (quotient, o) =
3316 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 5, Ceiling);
3317 /// assert_eq!(quotient.to_string(), "9.50");
3318 /// assert_eq!(o, Greater);
3319 ///
3320 /// let (quotient, o) =
3321 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 5, Nearest);
3322 /// assert_eq!(quotient.to_string(), "9.50");
3323 /// assert_eq!(o, Greater);
3324 ///
3325 /// let (quotient, o) =
3326 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 20, Floor);
3327 /// assert_eq!(quotient.to_string(), "9.4247742");
3328 /// assert_eq!(o, Less);
3329 ///
3330 /// let (quotient, o) =
3331 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 20, Ceiling);
3332 /// assert_eq!(quotient.to_string(), "9.4247894");
3333 /// assert_eq!(o, Greater);
3334 ///
3335 /// let (quotient, o) =
3336 /// Float::from(PI).div_rational_prec_round(Rational::from_unsigneds(1u8, 3), 20, Nearest);
3337 /// assert_eq!(quotient.to_string(), "9.4247742");
3338 /// assert_eq!(o, Less);
3339 /// ```
3340 #[inline]
3341 pub fn div_rational_prec_round(
3342 mut self,
3343 other: Rational,
3344 prec: u64,
3345 rm: RoundingMode,
3346 ) -> (Self, Ordering) {
3347 let o = self.div_rational_prec_round_assign(other, prec, rm);
3348 (self, o)
3349 }
3350
3351 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
3352 /// with the specified rounding mode. The [`Float`] is taken by value and the [`Rational`] by
3353 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
3354 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
3355 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3356 ///
3357 /// See [`RoundingMode`] for a description of the possible rounding modes.
3358 ///
3359 /// $$
3360 /// f(x,y,p,m) = x/y+\varepsilon.
3361 /// $$
3362 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3363 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3364 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3365 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3366 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3367 ///
3368 /// If the output has a precision, it is `prec`.
3369 ///
3370 /// Special cases:
3371 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
3372 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
3373 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
3374 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
3375 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
3376 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
3377 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
3378 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
3379 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
3380 ///
3381 /// Overflow and underflow:
3382 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
3383 /// returned instead.
3384 /// - 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}$
3385 /// is returned instead, where `p` is the precision of the input.
3386 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
3387 /// returned instead.
3388 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
3389 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
3390 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
3391 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
3392 /// instead.
3393 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
3394 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
3395 /// instead.
3396 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
3397 /// instead.
3398 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
3399 /// instead.
3400 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
3401 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
3402 /// returned instead.
3403 ///
3404 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_val_ref`]
3405 /// instead. If you know that your target precision is the precision of the [`Float`] input,
3406 /// consider using [`Float::div_rational_round_val_ref`] instead. If both of these things are
3407 /// true, consider using `/` instead.
3408 ///
3409 /// # Worst-case complexity
3410 /// $T(n) = O(n \log n \log\log n)$
3411 ///
3412 /// $M(n) = O(n \log n)$
3413 ///
3414 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3415 /// other.significant_bits(), prec)`.
3416 ///
3417 /// # Panics
3418 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3419 ///
3420 /// # Examples
3421 /// ```
3422 /// use core::f64::consts::PI;
3423 /// use malachite_base::rounding_modes::RoundingMode::*;
3424 /// use malachite_float::Float;
3425 /// use malachite_q::Rational;
3426 /// use std::cmp::Ordering::*;
3427 ///
3428 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3429 /// &Rational::from_unsigneds(1u8, 3),
3430 /// 5,
3431 /// Floor,
3432 /// );
3433 /// assert_eq!(quotient.to_string(), "9.00");
3434 /// assert_eq!(o, Less);
3435 ///
3436 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3437 /// &Rational::from_unsigneds(1u8, 3),
3438 /// 5,
3439 /// Ceiling,
3440 /// );
3441 /// assert_eq!(quotient.to_string(), "9.50");
3442 /// assert_eq!(o, Greater);
3443 ///
3444 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3445 /// &Rational::from_unsigneds(1u8, 3),
3446 /// 5,
3447 /// Nearest,
3448 /// );
3449 /// assert_eq!(quotient.to_string(), "9.50");
3450 /// assert_eq!(o, Greater);
3451 ///
3452 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3453 /// &Rational::from_unsigneds(1u8, 3),
3454 /// 20,
3455 /// Floor,
3456 /// );
3457 /// assert_eq!(quotient.to_string(), "9.4247742");
3458 /// assert_eq!(o, Less);
3459 ///
3460 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3461 /// &Rational::from_unsigneds(1u8, 3),
3462 /// 20,
3463 /// Ceiling,
3464 /// );
3465 /// assert_eq!(quotient.to_string(), "9.4247894");
3466 /// assert_eq!(o, Greater);
3467 ///
3468 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_val_ref(
3469 /// &Rational::from_unsigneds(1u8, 3),
3470 /// 20,
3471 /// Nearest,
3472 /// );
3473 /// assert_eq!(quotient.to_string(), "9.4247742");
3474 /// assert_eq!(o, Less);
3475 /// ```
3476 #[inline]
3477 pub fn div_rational_prec_round_val_ref(
3478 mut self,
3479 other: &Rational,
3480 prec: u64,
3481 rm: RoundingMode,
3482 ) -> (Self, Ordering) {
3483 let o = self.div_rational_prec_round_assign_ref(other, prec, rm);
3484 (self, o)
3485 }
3486
3487 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
3488 /// with the specified rounding mode. The [`Float`] is taken by reference and the [`Rational`]
3489 /// by value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
3490 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
3491 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3492 ///
3493 /// See [`RoundingMode`] for a description of the possible rounding modes.
3494 ///
3495 /// $$
3496 /// f(x,y,p,m) = x/y+\varepsilon.
3497 /// $$
3498 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3499 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3500 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3501 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3502 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3503 ///
3504 /// If the output has a precision, it is `prec`.
3505 ///
3506 /// Special cases:
3507 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
3508 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
3509 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
3510 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
3511 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
3512 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
3513 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
3514 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
3515 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
3516 ///
3517 /// Overflow and underflow:
3518 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
3519 /// returned instead.
3520 /// - 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}$
3521 /// is returned instead, where `p` is the precision of the input.
3522 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
3523 /// returned instead.
3524 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
3525 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
3526 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
3527 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
3528 /// instead.
3529 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
3530 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
3531 /// instead.
3532 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
3533 /// instead.
3534 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
3535 /// instead.
3536 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
3537 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
3538 /// returned instead.
3539 ///
3540 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_ref_val`]
3541 /// instead. If you know that your target precision is the precision of the [`Float`] input,
3542 /// consider using [`Float::div_rational_round_ref_val`] instead. If both of these things are
3543 /// true, consider using `/` instead.
3544 ///
3545 /// # Worst-case complexity
3546 /// $T(n) = O(n \log n \log\log n)$
3547 ///
3548 /// $M(n) = O(n \log n)$
3549 ///
3550 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3551 /// other.significant_bits(), prec)`.
3552 ///
3553 /// # Panics
3554 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3555 ///
3556 /// # Examples
3557 /// ```
3558 /// use core::f64::consts::PI;
3559 /// use malachite_base::rounding_modes::RoundingMode::*;
3560 /// use malachite_float::Float;
3561 /// use malachite_q::Rational;
3562 /// use std::cmp::Ordering::*;
3563 ///
3564 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3565 /// Rational::from_unsigneds(1u8, 3),
3566 /// 5,
3567 /// Floor,
3568 /// );
3569 /// assert_eq!(quotient.to_string(), "9.00");
3570 /// assert_eq!(o, Less);
3571 ///
3572 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3573 /// Rational::from_unsigneds(1u8, 3),
3574 /// 5,
3575 /// Ceiling,
3576 /// );
3577 /// assert_eq!(quotient.to_string(), "9.50");
3578 /// assert_eq!(o, Greater);
3579 ///
3580 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3581 /// Rational::from_unsigneds(1u8, 3),
3582 /// 5,
3583 /// Nearest,
3584 /// );
3585 /// assert_eq!(quotient.to_string(), "9.50");
3586 /// assert_eq!(o, Greater);
3587 ///
3588 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3589 /// Rational::from_unsigneds(1u8, 3),
3590 /// 20,
3591 /// Floor,
3592 /// );
3593 /// assert_eq!(quotient.to_string(), "9.4247742");
3594 /// assert_eq!(o, Less);
3595 ///
3596 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3597 /// Rational::from_unsigneds(1u8, 3),
3598 /// 20,
3599 /// Ceiling,
3600 /// );
3601 /// assert_eq!(quotient.to_string(), "9.4247894");
3602 /// assert_eq!(o, Greater);
3603 ///
3604 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_val(
3605 /// Rational::from_unsigneds(1u8, 3),
3606 /// 20,
3607 /// Nearest,
3608 /// );
3609 /// assert_eq!(quotient.to_string(), "9.4247742");
3610 /// assert_eq!(o, Less);
3611 /// ```
3612 #[inline]
3613 pub fn div_rational_prec_round_ref_val(
3614 &self,
3615 other: Rational,
3616 prec: u64,
3617 rm: RoundingMode,
3618 ) -> (Self, Ordering) {
3619 if !self.is_normal()
3620 || max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD
3621 {
3622 div_rational_prec_round_naive_ref_val(self, other, prec, rm)
3623 } else {
3624 div_rational_prec_round_direct_ref_val(self, other, prec, rm)
3625 }
3626 }
3627
3628 /// Divides a [`Float`] by a [`Rational`], rounding the result to the specified precision and
3629 /// with the specified rounding mode. The [`Float`] and the [`Rational`] are both taken by
3630 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
3631 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
3632 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3633 ///
3634 /// See [`RoundingMode`] for a description of the possible rounding modes.
3635 ///
3636 /// $$
3637 /// f(x,y,p,m) = x/y+\varepsilon.
3638 /// $$
3639 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3640 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3641 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
3642 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3643 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3644 ///
3645 /// If the output has a precision, it is `prec`.
3646 ///
3647 /// Special cases:
3648 /// - $f(\text{NaN},x,p,m)=f(\pm\infty,0,p,m)=f(\pm0.0,0,p,m)=\text{NaN}$
3649 /// - $f(\infty,x,p,m)=\infty$ if $x\geq 0$
3650 /// - $f(\infty,x,p,m)=-\infty$ if $x<0$
3651 /// - $f(-\infty,x,p,m)=-\infty$ if $x\geq 0$
3652 /// - $f(-\infty,x,p,m)=\infty$ if $x<0$
3653 /// - $f(0.0,x,p,m)=0.0$ if $x>0$
3654 /// - $f(0.0,x,p,m)=-0.0$ if $x<0$
3655 /// - $f(-0.0,x,p,m)=-0.0$ if $x>0$
3656 /// - $f(-0.0,x,p,m)=0.0$ if $x<0$
3657 ///
3658 /// Overflow and underflow:
3659 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
3660 /// returned instead.
3661 /// - 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}$
3662 /// is returned instead, where `p` is the precision of the input.
3663 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
3664 /// returned instead.
3665 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
3666 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
3667 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
3668 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
3669 /// instead.
3670 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
3671 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
3672 /// instead.
3673 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
3674 /// instead.
3675 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
3676 /// instead.
3677 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
3678 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
3679 /// returned instead.
3680 ///
3681 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_ref_ref`]
3682 /// instead. If you know that your target precision is the precision of the [`Float`] input,
3683 /// consider using [`Float::div_rational_round_ref_ref`] instead. If both of these things are
3684 /// true, consider using `/` instead.
3685 ///
3686 /// # Worst-case complexity
3687 /// $T(n) = O(n \log n \log\log n)$
3688 ///
3689 /// $M(n) = O(n \log n)$
3690 ///
3691 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3692 /// other.significant_bits(), prec)`.
3693 ///
3694 /// # Panics
3695 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
3696 ///
3697 /// # Examples
3698 /// ```
3699 /// use core::f64::consts::PI;
3700 /// use malachite_base::rounding_modes::RoundingMode::*;
3701 /// use malachite_float::Float;
3702 /// use malachite_q::Rational;
3703 /// use std::cmp::Ordering::*;
3704 ///
3705 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3706 /// &Rational::from_unsigneds(1u8, 3),
3707 /// 5,
3708 /// Floor,
3709 /// );
3710 /// assert_eq!(quotient.to_string(), "9.00");
3711 /// assert_eq!(o, Less);
3712 ///
3713 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3714 /// &Rational::from_unsigneds(1u8, 3),
3715 /// 5,
3716 /// Ceiling,
3717 /// );
3718 /// assert_eq!(quotient.to_string(), "9.50");
3719 /// assert_eq!(o, Greater);
3720 ///
3721 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3722 /// &Rational::from_unsigneds(1u8, 3),
3723 /// 5,
3724 /// Nearest,
3725 /// );
3726 /// assert_eq!(quotient.to_string(), "9.50");
3727 /// assert_eq!(o, Greater);
3728 ///
3729 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3730 /// &Rational::from_unsigneds(1u8, 3),
3731 /// 20,
3732 /// Floor,
3733 /// );
3734 /// assert_eq!(quotient.to_string(), "9.4247742");
3735 /// assert_eq!(o, Less);
3736 ///
3737 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3738 /// &Rational::from_unsigneds(1u8, 3),
3739 /// 20,
3740 /// Ceiling,
3741 /// );
3742 /// assert_eq!(quotient.to_string(), "9.4247894");
3743 /// assert_eq!(o, Greater);
3744 ///
3745 /// let (quotient, o) = Float::from(PI).div_rational_prec_round_ref_ref(
3746 /// &Rational::from_unsigneds(1u8, 3),
3747 /// 20,
3748 /// Nearest,
3749 /// );
3750 /// assert_eq!(quotient.to_string(), "9.4247742");
3751 /// assert_eq!(o, Less);
3752 /// ```
3753 #[inline]
3754 pub fn div_rational_prec_round_ref_ref(
3755 &self,
3756 other: &Rational,
3757 prec: u64,
3758 rm: RoundingMode,
3759 ) -> (Self, Ordering) {
3760 if !self.is_normal()
3761 || max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD
3762 {
3763 div_rational_prec_round_naive_ref_ref(self, other, prec, rm)
3764 } else {
3765 div_rational_prec_round_direct_ref_ref(self, other, prec, rm)
3766 }
3767 }
3768
3769 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3770 /// specified precision. The [`Float`] and the [`Rational`] are both are taken by value. An
3771 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3772 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3773 /// whenever this function returns a `NaN` it also returns `Equal`.
3774 ///
3775 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3776 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3777 /// description of the `Nearest` rounding mode.
3778 ///
3779 /// $$
3780 /// f(x,y,p) = x/y+\varepsilon.
3781 /// $$
3782 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3783 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3784 ///
3785 /// If the output has a precision, it is `prec`.
3786 ///
3787 /// Special cases:
3788 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
3789 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
3790 /// - $f(\infty,x,p)=-\infty$ if $x<0$
3791 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
3792 /// - $f(-\infty,x,p)=\infty$ if $x<0$
3793 /// - $f(0.0,x,p)=0.0$ if $x>0$
3794 /// - $f(0.0,x,p)=-0.0$ if $x<0$
3795 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
3796 /// - $f(-0.0,x,p)=0.0$ if $x<0$
3797 ///
3798 /// Overflow and underflow:
3799 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
3800 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
3801 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3802 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3803 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3804 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3805 ///
3806 /// If you want to use a rounding mode other than `Nearest`, consider using
3807 /// [`Float::div_rational_prec_round`] instead. If you know that your target precision is the
3808 /// precision of the [`Float`] input, consider using `/` instead.
3809 ///
3810 /// # Worst-case complexity
3811 /// $T(n) = O(n \log n \log\log n)$
3812 ///
3813 /// $M(n) = O(n \log n)$
3814 ///
3815 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3816 /// other.significant_bits(), prec)`.
3817 ///
3818 /// # Examples
3819 /// ```
3820 /// use core::f64::consts::PI;
3821 /// use malachite_base::num::conversion::traits::ExactFrom;
3822 /// use malachite_float::Float;
3823 /// use malachite_q::Rational;
3824 /// use std::cmp::Ordering::*;
3825 ///
3826 /// let (quotient, o) = Float::from(PI).div_rational_prec(Rational::exact_from(1.5), 5);
3827 /// assert_eq!(quotient.to_string(), "2.12");
3828 /// assert_eq!(o, Greater);
3829 ///
3830 /// let (quotient, o) = Float::from(PI).div_rational_prec(Rational::exact_from(1.5), 20);
3831 /// assert_eq!(quotient.to_string(), "2.0943947");
3832 /// assert_eq!(o, Less);
3833 /// ```
3834 #[inline]
3835 pub fn div_rational_prec(self, other: Rational, prec: u64) -> (Self, Ordering) {
3836 self.div_rational_prec_round(other, prec, Nearest)
3837 }
3838
3839 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3840 /// specified precision. The [`Float`] is taken by value and the [`Rational`] by reference. An
3841 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3842 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3843 /// whenever this function returns a `NaN` it also returns `Equal`.
3844 ///
3845 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3846 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3847 /// description of the `Nearest` rounding mode.
3848 ///
3849 /// $$
3850 /// f(x,y,p) = x/y+\varepsilon.
3851 /// $$
3852 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3853 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3854 ///
3855 /// If the output has a precision, it is `prec`.
3856 ///
3857 /// Special cases:
3858 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
3859 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
3860 /// - $f(\infty,x,p)=-\infty$ if $x<0$
3861 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
3862 /// - $f(-\infty,x,p)=\infty$ if $x<0$
3863 /// - $f(0.0,x,p)=0.0$ if $x>0$
3864 /// - $f(0.0,x,p)=-0.0$ if $x<0$
3865 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
3866 /// - $f(-0.0,x,p)=0.0$ if $x<0$
3867 ///
3868 /// Overflow and underflow:
3869 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
3870 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
3871 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3872 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3873 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3874 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3875 ///
3876 /// If you want to use a rounding mode other than `Nearest`, consider using
3877 /// [`Float::div_rational_prec_round_val_ref`] instead. If you know that your target precision
3878 /// is the precision of the [`Float`] input, consider using `/` instead.
3879 ///
3880 /// # Worst-case complexity
3881 /// $T(n) = O(n \log n \log\log n)$
3882 ///
3883 /// $M(n) = O(n \log n)$
3884 ///
3885 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3886 /// other.significant_bits(), prec)`.
3887 ///
3888 /// # Examples
3889 /// ```
3890 /// use core::f64::consts::PI;
3891 /// use malachite_base::num::conversion::traits::ExactFrom;
3892 /// use malachite_float::Float;
3893 /// use malachite_q::Rational;
3894 /// use std::cmp::Ordering::*;
3895 ///
3896 /// let (quotient, o) =
3897 /// Float::from(PI).div_rational_prec_val_ref(&Rational::exact_from(1.5), 5);
3898 /// assert_eq!(quotient.to_string(), "2.12");
3899 /// assert_eq!(o, Greater);
3900 ///
3901 /// let (quotient, o) =
3902 /// Float::from(PI).div_rational_prec_val_ref(&Rational::exact_from(1.5), 20);
3903 /// assert_eq!(quotient.to_string(), "2.0943947");
3904 /// assert_eq!(o, Less);
3905 /// ```
3906 #[inline]
3907 pub fn div_rational_prec_val_ref(self, other: &Rational, prec: u64) -> (Self, Ordering) {
3908 self.div_rational_prec_round_val_ref(other, prec, Nearest)
3909 }
3910
3911 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3912 /// specified precision. The [`Float`] is taken by reference and the [`Rational`] by value. An
3913 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3914 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3915 /// whenever this function returns a `NaN` it also returns `Equal`.
3916 ///
3917 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3918 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3919 /// description of the `Nearest` rounding mode.
3920 ///
3921 /// $$
3922 /// f(x,y,p) = x/y+\varepsilon.
3923 /// $$
3924 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3925 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3926 ///
3927 /// If the output has a precision, it is `prec`.
3928 ///
3929 /// Special cases:
3930 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
3931 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
3932 /// - $f(\infty,x,p)=-\infty$ if $x<0$
3933 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
3934 /// - $f(-\infty,x,p)=\infty$ if $x<0$
3935 /// - $f(0.0,x,p)=0.0$ if $x>0$
3936 /// - $f(0.0,x,p)=-0.0$ if $x<0$
3937 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
3938 /// - $f(-0.0,x,p)=0.0$ if $x<0$
3939 ///
3940 /// Overflow and underflow:
3941 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
3942 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
3943 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
3944 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
3945 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
3946 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
3947 ///
3948 /// If you want to use a rounding mode other than `Nearest`, consider using
3949 /// [`Float::div_rational_prec_round_ref_val`] instead. If you know that your target precision
3950 /// is the precision of the [`Float`] input, consider using `/` instead.
3951 ///
3952 /// # Worst-case complexity
3953 /// $T(n) = O(n \log n \log\log n)$
3954 ///
3955 /// $M(n) = O(n \log n)$
3956 ///
3957 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3958 /// other.significant_bits(), prec)`.
3959 ///
3960 /// # Examples
3961 /// ```
3962 /// use core::f64::consts::PI;
3963 /// use malachite_base::num::conversion::traits::ExactFrom;
3964 /// use malachite_float::Float;
3965 /// use malachite_q::Rational;
3966 /// use std::cmp::Ordering::*;
3967 ///
3968 /// let (quotient, o) = Float::from(PI).div_rational_prec_ref_val(Rational::exact_from(1.5), 5);
3969 /// assert_eq!(quotient.to_string(), "2.12");
3970 /// assert_eq!(o, Greater);
3971 ///
3972 /// let (quotient, o) =
3973 /// Float::from(PI).div_rational_prec_ref_val(Rational::exact_from(1.5), 20);
3974 /// assert_eq!(quotient.to_string(), "2.0943947");
3975 /// assert_eq!(o, Less);
3976 /// ```
3977 #[inline]
3978 pub fn div_rational_prec_ref_val(&self, other: Rational, prec: u64) -> (Self, Ordering) {
3979 self.div_rational_prec_round_ref_val(other, prec, Nearest)
3980 }
3981
3982 /// Divides a [`Float`] by a [`Rational`], rounding the result to the nearest value of the
3983 /// specified precision. The [`Float`] and the [`Rational`] are both are taken by reference. An
3984 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
3985 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
3986 /// whenever this function returns a `NaN` it also returns `Equal`.
3987 ///
3988 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
3989 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
3990 /// description of the `Nearest` rounding mode.
3991 ///
3992 /// $$
3993 /// f(x,y,p) = x/y+\varepsilon.
3994 /// $$
3995 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3996 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
3997 ///
3998 /// If the output has a precision, it is `prec`.
3999 ///
4000 /// Special cases:
4001 /// - $f(\text{NaN},x,p)=f(\pm\infty,0,p)=f(\pm0.0,0,p)=\text{NaN}$
4002 /// - $f(\infty,x,p)=\infty$ if $x\geq 0$
4003 /// - $f(\infty,x,p)=-\infty$ if $x<0$
4004 /// - $f(-\infty,x,p)=-\infty$ if $x\geq 0$
4005 /// - $f(-\infty,x,p)=\infty$ if $x<0$
4006 /// - $f(0.0,x,p)=0.0$ if $x>0$
4007 /// - $f(0.0,x,p)=-0.0$ if $x<0$
4008 /// - $f(-0.0,x,p)=-0.0$ if $x>0$
4009 /// - $f(-0.0,x,p)=0.0$ if $x<0$
4010 ///
4011 /// Overflow and underflow:
4012 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
4013 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
4014 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
4015 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
4016 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
4017 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
4018 ///
4019 /// If you want to use a rounding mode other than `Nearest`, consider using
4020 /// [`Float::div_rational_prec_round_ref_ref`] instead. If you know that your target precision
4021 /// is the precision of the [`Float`] input, consider using `/` instead.
4022 ///
4023 /// # Worst-case complexity
4024 /// $T(n) = O(n \log n \log\log n)$
4025 ///
4026 /// $M(n) = O(n \log n)$
4027 ///
4028 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4029 /// other.significant_bits(), prec)`.
4030 ///
4031 /// # Examples
4032 /// ```
4033 /// use core::f64::consts::PI;
4034 /// use malachite_base::num::conversion::traits::ExactFrom;
4035 /// use malachite_float::Float;
4036 /// use malachite_q::Rational;
4037 /// use std::cmp::Ordering::*;
4038 ///
4039 /// let (quotient, o) =
4040 /// Float::from(PI).div_rational_prec_ref_ref(&Rational::exact_from(1.5), 5);
4041 /// assert_eq!(quotient.to_string(), "2.12");
4042 /// assert_eq!(o, Greater);
4043 ///
4044 /// let (quotient, o) =
4045 /// Float::from(PI).div_rational_prec_ref_ref(&Rational::exact_from(1.5), 20);
4046 /// assert_eq!(quotient.to_string(), "2.0943947");
4047 /// assert_eq!(o, Less);
4048 /// ```
4049 #[inline]
4050 pub fn div_rational_prec_ref_ref(&self, other: &Rational, prec: u64) -> (Self, Ordering) {
4051 self.div_rational_prec_round_ref_ref(other, prec, Nearest)
4052 }
4053
4054 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
4055 /// The [`Float`] and the [`Rational`] are both are taken by value. An [`Ordering`] is also
4056 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4057 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4058 /// function returns a `NaN` it also returns `Equal`.
4059 ///
4060 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4061 /// for a description of the possible rounding modes.
4062 ///
4063 /// $$
4064 /// f(x,y,m) = x/y+\varepsilon.
4065 /// $$
4066 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4067 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4068 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4069 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4070 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4071 ///
4072 /// If the output has a precision, it is the precision of the [`Float`] input.
4073 ///
4074 /// Special cases:
4075 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
4076 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
4077 /// - $f(\infty,x,m)=-\infty$ if $x<0$
4078 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
4079 /// - $f(-\infty,x,m)=\infty$ if $x<0$
4080 /// - $f(0.0,x,m)=0.0$ if $x>0$
4081 /// - $f(0.0,x,m)=-0.0$ if $x<0$
4082 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
4083 /// - $f(-0.0,x,m)=0.0$ if $x<0$
4084 ///
4085 /// Overflow and underflow:
4086 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
4087 /// returned instead.
4088 /// - 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
4089 /// returned instead, where `p` is the precision of the input.
4090 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
4091 /// returned instead.
4092 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
4093 /// is returned instead, where `p` is the precision of the input.
4094 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
4095 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
4096 /// instead.
4097 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
4098 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
4099 /// instead.
4100 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
4101 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
4102 /// instead.
4103 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
4104 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
4105 /// returned instead.
4106 ///
4107 /// If you want to specify an output precision, consider using
4108 /// [`Float::div_rational_prec_round`] instead. If you know you'll be using the `Nearest`
4109 /// rounding mode, consider using `/` instead.
4110 ///
4111 /// # Worst-case complexity
4112 /// $T(n) = O(n \log n \log\log n)$
4113 ///
4114 /// $M(n) = O(n \log n)$
4115 ///
4116 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4117 /// other.significant_bits())`.
4118 ///
4119 /// # Panics
4120 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4121 /// represent the output.
4122 ///
4123 /// # Examples
4124 /// ```
4125 /// use core::f64::consts::PI;
4126 /// use malachite_base::rounding_modes::RoundingMode::*;
4127 /// use malachite_float::Float;
4128 /// use malachite_q::Rational;
4129 /// use std::cmp::Ordering::*;
4130 ///
4131 /// let (quotient, o) =
4132 /// Float::from(PI).div_rational_round(Rational::from_unsigneds(1u8, 3), Floor);
4133 /// assert_eq!(quotient.to_string(), "9.4247779607693758");
4134 /// assert_eq!(o, Less);
4135 ///
4136 /// let (quotient, o) =
4137 /// Float::from(PI).div_rational_round(Rational::from_unsigneds(1u8, 3), Ceiling);
4138 /// assert_eq!(quotient.to_string(), "9.4247779607693900");
4139 /// assert_eq!(o, Greater);
4140 ///
4141 /// let (quotient, o) =
4142 /// Float::from(PI).div_rational_round(Rational::from_unsigneds(1u8, 3), Nearest);
4143 /// assert_eq!(quotient.to_string(), "9.4247779607693758");
4144 /// assert_eq!(o, Less);
4145 /// ```
4146 #[inline]
4147 pub fn div_rational_round(self, other: Rational, rm: RoundingMode) -> (Self, Ordering) {
4148 let prec = self.significant_bits();
4149 self.div_rational_prec_round(other, prec, rm)
4150 }
4151
4152 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
4153 /// The [`Float`] is taken by value and the [`Rational`] by reference. An [`Ordering`] is also
4154 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4155 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4156 /// function returns a `NaN` it also returns `Equal`.
4157 ///
4158 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4159 /// for a description of the possible rounding modes.
4160 ///
4161 /// $$
4162 /// f(x,y,m) = x/y+\varepsilon.
4163 /// $$
4164 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4165 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4166 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4167 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4168 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4169 ///
4170 /// If the output has a precision, it is the precision of the [`Float`] input.
4171 ///
4172 /// Special cases:
4173 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
4174 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
4175 /// - $f(\infty,x,m)=-\infty$ if $x<0$
4176 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
4177 /// - $f(-\infty,x,m)=\infty$ if $x<0$
4178 /// - $f(0.0,x,m)=0.0$ if $x>0$
4179 /// - $f(0.0,x,m)=-0.0$ if $x<0$
4180 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
4181 /// - $f(-0.0,x,m)=0.0$ if $x<0$
4182 ///
4183 /// Overflow and underflow:
4184 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
4185 /// returned instead.
4186 /// - 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
4187 /// returned instead, where `p` is the precision of the input.
4188 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
4189 /// returned instead.
4190 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
4191 /// is returned instead, where `p` is the precision of the input.
4192 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
4193 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
4194 /// instead.
4195 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
4196 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
4197 /// instead.
4198 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
4199 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
4200 /// instead.
4201 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
4202 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
4203 /// returned instead.
4204 ///
4205 /// If you want to specify an output precision, consider using
4206 /// [`Float::div_rational_prec_round_val_ref`] instead. If you know you'll be using the
4207 /// `Nearest` rounding mode, consider using `/` instead.
4208 ///
4209 /// # Worst-case complexity
4210 /// $T(n) = O(n \log n \log\log n)$
4211 ///
4212 /// $M(n) = O(n \log n)$
4213 ///
4214 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4215 /// other.significant_bits())`.
4216 ///
4217 /// # Panics
4218 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4219 /// represent the output.
4220 ///
4221 /// # Examples
4222 /// ```
4223 /// use core::f64::consts::PI;
4224 /// use malachite_base::rounding_modes::RoundingMode::*;
4225 /// use malachite_float::Float;
4226 /// use malachite_q::Rational;
4227 /// use std::cmp::Ordering::*;
4228 ///
4229 /// let (quotient, o) =
4230 /// Float::from(PI).div_rational_round_val_ref(&Rational::from_unsigneds(1u8, 3), Floor);
4231 /// assert_eq!(quotient.to_string(), "9.4247779607693758");
4232 /// assert_eq!(o, Less);
4233 ///
4234 /// let (quotient, o) =
4235 /// Float::from(PI).div_rational_round_val_ref(&Rational::from_unsigneds(1u8, 3), Ceiling);
4236 /// assert_eq!(quotient.to_string(), "9.4247779607693900");
4237 /// assert_eq!(o, Greater);
4238 ///
4239 /// let (quotient, o) =
4240 /// Float::from(PI).div_rational_round_val_ref(&Rational::from_unsigneds(1u8, 3), Nearest);
4241 /// assert_eq!(quotient.to_string(), "9.4247779607693758");
4242 /// assert_eq!(o, Less);
4243 /// ```
4244 #[inline]
4245 pub fn div_rational_round_val_ref(
4246 self,
4247 other: &Rational,
4248 rm: RoundingMode,
4249 ) -> (Self, Ordering) {
4250 let prec = self.significant_bits();
4251 self.div_rational_prec_round_val_ref(other, prec, rm)
4252 }
4253
4254 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
4255 /// The [`Float`] is taken by reference and the [`Rational`] by value. An [`Ordering`] is also
4256 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4257 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4258 /// function returns a `NaN` it also returns `Equal`.
4259 ///
4260 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4261 /// for a description of the possible rounding modes.
4262 ///
4263 /// $$
4264 /// f(x,y,m) = x/y+\varepsilon.
4265 /// $$
4266 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4267 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4268 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4269 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4270 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4271 ///
4272 /// If the output has a precision, it is the precision of the [`Float`] input.
4273 ///
4274 /// Special cases:
4275 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
4276 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
4277 /// - $f(\infty,x,m)=-\infty$ if $x<0$
4278 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
4279 /// - $f(-\infty,x,m)=\infty$ if $x<0$
4280 /// - $f(0.0,x,m)=0.0$ if $x>0$
4281 /// - $f(0.0,x,m)=-0.0$ if $x<0$
4282 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
4283 /// - $f(-0.0,x,m)=0.0$ if $x<0$
4284 ///
4285 /// Overflow and underflow:
4286 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
4287 /// returned instead.
4288 /// - 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
4289 /// returned instead, where `p` is the precision of the input.
4290 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
4291 /// returned instead.
4292 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
4293 /// is returned instead, where `p` is the precision of the input.
4294 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
4295 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
4296 /// instead.
4297 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
4298 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
4299 /// instead.
4300 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
4301 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
4302 /// instead.
4303 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
4304 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
4305 /// returned instead.
4306 ///
4307 /// If you want to specify an output precision, consider using
4308 /// [`Float::div_rational_prec_round_ref_val`] instead. If you know you'll be using the
4309 /// `Nearest` rounding mode, consider using `/` instead.
4310 ///
4311 /// # Worst-case complexity
4312 /// $T(n) = O(n \log n \log\log n)$
4313 ///
4314 /// $M(n) = O(n \log n)$
4315 ///
4316 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4317 /// other.significant_bits())`.
4318 ///
4319 /// # Panics
4320 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4321 /// represent the output.
4322 ///
4323 /// # Examples
4324 /// ```
4325 /// use core::f64::consts::PI;
4326 /// use malachite_base::rounding_modes::RoundingMode::*;
4327 /// use malachite_float::Float;
4328 /// use malachite_q::Rational;
4329 /// use std::cmp::Ordering::*;
4330 ///
4331 /// let (quotient, o) =
4332 /// Float::from(PI).div_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Floor);
4333 /// assert_eq!(quotient.to_string(), "9.4247779607693758");
4334 /// assert_eq!(o, Less);
4335 ///
4336 /// let (quotient, o) =
4337 /// Float::from(PI).div_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Ceiling);
4338 /// assert_eq!(quotient.to_string(), "9.4247779607693900");
4339 /// assert_eq!(o, Greater);
4340 ///
4341 /// let (quotient, o) =
4342 /// Float::from(PI).div_rational_round_ref_val(Rational::from_unsigneds(1u8, 3), Nearest);
4343 /// assert_eq!(quotient.to_string(), "9.4247779607693758");
4344 /// assert_eq!(o, Less);
4345 /// ```
4346 #[inline]
4347 pub fn div_rational_round_ref_val(
4348 &self,
4349 other: Rational,
4350 rm: RoundingMode,
4351 ) -> (Self, Ordering) {
4352 let prec = self.significant_bits();
4353 self.div_rational_prec_round_ref_val(other, prec, rm)
4354 }
4355
4356 /// Divides a [`Float`] by a [`Rational`], rounding the result with the specified rounding mode.
4357 /// The [`Float`] and the [`Rational`] are both are taken by reference. An [`Ordering`] is also
4358 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4359 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4360 /// function returns a `NaN` it also returns `Equal`.
4361 ///
4362 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
4363 /// for a description of the possible rounding modes.
4364 ///
4365 /// $$
4366 /// f(x,y,m) = x/y+\varepsilon.
4367 /// $$
4368 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4369 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4370 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4371 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4372 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4373 ///
4374 /// If the output has a precision, it is the precision of the [`Float`] input.
4375 ///
4376 /// Special cases:
4377 /// - $f(\text{NaN},x,m)=f(\pm\infty,0,m)=f(\pm0.0,0,m)=\text{NaN}$
4378 /// - $f(\infty,x,m)=\infty$ if $x\geq 0$
4379 /// - $f(\infty,x,m)=-\infty$ if $x<0$
4380 /// - $f(-\infty,x,m)=-\infty$ if $x\geq 0$
4381 /// - $f(-\infty,x,m)=\infty$ if $x<0$
4382 /// - $f(0.0,x,m)=0.0$ if $x>0$
4383 /// - $f(0.0,x,m)=-0.0$ if $x<0$
4384 /// - $f(-0.0,x,m)=-0.0$ if $x>0$
4385 /// - $f(-0.0,x,m)=0.0$ if $x<0$
4386 ///
4387 /// Overflow and underflow:
4388 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
4389 /// returned instead.
4390 /// - 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
4391 /// returned instead, where `p` is the precision of the input.
4392 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
4393 /// returned instead.
4394 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
4395 /// is returned instead, where `p` is the precision of the input.
4396 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
4397 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
4398 /// instead.
4399 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
4400 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
4401 /// instead.
4402 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
4403 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
4404 /// instead.
4405 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
4406 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
4407 /// returned instead.
4408 ///
4409 /// If you want to specify an output precision, consider using
4410 /// [`Float::div_rational_prec_round_ref_ref`] instead. If you know you'll be using the
4411 /// `Nearest` rounding mode, consider using `/` instead.
4412 ///
4413 /// # Worst-case complexity
4414 /// $T(n) = O(n \log n \log\log n)$
4415 ///
4416 /// $M(n) = O(n \log n)$
4417 ///
4418 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4419 /// other.significant_bits())`.
4420 ///
4421 /// # Panics
4422 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
4423 /// represent the output.
4424 ///
4425 /// # Examples
4426 /// ```
4427 /// use core::f64::consts::PI;
4428 /// use malachite_base::rounding_modes::RoundingMode::*;
4429 /// use malachite_float::Float;
4430 /// use malachite_q::Rational;
4431 /// use std::cmp::Ordering::*;
4432 ///
4433 /// let (quotient, o) =
4434 /// Float::from(PI).div_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Floor);
4435 /// assert_eq!(quotient.to_string(), "9.4247779607693758");
4436 /// assert_eq!(o, Less);
4437 ///
4438 /// let (quotient, o) =
4439 /// Float::from(PI).div_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Ceiling);
4440 /// assert_eq!(quotient.to_string(), "9.4247779607693900");
4441 /// assert_eq!(o, Greater);
4442 ///
4443 /// let (quotient, o) =
4444 /// Float::from(PI).div_rational_round_ref_ref(&Rational::from_unsigneds(1u8, 3), Nearest);
4445 /// assert_eq!(quotient.to_string(), "9.4247779607693758");
4446 /// assert_eq!(o, Less);
4447 /// ```
4448 #[inline]
4449 pub fn div_rational_round_ref_ref(
4450 &self,
4451 other: &Rational,
4452 rm: RoundingMode,
4453 ) -> (Self, Ordering) {
4454 let prec = self.significant_bits();
4455 self.div_rational_prec_round_ref_ref(other, prec, rm)
4456 }
4457
4458 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the specified
4459 /// precision and with the specified rounding mode. The [`Rational`] is taken by value. An
4460 /// [`Ordering`] is returned, indicating whether the rounded quotient is less than, equal to, or
4461 /// greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
4462 /// whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
4463 ///
4464 /// See [`RoundingMode`] for a description of the possible rounding modes.
4465 ///
4466 /// $$
4467 /// x \gets x/y+\varepsilon.
4468 /// $$
4469 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4470 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4471 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4472 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4473 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4474 ///
4475 /// If the output has a precision, it is `prec`.
4476 ///
4477 /// See the [`Float::div_rational_prec_round`] documentation for information on special cases,
4478 /// overflow, and underflow.
4479 ///
4480 /// If you know you'll be using `Nearest`, consider using [`Float::div_rational_prec_assign`]
4481 /// instead. If you know that your target precision is the precision of the [`Float`] input,
4482 /// consider using [`Float::div_rational_round_assign`] instead. If both of these things are
4483 /// true, consider using `/=` instead.
4484 ///
4485 /// # Worst-case complexity
4486 /// $T(n) = O(n \log n \log\log n)$
4487 ///
4488 /// $M(n) = O(n \log n)$
4489 ///
4490 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4491 /// other.significant_bits(), prec)`.
4492 ///
4493 /// # Panics
4494 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4495 ///
4496 /// # Examples
4497 /// ```
4498 /// use core::f64::consts::PI;
4499 /// use malachite_base::rounding_modes::RoundingMode::*;
4500 /// use malachite_float::Float;
4501 /// use malachite_q::Rational;
4502 /// use std::cmp::Ordering::*;
4503 ///
4504 /// let mut x = Float::from(PI);
4505 /// assert_eq!(
4506 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 5, Floor),
4507 /// Less
4508 /// );
4509 /// assert_eq!(x.to_string(), "9.00");
4510 ///
4511 /// let mut x = Float::from(PI);
4512 /// assert_eq!(
4513 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 5, Ceiling),
4514 /// Greater
4515 /// );
4516 /// assert_eq!(x.to_string(), "9.50");
4517 ///
4518 /// let mut x = Float::from(PI);
4519 /// assert_eq!(
4520 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 5, Nearest),
4521 /// Greater
4522 /// );
4523 /// assert_eq!(x.to_string(), "9.50");
4524 ///
4525 /// let mut x = Float::from(PI);
4526 /// assert_eq!(
4527 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 20, Floor),
4528 /// Less
4529 /// );
4530 /// assert_eq!(x.to_string(), "9.4247742");
4531 ///
4532 /// let mut x = Float::from(PI);
4533 /// assert_eq!(
4534 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 20, Ceiling),
4535 /// Greater
4536 /// );
4537 /// assert_eq!(x.to_string(), "9.4247894");
4538 ///
4539 /// let mut x = Float::from(PI);
4540 /// assert_eq!(
4541 /// x.div_rational_prec_round_assign(Rational::from_unsigneds(1u8, 3), 20, Nearest),
4542 /// Less
4543 /// );
4544 /// assert_eq!(x.to_string(), "9.4247742");
4545 /// ```
4546 #[inline]
4547 pub fn div_rational_prec_round_assign(
4548 &mut self,
4549 other: Rational,
4550 prec: u64,
4551 rm: RoundingMode,
4552 ) -> Ordering {
4553 if !self.is_normal()
4554 || max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD
4555 {
4556 div_rational_prec_round_assign_naive(self, other, prec, rm)
4557 } else {
4558 div_rational_prec_round_assign_direct(self, other, prec, rm)
4559 }
4560 }
4561
4562 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the specified
4563 /// precision and with the specified rounding mode. The [`Rational`] is taken by reference. An
4564 /// [`Ordering`] is returned, indicating whether the rounded quotient is less than, equal to, or
4565 /// greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
4566 /// whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
4567 ///
4568 /// See [`RoundingMode`] for a description of the possible rounding modes.
4569 ///
4570 /// $$
4571 /// x \gets x/y+\varepsilon.
4572 /// $$
4573 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4574 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4575 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4576 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4577 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4578 ///
4579 /// If the output has a precision, it is `prec`.
4580 ///
4581 /// See the [`Float::div_rational_prec_round`] documentation for information on special cases,
4582 /// overflow, and underflow.
4583 ///
4584 /// If you know you'll be using `Nearest`, consider using
4585 /// [`Float::div_rational_prec_assign_ref`] instead. If you know that your target precision is
4586 /// the precision of the [`Float`] input, consider using
4587 /// [`Float::div_rational_round_assign_ref`] instead. If both of these things are true, consider
4588 /// using `/=` instead.
4589 ///
4590 /// # Worst-case complexity
4591 /// $T(n) = O(n \log n \log\log n)$
4592 ///
4593 /// $M(n) = O(n \log n)$
4594 ///
4595 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4596 /// other.significant_bits(), prec)`.
4597 ///
4598 /// # Panics
4599 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
4600 ///
4601 /// # Examples
4602 /// ```
4603 /// use core::f64::consts::PI;
4604 /// use malachite_base::rounding_modes::RoundingMode::*;
4605 /// use malachite_float::Float;
4606 /// use malachite_q::Rational;
4607 /// use std::cmp::Ordering::*;
4608 ///
4609 /// let mut x = Float::from(PI);
4610 /// assert_eq!(
4611 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 5, Floor),
4612 /// Less
4613 /// );
4614 /// assert_eq!(x.to_string(), "9.00");
4615 ///
4616 /// let mut x = Float::from(PI);
4617 /// assert_eq!(
4618 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 5, Ceiling),
4619 /// Greater
4620 /// );
4621 /// assert_eq!(x.to_string(), "9.50");
4622 ///
4623 /// let mut x = Float::from(PI);
4624 /// assert_eq!(
4625 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 5, Nearest),
4626 /// Greater
4627 /// );
4628 /// assert_eq!(x.to_string(), "9.50");
4629 ///
4630 /// let mut x = Float::from(PI);
4631 /// assert_eq!(
4632 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 20, Floor),
4633 /// Less
4634 /// );
4635 /// assert_eq!(x.to_string(), "9.4247742");
4636 ///
4637 /// let mut x = Float::from(PI);
4638 /// assert_eq!(
4639 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 20, Ceiling),
4640 /// Greater
4641 /// );
4642 /// assert_eq!(x.to_string(), "9.4247894");
4643 ///
4644 /// let mut x = Float::from(PI);
4645 /// assert_eq!(
4646 /// x.div_rational_prec_round_assign_ref(&Rational::from_unsigneds(1u8, 3), 20, Nearest),
4647 /// Less
4648 /// );
4649 /// assert_eq!(x.to_string(), "9.4247742");
4650 /// ```
4651 #[inline]
4652 pub fn div_rational_prec_round_assign_ref(
4653 &mut self,
4654 other: &Rational,
4655 prec: u64,
4656 rm: RoundingMode,
4657 ) -> Ordering {
4658 if !self.is_normal()
4659 || max(self.complexity(), other.significant_bits()) < DIV_RATIONAL_THRESHOLD
4660 {
4661 div_rational_prec_round_assign_naive_ref(self, other, prec, rm)
4662 } else {
4663 div_rational_prec_round_assign_direct_ref(self, other, prec, rm)
4664 }
4665 }
4666
4667 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the nearest value of
4668 /// the specified precision. The [`Rational`] is taken by value. An [`Ordering`] is returned,
4669 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
4670 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
4671 /// the [`Float`] to `NaN` it also returns `Equal`.
4672 ///
4673 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4674 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4675 /// description of the `Nearest` rounding mode.
4676 ///
4677 /// $$
4678 /// x \gets x/y+\varepsilon.
4679 /// $$
4680 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4681 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4682 ///
4683 /// If the output has a precision, it is `prec`.
4684 ///
4685 /// See the [`Float::div_rational_prec`] documentation for information on special cases,
4686 /// overflow, and underflow.
4687 ///
4688 /// If you want to use a rounding mode other than `Nearest`, consider using
4689 /// [`Float::div_rational_prec_round_assign`] instead. If you know that your target precision is
4690 /// the maximum of the precisions of the two inputs, consider using `/=` instead.
4691 ///
4692 /// # Worst-case complexity
4693 /// $T(n) = O(n \log n \log\log n)$
4694 ///
4695 /// $M(n) = O(n \log n)$
4696 ///
4697 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4698 /// other.significant_bits(), prec)`.
4699 ///
4700 /// # Examples
4701 /// ```
4702 /// use core::f64::consts::PI;
4703 /// use malachite_base::num::conversion::traits::ExactFrom;
4704 /// use malachite_float::Float;
4705 /// use malachite_q::Rational;
4706 /// use std::cmp::Ordering::*;
4707 ///
4708 /// let mut x = Float::from(PI);
4709 /// assert_eq!(
4710 /// x.div_rational_prec_assign(Rational::exact_from(1.5), 5),
4711 /// Greater
4712 /// );
4713 /// assert_eq!(x.to_string(), "2.12");
4714 ///
4715 /// let mut x = Float::from(PI);
4716 /// assert_eq!(
4717 /// x.div_rational_prec_assign(Rational::exact_from(1.5), 20),
4718 /// Less
4719 /// );
4720 /// assert_eq!(x.to_string(), "2.0943947");
4721 /// ```
4722 #[inline]
4723 pub fn div_rational_prec_assign(&mut self, other: Rational, prec: u64) -> Ordering {
4724 self.div_rational_prec_round_assign(other, prec, Nearest)
4725 }
4726
4727 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result to the nearest value of
4728 /// the specified precision. The [`Rational`] is taken by reference. An [`Ordering`] is
4729 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
4730 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
4731 /// function sets the [`Float`] to `NaN` it also returns `Equal`.
4732 ///
4733 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
4734 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
4735 /// description of the `Nearest` rounding mode.
4736 ///
4737 /// $$
4738 /// x \gets x/y+\varepsilon.
4739 /// $$
4740 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4741 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4742 ///
4743 /// If the output has a precision, it is `prec`.
4744 ///
4745 /// See the [`Float::div_rational_prec`] documentation for information on special cases,
4746 /// overflow, and underflow.
4747 ///
4748 /// If you want to use a rounding mode other than `Nearest`, consider using
4749 /// [`Float::div_rational_prec_round_assign`] instead. If you know that your target precision is
4750 /// the maximum of the precisions of the two inputs, consider using `/=` instead.
4751 ///
4752 /// # Worst-case complexity
4753 /// $T(n) = O(n \log n \log\log n)$
4754 ///
4755 /// $M(n) = O(n \log n)$
4756 ///
4757 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4758 /// other.significant_bits(), prec)`.
4759 ///
4760 /// # Examples
4761 /// ```
4762 /// use core::f64::consts::PI;
4763 /// use malachite_base::num::conversion::traits::ExactFrom;
4764 /// use malachite_float::Float;
4765 /// use malachite_q::Rational;
4766 /// use std::cmp::Ordering::*;
4767 ///
4768 /// let mut x = Float::from(PI);
4769 /// assert_eq!(
4770 /// x.div_rational_prec_assign_ref(&Rational::exact_from(1.5), 5),
4771 /// Greater
4772 /// );
4773 /// assert_eq!(x.to_string(), "2.12");
4774 ///
4775 /// let mut x = Float::from(PI);
4776 /// assert_eq!(
4777 /// x.div_rational_prec_assign_ref(&Rational::exact_from(1.5), 20),
4778 /// Less
4779 /// );
4780 /// assert_eq!(x.to_string(), "2.0943947");
4781 /// ```
4782 #[inline]
4783 pub fn div_rational_prec_assign_ref(&mut self, other: &Rational, prec: u64) -> Ordering {
4784 self.div_rational_prec_round_assign_ref(other, prec, Nearest)
4785 }
4786
4787 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result with the specified
4788 /// rounding mode. The [`Rational`] is taken by value. An [`Ordering`] is returned, indicating
4789 /// whether the rounded quotient is less than, equal to, or greater than the exact quotient.
4790 /// Although `NaN`s are not comparable to any [`Float`], whenever this function sets the
4791 /// [`Float`] to `NaN` it also returns `Equal`.
4792 ///
4793 /// The precision of the output is the precision of the input [`Float`]. See [`RoundingMode`]
4794 /// for a description of the possible rounding modes.
4795 ///
4796 /// $$
4797 /// x \gets x/y+\varepsilon.
4798 /// $$
4799 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4800 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4801 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4802 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4803 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4804 ///
4805 /// If the output has a precision, it is the precision of the input [`Float`].
4806 ///
4807 /// See the [`Float::div_rational_round`] documentation for information on special cases,
4808 /// overflow, and underflow.
4809 ///
4810 /// If you want to specify an output precision, consider using
4811 /// [`Float::div_rational_prec_round_assign`] instead. If you know you'll be using the `Nearest`
4812 /// rounding mode, consider using `/=` instead.
4813 ///
4814 /// # Worst-case complexity
4815 /// $T(n) = O(n \log n \log\log n)$
4816 ///
4817 /// $M(n) = O(n \log n)$
4818 ///
4819 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4820 /// other.significant_bits())`.
4821 ///
4822 /// # Panics
4823 /// Panics if `rm` is `Exact` but the precision of the input [`Float`] is not high enough to
4824 /// represent the output.
4825 ///
4826 /// # Examples
4827 /// ```
4828 /// use core::f64::consts::PI;
4829 /// use malachite_base::rounding_modes::RoundingMode::*;
4830 /// use malachite_float::Float;
4831 /// use malachite_q::Rational;
4832 /// use std::cmp::Ordering::*;
4833 ///
4834 /// let mut x = Float::from(PI);
4835 /// assert_eq!(
4836 /// x.div_rational_round_assign(Rational::from_unsigneds(1u8, 3), Floor),
4837 /// Less
4838 /// );
4839 /// assert_eq!(x.to_string(), "9.4247779607693758");
4840 ///
4841 /// let mut x = Float::from(PI);
4842 /// assert_eq!(
4843 /// x.div_rational_round_assign(Rational::from_unsigneds(1u8, 3), Ceiling),
4844 /// Greater
4845 /// );
4846 /// assert_eq!(x.to_string(), "9.4247779607693900");
4847 ///
4848 /// let mut x = Float::from(PI);
4849 /// assert_eq!(
4850 /// x.div_rational_round_assign(Rational::from_unsigneds(1u8, 3), Nearest),
4851 /// Less
4852 /// );
4853 /// assert_eq!(x.to_string(), "9.4247779607693758");
4854 /// ```
4855 #[inline]
4856 pub fn div_rational_round_assign(&mut self, other: Rational, rm: RoundingMode) -> Ordering {
4857 let prec = self.significant_bits();
4858 self.div_rational_prec_round_assign(other, prec, rm)
4859 }
4860
4861 /// Divides a [`Float`] by a [`Rational`] in place, rounding the result with the specified
4862 /// rounding mode. The [`Rational`] is taken by reference. An [`Ordering`] is returned,
4863 /// indicating whether the rounded quotient is less than, equal to, or greater than the exact
4864 /// quotient. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
4865 /// the [`Float`] to `NaN` it also returns `Equal`.
4866 ///
4867 /// The precision of the output is the precision of the input [`Float`]. See [`RoundingMode`]
4868 /// for a description of the possible rounding modes.
4869 ///
4870 /// $$
4871 /// x \gets x/y+\varepsilon.
4872 /// $$
4873 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4874 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4875 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
4876 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4877 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
4878 ///
4879 /// If the output has a precision, it is the precision of the input [`Float`].
4880 ///
4881 /// See the [`Float::div_rational_round`] documentation for information on special cases,
4882 /// overflow, and underflow.
4883 ///
4884 /// If you want to specify an output precision, consider using
4885 /// [`Float::div_rational_prec_round_assign`] instead. If you know you'll be using the `Nearest`
4886 /// rounding mode, consider using `/=` instead.
4887 ///
4888 /// # Worst-case complexity
4889 /// $T(n) = O(n \log n \log\log n)$
4890 ///
4891 /// $M(n) = O(n \log n)$
4892 ///
4893 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4894 /// other.significant_bits())`.
4895 ///
4896 /// # Panics
4897 /// Panics if `rm` is `Exact` but the precision of the input [`Float`] is not high enough to
4898 /// represent the output.
4899 ///
4900 /// # Examples
4901 /// ```
4902 /// use core::f64::consts::PI;
4903 /// use malachite_base::rounding_modes::RoundingMode::*;
4904 /// use malachite_float::Float;
4905 /// use malachite_q::Rational;
4906 /// use std::cmp::Ordering::*;
4907 ///
4908 /// let mut x = Float::from(PI);
4909 /// assert_eq!(
4910 /// x.div_rational_round_assign_ref(&Rational::from_unsigneds(1u8, 3), Floor),
4911 /// Less
4912 /// );
4913 /// assert_eq!(x.to_string(), "9.4247779607693758");
4914 ///
4915 /// let mut x = Float::from(PI);
4916 /// assert_eq!(
4917 /// x.div_rational_round_assign_ref(&Rational::from_unsigneds(1u8, 3), Ceiling),
4918 /// Greater
4919 /// );
4920 /// assert_eq!(x.to_string(), "9.4247779607693900");
4921 ///
4922 /// let mut x = Float::from(PI);
4923 /// assert_eq!(
4924 /// x.div_rational_round_assign_ref(&Rational::from_unsigneds(1u8, 3), Nearest),
4925 /// Less
4926 /// );
4927 /// assert_eq!(x.to_string(), "9.4247779607693758");
4928 /// ```
4929 #[inline]
4930 pub fn div_rational_round_assign_ref(
4931 &mut self,
4932 other: &Rational,
4933 rm: RoundingMode,
4934 ) -> Ordering {
4935 let prec = self.significant_bits();
4936 self.div_rational_prec_round_assign_ref(other, prec, rm)
4937 }
4938
4939 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
4940 /// with the specified rounding mode. The [`Rational`] and the [`Float`] are both taken by
4941 /// value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
4942 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
4943 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
4944 ///
4945 /// See [`RoundingMode`] for a description of the possible rounding modes.
4946 ///
4947 /// $$
4948 /// f(x,y,p,m) = x/y+\varepsilon.
4949 /// $$
4950 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4951 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4952 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
4953 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4954 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
4955 ///
4956 /// If the output has a precision, it is `prec`.
4957 ///
4958 /// Special cases:
4959 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
4960 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
4961 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or $x=-0.0$
4962 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
4963 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or $x=-0.0$
4964 /// - $f(0,x,p,m)=0.0$ if $x>0$
4965 /// - $f(0,x,p,m)=-0.0$ if $x<0$
4966 ///
4967 /// Overflow and underflow:
4968 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
4969 /// returned instead.
4970 /// - 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}$
4971 /// is returned instead, where `p` is the precision of the input.
4972 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
4973 /// returned instead.
4974 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
4975 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
4976 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
4977 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
4978 /// instead.
4979 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
4980 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
4981 /// instead.
4982 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
4983 /// instead.
4984 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
4985 /// instead.
4986 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
4987 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
4988 /// returned instead.
4989 ///
4990 /// If you know you'll be using `Nearest`, consider using [`Float::rational_div_float_prec`]
4991 /// instead. If you know that your target precision is the precision of the [`Float`] input,
4992 /// consider using [`Float::rational_div_float_round`] instead. If both of these things are
4993 /// true, consider using `/` instead.
4994 ///
4995 /// # Worst-case complexity
4996 /// $T(n) = O(n \log n \log\log n)$
4997 ///
4998 /// $M(n) = O(n \log n)$
4999 ///
5000 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5001 /// y.significant_bits(), prec)`.
5002 ///
5003 /// # Panics
5004 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
5005 ///
5006 /// # Examples
5007 /// ```
5008 /// use core::f64::consts::PI;
5009 /// use malachite_base::rounding_modes::RoundingMode::*;
5010 /// use malachite_float::Float;
5011 /// use malachite_q::Rational;
5012 /// use std::cmp::Ordering::*;
5013 ///
5014 /// let (quotient, o) =
5015 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 5, Floor);
5016 /// assert_eq!(quotient.to_string(), "0.938");
5017 /// assert_eq!(o, Less);
5018 ///
5019 /// let (quotient, o) =
5020 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 5, Ceiling);
5021 /// assert_eq!(quotient.to_string(), "0.969");
5022 /// assert_eq!(o, Greater);
5023 ///
5024 /// let (quotient, o) =
5025 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 5, Nearest);
5026 /// assert_eq!(quotient.to_string(), "0.969");
5027 /// assert_eq!(o, Greater);
5028 ///
5029 /// let (quotient, o) =
5030 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 20, Floor);
5031 /// assert_eq!(quotient.to_string(), "0.95492935");
5032 /// assert_eq!(o, Less);
5033 ///
5034 /// let (quotient, o) =
5035 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 20, Ceiling);
5036 /// assert_eq!(quotient.to_string(), "0.95493031");
5037 /// assert_eq!(o, Greater);
5038 ///
5039 /// let (quotient, o) =
5040 /// Float::rational_div_float_prec_round(Rational::from(3), Float::from(PI), 20, Nearest);
5041 /// assert_eq!(quotient.to_string(), "0.95492935");
5042 /// assert_eq!(o, Less);
5043 /// ```
5044 #[inline]
5045 pub fn rational_div_float_prec_round(
5046 x: Rational,
5047 y: Self,
5048 prec: u64,
5049 rm: RoundingMode,
5050 ) -> (Self, Ordering) {
5051 if !y.is_normal() || max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
5052 rational_div_float_prec_round_naive(x, y, prec, rm)
5053 } else {
5054 rational_div_float_prec_round_direct(x, y, prec, rm)
5055 }
5056 }
5057
5058 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
5059 /// with the specified rounding mode. The [`Rational`] is taken by value and the [`Float`] by
5060 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
5061 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
5062 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
5063 ///
5064 /// See [`RoundingMode`] for a description of the possible rounding modes.
5065 ///
5066 /// $$
5067 /// f(x,y,p,m) = x/y+\varepsilon.
5068 /// $$
5069 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5070 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5071 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
5072 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5073 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5074 ///
5075 /// If the output has a precision, it is `prec`.
5076 ///
5077 /// Special cases:
5078 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
5079 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
5080 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or $x=-0.0$
5081 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
5082 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or $x=-0.0$
5083 /// - $f(0,x,p,m)=0.0$ if $x>0$
5084 /// - $f(0,x,p,m)=-0.0$ if $x<0$
5085 ///
5086 /// Overflow and underflow:
5087 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5088 /// returned instead.
5089 /// - 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}$
5090 /// is returned instead, where `p` is the precision of the input.
5091 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
5092 /// returned instead.
5093 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
5094 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
5095 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5096 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
5097 /// instead.
5098 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
5099 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
5100 /// instead.
5101 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
5102 /// instead.
5103 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
5104 /// instead.
5105 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
5106 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
5107 /// returned instead.
5108 ///
5109 /// If you know you'll be using `Nearest`, consider using
5110 /// [`Float::rational_div_float_prec_val_ref`] instead. If you know that your target precision
5111 /// is the precision of the [`Float`] input, consider using
5112 /// [`Float::rational_div_float_round_val_ref`] instead. If both of these things are true,
5113 /// consider using `/` instead.
5114 ///
5115 /// # Worst-case complexity
5116 /// $T(n) = O(n \log n \log\log n)$
5117 ///
5118 /// $M(n) = O(n \log n)$
5119 ///
5120 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5121 /// y.significant_bits(), prec)`.
5122 ///
5123 /// # Panics
5124 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
5125 ///
5126 /// # Examples
5127 /// ```
5128 /// use core::f64::consts::PI;
5129 /// use malachite_base::rounding_modes::RoundingMode::*;
5130 /// use malachite_float::Float;
5131 /// use malachite_q::Rational;
5132 /// use std::cmp::Ordering::*;
5133 ///
5134 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
5135 /// Rational::from(3),
5136 /// &Float::from(PI),
5137 /// 5,
5138 /// Floor,
5139 /// );
5140 /// assert_eq!(quotient.to_string(), "0.938");
5141 /// assert_eq!(o, Less);
5142 ///
5143 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
5144 /// Rational::from(3),
5145 /// &Float::from(PI),
5146 /// 5,
5147 /// Ceiling,
5148 /// );
5149 /// assert_eq!(quotient.to_string(), "0.969");
5150 /// assert_eq!(o, Greater);
5151 ///
5152 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
5153 /// Rational::from(3),
5154 /// &Float::from(PI),
5155 /// 5,
5156 /// Nearest,
5157 /// );
5158 /// assert_eq!(quotient.to_string(), "0.969");
5159 /// assert_eq!(o, Greater);
5160 ///
5161 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
5162 /// Rational::from(3),
5163 /// &Float::from(PI),
5164 /// 20,
5165 /// Floor,
5166 /// );
5167 /// assert_eq!(quotient.to_string(), "0.95492935");
5168 /// assert_eq!(o, Less);
5169 ///
5170 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
5171 /// Rational::from(3),
5172 /// &Float::from(PI),
5173 /// 20,
5174 /// Ceiling,
5175 /// );
5176 /// assert_eq!(quotient.to_string(), "0.95493031");
5177 /// assert_eq!(o, Greater);
5178 ///
5179 /// let (quotient, o) = Float::rational_div_float_prec_round_val_ref(
5180 /// Rational::from(3),
5181 /// &Float::from(PI),
5182 /// 20,
5183 /// Nearest,
5184 /// );
5185 /// assert_eq!(quotient.to_string(), "0.95492935");
5186 /// assert_eq!(o, Less);
5187 /// ```
5188 #[inline]
5189 pub fn rational_div_float_prec_round_val_ref(
5190 x: Rational,
5191 y: &Self,
5192 prec: u64,
5193 rm: RoundingMode,
5194 ) -> (Self, Ordering) {
5195 if !y.is_normal() || max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
5196 rational_div_float_prec_round_naive_val_ref(x, y, prec, rm)
5197 } else {
5198 rational_div_float_prec_round_direct_val_ref(x, y, prec, rm)
5199 }
5200 }
5201
5202 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
5203 /// with the specified rounding mode. The [`Rational`] is taken by reference and the [`Float`]
5204 /// by value. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
5205 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
5206 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
5207 ///
5208 /// See [`RoundingMode`] for a description of the possible rounding modes.
5209 ///
5210 /// $$
5211 /// f(x,y,p,m) = x/y+\varepsilon.
5212 /// $$
5213 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5214 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5215 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
5216 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5217 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5218 ///
5219 /// If the output has a precision, it is `prec`.
5220 ///
5221 /// Special cases:
5222 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
5223 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
5224 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or $x=-0.0$
5225 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
5226 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or $x=-0.0$
5227 /// - $f(0,x,p,m)=0.0$ if $x>0$
5228 /// - $f(0,x,p,m)=-0.0$ if $x<0$
5229 ///
5230 /// Overflow and underflow:
5231 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5232 /// returned instead.
5233 /// - 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}$
5234 /// is returned instead, where `p` is the precision of the input.
5235 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
5236 /// returned instead.
5237 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
5238 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
5239 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5240 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
5241 /// instead.
5242 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
5243 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
5244 /// instead.
5245 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
5246 /// instead.
5247 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
5248 /// instead.
5249 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
5250 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
5251 /// returned instead.
5252 ///
5253 /// If you know you'll be using `Nearest`, consider using
5254 /// [`Float::rational_div_float_prec_ref_val`] instead. If you know that your target precision
5255 /// is the precision of the [`Float`] input, consider using
5256 /// [`Float::rational_div_float_round_ref_val`] instead. If both of these things are true,
5257 /// consider using `/` instead.
5258 ///
5259 /// # Worst-case complexity
5260 /// $T(n) = O(n \log n \log\log n)$
5261 ///
5262 /// $M(n) = O(n \log n)$
5263 ///
5264 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5265 /// y.significant_bits(), prec)`.
5266 ///
5267 /// # Panics
5268 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
5269 ///
5270 /// # Examples
5271 /// ```
5272 /// use core::f64::consts::PI;
5273 /// use malachite_base::rounding_modes::RoundingMode::*;
5274 /// use malachite_float::Float;
5275 /// use malachite_q::Rational;
5276 /// use std::cmp::Ordering::*;
5277 ///
5278 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
5279 /// &Rational::from(3),
5280 /// Float::from(PI),
5281 /// 5,
5282 /// Floor,
5283 /// );
5284 /// assert_eq!(quotient.to_string(), "0.938");
5285 /// assert_eq!(o, Less);
5286 ///
5287 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
5288 /// &Rational::from(3),
5289 /// Float::from(PI),
5290 /// 5,
5291 /// Ceiling,
5292 /// );
5293 /// assert_eq!(quotient.to_string(), "0.969");
5294 /// assert_eq!(o, Greater);
5295 ///
5296 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
5297 /// &Rational::from(3),
5298 /// Float::from(PI),
5299 /// 5,
5300 /// Nearest,
5301 /// );
5302 /// assert_eq!(quotient.to_string(), "0.969");
5303 /// assert_eq!(o, Greater);
5304 ///
5305 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
5306 /// &Rational::from(3),
5307 /// Float::from(PI),
5308 /// 20,
5309 /// Floor,
5310 /// );
5311 /// assert_eq!(quotient.to_string(), "0.95492935");
5312 /// assert_eq!(o, Less);
5313 ///
5314 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
5315 /// &Rational::from(3),
5316 /// Float::from(PI),
5317 /// 20,
5318 /// Ceiling,
5319 /// );
5320 /// assert_eq!(quotient.to_string(), "0.95493031");
5321 /// assert_eq!(o, Greater);
5322 ///
5323 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_val(
5324 /// &Rational::from(3),
5325 /// Float::from(PI),
5326 /// 20,
5327 /// Nearest,
5328 /// );
5329 /// assert_eq!(quotient.to_string(), "0.95492935");
5330 /// assert_eq!(o, Less);
5331 /// ```
5332 #[inline]
5333 pub fn rational_div_float_prec_round_ref_val(
5334 x: &Rational,
5335 y: Self,
5336 prec: u64,
5337 rm: RoundingMode,
5338 ) -> (Self, Ordering) {
5339 if !y.is_normal() || max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
5340 rational_div_float_prec_round_naive_ref_val(x, y, prec, rm)
5341 } else {
5342 rational_div_float_prec_round_direct_ref_val(x, y, prec, rm)
5343 }
5344 }
5345
5346 /// Divides a [`Rational`] by a [`Float`], rounding the result to the specified precision and
5347 /// with the specified rounding mode. The [`Rational`] and the [`Float`] are both taken by
5348 /// reference. An [`Ordering`] is also returned, indicating whether the rounded quotient is less
5349 /// than, equal to, or greater than the exact quotient. Although `NaN`s are not comparable to
5350 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
5351 ///
5352 /// See [`RoundingMode`] for a description of the possible rounding modes.
5353 ///
5354 /// $$
5355 /// f(x,y,p,m) = x/y+\varepsilon.
5356 /// $$
5357 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5358 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5359 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$.
5360 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5361 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5362 ///
5363 /// If the output has a precision, it is `prec`.
5364 ///
5365 /// Special cases:
5366 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
5367 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
5368 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or $x=-0.0$
5369 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
5370 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or $x=-0.0$
5371 /// - $f(0,x,p,m)=0.0$ if $x>0$
5372 /// - $f(0,x,p,m)=-0.0$ if $x<0$
5373 ///
5374 /// Overflow and underflow:
5375 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5376 /// returned instead.
5377 /// - 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}$
5378 /// is returned instead, where `p` is the precision of the input.
5379 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
5380 /// returned instead.
5381 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
5382 /// $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead, where `p` is the precision of the input.
5383 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5384 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
5385 /// instead.
5386 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
5387 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
5388 /// instead.
5389 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned
5390 /// instead.
5391 /// - If $-2^{-2^{30}}<f(x,y,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
5392 /// instead.
5393 /// - If $-2^{-2^{30}-1}\leq f(x,y,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
5394 /// - If $-2^{-2^{30}}<f(x,y,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
5395 /// returned instead.
5396 ///
5397 /// If you know you'll be using `Nearest`, consider using
5398 /// [`Float::rational_div_float_prec_ref_ref`] instead. If you know that your target precision
5399 /// is the precision of the [`Float`] input, consider using
5400 /// [`Float::rational_div_float_round_ref_ref`] instead. If both of these things are true,
5401 /// consider using `/` instead.
5402 ///
5403 /// # Worst-case complexity
5404 /// $T(n) = O(n \log n \log\log n)$
5405 ///
5406 /// $M(n) = O(n \log n)$
5407 ///
5408 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5409 /// y.significant_bits(), prec)`.
5410 ///
5411 /// # Panics
5412 /// Panics if `rm` is `Exact` but `prec` is too small for an exact division.
5413 ///
5414 /// # Examples
5415 /// ```
5416 /// use core::f64::consts::PI;
5417 /// use malachite_base::rounding_modes::RoundingMode::*;
5418 /// use malachite_float::Float;
5419 /// use malachite_q::Rational;
5420 /// use std::cmp::Ordering::*;
5421 ///
5422 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
5423 /// &Rational::from(3),
5424 /// &Float::from(PI),
5425 /// 5,
5426 /// Floor,
5427 /// );
5428 /// assert_eq!(quotient.to_string(), "0.938");
5429 /// assert_eq!(o, Less);
5430 ///
5431 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
5432 /// &Rational::from(3),
5433 /// &Float::from(PI),
5434 /// 5,
5435 /// Ceiling,
5436 /// );
5437 /// assert_eq!(quotient.to_string(), "0.969");
5438 /// assert_eq!(o, Greater);
5439 ///
5440 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
5441 /// &Rational::from(3),
5442 /// &Float::from(PI),
5443 /// 5,
5444 /// Nearest,
5445 /// );
5446 /// assert_eq!(quotient.to_string(), "0.969");
5447 /// assert_eq!(o, Greater);
5448 ///
5449 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
5450 /// &Rational::from(3),
5451 /// &Float::from(PI),
5452 /// 20,
5453 /// Floor,
5454 /// );
5455 /// assert_eq!(quotient.to_string(), "0.95492935");
5456 /// assert_eq!(o, Less);
5457 ///
5458 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
5459 /// &Rational::from(3),
5460 /// &Float::from(PI),
5461 /// 20,
5462 /// Ceiling,
5463 /// );
5464 /// assert_eq!(quotient.to_string(), "0.95493031");
5465 /// assert_eq!(o, Greater);
5466 ///
5467 /// let (quotient, o) = Float::rational_div_float_prec_round_ref_ref(
5468 /// &Rational::from(3),
5469 /// &Float::from(PI),
5470 /// 20,
5471 /// Nearest,
5472 /// );
5473 /// assert_eq!(quotient.to_string(), "0.95492935");
5474 /// assert_eq!(o, Less);
5475 /// ```
5476 #[inline]
5477 pub fn rational_div_float_prec_round_ref_ref(
5478 x: &Rational,
5479 y: &Self,
5480 prec: u64,
5481 rm: RoundingMode,
5482 ) -> (Self, Ordering) {
5483 if !y.is_normal() || max(x.significant_bits(), y.complexity()) < RATIONAL_DIV_THRESHOLD {
5484 rational_div_float_prec_round_naive_ref_ref(x, y, prec, rm)
5485 } else {
5486 rational_div_float_prec_round_direct_ref_ref(x, y, prec, rm)
5487 }
5488 }
5489
5490 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
5491 /// specified precision. The [`Rational`] and the [`Float`] are both are taken by value. An
5492 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
5493 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
5494 /// whenever this function returns a `NaN` it also returns `Equal`.
5495 ///
5496 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
5497 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
5498 /// description of the `Nearest` rounding mode.
5499 ///
5500 /// $$
5501 /// f(x,y,p) = x/y+\varepsilon.
5502 /// $$
5503 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5504 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5505 ///
5506 /// If the output has a precision, it is `prec`.
5507 ///
5508 /// Special cases:
5509 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
5510 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
5511 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or $x=-0.0$
5512 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
5513 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or $x=-0.0$
5514 /// - $f(0,x,p)=0.0$ if $x>0$
5515 /// - $f(0,x,p)=-0.0$ if $x<0$
5516 ///
5517 /// Overflow and underflow:
5518 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
5519 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
5520 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5521 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5522 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5523 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5524 ///
5525 /// If you want to use a rounding mode other than `Nearest`, consider using
5526 /// [`Float::rational_div_float_prec_round`] instead. If you know that your target precision is
5527 /// the precision of the [`Float`] input, consider using `/` instead.
5528 ///
5529 /// # Worst-case complexity
5530 /// $T(n) = O(n \log n \log\log n)$
5531 ///
5532 /// $M(n) = O(n \log n)$
5533 ///
5534 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5535 /// y.significant_bits(), prec)`.
5536 ///
5537 /// # Examples
5538 /// ```
5539 /// use core::f64::consts::PI;
5540 /// use malachite_float::Float;
5541 /// use malachite_q::Rational;
5542 /// use std::cmp::Ordering::*;
5543 ///
5544 /// let (quotient, o) = Float::rational_div_float_prec(Rational::from(3), Float::from(PI), 5);
5545 /// assert_eq!(quotient.to_string(), "0.969");
5546 /// assert_eq!(o, Greater);
5547 ///
5548 /// let (quotient, o) = Float::rational_div_float_prec(Rational::from(3), Float::from(PI), 20);
5549 /// assert_eq!(quotient.to_string(), "0.95492935");
5550 /// assert_eq!(o, Less);
5551 /// ```
5552 #[inline]
5553 pub fn rational_div_float_prec(x: Rational, y: Self, prec: u64) -> (Self, Ordering) {
5554 Self::rational_div_float_prec_round(x, y, prec, Nearest)
5555 }
5556
5557 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
5558 /// specified precision. The [`Rational`] is taken by value and the [`Float`] by reference. An
5559 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
5560 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
5561 /// whenever this function returns a `NaN` it also returns `Equal`.
5562 ///
5563 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
5564 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
5565 /// description of the `Nearest` rounding mode.
5566 ///
5567 /// $$
5568 /// f(x,y,p) = x/y+\varepsilon.
5569 /// $$
5570 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5571 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5572 ///
5573 /// If the output has a precision, it is `prec`.
5574 ///
5575 /// Special cases:
5576 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
5577 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
5578 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or $x=-0.0$
5579 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
5580 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or $x=-0.0$
5581 /// - $f(0,x,p)=0.0$ if $x>0$
5582 /// - $f(0,x,p)=-0.0$ if $x<0$
5583 ///
5584 /// Overflow and underflow:
5585 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
5586 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
5587 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5588 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5589 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5590 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5591 ///
5592 /// If you want to use a rounding mode other than `Nearest`, consider using
5593 /// [`Float::rational_div_float_prec_round_val_ref`] instead. If you know that your target
5594 /// precision is the precision of the [`Float`] input, consider using `/` instead.
5595 ///
5596 /// # Worst-case complexity
5597 /// $T(n) = O(n \log n \log\log n)$
5598 ///
5599 /// $M(n) = O(n \log n)$
5600 ///
5601 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5602 /// y.significant_bits(), prec)`.
5603 ///
5604 /// # Examples
5605 /// ```
5606 /// use core::f64::consts::PI;
5607 /// use malachite_float::Float;
5608 /// use malachite_q::Rational;
5609 /// use std::cmp::Ordering::*;
5610 ///
5611 /// let (quotient, o) =
5612 /// Float::rational_div_float_prec_val_ref(Rational::from(3), &Float::from(PI), 5);
5613 /// assert_eq!(quotient.to_string(), "0.969");
5614 /// assert_eq!(o, Greater);
5615 ///
5616 /// let (quotient, o) =
5617 /// Float::rational_div_float_prec_val_ref(Rational::from(3), &Float::from(PI), 20);
5618 /// assert_eq!(quotient.to_string(), "0.95492935");
5619 /// assert_eq!(o, Less);
5620 /// ```
5621 #[inline]
5622 pub fn rational_div_float_prec_val_ref(x: Rational, y: &Self, prec: u64) -> (Self, Ordering) {
5623 Self::rational_div_float_prec_round_val_ref(x, y, prec, Nearest)
5624 }
5625
5626 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
5627 /// specified precision. The [`Rational`] is taken by reference and the [`Float`] by value. An
5628 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
5629 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
5630 /// whenever this function returns a `NaN` it also returns `Equal`.
5631 ///
5632 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
5633 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
5634 /// description of the `Nearest` rounding mode.
5635 ///
5636 /// $$
5637 /// f(x,y,p) = x/y+\varepsilon.
5638 /// $$
5639 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5640 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5641 ///
5642 /// If the output has a precision, it is `prec`.
5643 ///
5644 /// Special cases:
5645 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
5646 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
5647 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or $x=-0.0$
5648 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
5649 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or $x=-0.0$
5650 /// - $f(0,x,p)=0.0$ if $x>0$
5651 /// - $f(0,x,p)=-0.0$ if $x<0$
5652 ///
5653 /// Overflow and underflow:
5654 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
5655 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
5656 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5657 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5658 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5659 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5660 ///
5661 /// If you want to use a rounding mode other than `Nearest`, consider using
5662 /// [`Float::rational_div_float_prec_round_ref_val`] instead. If you know that your target
5663 /// precision is the precision of the [`Float`] input, consider using `/` instead.
5664 ///
5665 /// # Worst-case complexity
5666 /// $T(n) = O(n \log n \log\log n)$
5667 ///
5668 /// $M(n) = O(n \log n)$
5669 ///
5670 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5671 /// y.significant_bits(), prec)`.
5672 ///
5673 /// # Examples
5674 /// ```
5675 /// use core::f64::consts::PI;
5676 /// use malachite_float::Float;
5677 /// use malachite_q::Rational;
5678 /// use std::cmp::Ordering::*;
5679 ///
5680 /// let (quotient, o) =
5681 /// Float::rational_div_float_prec_ref_val(&Rational::from(3), Float::from(PI), 5);
5682 /// assert_eq!(quotient.to_string(), "0.969");
5683 /// assert_eq!(o, Greater);
5684 ///
5685 /// let (quotient, o) =
5686 /// Float::rational_div_float_prec_ref_val(&Rational::from(3), Float::from(PI), 20);
5687 /// assert_eq!(quotient.to_string(), "0.95492935");
5688 /// assert_eq!(o, Less);
5689 /// ```
5690 #[inline]
5691 pub fn rational_div_float_prec_ref_val(x: &Rational, y: Self, prec: u64) -> (Self, Ordering) {
5692 Self::rational_div_float_prec_round_ref_val(x, y, prec, Nearest)
5693 }
5694
5695 /// Divides a [`Rational`] by a [`Float`], rounding the result to the nearest value of the
5696 /// specified precision. The [`Rational`] and the [`Float`] are both are taken by reference. An
5697 /// [`Ordering`] is also returned, indicating whether the rounded quotient is less than, equal
5698 /// to, or greater than the exact quotient. Although `NaN`s are not comparable to any [`Float`],
5699 /// whenever this function returns a `NaN` it also returns `Equal`.
5700 ///
5701 /// If the quotient is equidistant from two [`Float`]s with the specified precision, the
5702 /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
5703 /// description of the `Nearest` rounding mode.
5704 ///
5705 /// $$
5706 /// f(x,y,p) = x/y+\varepsilon.
5707 /// $$
5708 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5709 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$.
5710 ///
5711 /// If the output has a precision, it is `prec`.
5712 ///
5713 /// Special cases:
5714 /// - $f(x,\text{NaN},p)=f(0,\pm0.0,p)=\text{NaN}$
5715 /// - $f(x,\infty,x,p)=0.0$ if $x>0.0$ or $x=0.0$
5716 /// - $f(x,\infty,x,p)=-0.0$ if $x<0.0$ or $x=-0.0$
5717 /// - $f(x,-\infty,x,p)=-0.0$ if $x>0.0$ or $x=0.0$
5718 /// - $f(x,-\infty,x,p)=0.0$ if $x<0.0$ or $x=-0.0$
5719 /// - $f(0,x,p)=0.0$ if $x>0$
5720 /// - $f(0,x,p)=-0.0$ if $x<0$
5721 ///
5722 /// Overflow and underflow:
5723 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
5724 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
5725 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
5726 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
5727 /// - If $-2^{-2^{30}-1}\leq f(x,y,p)<0$, $-0.0$ is returned instead.
5728 /// - If $-2^{-2^{30}}<f(x,y,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
5729 ///
5730 /// If you want to use a rounding mode other than `Nearest`, consider using
5731 /// [`Float::rational_div_float_prec_round_ref_ref`] instead. If you know that your target
5732 /// precision is the precision of the [`Float`] input, consider using `/` instead.
5733 ///
5734 /// # Worst-case complexity
5735 /// $T(n) = O(n \log n \log\log n)$
5736 ///
5737 /// $M(n) = O(n \log n)$
5738 ///
5739 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5740 /// y.significant_bits(), prec)`.
5741 ///
5742 /// # Examples
5743 /// ```
5744 /// use core::f64::consts::PI;
5745 /// use malachite_float::Float;
5746 /// use malachite_q::Rational;
5747 /// use std::cmp::Ordering::*;
5748 ///
5749 /// let (quotient, o) =
5750 /// Float::rational_div_float_prec_ref_ref(&Rational::from(3), &Float::from(PI), 5);
5751 /// assert_eq!(quotient.to_string(), "0.969");
5752 /// assert_eq!(o, Greater);
5753 ///
5754 /// let (quotient, o) =
5755 /// Float::rational_div_float_prec_ref_ref(&Rational::from(3), &Float::from(PI), 20);
5756 /// assert_eq!(quotient.to_string(), "0.95492935");
5757 /// assert_eq!(o, Less);
5758 /// ```
5759 #[inline]
5760 pub fn rational_div_float_prec_ref_ref(x: &Rational, y: &Self, prec: u64) -> (Self, Ordering) {
5761 Self::rational_div_float_prec_round_ref_ref(x, y, prec, Nearest)
5762 }
5763
5764 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
5765 /// The [`Rational`] and the [`Float`] are both are taken by value. An [`Ordering`] is also
5766 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
5767 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
5768 /// function returns a `NaN` it also returns `Equal`.
5769 ///
5770 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
5771 /// for a description of the possible rounding modes.
5772 ///
5773 /// $$
5774 /// f(x,y,m) = x/y+\varepsilon.
5775 /// $$
5776 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5777 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5778 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
5779 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5780 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
5781 ///
5782 /// If the output has a precision, it is the precision of the [`Float`] input.
5783 ///
5784 /// Special cases:
5785 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
5786 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
5787 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or $x=-0.0$
5788 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
5789 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or $x=-0.0$
5790 /// - $f(0,x,m)=0.0$ if $x>0$
5791 /// - $f(0,x,m)=-0.0$ if $x<0$
5792 ///
5793 /// Overflow and underflow:
5794 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5795 /// returned instead.
5796 /// - 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
5797 /// returned instead, where `p` is the precision of the input.
5798 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
5799 /// returned instead.
5800 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
5801 /// is returned instead, where `p` is the precision of the input.
5802 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5803 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
5804 /// instead.
5805 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
5806 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
5807 /// instead.
5808 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
5809 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
5810 /// instead.
5811 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
5812 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
5813 /// returned instead.
5814 ///
5815 /// If you want to specify an output precision, consider using
5816 /// [`Float::rational_div_float_prec_round`] instead. If you know you'll be using the `Nearest`
5817 /// rounding mode, consider using `/` instead.
5818 ///
5819 /// # Worst-case complexity
5820 /// $T(n) = O(n \log n \log\log n)$
5821 ///
5822 /// $M(n) = O(n \log n)$
5823 ///
5824 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5825 /// y.significant_bits())`.
5826 ///
5827 /// # Panics
5828 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
5829 /// represent the output.
5830 ///
5831 /// # Examples
5832 /// ```
5833 /// use core::f64::consts::PI;
5834 /// use malachite_base::rounding_modes::RoundingMode::*;
5835 /// use malachite_float::Float;
5836 /// use malachite_q::Rational;
5837 /// use std::cmp::Ordering::*;
5838 ///
5839 /// let (quotient, o) =
5840 /// Float::rational_div_float_round(Rational::from(3), Float::from(PI), Floor);
5841 /// assert_eq!(quotient.to_string(), "0.95492965855137157");
5842 /// assert_eq!(o, Less);
5843 ///
5844 /// let (quotient, o) =
5845 /// Float::rational_div_float_round(Rational::from(3), Float::from(PI), Ceiling);
5846 /// assert_eq!(quotient.to_string(), "0.95492965855137246");
5847 /// assert_eq!(o, Greater);
5848 ///
5849 /// let (quotient, o) =
5850 /// Float::rational_div_float_round(Rational::from(3), Float::from(PI), Nearest);
5851 /// assert_eq!(quotient.to_string(), "0.95492965855137246");
5852 /// assert_eq!(o, Greater);
5853 /// ```
5854 #[inline]
5855 pub fn rational_div_float_round(x: Rational, y: Self, rm: RoundingMode) -> (Self, Ordering) {
5856 let prec = y.significant_bits();
5857 Self::rational_div_float_prec_round(x, y, prec, rm)
5858 }
5859
5860 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
5861 /// The [`Rational`] is taken by value and the [`Float`] by reference. An [`Ordering`] is also
5862 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
5863 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
5864 /// function returns a `NaN` it also returns `Equal`.
5865 ///
5866 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
5867 /// for a description of the possible rounding modes.
5868 ///
5869 /// $$
5870 /// f(x,y,m) = x/y+\varepsilon.
5871 /// $$
5872 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5873 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5874 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
5875 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5876 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
5877 ///
5878 /// If the output has a precision, it is the precision of the [`Float`] input.
5879 ///
5880 /// Special cases:
5881 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
5882 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
5883 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or $x=-0.0$
5884 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
5885 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or $x=-0.0$
5886 /// - $f(0,x,m)=0.0$ if $x>0$
5887 /// - $f(0,x,m)=-0.0$ if $x<0$
5888 ///
5889 /// Overflow and underflow:
5890 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5891 /// returned instead.
5892 /// - 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
5893 /// returned instead, where `p` is the precision of the input.
5894 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
5895 /// returned instead.
5896 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
5897 /// is returned instead, where `p` is the precision of the input.
5898 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5899 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
5900 /// instead.
5901 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
5902 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
5903 /// instead.
5904 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
5905 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
5906 /// instead.
5907 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
5908 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
5909 /// returned instead.
5910 ///
5911 /// If you want to specify an output precision, consider using
5912 /// [`Float::rational_div_float_prec_round_val_ref`] instead. If you know you'll be using the
5913 /// `Nearest` rounding mode, consider using `/` instead.
5914 ///
5915 /// # Worst-case complexity
5916 /// $T(n) = O(n \log n \log\log n)$
5917 ///
5918 /// $M(n) = O(n \log n)$
5919 ///
5920 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
5921 /// y.significant_bits())`.
5922 ///
5923 /// # Panics
5924 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
5925 /// represent the output.
5926 ///
5927 /// # Examples
5928 /// ```
5929 /// use core::f64::consts::PI;
5930 /// use malachite_base::rounding_modes::RoundingMode::*;
5931 /// use malachite_float::Float;
5932 /// use malachite_q::Rational;
5933 /// use std::cmp::Ordering::*;
5934 ///
5935 /// let (quotient, o) =
5936 /// Float::rational_div_float_round_val_ref(Rational::from(3), &Float::from(PI), Floor);
5937 /// assert_eq!(quotient.to_string(), "0.95492965855137157");
5938 /// assert_eq!(o, Less);
5939 ///
5940 /// let (quotient, o) =
5941 /// Float::rational_div_float_round_val_ref(Rational::from(3), &Float::from(PI), Ceiling);
5942 /// assert_eq!(quotient.to_string(), "0.95492965855137246");
5943 /// assert_eq!(o, Greater);
5944 ///
5945 /// let (quotient, o) =
5946 /// Float::rational_div_float_round_val_ref(Rational::from(3), &Float::from(PI), Nearest);
5947 /// assert_eq!(quotient.to_string(), "0.95492965855137246");
5948 /// assert_eq!(o, Greater);
5949 /// ```
5950 #[inline]
5951 pub fn rational_div_float_round_val_ref(
5952 x: Rational,
5953 y: &Self,
5954 rm: RoundingMode,
5955 ) -> (Self, Ordering) {
5956 let prec = y.significant_bits();
5957 Self::rational_div_float_prec_round_val_ref(x, y, prec, rm)
5958 }
5959
5960 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
5961 /// The [`Rational`] is taken by reference and the [`Float`] by value. An [`Ordering`] is also
5962 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
5963 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
5964 /// function returns a `NaN` it also returns `Equal`.
5965 ///
5966 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
5967 /// for a description of the possible rounding modes.
5968 ///
5969 /// $$
5970 /// f(x,y,m) = x/y+\varepsilon.
5971 /// $$
5972 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5973 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5974 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
5975 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5976 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
5977 ///
5978 /// If the output has a precision, it is the precision of the [`Float`] input.
5979 ///
5980 /// Special cases:
5981 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
5982 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
5983 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or $x=-0.0$
5984 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
5985 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or $x=-0.0$
5986 /// - $f(0,x,m)=0.0$ if $x>0$
5987 /// - $f(0,x,m)=-0.0$ if $x<0$
5988 ///
5989 /// Overflow and underflow:
5990 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5991 /// returned instead.
5992 /// - 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
5993 /// returned instead, where `p` is the precision of the input.
5994 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
5995 /// returned instead.
5996 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
5997 /// is returned instead, where `p` is the precision of the input.
5998 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5999 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
6000 /// instead.
6001 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
6002 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
6003 /// instead.
6004 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
6005 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
6006 /// instead.
6007 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
6008 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
6009 /// returned instead.
6010 ///
6011 /// If you want to specify an output precision, consider using
6012 /// [`Float::rational_div_float_prec_round_ref_val`] instead. If you know you'll be using the
6013 /// `Nearest` rounding mode, consider using `/` instead.
6014 ///
6015 /// # Worst-case complexity
6016 /// $T(n) = O(n \log n \log\log n)$
6017 ///
6018 /// $M(n) = O(n \log n)$
6019 ///
6020 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
6021 /// y.significant_bits())`.
6022 ///
6023 /// # Panics
6024 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
6025 /// represent the output.
6026 ///
6027 /// # Examples
6028 /// ```
6029 /// use core::f64::consts::PI;
6030 /// use malachite_base::rounding_modes::RoundingMode::*;
6031 /// use malachite_float::Float;
6032 /// use malachite_q::Rational;
6033 /// use std::cmp::Ordering::*;
6034 ///
6035 /// let (quotient, o) =
6036 /// Float::rational_div_float_round_ref_val(&Rational::from(3), Float::from(PI), Floor);
6037 /// assert_eq!(quotient.to_string(), "0.95492965855137157");
6038 /// assert_eq!(o, Less);
6039 ///
6040 /// let (quotient, o) =
6041 /// Float::rational_div_float_round_ref_val(&Rational::from(3), Float::from(PI), Ceiling);
6042 /// assert_eq!(quotient.to_string(), "0.95492965855137246");
6043 /// assert_eq!(o, Greater);
6044 ///
6045 /// let (quotient, o) =
6046 /// Float::rational_div_float_round_ref_val(&Rational::from(3), Float::from(PI), Nearest);
6047 /// assert_eq!(quotient.to_string(), "0.95492965855137246");
6048 /// assert_eq!(o, Greater);
6049 /// ```
6050 #[inline]
6051 pub fn rational_div_float_round_ref_val(
6052 x: &Rational,
6053 y: Self,
6054 rm: RoundingMode,
6055 ) -> (Self, Ordering) {
6056 let prec = y.significant_bits();
6057 Self::rational_div_float_prec_round_ref_val(x, y, prec, rm)
6058 }
6059
6060 /// Divides a [`Rational`] by a [`Float`], rounding the result with the specified rounding mode.
6061 /// The [`Rational`] and the [`Float`] are both are taken by reference. An [`Ordering`] is also
6062 /// returned, indicating whether the rounded quotient is less than, equal to, or greater than
6063 /// the exact quotient. Although `NaN`s are not comparable to any [`Float`], whenever this
6064 /// function returns a `NaN` it also returns `Equal`.
6065 ///
6066 /// The precision of the output is the precision of the [`Float`] input. See [`RoundingMode`]
6067 /// for a description of the possible rounding modes.
6068 ///
6069 /// $$
6070 /// f(x,y,m) = x/y+\varepsilon.
6071 /// $$
6072 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6073 /// - If $x/y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
6074 /// 2^{\lfloor\log_2 |x/y|\rfloor-p+1}$, where $p$ is the precision of the input [`Float`].
6075 /// - If $x/y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
6076 /// 2^{\lfloor\log_2 |x/y|\rfloor-p}$, where $p$ is the precision of the input [`Float`].
6077 ///
6078 /// If the output has a precision, it is the precision of the [`Float`] input.
6079 ///
6080 /// Special cases:
6081 /// - $f(x,\text{NaN},m)=f(0,\pm0.0,m)=\text{NaN}$
6082 /// - $f(x,\infty,x,m)=0.0$ if $x>0.0$ or $x=0.0$
6083 /// - $f(x,\infty,x,m)=-0.0$ if $x<0.0$ or $x=-0.0$
6084 /// - $f(x,-\infty,x,m)=-0.0$ if $x>0.0$ or $x=0.0$
6085 /// - $f(x,-\infty,x,m)=0.0$ if $x<0.0$ or $x=-0.0$
6086 /// - $f(0,x,m)=0.0$ if $x>0$
6087 /// - $f(0,x,m)=-0.0$ if $x<0$
6088 ///
6089 /// Overflow and underflow:
6090 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
6091 /// returned instead.
6092 /// - 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
6093 /// returned instead, where `p` is the precision of the input.
6094 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
6095 /// returned instead.
6096 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
6097 /// is returned instead, where `p` is the precision of the input.
6098 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
6099 /// - If $0<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
6100 /// instead.
6101 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
6102 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
6103 /// instead.
6104 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
6105 /// - If $-2^{-2^{30}}<f(x,y,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
6106 /// instead.
6107 /// - If $-2^{-2^{30}-1}\leq f(x,y,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
6108 /// - If $-2^{-2^{30}}<f(x,y,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
6109 /// returned instead.
6110 ///
6111 /// If you want to specify an output precision, consider using
6112 /// [`Float::rational_div_float_prec_round_ref_ref`] instead. If you know you'll be using the
6113 /// `Nearest` rounding mode, consider using `/` instead.
6114 ///
6115 /// # Worst-case complexity
6116 /// $T(n) = O(n \log n \log\log n)$
6117 ///
6118 /// $M(n) = O(n \log n)$
6119 ///
6120 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(x.significant_bits(),
6121 /// y.significant_bits())`.
6122 ///
6123 /// # Panics
6124 /// Panics if `rm` is `Exact` but the precision of the [`Float`] input is not high enough to
6125 /// represent the output.
6126 ///
6127 /// # Examples
6128 /// ```
6129 /// use core::f64::consts::PI;
6130 /// use malachite_base::rounding_modes::RoundingMode::*;
6131 /// use malachite_float::Float;
6132 /// use malachite_q::Rational;
6133 /// use std::cmp::Ordering::*;
6134 ///
6135 /// let (quotient, o) =
6136 /// Float::rational_div_float_round_ref_ref(&Rational::from(3), &Float::from(PI), Floor);
6137 /// assert_eq!(quotient.to_string(), "0.95492965855137157");
6138 /// assert_eq!(o, Less);
6139 ///
6140 /// let (quotient, o) =
6141 /// Float::rational_div_float_round_ref_ref(&Rational::from(3), &Float::from(PI), Ceiling);
6142 /// assert_eq!(quotient.to_string(), "0.95492965855137246");
6143 /// assert_eq!(o, Greater);
6144 ///
6145 /// let (quotient, o) =
6146 /// Float::rational_div_float_round_ref_ref(&Rational::from(3), &Float::from(PI), Nearest);
6147 /// assert_eq!(quotient.to_string(), "0.95492965855137246");
6148 /// assert_eq!(o, Greater);
6149 /// ```
6150 #[inline]
6151 pub fn rational_div_float_round_ref_ref(
6152 x: &Rational,
6153 y: &Self,
6154 rm: RoundingMode,
6155 ) -> (Self, Ordering) {
6156 let prec = y.significant_bits();
6157 Self::rational_div_float_prec_round_ref_ref(x, y, prec, rm)
6158 }
6159}
6160
6161impl Div<Self> for Float {
6162 type Output = Self;
6163
6164 /// Divides two [`Float`]s, taking both by value.
6165 ///
6166 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
6167 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
6168 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
6169 /// `Nearest` rounding mode.
6170 ///
6171 /// $$
6172 /// f(x,y) = x/y+\varepsilon.
6173 /// $$
6174 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6175 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6176 /// where $p$ is the maximum precision of the inputs.
6177 ///
6178 /// Special cases:
6179 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
6180 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
6181 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
6182 /// - $f(x,0.0)=\infty$ if $x>0.0$
6183 /// - $f(x,0.0)=-\infty$ if $x<0.0$
6184 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
6185 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
6186 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
6187 /// - $f(x,-0.0)=\infty$ if $x<0.0$
6188 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
6189 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
6190 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6191 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6192 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
6193 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
6194 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6195 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6196 ///
6197 /// Overflow and underflow:
6198 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
6199 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
6200 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6201 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6202 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
6203 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6204 ///
6205 /// If you want to use a rounding mode other than `Nearest`, consider using [`Float::div_prec`]
6206 /// instead. If you want to specify the output precision, consider using [`Float::div_round`].
6207 /// If you want both of these things, consider using [`Float::div_prec_round`].
6208 ///
6209 /// # Worst-case complexity
6210 /// $T(n) = O(n \log n \log\log n)$
6211 ///
6212 /// $M(n) = O(n \log n)$
6213 ///
6214 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6215 /// other.significant_bits())`.
6216 ///
6217 /// # Examples
6218 /// ```
6219 /// use malachite_base::num::basic::traits::{
6220 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6221 /// };
6222 /// use malachite_float::Float;
6223 ///
6224 /// assert!((Float::from(1.5) / Float::NAN).is_nan());
6225 /// assert_eq!(Float::from(1.5) / Float::ZERO, Float::INFINITY);
6226 /// assert_eq!(
6227 /// Float::from(1.5) / Float::NEGATIVE_ZERO,
6228 /// Float::NEGATIVE_INFINITY
6229 /// );
6230 /// assert_eq!(Float::from(-1.5) / Float::ZERO, Float::NEGATIVE_INFINITY);
6231 /// assert_eq!(Float::from(-1.5) / Float::NEGATIVE_ZERO, Float::INFINITY);
6232 /// assert!((Float::ZERO / Float::ZERO).is_nan());
6233 ///
6234 /// assert_eq!((Float::from(1.5) / Float::from(2.5)).to_string(), "0.62");
6235 /// assert_eq!((Float::from(1.5) / Float::from(-2.5)).to_string(), "-0.62");
6236 /// assert_eq!((Float::from(-1.5) / Float::from(2.5)).to_string(), "-0.62");
6237 /// assert_eq!((Float::from(-1.5) / Float::from(-2.5)).to_string(), "0.62");
6238 /// ```
6239 #[inline]
6240 fn div(self, other: Self) -> Self {
6241 let prec = max(self.significant_bits(), other.significant_bits());
6242 self.div_prec_round(other, prec, Nearest).0
6243 }
6244}
6245
6246impl Div<&Self> for Float {
6247 type Output = Self;
6248
6249 /// Divides two [`Float`]s, taking the first by value and the second by reference.
6250 ///
6251 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
6252 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
6253 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
6254 /// `Nearest` rounding mode.
6255 ///
6256 /// $$
6257 /// f(x,y) = x/y+\varepsilon.
6258 /// $$
6259 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6260 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6261 /// where $p$ is the maximum precision of the inputs.
6262 ///
6263 /// Special cases:
6264 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
6265 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
6266 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
6267 /// - $f(x,0.0)=\infty$ if $x>0.0$
6268 /// - $f(x,0.0)=-\infty$ if $x<0.0$
6269 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
6270 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
6271 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
6272 /// - $f(x,-0.0)=\infty$ if $x<0.0$
6273 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
6274 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
6275 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6276 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6277 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
6278 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
6279 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6280 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6281 ///
6282 /// Overflow and underflow:
6283 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
6284 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
6285 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6286 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6287 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
6288 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6289 ///
6290 /// If you want to use a rounding mode other than `Nearest`, consider using
6291 /// [`Float::div_prec_val_ref`] instead. If you want to specify the output precision, consider
6292 /// using [`Float::div_round_val_ref`]. If you want both of these things, consider using
6293 /// [`Float::div_prec_round_val_ref`].
6294 ///
6295 /// # Worst-case complexity
6296 /// $T(n) = O(n \log n \log\log n)$
6297 ///
6298 /// $M(n) = O(n \log n)$
6299 ///
6300 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6301 /// other.significant_bits())`.
6302 ///
6303 /// # Examples
6304 /// ```
6305 /// use malachite_base::num::basic::traits::{
6306 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6307 /// };
6308 /// use malachite_float::Float;
6309 ///
6310 /// assert!((Float::from(1.5) / &Float::NAN).is_nan());
6311 /// assert_eq!(Float::from(1.5) / &Float::ZERO, Float::INFINITY);
6312 /// assert_eq!(
6313 /// Float::from(1.5) / &Float::NEGATIVE_ZERO,
6314 /// Float::NEGATIVE_INFINITY
6315 /// );
6316 /// assert_eq!(Float::from(-1.5) / &Float::ZERO, Float::NEGATIVE_INFINITY);
6317 /// assert_eq!(Float::from(-1.5) / &Float::NEGATIVE_ZERO, Float::INFINITY);
6318 /// assert!((Float::ZERO / &Float::ZERO).is_nan());
6319 ///
6320 /// assert_eq!((Float::from(1.5) / &Float::from(2.5)).to_string(), "0.62");
6321 /// assert_eq!((Float::from(1.5) / &Float::from(-2.5)).to_string(), "-0.62");
6322 /// assert_eq!((Float::from(-1.5) / &Float::from(2.5)).to_string(), "-0.62");
6323 /// assert_eq!((Float::from(-1.5) / &Float::from(-2.5)).to_string(), "0.62");
6324 /// ```
6325 #[inline]
6326 fn div(self, other: &Self) -> Self {
6327 let prec = max(self.significant_bits(), other.significant_bits());
6328 self.div_prec_round_val_ref(other, prec, Nearest).0
6329 }
6330}
6331
6332impl Div<Float> for &Float {
6333 type Output = Float;
6334
6335 /// Divides two [`Float`]s, taking the first by reference and the second by value.
6336 ///
6337 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
6338 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
6339 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
6340 /// `Nearest` rounding mode.
6341 ///
6342 /// $$
6343 /// f(x,y) = x/y+\varepsilon.
6344 /// $$
6345 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6346 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6347 /// where $p$ is the maximum precision of the inputs.
6348 ///
6349 /// Special cases:
6350 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
6351 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
6352 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
6353 /// - $f(x,0.0)=\infty$ if $x>0.0$
6354 /// - $f(x,0.0)=-\infty$ if $x<0.0$
6355 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
6356 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
6357 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
6358 /// - $f(x,-0.0)=\infty$ if $x<0.0$
6359 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
6360 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
6361 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6362 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6363 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
6364 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
6365 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6366 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6367 ///
6368 /// Overflow and underflow:
6369 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
6370 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
6371 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6372 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6373 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
6374 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6375 ///
6376 /// If you want to use a rounding mode other than `Nearest`, consider using
6377 /// [`Float::div_prec_ref_val`] instead. If you want to specify the output precision, consider
6378 /// using [`Float::div_round_ref_val`]. If you want both of these things, consider using
6379 /// [`Float::div_prec_round_ref_val`].
6380 ///
6381 /// # Worst-case complexity
6382 /// $T(n) = O(n \log n \log\log n)$
6383 ///
6384 /// $M(n) = O(n \log n)$
6385 ///
6386 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6387 /// other.significant_bits())`.
6388 ///
6389 /// # Examples
6390 /// ```
6391 /// use malachite_base::num::basic::traits::{
6392 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6393 /// };
6394 /// use malachite_float::Float;
6395 ///
6396 /// assert!((&Float::from(1.5) / Float::NAN).is_nan());
6397 /// assert_eq!(&Float::from(1.5) / Float::ZERO, Float::INFINITY);
6398 /// assert_eq!(
6399 /// &Float::from(1.5) / Float::NEGATIVE_ZERO,
6400 /// Float::NEGATIVE_INFINITY
6401 /// );
6402 /// assert_eq!(&Float::from(-1.5) / Float::ZERO, Float::NEGATIVE_INFINITY);
6403 /// assert_eq!(&Float::from(-1.5) / Float::NEGATIVE_ZERO, Float::INFINITY);
6404 /// assert!((&Float::ZERO / Float::ZERO).is_nan());
6405 ///
6406 /// assert_eq!((&Float::from(1.5) / Float::from(2.5)).to_string(), "0.62");
6407 /// assert_eq!((&Float::from(1.5) / Float::from(-2.5)).to_string(), "-0.62");
6408 /// assert_eq!((&Float::from(-1.5) / Float::from(2.5)).to_string(), "-0.62");
6409 /// assert_eq!((&Float::from(-1.5) / Float::from(-2.5)).to_string(), "0.62");
6410 /// ```
6411 #[inline]
6412 fn div(self, other: Float) -> Float {
6413 let prec = max(self.significant_bits(), other.significant_bits());
6414 self.div_prec_round_ref_val(other, prec, Nearest).0
6415 }
6416}
6417
6418impl Div<&Float> for &Float {
6419 type Output = Float;
6420
6421 /// Divides two [`Float`]s, taking both by reference.
6422 ///
6423 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
6424 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
6425 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
6426 /// `Nearest` rounding mode.
6427 ///
6428 /// $$
6429 /// f(x,y) = x/y+\varepsilon.
6430 /// $$
6431 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6432 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6433 /// where $p$ is the maximum precision of the inputs.
6434 ///
6435 /// Special cases:
6436 /// - $f(\text{NaN},x)=f(x,\text{NaN})=f(\pm\infty,\pm\infty)=f(\pm0.0,\pm0.0) = \text{NaN}$
6437 /// - $f(\infty,x)=\infty$ if $0.0<x<\infty$
6438 /// - $f(\infty,x)=-\infty$ if $-\infty<x<0.0$
6439 /// - $f(x,0.0)=\infty$ if $x>0.0$
6440 /// - $f(x,0.0)=-\infty$ if $x<0.0$
6441 /// - $f(-\infty,x)=-\infty$ if $0.0<x<\infty$
6442 /// - $f(-\infty,x)=\infty$ if $-\infty<x<0.0$
6443 /// - $f(x,-0.0)=-\infty$ if $x>0.0$
6444 /// - $f(x,-0.0)=\infty$ if $x<0.0$
6445 /// - $f(0.0,x)=0.0$ if $x$ is not NaN and $x>0.0$
6446 /// - $f(0.0,x)=-0.0$ if $x$ is not NaN and $x<0.0$
6447 /// - $f(x,\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6448 /// - $f(x,\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6449 /// - $f(-0.0,x)=-0.0$ if $x$ is not NaN and $x>0.0$
6450 /// - $f(-0.0,x)=0.0$ if $x$ is not NaN and $x<0.0$
6451 /// - $f(x,-\infty)=-0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=0.0$ or $x>0.0$
6452 /// - $f(x,-\infty)=0.0$ if $x$ is not NaN or $\pm\infty$, and if $x=-0.0$ or $x<0.0$
6453 ///
6454 /// Overflow and underflow:
6455 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
6456 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
6457 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6458 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6459 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
6460 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6461 ///
6462 /// If you want to use a rounding mode other than `Nearest`, consider using
6463 /// [`Float::div_prec_ref_ref`] instead. If you want to specify the output precision, consider
6464 /// using [`Float::div_round_ref_ref`]. If you want both of these things, consider using
6465 /// [`Float::div_prec_round_ref_ref`].
6466 ///
6467 /// # Worst-case complexity
6468 /// $T(n) = O(n \log n \log\log n)$
6469 ///
6470 /// $M(n) = O(n \log n)$
6471 ///
6472 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6473 /// other.significant_bits())`.
6474 ///
6475 /// # Examples
6476 /// ```
6477 /// use malachite_base::num::basic::traits::{
6478 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6479 /// };
6480 /// use malachite_float::Float;
6481 ///
6482 /// assert!((&Float::from(1.5) / &Float::NAN).is_nan());
6483 /// assert_eq!(&Float::from(1.5) / &Float::ZERO, Float::INFINITY);
6484 /// assert_eq!(
6485 /// &Float::from(1.5) / &Float::NEGATIVE_ZERO,
6486 /// Float::NEGATIVE_INFINITY
6487 /// );
6488 /// assert_eq!(&Float::from(-1.5) / &Float::ZERO, Float::NEGATIVE_INFINITY);
6489 /// assert_eq!(&Float::from(-1.5) / &Float::NEGATIVE_ZERO, Float::INFINITY);
6490 /// assert!((&Float::ZERO / &Float::ZERO).is_nan());
6491 ///
6492 /// assert_eq!((&Float::from(1.5) / &Float::from(2.5)).to_string(), "0.62");
6493 /// assert_eq!(
6494 /// (&Float::from(1.5) / &Float::from(-2.5)).to_string(),
6495 /// "-0.62"
6496 /// );
6497 /// assert_eq!(
6498 /// (&Float::from(-1.5) / &Float::from(2.5)).to_string(),
6499 /// "-0.62"
6500 /// );
6501 /// assert_eq!(
6502 /// (&Float::from(-1.5) / &Float::from(-2.5)).to_string(),
6503 /// "0.62"
6504 /// );
6505 /// ```
6506 #[inline]
6507 fn div(self, other: &Float) -> Float {
6508 let prec = max(self.significant_bits(), other.significant_bits());
6509 self.div_prec_round_ref_ref(other, prec, Nearest).0
6510 }
6511}
6512
6513impl DivAssign<Self> for Float {
6514 /// Divides a [`Float`] by a [`Float`] in place, taking the [`Float`] on the right-hand side by
6515 /// value.
6516 ///
6517 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
6518 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
6519 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
6520 /// `Nearest` rounding mode.
6521 ///
6522 /// $$
6523 /// x\gets = x/y+\varepsilon.
6524 /// $$
6525 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6526 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6527 /// where $p$ is the maximum precision of the inputs.
6528 ///
6529 /// See the `/` documentation for information on special cases, overflow, and underflow.
6530 ///
6531 /// If you want to use a rounding mode other than `Nearest`, consider using
6532 /// [`Float::div_prec_assign`] instead. If you want to specify the output precision, consider
6533 /// using [`Float::div_round_assign`]. If you want both of these things, consider using
6534 /// [`Float::div_prec_round_assign`].
6535 ///
6536 /// # Worst-case complexity
6537 /// $T(n) = O(n \log n \log\log n)$
6538 ///
6539 /// $M(n) = O(n \log n)$
6540 ///
6541 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6542 /// other.significant_bits())`.
6543 ///
6544 /// # Examples
6545 /// ```
6546 /// use malachite_base::num::basic::traits::{
6547 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6548 /// };
6549 /// use malachite_float::Float;
6550 ///
6551 /// let mut x = Float::from(1.5);
6552 /// x /= Float::NAN;
6553 /// assert!(x.is_nan());
6554 ///
6555 /// let mut x = Float::from(1.5);
6556 /// x /= Float::ZERO;
6557 /// assert_eq!(x, Float::INFINITY);
6558 ///
6559 /// let mut x = Float::from(1.5);
6560 /// x /= Float::NEGATIVE_ZERO;
6561 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
6562 ///
6563 /// let mut x = Float::from(-1.5);
6564 /// x /= Float::ZERO;
6565 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
6566 ///
6567 /// let mut x = Float::from(-1.5);
6568 /// x /= Float::NEGATIVE_ZERO;
6569 /// assert_eq!(x, Float::INFINITY);
6570 ///
6571 /// let mut x = Float::INFINITY;
6572 /// x /= Float::INFINITY;
6573 /// assert!(x.is_nan());
6574 ///
6575 /// let mut x = Float::from(1.5);
6576 /// x /= Float::from(2.5);
6577 /// assert_eq!(x.to_string(), "0.62");
6578 ///
6579 /// let mut x = Float::from(1.5);
6580 /// x /= Float::from(-2.5);
6581 /// assert_eq!(x.to_string(), "-0.62");
6582 ///
6583 /// let mut x = Float::from(-1.5);
6584 /// x /= Float::from(2.5);
6585 /// assert_eq!(x.to_string(), "-0.62");
6586 ///
6587 /// let mut x = Float::from(-1.5);
6588 /// x /= Float::from(-2.5);
6589 /// assert_eq!(x.to_string(), "0.62");
6590 /// ```
6591 #[inline]
6592 fn div_assign(&mut self, other: Self) {
6593 let prec = max(self.significant_bits(), other.significant_bits());
6594 self.div_prec_round_assign(other, prec, Nearest);
6595 }
6596}
6597
6598impl DivAssign<&Self> for Float {
6599 /// Divides a [`Float`] by a [`Float`] in place, taking the [`Float`] on the right-hand side by
6600 /// reference.
6601 ///
6602 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
6603 /// quotient is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
6604 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
6605 /// `Nearest` rounding mode.
6606 ///
6607 /// $$
6608 /// x\gets = x/y+\varepsilon.
6609 /// $$
6610 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6611 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6612 /// where $p$ is the maximum precision of the inputs.
6613 ///
6614 /// See the `/` documentation for information on special cases, overflow, and underflow.
6615 ///
6616 /// If you want to use a rounding mode other than `Nearest`, consider using
6617 /// [`Float::div_prec_assign`] instead. If you want to specify the output precision, consider
6618 /// using [`Float::div_round_assign`]. If you want both of these things, consider using
6619 /// [`Float::div_prec_round_assign`].
6620 ///
6621 /// # Worst-case complexity
6622 /// $T(n) = O(n \log n \log\log n)$
6623 ///
6624 /// $M(n) = O(n \log n)$
6625 ///
6626 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6627 /// other.significant_bits())`.
6628 ///
6629 /// # Examples
6630 /// ```
6631 /// use malachite_base::num::basic::traits::{
6632 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
6633 /// };
6634 /// use malachite_float::Float;
6635 ///
6636 /// let mut x = Float::from(1.5);
6637 /// x /= &Float::NAN;
6638 /// assert!(x.is_nan());
6639 ///
6640 /// let mut x = Float::from(1.5);
6641 /// x /= &Float::ZERO;
6642 /// assert_eq!(x, Float::INFINITY);
6643 ///
6644 /// let mut x = Float::from(1.5);
6645 /// x /= &Float::NEGATIVE_ZERO;
6646 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
6647 ///
6648 /// let mut x = Float::from(-1.5);
6649 /// x /= &Float::ZERO;
6650 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
6651 ///
6652 /// let mut x = Float::from(-1.5);
6653 /// x /= &Float::NEGATIVE_ZERO;
6654 /// assert_eq!(x, Float::INFINITY);
6655 ///
6656 /// let mut x = Float::INFINITY;
6657 /// x /= &Float::INFINITY;
6658 /// assert!(x.is_nan());
6659 ///
6660 /// let mut x = Float::from(1.5);
6661 /// x /= &Float::from(2.5);
6662 /// assert_eq!(x.to_string(), "0.62");
6663 ///
6664 /// let mut x = Float::from(1.5);
6665 /// x /= &Float::from(-2.5);
6666 /// assert_eq!(x.to_string(), "-0.62");
6667 ///
6668 /// let mut x = Float::from(-1.5);
6669 /// x /= &Float::from(2.5);
6670 /// assert_eq!(x.to_string(), "-0.62");
6671 ///
6672 /// let mut x = Float::from(-1.5);
6673 /// x /= &Float::from(-2.5);
6674 /// assert_eq!(x.to_string(), "0.62");
6675 /// ```
6676 #[inline]
6677 fn div_assign(&mut self, other: &Self) {
6678 let prec = max(self.significant_bits(), other.significant_bits());
6679 self.div_prec_round_assign_ref(other, prec, Nearest);
6680 }
6681}
6682
6683impl Div<Rational> for Float {
6684 type Output = Self;
6685
6686 /// Divides a [`Float`] by a [`Rational`], taking both by value.
6687 ///
6688 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6689 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6690 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6691 /// rounding mode.
6692 ///
6693 /// $$
6694 /// f(x,y) = x/y+\varepsilon.
6695 /// $$
6696 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6697 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6698 /// where $p$ is the precision of the input [`Float`].
6699 ///
6700 /// Special cases:
6701 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
6702 /// - $f(\infty,x)=\infty$ if $x\geq 0$
6703 /// - $f(\infty,x)=-\infty$ if $x<0$
6704 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
6705 /// - $f(-\infty,x)=\infty$ if $x<0$
6706 /// - $f(0.0,x)=0.0$ if $x>0$
6707 /// - $f(0.0,x)=-0.0$ if $x<0$
6708 /// - $f(-0.0,x)=-0.0$ if $x>0$
6709 /// - $f(-0.0,x)=0.0$ if $x<0$
6710 ///
6711 /// Overflow and underflow:
6712 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
6713 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
6714 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6715 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6716 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
6717 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6718 ///
6719 /// If you want to use a rounding mode other than `Nearest`, consider using
6720 /// [`Float::div_rational_prec`] instead. If you want to specify the output precision, consider
6721 /// using [`Float::div_rational_round`]. If you want both of these things, consider using
6722 /// [`Float::div_rational_prec_round`].
6723 ///
6724 /// # Worst-case complexity
6725 /// $T(n) = O(n \log n \log\log n)$
6726 ///
6727 /// $M(n) = O(n \log n)$
6728 ///
6729 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6730 /// other.significant_bits())`.
6731 ///
6732 /// # Examples
6733 /// ```
6734 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
6735 /// use malachite_base::num::conversion::traits::ExactFrom;
6736 /// use malachite_float::Float;
6737 /// use malachite_q::Rational;
6738 ///
6739 /// assert!((Float::NAN / Rational::exact_from(1.5)).is_nan());
6740 /// assert_eq!(Float::INFINITY / Rational::exact_from(1.5), Float::INFINITY);
6741 /// assert_eq!(
6742 /// Float::NEGATIVE_INFINITY / Rational::exact_from(1.5),
6743 /// Float::NEGATIVE_INFINITY
6744 /// );
6745 /// assert_eq!(
6746 /// Float::INFINITY / Rational::exact_from(-1.5),
6747 /// Float::NEGATIVE_INFINITY
6748 /// );
6749 /// assert_eq!(
6750 /// Float::NEGATIVE_INFINITY / Rational::exact_from(-1.5),
6751 /// Float::INFINITY
6752 /// );
6753 ///
6754 /// assert_eq!(
6755 /// (Float::from(2.5) / Rational::exact_from(1.5)).to_string(),
6756 /// "1.8"
6757 /// );
6758 /// assert_eq!(
6759 /// (Float::from(2.5) / Rational::exact_from(-1.5)).to_string(),
6760 /// "-1.8"
6761 /// );
6762 /// assert_eq!(
6763 /// (Float::from(-2.5) / Rational::exact_from(1.5)).to_string(),
6764 /// "-1.8"
6765 /// );
6766 /// assert_eq!(
6767 /// (Float::from(-2.5) / Rational::exact_from(-1.5)).to_string(),
6768 /// "1.8"
6769 /// );
6770 /// ```
6771 #[inline]
6772 fn div(self, other: Rational) -> Self {
6773 let prec = self.significant_bits();
6774 self.div_rational_prec_round(other, prec, Nearest).0
6775 }
6776}
6777
6778impl Div<&Rational> for Float {
6779 type Output = Self;
6780
6781 /// Divides a [`Float`] by a [`Rational`], taking the first by value and the second by
6782 /// reference.
6783 ///
6784 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6785 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6786 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6787 /// rounding mode.
6788 ///
6789 /// $$
6790 /// f(x,y) = x/y+\varepsilon.
6791 /// $$
6792 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6793 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6794 /// where $p$ is the precision of the input [`Float`].
6795 ///
6796 /// Special cases:
6797 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
6798 /// - $f(\infty,x)=\infty$ if $x\geq 0$
6799 /// - $f(\infty,x)=-\infty$ if $x<0$
6800 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
6801 /// - $f(-\infty,x)=\infty$ if $x<0$
6802 /// - $f(0.0,x)=0.0$ if $x>0$
6803 /// - $f(0.0,x)=-0.0$ if $x<0$
6804 /// - $f(-0.0,x)=-0.0$ if $x>0$
6805 /// - $f(-0.0,x)=0.0$ if $x<0$
6806 ///
6807 /// Overflow and underflow:
6808 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
6809 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
6810 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6811 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6812 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
6813 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6814 ///
6815 /// If you want to use a rounding mode other than `Nearest`, consider using
6816 /// [`Float::div_rational_prec_val_ref`] instead. If you want to specify the output precision,
6817 /// consider using [`Float::div_rational_round_val_ref`]. If you want both of these things,
6818 /// consider using [`Float::div_rational_prec_round_val_ref`].
6819 ///
6820 /// # Worst-case complexity
6821 /// $T(n) = O(n \log n \log\log n)$
6822 ///
6823 /// $M(n) = O(n \log n)$
6824 ///
6825 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6826 /// other.significant_bits())`.
6827 ///
6828 /// # Examples
6829 /// ```
6830 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
6831 /// use malachite_base::num::conversion::traits::ExactFrom;
6832 /// use malachite_float::Float;
6833 /// use malachite_q::Rational;
6834 ///
6835 /// assert!((Float::NAN / &Rational::exact_from(1.5)).is_nan());
6836 /// assert_eq!(
6837 /// Float::INFINITY / &Rational::exact_from(1.5),
6838 /// Float::INFINITY
6839 /// );
6840 /// assert_eq!(
6841 /// Float::NEGATIVE_INFINITY / &Rational::exact_from(1.5),
6842 /// Float::NEGATIVE_INFINITY
6843 /// );
6844 /// assert_eq!(
6845 /// Float::INFINITY / &Rational::exact_from(-1.5),
6846 /// Float::NEGATIVE_INFINITY
6847 /// );
6848 /// assert_eq!(
6849 /// Float::NEGATIVE_INFINITY / &Rational::exact_from(-1.5),
6850 /// Float::INFINITY
6851 /// );
6852 ///
6853 /// assert_eq!(
6854 /// (Float::from(2.5) / &Rational::exact_from(1.5)).to_string(),
6855 /// "1.8"
6856 /// );
6857 /// assert_eq!(
6858 /// (Float::from(2.5) / &Rational::exact_from(-1.5)).to_string(),
6859 /// "-1.8"
6860 /// );
6861 /// assert_eq!(
6862 /// (Float::from(-2.5) / &Rational::exact_from(1.5)).to_string(),
6863 /// "-1.8"
6864 /// );
6865 /// assert_eq!(
6866 /// (Float::from(-2.5) / &Rational::exact_from(-1.5)).to_string(),
6867 /// "1.8"
6868 /// );
6869 /// ```
6870 #[inline]
6871 fn div(self, other: &Rational) -> Self {
6872 let prec = self.significant_bits();
6873 self.div_rational_prec_round_val_ref(other, prec, Nearest).0
6874 }
6875}
6876
6877impl Div<Rational> for &Float {
6878 type Output = Float;
6879
6880 /// Divides a [`Float`] by a [`Rational`], taking the first by reference and the second by
6881 /// value.
6882 ///
6883 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6884 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6885 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6886 /// rounding mode.
6887 ///
6888 /// $$
6889 /// f(x,y) = x/y+\varepsilon.
6890 /// $$
6891 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6892 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6893 /// where $p$ is the precision of the input [`Float`].
6894 ///
6895 /// Special cases:
6896 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
6897 /// - $f(\infty,x)=\infty$ if $x\geq 0$
6898 /// - $f(\infty,x)=-\infty$ if $x<0$
6899 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
6900 /// - $f(-\infty,x)=\infty$ if $x<0$
6901 /// - $f(0.0,x)=0.0$ if $x>0$
6902 /// - $f(0.0,x)=-0.0$ if $x<0$
6903 /// - $f(-0.0,x)=-0.0$ if $x>0$
6904 /// - $f(-0.0,x)=0.0$ if $x<0$
6905 ///
6906 /// Overflow and underflow:
6907 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
6908 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
6909 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
6910 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
6911 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
6912 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
6913 ///
6914 /// If you want to use a rounding mode other than `Nearest`, consider using
6915 /// [`Float::div_rational_prec_ref_val`] instead. If you want to specify the output precision,
6916 /// consider using [`Float::div_rational_round_ref_val`]. If you want both of these things,
6917 /// consider using [`Float::div_rational_prec_round_ref_val`].
6918 ///
6919 /// # Worst-case complexity
6920 /// $T(n) = O(n \log n \log\log n)$
6921 ///
6922 /// $M(n) = O(n \log n)$
6923 ///
6924 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
6925 /// other.significant_bits())`.
6926 ///
6927 /// # Examples
6928 /// ```
6929 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
6930 /// use malachite_base::num::conversion::traits::ExactFrom;
6931 /// use malachite_float::Float;
6932 /// use malachite_q::Rational;
6933 ///
6934 /// assert!((&Float::NAN / Rational::exact_from(1.5)).is_nan());
6935 /// assert_eq!(
6936 /// &Float::INFINITY / Rational::exact_from(1.5),
6937 /// Float::INFINITY
6938 /// );
6939 /// assert_eq!(
6940 /// &Float::NEGATIVE_INFINITY / Rational::exact_from(1.5),
6941 /// Float::NEGATIVE_INFINITY
6942 /// );
6943 /// assert_eq!(
6944 /// &Float::INFINITY / Rational::exact_from(-1.5),
6945 /// Float::NEGATIVE_INFINITY
6946 /// );
6947 /// assert_eq!(
6948 /// &Float::NEGATIVE_INFINITY / Rational::exact_from(-1.5),
6949 /// Float::INFINITY
6950 /// );
6951 ///
6952 /// assert_eq!(
6953 /// (&Float::from(2.5) / Rational::exact_from(1.5)).to_string(),
6954 /// "1.8"
6955 /// );
6956 /// assert_eq!(
6957 /// (&Float::from(2.5) / Rational::exact_from(-1.5)).to_string(),
6958 /// "-1.8"
6959 /// );
6960 /// assert_eq!(
6961 /// (&Float::from(-2.5) / Rational::exact_from(1.5)).to_string(),
6962 /// "-1.8"
6963 /// );
6964 /// assert_eq!(
6965 /// (&Float::from(-2.5) / Rational::exact_from(-1.5)).to_string(),
6966 /// "1.8"
6967 /// );
6968 /// ```
6969 #[inline]
6970 fn div(self, other: Rational) -> Float {
6971 let prec = self.significant_bits();
6972 self.div_rational_prec_round_ref_val(other, prec, Nearest).0
6973 }
6974}
6975
6976impl Div<&Rational> for &Float {
6977 type Output = Float;
6978
6979 /// Divides a [`Float`] by a [`Rational`], taking both by reference.
6980 ///
6981 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
6982 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
6983 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
6984 /// rounding mode.
6985 ///
6986 /// $$
6987 /// f(x,y) = x/y+\varepsilon.
6988 /// $$
6989 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6990 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
6991 /// where $p$ is the precision of the input [`Float`].
6992 ///
6993 /// Special cases:
6994 /// - $f(\text{NaN},x)=f(\pm\infty,0)=f(\pm0.0,0)=\text{NaN}$
6995 /// - $f(\infty,x)=\infty$ if $x\geq 0$
6996 /// - $f(\infty,x)=-\infty$ if $x<0$
6997 /// - $f(-\infty,x)=-\infty$ if $x\geq 0$
6998 /// - $f(-\infty,x)=\infty$ if $x<0$
6999 /// - $f(0.0,x)=0.0$ if $x>0$
7000 /// - $f(0.0,x)=-0.0$ if $x<0$
7001 /// - $f(-0.0,x)=-0.0$ if $x>0$
7002 /// - $f(-0.0,x)=0.0$ if $x<0$
7003 ///
7004 /// Overflow and underflow:
7005 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7006 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
7007 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7008 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7009 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
7010 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7011 ///
7012 /// If you want to use a rounding mode other than `Nearest`, consider using
7013 /// [`Float::div_rational_prec_ref_ref`] instead. If you want to specify the output precision,
7014 /// consider using [`Float::div_rational_round_ref_ref`]. If you want both of these things,
7015 /// consider using [`Float::div_rational_prec_round_ref_ref`].
7016 ///
7017 /// # Worst-case complexity
7018 /// $T(n) = O(n \log n \log\log n)$
7019 ///
7020 /// $M(n) = O(n \log n)$
7021 ///
7022 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
7023 /// other.significant_bits())`.
7024 ///
7025 /// # Examples
7026 /// ```
7027 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
7028 /// use malachite_base::num::conversion::traits::ExactFrom;
7029 /// use malachite_float::Float;
7030 /// use malachite_q::Rational;
7031 ///
7032 /// assert!((&Float::NAN / &Rational::exact_from(1.5)).is_nan());
7033 /// assert_eq!(
7034 /// &Float::INFINITY / &Rational::exact_from(1.5),
7035 /// Float::INFINITY
7036 /// );
7037 /// assert_eq!(
7038 /// &Float::NEGATIVE_INFINITY / &Rational::exact_from(1.5),
7039 /// Float::NEGATIVE_INFINITY
7040 /// );
7041 /// assert_eq!(
7042 /// &Float::INFINITY / &Rational::exact_from(-1.5),
7043 /// Float::NEGATIVE_INFINITY
7044 /// );
7045 /// assert_eq!(
7046 /// &Float::NEGATIVE_INFINITY / &Rational::exact_from(-1.5),
7047 /// Float::INFINITY
7048 /// );
7049 ///
7050 /// assert_eq!(
7051 /// (&Float::from(2.5) / &Rational::exact_from(1.5)).to_string(),
7052 /// "1.8"
7053 /// );
7054 /// assert_eq!(
7055 /// (&Float::from(2.5) / &Rational::exact_from(-1.5)).to_string(),
7056 /// "-1.8"
7057 /// );
7058 /// assert_eq!(
7059 /// (&Float::from(-2.5) / &Rational::exact_from(1.5)).to_string(),
7060 /// "-1.8"
7061 /// );
7062 /// assert_eq!(
7063 /// (&Float::from(-2.5) / &Rational::exact_from(-1.5)).to_string(),
7064 /// "1.8"
7065 /// );
7066 /// ```
7067 #[inline]
7068 fn div(self, other: &Rational) -> Float {
7069 let prec = self.significant_bits();
7070 self.div_rational_prec_round_ref_ref(other, prec, Nearest).0
7071 }
7072}
7073
7074impl DivAssign<Rational> for Float {
7075 /// Divides a [`Float`] by a [`Rational`] in place, taking the [`Rational`] by value.
7076 ///
7077 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
7078 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
7079 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
7080 /// rounding mode.
7081 ///
7082 /// $$
7083 /// x\gets = x/y+\varepsilon.
7084 /// $$
7085 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7086 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
7087 /// where $p$ is the precision of the input [`Float`].
7088 ///
7089 /// See the `/` documentation for information on special cases, overflow, and underflow.
7090 ///
7091 /// If you want to use a rounding mode other than `Nearest`, consider using
7092 /// [`Float::div_rational_prec_assign`] instead. If you want to specify the output precision,
7093 /// consider using [`Float::div_rational_round_assign`]. If you want both of these things,
7094 /// consider using [`Float::div_rational_prec_round_assign`].
7095 ///
7096 /// # Worst-case complexity
7097 /// $T(n) = O(n \log n \log\log n)$
7098 ///
7099 /// $M(n) = O(n \log n)$
7100 ///
7101 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
7102 /// other.significant_bits())`.
7103 ///
7104 /// # Examples
7105 /// ```
7106 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
7107 /// use malachite_base::num::conversion::traits::ExactFrom;
7108 /// use malachite_float::Float;
7109 /// use malachite_q::Rational;
7110 ///
7111 /// let mut x = Float::NAN;
7112 /// x /= Rational::exact_from(1.5);
7113 /// assert!(x.is_nan());
7114 ///
7115 /// let mut x = Float::INFINITY;
7116 /// x /= Rational::exact_from(1.5);
7117 /// assert_eq!(x, Float::INFINITY);
7118 ///
7119 /// let mut x = Float::NEGATIVE_INFINITY;
7120 /// x /= Rational::exact_from(1.5);
7121 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
7122 ///
7123 /// let mut x = Float::INFINITY;
7124 /// x /= Rational::exact_from(-1.5);
7125 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
7126 ///
7127 /// let mut x = Float::NEGATIVE_INFINITY;
7128 /// x /= Rational::exact_from(-1.5);
7129 /// assert_eq!(x, Float::INFINITY);
7130 ///
7131 /// let mut x = Float::from(2.5);
7132 /// x /= Rational::exact_from(1.5);
7133 /// assert_eq!(x.to_string(), "1.8");
7134 /// ```
7135 #[inline]
7136 fn div_assign(&mut self, other: Rational) {
7137 let prec = self.significant_bits();
7138 self.div_rational_prec_round_assign(other, prec, Nearest);
7139 }
7140}
7141
7142impl DivAssign<&Rational> for Float {
7143 /// Divides a [`Float`] by a [`Rational`] in place, taking the [`Rational`] by reference.
7144 ///
7145 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
7146 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
7147 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
7148 /// rounding mode.
7149 ///
7150 /// $$
7151 /// x\gets = x/y+\varepsilon.
7152 /// $$
7153 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7154 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
7155 /// where $p$ is the precision of the input [`Float`].
7156 ///
7157 /// See the `/` documentation for information on special cases, overflow, and underflow.
7158 ///
7159 /// If you want to use a rounding mode other than `Nearest`, consider using
7160 /// [`Float::div_rational_prec_assign_ref`] instead. If you want to specify the output
7161 /// precision, consider using [`Float::div_rational_round_assign_ref`]. If you want both of
7162 /// these things, consider using [`Float::div_rational_prec_round_assign_ref`].
7163 ///
7164 /// # Worst-case complexity
7165 /// $T(n) = O(n \log n \log\log n)$
7166 ///
7167 /// $M(n) = O(n \log n)$
7168 ///
7169 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
7170 /// other.significant_bits())`.
7171 ///
7172 /// # Examples
7173 /// ```
7174 /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
7175 /// use malachite_base::num::conversion::traits::ExactFrom;
7176 /// use malachite_float::Float;
7177 /// use malachite_q::Rational;
7178 ///
7179 /// let mut x = Float::NAN;
7180 /// x /= &Rational::exact_from(1.5);
7181 /// assert!(x.is_nan());
7182 ///
7183 /// let mut x = Float::INFINITY;
7184 /// x /= &Rational::exact_from(1.5);
7185 /// assert_eq!(x, Float::INFINITY);
7186 ///
7187 /// let mut x = Float::NEGATIVE_INFINITY;
7188 /// x /= &Rational::exact_from(1.5);
7189 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
7190 ///
7191 /// let mut x = Float::INFINITY;
7192 /// x /= &Rational::exact_from(-1.5);
7193 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
7194 ///
7195 /// let mut x = Float::NEGATIVE_INFINITY;
7196 /// x /= &Rational::exact_from(-1.5);
7197 /// assert_eq!(x, Float::INFINITY);
7198 ///
7199 /// let mut x = Float::from(2.5);
7200 /// x /= &Rational::exact_from(1.5);
7201 /// assert_eq!(x.to_string(), "1.8");
7202 /// ```
7203 #[inline]
7204 fn div_assign(&mut self, other: &Rational) {
7205 let prec = self.significant_bits();
7206 self.div_rational_prec_round_assign_ref(other, prec, Nearest);
7207 }
7208}
7209
7210impl Div<Float> for Rational {
7211 type Output = Float;
7212
7213 /// Divides a [`Rational`] by a [`Float`], taking both by value.
7214 ///
7215 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
7216 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
7217 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
7218 /// rounding mode.
7219 ///
7220 /// $$
7221 /// f(x,y) = x/y+\varepsilon.
7222 /// $$
7223 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7224 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
7225 /// where $p$ is the precision of the input [`Float`].
7226 ///
7227 /// Special cases:
7228 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
7229 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
7230 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or $x=-0.0$
7231 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
7232 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or $x=-0.0$
7233 /// - $f(0,x,p,m)=0.0$ if $x>0$
7234 /// - $f(0,x,p,m)=-0.0$ if $x<0$
7235 ///
7236 /// Overflow and underflow:
7237 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7238 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
7239 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7240 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7241 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
7242 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7243 ///
7244 /// # Worst-case complexity
7245 /// $T(n) = O(n \log n \log\log n)$
7246 ///
7247 /// $M(n) = O(n \log n)$
7248 ///
7249 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
7250 /// other.significant_bits())`.
7251 ///
7252 /// # Examples
7253 /// ```
7254 /// use malachite_base::num::basic::traits::{
7255 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
7256 /// };
7257 /// use malachite_base::num::conversion::traits::ExactFrom;
7258 /// use malachite_float::Float;
7259 /// use malachite_q::Rational;
7260 ///
7261 /// assert!((Rational::exact_from(1.5) / Float::NAN).is_nan());
7262 /// assert_eq!(Rational::exact_from(1.5) / Float::ZERO, Float::INFINITY);
7263 /// assert_eq!(
7264 /// Rational::exact_from(1.5) / Float::NEGATIVE_ZERO,
7265 /// Float::NEGATIVE_INFINITY
7266 /// );
7267 /// assert_eq!(
7268 /// Rational::exact_from(-1.5) / Float::ZERO,
7269 /// Float::NEGATIVE_INFINITY
7270 /// );
7271 /// assert_eq!(
7272 /// Rational::exact_from(-1.5) / Float::NEGATIVE_ZERO,
7273 /// Float::INFINITY
7274 /// );
7275 ///
7276 /// assert_eq!(
7277 /// (Rational::exact_from(1.5) / Float::from(2.5)).to_string(),
7278 /// "0.62"
7279 /// );
7280 /// assert_eq!(
7281 /// (Rational::exact_from(-1.5) / Float::from(2.5)).to_string(),
7282 /// "-0.62"
7283 /// );
7284 /// assert_eq!(
7285 /// (Rational::exact_from(1.5) / Float::from(-2.5)).to_string(),
7286 /// "-0.62"
7287 /// );
7288 /// assert_eq!(
7289 /// (Rational::exact_from(-1.5) / Float::from(-2.5)).to_string(),
7290 /// "0.62"
7291 /// );
7292 /// ```
7293 #[inline]
7294 fn div(self, other: Float) -> Float {
7295 let prec = other.significant_bits();
7296 Float::rational_div_float_prec_round(self, other, prec, Nearest).0
7297 }
7298}
7299
7300impl Div<&Float> for Rational {
7301 type Output = Float;
7302
7303 /// Divides a [`Rational`] by a [`Float`], taking the [`Rational`] by value and the [`Float`] by
7304 /// reference.
7305 ///
7306 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
7307 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
7308 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
7309 /// rounding mode.
7310 ///
7311 /// $$
7312 /// f(x,y) = x/y+\varepsilon.
7313 /// $$
7314 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7315 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
7316 /// where $p$ is the precision of the input [`Float`].
7317 ///
7318 /// Special cases:
7319 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
7320 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
7321 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or $x=-0.0$
7322 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
7323 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or $x=-0.0$
7324 /// - $f(0,x,p,m)=0.0$ if $x>0$
7325 /// - $f(0,x,p,m)=-0.0$ if $x<0$
7326 ///
7327 /// Overflow and underflow:
7328 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7329 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
7330 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7331 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7332 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
7333 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7334 ///
7335 /// # Worst-case complexity
7336 /// $T(n) = O(n \log n \log\log n)$
7337 ///
7338 /// $M(n) = O(n \log n)$
7339 ///
7340 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
7341 /// other.significant_bits())`.
7342 ///
7343 /// # Examples
7344 /// ```
7345 /// use malachite_base::num::basic::traits::{
7346 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
7347 /// };
7348 /// use malachite_base::num::conversion::traits::ExactFrom;
7349 /// use malachite_float::Float;
7350 /// use malachite_q::Rational;
7351 ///
7352 /// assert!((Rational::exact_from(1.5) / &Float::NAN).is_nan());
7353 /// assert_eq!(Rational::exact_from(1.5) / &Float::ZERO, Float::INFINITY);
7354 /// assert_eq!(
7355 /// Rational::exact_from(1.5) / &Float::NEGATIVE_ZERO,
7356 /// Float::NEGATIVE_INFINITY
7357 /// );
7358 /// assert_eq!(
7359 /// Rational::exact_from(-1.5) / &Float::ZERO,
7360 /// Float::NEGATIVE_INFINITY
7361 /// );
7362 /// assert_eq!(
7363 /// Rational::exact_from(-1.5) / &Float::NEGATIVE_ZERO,
7364 /// Float::INFINITY
7365 /// );
7366 ///
7367 /// assert_eq!(
7368 /// (Rational::exact_from(1.5) / &Float::from(2.5)).to_string(),
7369 /// "0.62"
7370 /// );
7371 /// assert_eq!(
7372 /// (Rational::exact_from(-1.5) / &Float::from(2.5)).to_string(),
7373 /// "-0.62"
7374 /// );
7375 /// assert_eq!(
7376 /// (Rational::exact_from(1.5) / &Float::from(-2.5)).to_string(),
7377 /// "-0.62"
7378 /// );
7379 /// assert_eq!(
7380 /// (Rational::exact_from(-1.5) / &Float::from(-2.5)).to_string(),
7381 /// "0.62"
7382 /// );
7383 /// ```
7384 #[inline]
7385 fn div(self, other: &Float) -> Float {
7386 let prec = other.significant_bits();
7387 Float::rational_div_float_prec_round_val_ref(self, other, prec, Nearest).0
7388 }
7389}
7390
7391impl Div<Float> for &Rational {
7392 type Output = Float;
7393
7394 /// Divides a [`Rational`] by a [`Float`], taking the [`Rational`] by reference and the
7395 /// [`Float`] by value.
7396 ///
7397 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
7398 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
7399 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
7400 /// rounding mode.
7401 ///
7402 /// $$
7403 /// f(x,y) = x/y+\varepsilon.
7404 /// $$
7405 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7406 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
7407 /// where $p$ is the precision of the input [`Float`].
7408 ///
7409 /// Special cases:
7410 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
7411 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
7412 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or $x=-0.0$
7413 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
7414 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or $x=-0.0$
7415 /// - $f(0,x,p,m)=0.0$ if $x>0$
7416 /// - $f(0,x,p,m)=-0.0$ if $x<0$
7417 ///
7418 /// Overflow and underflow:
7419 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7420 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
7421 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7422 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7423 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
7424 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7425 ///
7426 /// # Worst-case complexity
7427 /// $T(n) = O(n \log n \log\log n)$
7428 ///
7429 /// $M(n) = O(n \log n)$
7430 ///
7431 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
7432 /// other.significant_bits())`.
7433 ///
7434 /// # Examples
7435 /// ```
7436 /// use malachite_base::num::basic::traits::{
7437 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
7438 /// };
7439 /// use malachite_base::num::conversion::traits::ExactFrom;
7440 /// use malachite_float::Float;
7441 /// use malachite_q::Rational;
7442 ///
7443 /// assert!((&Rational::exact_from(1.5) / Float::NAN).is_nan());
7444 /// assert_eq!(&Rational::exact_from(1.5) / Float::ZERO, Float::INFINITY);
7445 /// assert_eq!(
7446 /// &Rational::exact_from(1.5) / Float::NEGATIVE_ZERO,
7447 /// Float::NEGATIVE_INFINITY
7448 /// );
7449 /// assert_eq!(
7450 /// &Rational::exact_from(-1.5) / Float::ZERO,
7451 /// Float::NEGATIVE_INFINITY
7452 /// );
7453 /// assert_eq!(
7454 /// &Rational::exact_from(-1.5) / Float::NEGATIVE_ZERO,
7455 /// Float::INFINITY
7456 /// );
7457 ///
7458 /// assert_eq!(
7459 /// (&Rational::exact_from(1.5) / Float::from(2.5)).to_string(),
7460 /// "0.62"
7461 /// );
7462 /// assert_eq!(
7463 /// (&Rational::exact_from(-1.5) / Float::from(2.5)).to_string(),
7464 /// "-0.62"
7465 /// );
7466 /// assert_eq!(
7467 /// (&Rational::exact_from(1.5) / Float::from(-2.5)).to_string(),
7468 /// "-0.62"
7469 /// );
7470 /// assert_eq!(
7471 /// (&Rational::exact_from(-1.5) / Float::from(-2.5)).to_string(),
7472 /// "0.62"
7473 /// );
7474 /// ```
7475 #[inline]
7476 fn div(self, other: Float) -> Float {
7477 let prec = other.significant_bits();
7478 Float::rational_div_float_prec_round_ref_val(self, other, prec, Nearest).0
7479 }
7480}
7481
7482impl Div<&Float> for &Rational {
7483 type Output = Float;
7484
7485 /// Divides a [`Rational`] by a [`Float`], taking both by reference.
7486 ///
7487 /// If the output has a precision, it is the precision of the input [`Float`]. If the quotient
7488 /// is equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s
7489 /// in its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
7490 /// rounding mode.
7491 ///
7492 /// $$
7493 /// f(x,y) = x/y+\varepsilon.
7494 /// $$
7495 /// - If $x/y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7496 /// - If $x/y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x/y|\rfloor-p}$,
7497 /// where $p$ is the precision of the input [`Float`].
7498 ///
7499 /// Special cases:
7500 /// - $f(x,\text{NaN},p,m)=f(0,\pm0.0,p,m)=\text{NaN}$
7501 /// - $f(x,\infty,x,p,m)=0.0$ if $x>0.0$ or $x=0.0$
7502 /// - $f(x,\infty,x,p,m)=-0.0$ if $x<0.0$ or $x=-0.0$
7503 /// - $f(x,-\infty,x,p,m)=-0.0$ if $x>0.0$ or $x=0.0$
7504 /// - $f(x,-\infty,x,p,m)=0.0$ if $x<0.0$ or $x=-0.0$
7505 /// - $f(0,x,p,m)=0.0$ if $x>0$
7506 /// - $f(0,x,p,m)=-0.0$ if $x<0$
7507 ///
7508 /// Overflow and underflow:
7509 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7510 /// - If $f(x,y)\geq 2^{2^{30}-1}$, $-\infty$ is returned instead.
7511 /// - If $0<f(x,y)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7512 /// - If $2^{-2^{30}-1}<f(x,y)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7513 /// - If $-2^{-2^{30}-1}\leq f(x,y)<0$, $-0.0$ is returned instead.
7514 /// - If $-2^{-2^{30}}<f(x,y)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
7515 ///
7516 /// # Worst-case complexity
7517 /// $T(n) = O(n \log n \log\log n)$
7518 ///
7519 /// $M(n) = O(n \log n)$
7520 ///
7521 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
7522 /// other.significant_bits())`.
7523 ///
7524 /// # Examples
7525 /// ```
7526 /// use malachite_base::num::basic::traits::{
7527 /// Infinity, NaN, NegativeInfinity, NegativeZero, Zero,
7528 /// };
7529 /// use malachite_base::num::conversion::traits::ExactFrom;
7530 /// use malachite_float::Float;
7531 /// use malachite_q::Rational;
7532 ///
7533 /// assert!((&Rational::exact_from(1.5) / &Float::NAN).is_nan());
7534 /// assert_eq!(&Rational::exact_from(1.5) / &Float::ZERO, Float::INFINITY);
7535 /// assert_eq!(
7536 /// &Rational::exact_from(1.5) / &Float::NEGATIVE_ZERO,
7537 /// Float::NEGATIVE_INFINITY
7538 /// );
7539 /// assert_eq!(
7540 /// &Rational::exact_from(-1.5) / &Float::ZERO,
7541 /// Float::NEGATIVE_INFINITY
7542 /// );
7543 /// assert_eq!(
7544 /// &Rational::exact_from(-1.5) / &Float::NEGATIVE_ZERO,
7545 /// Float::INFINITY
7546 /// );
7547 ///
7548 /// assert_eq!(
7549 /// (&Rational::exact_from(1.5) / &Float::from(2.5)).to_string(),
7550 /// "0.62"
7551 /// );
7552 /// assert_eq!(
7553 /// (&Rational::exact_from(-1.5) / &Float::from(2.5)).to_string(),
7554 /// "-0.62"
7555 /// );
7556 /// assert_eq!(
7557 /// (&Rational::exact_from(1.5) / &Float::from(-2.5)).to_string(),
7558 /// "-0.62"
7559 /// );
7560 /// assert_eq!(
7561 /// (&Rational::exact_from(-1.5) / &Float::from(-2.5)).to_string(),
7562 /// "0.62"
7563 /// );
7564 /// ```
7565 #[inline]
7566 fn div(self, other: &Float) -> Float {
7567 let prec = other.significant_bits();
7568 Float::rational_div_float_prec_round_ref_ref(self, other, prec, Nearest).0
7569 }
7570}