malachite_float/arithmetic/reciprocal.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
// Copyright © 2025 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
use crate::InnerFloat::{Finite, Infinity, NaN, Zero};
use crate::{float_nan, Float};
use core::cmp::Ordering::{self, *};
use malachite_base::num::arithmetic::traits::{
IsPowerOf2, NegAssign, Reciprocal, ReciprocalAssign,
};
use malachite_base::num::conversion::traits::ExactFrom;
use malachite_base::num::logic::traits::SignificantBits;
use malachite_base::rounding_modes::RoundingMode::{self, *};
use malachite_nz::natural::arithmetic::float_reciprocal::reciprocal_float_significand_ref;
impl Float {
/// Takes the reciprocal of a [`Float`], rounding the result to the specified precision and with
/// the specified rounding mode. The [`Float`] is taken by value. An [`Ordering`] is also
/// returned, indicating whether the rounded reciprocal is less than, equal to, or greater than
/// the exact reciprocal. Although `NaN`s are not comparable to any [`Float`], whenever this
/// function returns a `NaN` it also returns `Equal`.
///
/// See [`RoundingMode`] for a description of the possible rounding modes.
///
/// $$
/// f(x,p,m) = 1/x+\varepsilon.
/// $$
/// - If $1/x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
/// - If $1/x$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
/// 2^{\lfloor\log_2 |1/x|\rfloor-p+1}$.
/// - If $1/x$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
/// 2^{\lfloor\log_2 |1/x|\rfloor-p}$.
///
/// If the output has a precision, it is `prec`.
///
/// Special cases:
/// - $f(\text{NaN},p,m)=\text{NaN}$
/// - $f(\infty,p,m)=0.0$
/// - $f(-\infty,p,m)=-0.0$
/// - $f(0.0,p,m)=\infty$
/// - $f(-0.0,p,m)=-\infty$
///
/// If you know you'll be using `Nearest`, consider using [`Float::reciprocal_prec`] instead. If
/// you know that your target precision is the precision of the input, consider using
/// [`Float::reciprocal_round`] instead. If both of these things are true, consider using
/// [`Float::reciprocal`] instead.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
/// prec)`.
///
/// # Panics
/// Panics if `rm` is `Exact` but `prec` is too small for an exact reciprocation.
///
/// # Examples
/// ```
/// use core::f64::consts::PI;
/// use malachite_base::rounding_modes::RoundingMode::*;
/// use malachite_float::Float;
/// use std::cmp::Ordering::*;
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec_round(5, Floor);
/// assert_eq!(reciprocal.to_string(), "0.31");
/// assert_eq!(o, Less);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec_round(5, Ceiling);
/// assert_eq!(reciprocal.to_string(), "0.33");
/// assert_eq!(o, Greater);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec_round(5, Nearest);
/// assert_eq!(reciprocal.to_string(), "0.31");
/// assert_eq!(o, Less);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec_round(20, Floor);
/// assert_eq!(reciprocal.to_string(), "0.3183098");
/// assert_eq!(o, Less);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec_round(20, Ceiling);
/// assert_eq!(reciprocal.to_string(), "0.3183103");
/// assert_eq!(o, Greater);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec_round(20, Nearest);
/// assert_eq!(reciprocal.to_string(), "0.3183098");
/// assert_eq!(o, Less);
/// ```
#[inline]
pub fn reciprocal_prec_round(mut self, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
let o = self.reciprocal_prec_round_assign(prec, rm);
(self, o)
}
/// Takes the reciprocal of a [`Float`], rounding the result to the specified precision and with
/// the specified rounding mode. The [`Float`] is taken by reference. An [`Ordering`] is also
/// returned, indicating whether the rounded reciprocal is less than, equal to, or greater than
/// the exact reciprocal. Although `NaN`s are not comparable to any [`Float`], whenever this
/// function returns a `NaN` it also returns `Equal`.
///
/// See [`RoundingMode`] for a description of the possible rounding modes.
///
/// $$
/// f(x,p,m) = 1/x+\varepsilon.
/// $$
/// - If $1/x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
/// - If $1/x$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
/// 2^{\lfloor\log_2 |1/x|\rfloor-p+1}$.
/// - If $1/x$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
/// 2^{\lfloor\log_2 |1/x|\rfloor-p}$.
///
/// If the output has a precision, it is `prec`.
///
/// Special cases:
/// - $f(\text{NaN},p,m)=\text{NaN}$
/// - $f(\infty,p,m)=0.0$
/// - $f(-\infty,p,m)=-0.0$
/// - $f(0.0,p,m)=\infty$
/// - $f(-0.0,p,m)=-\infty$
///
/// If you know you'll be using `Nearest`, consider using [`Float::reciprocal_prec_ref`]
/// instead. If you know that your target precision is the precision of the input, consider
/// using [`Float::reciprocal_round_ref`] instead. If both of these things are true, consider
/// using `(&Float)::reciprocal()` instead.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
/// prec)`.
///
/// # Panics
/// Panics if `rm` is `Exact` but `prec` is too small for an exact reciprocation.
///
/// # Examples
/// ```
/// use core::f64::consts::PI;
/// use malachite_base::rounding_modes::RoundingMode::*;
/// use malachite_float::Float;
/// use std::cmp::Ordering::*;
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec_round_ref(5, Floor);
/// assert_eq!(reciprocal.to_string(), "0.31");
/// assert_eq!(o, Less);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec_round_ref(5, Ceiling);
/// assert_eq!(reciprocal.to_string(), "0.33");
/// assert_eq!(o, Greater);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec_round_ref(5, Nearest);
/// assert_eq!(reciprocal.to_string(), "0.31");
/// assert_eq!(o, Less);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec_round_ref(20, Floor);
/// assert_eq!(reciprocal.to_string(), "0.3183098");
/// assert_eq!(o, Less);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec_round_ref(20, Ceiling);
/// assert_eq!(reciprocal.to_string(), "0.3183103");
/// assert_eq!(o, Greater);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec_round_ref(20, Nearest);
/// assert_eq!(reciprocal.to_string(), "0.3183098");
/// assert_eq!(o, Less);
/// ```
#[inline]
pub fn reciprocal_prec_round_ref(&self, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
assert_ne!(prec, 0);
match self {
float_nan!() => (float_nan!(), Equal),
Float(Zero { sign }) => (Float(Infinity { sign: *sign }), Equal),
Float(Infinity { sign }) => (Float(Zero { sign: *sign }), Equal),
Float(Finite {
sign,
exponent: exp,
precision: x_prec,
significand: x,
}) => {
if x.is_power_of_2() {
let (reciprocal, o) = Float::power_of_2_prec(i64::from(1 - exp), prec);
return if *sign {
(reciprocal, o)
} else {
(-reciprocal, o.reverse())
};
}
let sign = *sign;
let (reciprocal, exp_offset, o) =
reciprocal_float_significand_ref(x, *x_prec, prec, if sign { rm } else { -rm });
let exp = 1i32
.checked_sub(*exp)
.unwrap()
.checked_add(i32::exact_from(exp_offset))
.unwrap();
(
Float(Finite {
sign,
exponent: exp,
precision: prec,
significand: reciprocal,
}),
if sign { o } else { o.reverse() },
)
}
}
}
/// Takes the reciprocal of a [`Float`], rounding the result to the nearest value of the
/// specified precision. The [`Float`] is taken by value. An [`Ordering`] is also returned,
/// indicating whether the rounded reciprocal is less than, equal to, or greater than the exact
/// reciprocal. Although `NaN`s are not comparable to any [`Float`], whenever this function
/// returns a `NaN` it also returns `Equal`.
///
/// If the reciprocal is equidistant from two [`Float`]s with the specified precision, the
/// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
/// description of the `Nearest` rounding mode.
///
/// $$
/// f(x,p) = 1/x+\varepsilon.
/// $$
/// - If $1/x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
/// - If $1/x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |1/x|\rfloor-p}$.
///
/// If the output has a precision, it is `prec`.
///
/// Special cases:
/// - $f(\text{NaN},p)=\text{NaN}$
/// - $f(\infty,p)=0.0$
/// - $f(-\infty,p)=-0.0$
/// - $f(0.0,p)=\infty$
/// - $f(-0.0,p)=-\infty$
///
/// If you want to use a rounding mode other than `Nearest`, consider using
/// [`Float::reciprocal_prec_round`] instead. If you know that your target precision is the
/// precision of the input, consider using [`Float::reciprocal`] instead.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
/// prec)`.
///
/// # Examples
/// ```
/// use core::f64::consts::PI;
/// use malachite_float::Float;
/// use std::cmp::Ordering::*;
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec(5);
/// assert_eq!(reciprocal.to_string(), "0.31");
/// assert_eq!(o, Less);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec(20);
/// assert_eq!(reciprocal.to_string(), "0.3183098");
/// assert_eq!(o, Less);
/// ```
#[inline]
pub fn reciprocal_prec(self, prec: u64) -> (Float, Ordering) {
self.reciprocal_prec_round(prec, Nearest)
}
/// Takes the reciprocal of a [`Float`], rounding the result to the nearest value of the
/// specified precision. The [`Float`] is taken by reference. An [`Ordering`] is also returned,
/// indicating whether the rounded reciprocal is less than, equal to, or greater than the exact
/// reciprocal. Although `NaN`s are not comparable to any [`Float`], whenever this function
/// returns a `NaN` it also returns `Equal`.
///
/// If the reciprocal is equidistant from two [`Float`]s with the specified precision, the
/// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
/// description of the `Nearest` rounding mode.
///
/// $$
/// f(x,p) = 1/x+\varepsilon.
/// $$
/// - If $1/x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
/// - If $1/x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |1/x|\rfloor-p}$.
///
/// If the output has a precision, it is `prec`.
///
/// Special cases:
/// - $f(\text{NaN},p)=\text{NaN}$
/// - $f(\infty,p)=0.0$
/// - $f(-\infty,p)=-0.0$
/// - $f(0.0,p)=\infty$
/// - $f(-0.0,p)=-\infty$
///
/// If you want to use a rounding mode other than `Nearest`, consider using
/// [`Float::reciprocal_prec_round_ref`] instead. If you know that your target precision is the
/// precision of the input, consider using `(&Float)::reciprocal()` instead.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
/// prec)`.
///
/// # Examples
/// ```
/// use core::f64::consts::PI;
/// use malachite_float::Float;
/// use std::cmp::Ordering::*;
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec_ref(5);
/// assert_eq!(reciprocal.to_string(), "0.31");
/// assert_eq!(o, Less);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_prec_ref(20);
/// assert_eq!(reciprocal.to_string(), "0.3183098");
/// assert_eq!(o, Less);
/// ```
#[inline]
pub fn reciprocal_prec_ref(&self, prec: u64) -> (Float, Ordering) {
self.reciprocal_prec_round_ref(prec, Nearest)
}
/// Takes the reciprocal of a [`Float`], rounding the result with the specified rounding mode.
/// The [`Float`] is taken by value. An [`Ordering`] is also returned, indicating whether the
/// rounded reciprocal is less than, equal to, or greater than the exact reciprocal. Although
/// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
/// returns `Equal`.
///
/// The precision of the output is the precision of the input. See [`RoundingMode`] for a
/// description of the possible rounding modes.
///
/// $$
/// f(x,y,m) = 1/x+\varepsilon.
/// $$
/// - If $1/x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
/// - If $1/x$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
/// 2^{\lfloor\log_2 |1/x|\rfloor-p+1}$, where $p$ is the precision of the input.
/// - If $1/x$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
/// 2^{\lfloor\log_2 |1/x|\rfloor-p}$, where $p$ is the precision of the input.
///
/// If the output has a precision, it is the precision of the input.
///
/// Special cases:
/// - $f(\text{NaN},m)=\text{NaN}$
/// - $f(\infty,m)=0.0$
/// - $f(-\infty,m)=-0.0$
/// - $f(0.0,m)=\infty$
/// - $f(-0.0,m)=-\infty$
///
/// If you want to specify an output precision, consider using [`Float::reciprocal_prec_round`]
/// instead. If you know you'll be using the `Nearest` rounding mode, consider using
/// [`Float::reciprocal`] instead.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Panics
/// Panics if `rm` is `Exact` but the precision of the input is not high enough to represent the
/// output.
///
/// # Examples
/// ```
/// use core::f64::consts::PI;
/// use malachite_base::rounding_modes::RoundingMode::*;
/// use malachite_float::Float;
/// use std::cmp::Ordering::*;
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_round(Floor);
/// assert_eq!(reciprocal.to_string(), "0.3183098861837905");
/// assert_eq!(o, Less);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_round(Ceiling);
/// assert_eq!(reciprocal.to_string(), "0.318309886183791");
/// assert_eq!(o, Greater);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_round(Nearest);
/// assert_eq!(reciprocal.to_string(), "0.3183098861837905");
/// assert_eq!(o, Less);
/// ```
#[inline]
pub fn reciprocal_round(self, rm: RoundingMode) -> (Float, Ordering) {
let prec = self.significant_bits();
self.reciprocal_prec_round(prec, rm)
}
/// Takes the reciprocal of a [`Float`], rounding the result with the specified rounding mode.
/// The [`Float`] is taken by reference. An [`Ordering`] is also returned, indicating whether
/// the rounded reciprocal is less than, equal to, or greater than the exact reciprocal.
/// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
/// it also returns `Equal`.
///
/// The precision of the output is the precision of the input. See [`RoundingMode`] for a
/// description of the possible rounding modes.
///
/// $$
/// f(x,y,m) = 1/x+\varepsilon.
/// $$
/// - If $1/x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
/// - If $1/x$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
/// 2^{\lfloor\log_2 |1/x|\rfloor-p+1}$, where $p$ is the precision of the input.
/// - If $1/x$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
/// 2^{\lfloor\log_2 |1/x|\rfloor-p}$, where $p$ is the precision of the input.
///
/// If the output has a precision, it is the precision of the input.
///
/// Special cases:
/// - $f(\text{NaN},m)=\text{NaN}$
/// - $f(\infty,m)=0.0$
/// - $f(-\infty,m)=-0.0$
/// - $f(0.0,m)=\infty$
/// - $f(-0.0,m)=-\infty$
///
/// If you want to specify an output precision, consider using
/// [`Float::reciprocal_prec_round_ref`] instead. If you know you'll be using the `Nearest`
/// rounding mode, consider using `(&Float)::reciprocal()` instead.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Panics
/// Panics if `rm` is `Exact` but the precision of the input is not high enough to represent the
/// output.
///
/// # Examples
/// ```
/// use core::f64::consts::PI;
/// use malachite_base::rounding_modes::RoundingMode::*;
/// use malachite_float::Float;
/// use std::cmp::Ordering::*;
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_round_ref(Floor);
/// assert_eq!(reciprocal.to_string(), "0.3183098861837905");
/// assert_eq!(o, Less);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_round_ref(Ceiling);
/// assert_eq!(reciprocal.to_string(), "0.318309886183791");
/// assert_eq!(o, Greater);
///
/// let (reciprocal, o) = Float::from(PI).reciprocal_round_ref(Nearest);
/// assert_eq!(reciprocal.to_string(), "0.3183098861837905");
/// assert_eq!(o, Less);
/// ```
#[inline]
pub fn reciprocal_round_ref(&self, rm: RoundingMode) -> (Float, Ordering) {
let prec = self.significant_bits();
self.reciprocal_prec_round_ref(prec, rm)
}
/// Takes the reciprocal of a [`Float`] in place, rounding the result to the specified precision
/// and with the specified rounding mode. An [`Ordering`] is returned, indicating whether the
/// rounded reciprocal is less than, equal to, or greater than the exact reciprocal. Although
/// `NaN`s are not comparable to any [`Float`], whenever this function sets the [`Float`] to
/// `NaN` it also returns `Equal`.
///
/// See [`RoundingMode`] for a description of the possible rounding modes.
///
/// $$
/// x \gets 1/x+\varepsilon.
/// $$
/// - If $1/x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
/// - If $1/x$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
/// 2^{\lfloor\log_2 |xy|\rfloor-p+1}$.
/// - If $1/x$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
/// 2^{\lfloor\log_2 |1/x|\rfloor-p}$.
///
/// If the output has a precision, it is `prec`.
///
/// See the [`Float::reciprocal_prec_round`] documentation for information on special cases.
///
/// If you know you'll be using `Nearest`, consider using [`Float::reciprocal_prec_assign`]
/// instead. If you know that your target precision is the precision of the input, consider
/// using [`Float::reciprocal_round_assign`] instead. If both of these things are true, consider
/// using [`Float::reciprocal_assign`] instead.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
/// prec)`.
///
/// # Panics
/// Panics if `rm` is `Exact` but `prec` is too small for an exact reciprocation;
///
/// # Examples
/// ```
/// use core::f64::consts::PI;
/// use malachite_base::rounding_modes::RoundingMode::*;
/// use malachite_float::Float;
/// use std::cmp::Ordering::*;
///
/// let mut x = Float::from(PI);
/// assert_eq!(x.reciprocal_prec_round_assign(5, Floor), Less);
/// assert_eq!(x.to_string(), "0.31");
///
/// let mut x = Float::from(PI);
/// assert_eq!(x.reciprocal_prec_round_assign(5, Ceiling), Greater);
/// assert_eq!(x.to_string(), "0.33");
///
/// let mut x = Float::from(PI);
/// assert_eq!(x.reciprocal_prec_round_assign(5, Nearest), Less);
/// assert_eq!(x.to_string(), "0.31");
///
/// let mut x = Float::from(PI);
/// assert_eq!(x.reciprocal_prec_round_assign(20, Floor), Less);
/// assert_eq!(x.to_string(), "0.3183098");
///
/// let mut x = Float::from(PI);
/// assert_eq!(x.reciprocal_prec_round_assign(20, Ceiling), Greater);
/// assert_eq!(x.to_string(), "0.3183103");
///
/// let mut x = Float::from(PI);
/// assert_eq!(x.reciprocal_prec_round_assign(20, Nearest), Less);
/// assert_eq!(x.to_string(), "0.3183098");
/// ```
#[inline]
pub fn reciprocal_prec_round_assign(&mut self, prec: u64, rm: RoundingMode) -> Ordering {
assert_ne!(prec, 0);
match &mut *self {
float_nan!() => Equal,
Float(Zero { sign }) => {
*self = Float(Infinity { sign: *sign });
Equal
}
Float(Infinity { sign }) => {
*self = Float(Zero { sign: *sign });
Equal
}
Float(Finite {
sign,
exponent: exp,
precision: x_prec,
significand: x,
}) => {
if x.is_power_of_2() {
let sign = *sign;
let o;
(*self, o) = Float::power_of_2_prec(i64::from(1 - *exp), prec);
return if sign {
o
} else {
self.neg_assign();
o.reverse()
};
}
let sign = *sign;
let (reciprocal, exp_offset, o) =
reciprocal_float_significand_ref(x, *x_prec, prec, if sign { rm } else { -rm });
*exp = 1i32
.checked_sub(*exp)
.unwrap()
.checked_add(i32::exact_from(exp_offset))
.unwrap();
*x_prec = prec;
*x = reciprocal;
if sign {
o
} else {
o.reverse()
}
}
}
}
/// Takes the reciprocal of a [`Float`] in place, rounding the result to the nearest value of
/// the specified precision. An [`Ordering`] is returned, indicating whether the rounded
/// reciprocal is less than, equal to, or greater than the exact reciprocal. Although `NaN`s are
/// not comparable to any [`Float`], whenever this function sets the [`Float`] to `NaN` it also
/// returns `Equal`.
///
/// If the reciprocal is equidistant from two [`Float`]s with the specified precision, the
/// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
/// description of the `Nearest` rounding mode.
///
/// $$
/// x \gets 1/x+\varepsilon.
/// $$
/// - If $1/x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
/// - If $1/x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |1/x|\rfloor-p}$.
///
/// If the output has a precision, it is `prec`.
///
/// See the [`Float::reciprocal_prec`] documentation for information on special cases.
///
/// If you want to use a rounding mode other than `Nearest`, consider using
/// [`Float::reciprocal_prec_round_assign`] instead. If you know that your target precision is
/// the precision of the input, consider using [`Float::reciprocal`] instead.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
/// prec)`.
///
/// # Examples
/// ```
/// use core::f64::consts::PI;
/// use malachite_float::Float;
/// use std::cmp::Ordering::*;
///
/// let mut x = Float::from(PI);
/// assert_eq!(x.reciprocal_prec_assign(5), Less);
/// assert_eq!(x.to_string(), "0.31");
///
/// let mut x = Float::from(PI);
/// assert_eq!(x.reciprocal_prec_assign(20), Less);
/// assert_eq!(x.to_string(), "0.3183098");
/// ```
#[inline]
pub fn reciprocal_prec_assign(&mut self, prec: u64) -> Ordering {
self.reciprocal_prec_round_assign(prec, Nearest)
}
/// Takes the reciprocal of a [`Float`] in place, rounding the result with the specified
/// rounding mode. An [`Ordering`] is returned, indicating whether the rounded reciprocal is
/// less than, equal to, or greater than the exact reciprocal. Although `NaN`s are not
/// comparable to any [`Float`], whenever this function sets the [`Float`] to `NaN` it also
/// returns `Equal`.
///
/// The precision of the output is the precision of the input. See [`RoundingMode`] for a
/// description of the possible rounding modes.
///
/// $$
/// x \gets 1/x+\varepsilon.
/// $$
/// - If $1/x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
/// - If $1/x$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
/// 2^{\lfloor\log_2 |1/x|\rfloor-p+1}$, where $p$ is the maximum precision of the inputs.
/// - If $1/x$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| <
/// 2^{\lfloor\log_2 |1/x|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
///
/// If the output has a precision, it is the precision of the input.
///
/// See the [`Float::reciprocal_round`] documentation for information on special cases.
///
/// If you want to specify an output precision, consider using
/// [`Float::reciprocal_prec_round_assign`] instead. If you know you'll be using the `Nearest`
/// rounding mode, consider using [`Float::reciprocal_assign`] instead.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Panics
/// Panics if `rm` is `Exact` but the precision of the input is not high enough to represent the
/// output.
///
/// # Examples
/// ```
/// use core::f64::consts::PI;
/// use malachite_base::rounding_modes::RoundingMode::*;
/// use malachite_float::Float;
/// use std::cmp::Ordering::*;
///
/// let mut x = Float::from(PI);
/// assert_eq!(x.reciprocal_round_assign(Floor), Less);
/// assert_eq!(x.to_string(), "0.3183098861837905");
///
/// let mut x = Float::from(PI);
/// assert_eq!(x.reciprocal_round_assign(Ceiling), Greater);
/// assert_eq!(x.to_string(), "0.318309886183791");
///
/// let mut x = Float::from(PI);
/// assert_eq!(x.reciprocal_round_assign(Nearest), Less);
/// assert_eq!(x.to_string(), "0.3183098861837905");
/// ```
#[inline]
pub fn reciprocal_round_assign(&mut self, rm: RoundingMode) -> Ordering {
let prec = self.significant_bits();
self.reciprocal_prec_round_assign(prec, rm)
}
}
impl Reciprocal for Float {
type Output = Float;
/// Takes the reciprocal of a [`Float`], taking it by value.
///
/// If the output has a precision, it is the precision of the input. If the reciprocal is
/// equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in
/// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
/// rounding mode.
///
/// $$
/// f(x,y) = 1/x+\varepsilon.
/// $$
/// - If $1/x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
/// - If $1/x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |1/x|\rfloor-p}$,
/// where $p$ is the maximum precision of the inputs.
///
/// Special cases:
/// - $f(\text{NaN})=\text{NaN}$
/// - $f(\infty)=0.0$
/// - $f(-\infty)=-0.0$
/// - $f(0.0)=\infty$
/// - $f(-0.0)=-\infty$
///
/// If you want to use a rounding mode other than `Nearest`, consider using
/// [`Float::reciprocal_prec`] instead. If you want to specify the output precision, consider
/// using [`Float::reciprocal_round`]. If you want both of these things, consider using
/// [`Float::reciprocal_prec_round`].
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::Reciprocal;
/// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
/// use malachite_float::Float;
///
/// assert!(Float::NAN.reciprocal().is_nan());
/// assert_eq!(Float::INFINITY.reciprocal().to_string(), "0.0");
/// assert_eq!(Float::NEGATIVE_INFINITY.reciprocal().to_string(), "-0.0");
/// assert_eq!(Float::from(1.5).reciprocal().to_string(), "0.8");
/// assert_eq!(Float::from(-1.5).reciprocal().to_string(), "-0.8");
/// ```
#[inline]
fn reciprocal(self) -> Float {
let prec = self.significant_bits();
self.reciprocal_prec_round(prec, Nearest).0
}
}
impl Reciprocal for &Float {
type Output = Float;
/// Takes the reciprocal of a [`Float`], taking it by reference.
///
/// If the output has a precision, it is the precision of the input. If the reciprocal is
/// equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in
/// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
/// rounding mode.
///
/// $$
/// f(x,y) = 1/x+\varepsilon.
/// $$
/// - If $1/x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
/// - If $1/x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |1/x|\rfloor-p}$,
/// where $p$ is the maximum precision of the inputs.
///
/// Special cases:
/// - $f(\text{NaN})=\text{NaN}$
/// - $f(\infty)=0.0$
/// - $f(-\infty)=-0.0$
/// - $f(0.0)=\infty$
/// - $f(-0.0)=-\infty$
///
/// If you want to use a rounding mode other than `Nearest`, consider using
/// [`Float::reciprocal_prec_ref`] instead. If you want to specify the output precision,
/// consider using [`Float::reciprocal_round_ref`]. If you want both of these things, consider
/// using [`Float::reciprocal_prec_round_ref`].
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::Reciprocal;
/// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
/// use malachite_float::Float;
///
/// assert!((&Float::NAN).reciprocal().is_nan());
/// assert_eq!((&Float::INFINITY).reciprocal().to_string(), "0.0");
/// assert_eq!((&Float::NEGATIVE_INFINITY).reciprocal().to_string(), "-0.0");
/// assert_eq!((&Float::from(1.5)).reciprocal().to_string(), "0.8");
/// assert_eq!((&Float::from(-1.5)).reciprocal().to_string(), "-0.8");
/// ```
#[inline]
fn reciprocal(self) -> Float {
let prec = self.significant_bits();
self.reciprocal_prec_round_ref(prec, Nearest).0
}
}
impl ReciprocalAssign for Float {
/// Takes the reciprocal of a [`Float`] in place.
///
/// If the output has a precision, it is the precision of the input. If the reciprocal is
/// equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in
/// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
/// rounding mode.
///
/// $$
/// x\gets = 1/x+\varepsilon.
/// $$
/// - If $1/x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
/// - If $1/x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |1/x|\rfloor-p}$,
/// where $p$ is the maximum precision of the inputs.
///
/// See the [`Float::reciprocal`] documentation for information on special cases.
///
/// If you want to use a rounding mode other than `Nearest`, consider using
/// [`Float::reciprocal_prec_assign`] instead. If you want to specify the output precision,
/// consider using [`Float::reciprocal_round_assign`]. If you want both of these things,
/// consider using [`Float::reciprocal_prec_round_assign`].
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::ReciprocalAssign;
/// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
/// use malachite_float::Float;
///
/// let mut x = Float::NAN;
/// x.reciprocal_assign();
/// assert!(x.is_nan());
///
/// let mut x = Float::INFINITY;
/// x.reciprocal_assign();
/// assert_eq!(x.to_string(), "0.0");
///
/// let mut x = Float::NEGATIVE_INFINITY;
/// x.reciprocal_assign();
/// assert_eq!(x.to_string(), "-0.0");
///
/// let mut x = Float::from(1.5);
/// x.reciprocal_assign();
/// assert_eq!(x.to_string(), "0.8");
///
/// let mut x = Float::from(-1.5);
/// x.reciprocal_assign();
/// assert_eq!(x.to_string(), "-0.8");
/// ```
#[inline]
fn reciprocal_assign(&mut self) {
let prec = self.significant_bits();
self.reciprocal_prec_round_assign(prec, Nearest);
}
}