pub struct F128 { /* private fields */ }
Expand description
A binary128 floating-point number (f128
).
This type can be used to
- convert between fixed-point numbers and the bit representation of 128-bit floating-point numbers.
- compare fixed-point numbers and the bit representation of 128-bit floating-point numbers.
This type does not support arithmetic or general analytic functions.
Please see Quadruple-precision floating-point format on Wikipedia for more information on binary128.
See also the fixed::f128::consts
module.
Examples
use fixed::{types::I16F16, F128};
assert_eq!(I16F16::ONE.to_num::<F128>(), F128::ONE);
assert_eq!(I16F16::from_num(F128::ONE), I16F16::ONE);
// fixed-point numbers can be compared directly to F128 values
assert!(I16F16::from_num(1.5) > F128::ONE);
assert!(I16F16::from_num(0.5) < F128::ONE);
Implementations§
source§impl F128
impl F128
sourcepub const MIN_POSITIVE_SUB: F128 = _
pub const MIN_POSITIVE_SUB: F128 = _
Smallest positive subnormal number.
Equal to 2MIN_EXP
− MANTISSA_DIGITS
.
sourcepub const MIN_POSITIVE: F128 = _
pub const MIN_POSITIVE: F128 = _
Smallest positive normal number.
Equal to 2MIN_EXP
− 1.
sourcepub const MAX: F128 = _
pub const MAX: F128 = _
Largest finite number.
Equal to
(1 − 2−MANTISSA_DIGITS
) 2MAX_EXP
.
sourcepub const NEG_INFINITY: F128 = _
pub const NEG_INFINITY: F128 = _
Negative infinity (−∞).
sourcepub const MANTISSA_DIGITS: u32 = 113u32
pub const MANTISSA_DIGITS: u32 = 113u32
Number of significant digits in base 2.
sourcepub const DIGITS: u32 = 33u32
pub const DIGITS: u32 = 33u32
Maximum x such that any decimal number with x significant
digits can be converted to F128
and back without loss.
Equal to
floor(log10 2MANTISSA_DIGITS
− 1).
sourcepub const EPSILON: F128 = _
pub const EPSILON: F128 = _
The difference between 1 and the next larger representable number.
Equal to 21 − MANTISSA_DIGITS
.
sourcepub const MIN_10_EXP: i32 = -4_931i32
pub const MIN_10_EXP: i32 = -4_931i32
Minimum x for which 10x is in the normal range
of F128
.
Equal to ceil(log10 MIN_POSITIVE
).
sourcepub const MAX_10_EXP: i32 = 4_932i32
pub const MAX_10_EXP: i32 = 4_932i32
sourcepub const fn from_bits(bits: u128) -> F128
pub const fn from_bits(bits: u128) -> F128
Raw transmutation from u128
.
Examples
use fixed::F128;
let infinity_bits = 0x7FFF_u128 << 112;
assert!(F128::from_bits(infinity_bits - 1).is_finite());
assert!(!F128::from_bits(infinity_bits).is_finite());
Examples found in repository?
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 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
pub const ZERO: F128 = F128::from_bits(0);
/// Negative zero (−0).
pub const NEG_ZERO: F128 = F128::from_bits(SIGN_MASK);
/// One.
pub const ONE: F128 = F128::from_bits((EXP_BIAS as u128) << (PREC - 1));
/// Negative one (−1).
pub const NEG_ONE: F128 = F128::from_bits(SIGN_MASK | F128::ONE.to_bits());
/// Smallest positive subnormal number.
///
/// Equal to 2<sup>[`MIN_EXP`] − [`MANTISSA_DIGITS`]</sup>.
///
/// [`MANTISSA_DIGITS`]: Self::MANTISSA_DIGITS
/// [`MIN_EXP`]: Self::MIN_EXP
pub const MIN_POSITIVE_SUB: F128 = F128::from_bits(1);
/// Smallest positive normal number.
///
/// Equal to 2<sup>[`MIN_EXP`] − 1</sup>.
///
/// [`MIN_EXP`]: Self::MIN_EXP
pub const MIN_POSITIVE: F128 = F128::from_bits(MANT_MASK + 1);
/// Largest finite number.
///
/// Equal to
/// (1 − 2<sup>−[`MANTISSA_DIGITS`]</sup>) 2<sup>[`MAX_EXP`]</sup>.
///
/// [`MANTISSA_DIGITS`]: Self::MANTISSA_DIGITS
/// [`MAX_EXP`]: Self::MAX_EXP
pub const MAX: F128 = F128::from_bits(EXP_MASK - 1);
/// Smallest finite number (−[`MAX`]).
///
/// [`MAX`]: Self::MAX
pub const MIN: F128 = F128::from_bits(SIGN_MASK | F128::MAX.to_bits());
/// Infinity (∞).
pub const INFINITY: F128 = F128::from_bits(EXP_MASK);
/// Negative infinity (−∞).
pub const NEG_INFINITY: F128 = F128::from_bits(SIGN_MASK | EXP_MASK);
/// NaN.
pub const NAN: F128 = F128::from_bits(EXP_MASK | (1u128 << (PREC - 2)));
/// The radix or base of the internal representation (2).
pub const RADIX: u32 = 2;
/// Number of significant digits in base 2.
pub const MANTISSA_DIGITS: u32 = PREC;
/// Maximum <i>x</i> such that any decimal number with <i>x</i> significant
/// digits can be converted to [`F128`] and back without loss.
///
/// Equal to
/// floor(log<sub>10</sub> 2<sup>[`MANTISSA_DIGITS`] − 1</sup>).
///
/// [`MANTISSA_DIGITS`]: Self::MANTISSA_DIGITS
pub const DIGITS: u32 = 33;
/// The difference between 1 and the next larger representable number.
///
/// Equal to 2<sup>1 − [`MANTISSA_DIGITS`]</sup>.
///
/// [`MANTISSA_DIGITS`]: Self::MANTISSA_DIGITS
pub const EPSILON: F128 = F128::from_bits(((EXP_BIAS - (PREC - 1)) as u128) << (PREC - 1));
/// If <i>x</i> = `MIN_EXP`, then normal numbers
/// ≥ 0.5 × 2<sup><i>x</i></sup>.
pub const MIN_EXP: i32 = 3 - F128::MAX_EXP;
/// If <i>x</i> = `MAX_EXP`, then normal numbers
/// < 1 × 2<sup><i>x</i></sup>.
pub const MAX_EXP: i32 = EXP_BIAS as i32 + 1;
/// Minimum <i>x</i> for which 10<sup><i>x</i></sup> is in the normal range
/// of [`F128`].
///
/// Equal to ceil(log<sub>10</sub> [`MIN_POSITIVE`]).
///
/// [`MIN_POSITIVE`]: Self::MIN_POSITIVE
pub const MIN_10_EXP: i32 = -4931;
/// Maximum <i>x</i> for which 10<sup><i>x</i></sup> is in the normal range
/// of [`F128`].
///
/// Equal to floor(log<sub>10</sub> [`MAX`]).
///
/// [`MAX`]: Self::MAX
pub const MAX_10_EXP: i32 = 4932;
/// Raw transmutation from [`u128`].
///
/// # Examples
///
/// ```rust
/// use fixed::F128;
/// let infinity_bits = 0x7FFF_u128 << 112;
/// assert!(F128::from_bits(infinity_bits - 1).is_finite());
/// assert!(!F128::from_bits(infinity_bits).is_finite());
/// ```
#[inline]
pub const fn from_bits(bits: u128) -> F128 {
F128 { bits }
}
/// Raw transmutation to [`u128`].
///
/// # Examples
///
/// ```rust
/// use fixed::F128;
/// assert_eq!(F128::ONE.to_bits(), 0x3FFF_u128 << 112);
/// assert_ne!(F128::ONE.to_bits(), 1u128);
/// ```
#[inline]
pub const fn to_bits(self) -> u128 {
self.bits
}
/// Creates a number from a byte array in big-endian byte order.
#[inline]
pub const fn from_be_bytes(bytes: [u8; 16]) -> F128 {
F128::from_bits(u128::from_be_bytes(bytes))
}
/// Creates a number from a byte array in little-endian byte order.
#[inline]
pub const fn from_le_bytes(bytes: [u8; 16]) -> F128 {
F128::from_bits(u128::from_le_bytes(bytes))
}
/// Creates a number from a byte array in native-endian byte order.
#[inline]
pub const fn from_ne_bytes(bytes: [u8; 16]) -> F128 {
F128::from_bits(u128::from_ne_bytes(bytes))
}
/// Returns the memory representation of the number as a byte array in
/// big-endian byte order.
#[inline]
pub const fn to_be_bytes(self) -> [u8; 16] {
self.to_bits().to_be_bytes()
}
/// Returns the memory representation of the number as a byte array in
/// little-endian byte order.
#[inline]
pub const fn to_le_bytes(self) -> [u8; 16] {
self.to_bits().to_le_bytes()
}
/// Returns the memory representation of the number as a byte array in
/// native-endian byte order.
#[inline]
pub const fn to_ne_bytes(self) -> [u8; 16] {
self.to_bits().to_ne_bytes()
}
/// Returns [`true`] if the number is NaN.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::NAN.is_nan());
///
/// assert!(!F128::ONE.is_nan());
/// assert!(!F128::INFINITY.is_nan());
/// assert!(!F128::NEG_INFINITY.is_nan());
/// ```
#[inline]
pub const fn is_nan(self) -> bool {
(self.to_bits() & !SIGN_MASK) > EXP_MASK
}
/// Returns [`true`] if the number is infinite.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::INFINITY.is_infinite());
/// assert!(F128::NEG_INFINITY.is_infinite());
///
/// assert!(!F128::ONE.is_infinite());
/// assert!(!F128::NAN.is_infinite());
/// ```
#[inline]
pub const fn is_infinite(self) -> bool {
(self.to_bits() & !SIGN_MASK) == EXP_MASK
}
/// Returns [`true`] if the number is neither infinite nor NaN.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::ONE.is_finite());
/// assert!(F128::MAX.is_finite());
///
/// assert!(!F128::INFINITY.is_finite());
/// assert!(!F128::NEG_INFINITY.is_finite());
/// assert!(!F128::NAN.is_finite());
/// ```
#[inline]
pub const fn is_finite(self) -> bool {
(self.to_bits() & EXP_MASK) != EXP_MASK
}
/// Returns [`true`] if the number is zero.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::ZERO.is_zero());
/// assert!(F128::NEG_ZERO.is_zero());
///
/// assert!(!F128::MIN_POSITIVE_SUB.is_zero());
/// assert!(!F128::NAN.is_zero());
/// ```
#[inline]
pub const fn is_zero(self) -> bool {
(self.to_bits() & !SIGN_MASK) == 0
}
/// Returns [`true`] if the number is subnormal.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::MIN_POSITIVE_SUB.is_subnormal());
///
/// assert!(!F128::ZERO.is_subnormal());
/// assert!(!F128::MIN_POSITIVE.is_subnormal());
/// ```
#[inline]
pub const fn is_subnormal(self) -> bool {
let abs = self.to_bits() & !SIGN_MASK;
0 < abs && abs < F128::MIN_POSITIVE.to_bits()
}
/// Returns [`true`] if the number is neither zero, infinite, subnormal, or NaN.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::MIN.is_normal());
/// assert!(F128::MIN_POSITIVE.is_normal());
/// assert!(F128::MAX.is_normal());
///
/// assert!(!F128::ZERO.is_normal());
/// assert!(!F128::MIN_POSITIVE_SUB.is_normal());
/// assert!(!F128::INFINITY.is_normal());
/// assert!(!F128::NAN.is_normal());
/// ```
#[inline]
pub const fn is_normal(self) -> bool {
let abs = self.to_bits() & !SIGN_MASK;
F128::MIN_POSITIVE.to_bits() <= abs && abs <= F128::MAX.to_bits()
}
/// Returns the floating point category of the number.
///
/// If only one property is going to be tested, it is generally faster to
/// use the specific predicate instead.
///
/// # Example
///
/// ```rust
/// use core::num::FpCategory;
/// use fixed::F128;
///
/// assert_eq!(F128::ZERO.classify(), FpCategory::Zero);
/// assert_eq!(F128::MIN_POSITIVE_SUB.classify(), FpCategory::Subnormal);
/// assert_eq!(F128::MIN_POSITIVE.classify(), FpCategory::Normal);
/// assert_eq!(F128::INFINITY.classify(), FpCategory::Infinite);
/// assert_eq!(F128::NAN.classify(), FpCategory::Nan);
/// ```
#[inline]
pub const fn classify(self) -> FpCategory {
let exp = self.to_bits() & EXP_MASK;
let mant = self.to_bits() & MANT_MASK;
if exp == 0 {
if mant == 0 {
FpCategory::Zero
} else {
FpCategory::Subnormal
}
} else if exp == EXP_MASK {
if mant == 0 {
FpCategory::Infinite
} else {
FpCategory::Nan
}
} else {
FpCategory::Normal
}
}
/// Returns the absolute value of the number.
///
/// The only difference possible between the input value and the returned
/// value is in the sign bit, which is always cleared in the return value.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// // -0 == +0, but -0 bits != +0 bits
/// assert_eq!(F128::NEG_ZERO, F128::ZERO);
/// assert_ne!(F128::NEG_ZERO.to_bits(), F128::ZERO.to_bits());
/// assert_eq!(F128::NEG_ZERO.abs().to_bits(), F128::ZERO.to_bits());
///
/// assert_eq!(F128::NEG_INFINITY.abs(), F128::INFINITY);
/// assert_eq!(F128::MIN.abs(), F128::MAX);
///
/// assert!(F128::NAN.abs().is_nan());
/// ```
#[inline]
pub const fn abs(self) -> F128 {
F128::from_bits(self.to_bits() & !SIGN_MASK)
}
/// Returns a number that represents the sign of the input value.
///
/// * 1 if the number is positive, +0, or +∞
/// * −1 if the number is negative, −0, or −∞
/// * NaN if the number is NaN
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert_eq!(F128::ONE.signum(), F128::ONE);
/// assert_eq!(F128::INFINITY.signum(), F128::ONE);
/// assert_eq!(F128::NEG_ZERO.signum(), F128::NEG_ONE);
/// assert_eq!(F128::MIN.signum(), F128::NEG_ONE);
///
/// assert!(F128::NAN.signum().is_nan());
/// ```
#[inline]
pub const fn signum(self) -> F128 {
if self.is_nan() {
self
} else if self.is_sign_positive() {
F128::ONE
} else {
F128::NEG_ONE
}
}
/// Returns a number composed of the magnitude of `self` and the sign of `sign`.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert_eq!(F128::ONE.copysign(F128::NEG_ZERO), F128::NEG_ONE);
/// assert_eq!(F128::ONE.copysign(F128::ZERO), F128::ONE);
/// assert_eq!(F128::NEG_ONE.copysign(F128::NEG_INFINITY), F128::NEG_ONE);
/// assert_eq!(F128::NEG_ONE.copysign(F128::INFINITY), F128::ONE);
///
/// assert!(F128::NAN.copysign(F128::ONE).is_nan());
/// assert!(F128::NAN.copysign(F128::ONE).is_sign_positive());
/// assert!(F128::NAN.copysign(F128::NEG_ONE).is_sign_negative());
/// ```
#[inline]
pub const fn copysign(self, sign: F128) -> F128 {
F128::from_bits((self.to_bits() & !SIGN_MASK) | (sign.to_bits() & SIGN_MASK))
}
/// Returns [`true`] if the number has a positive sign, including +0, +∞,
/// and NaN without a negative sign bit.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::ZERO.is_sign_positive());
/// assert!(F128::MAX.is_sign_positive());
/// assert!(F128::INFINITY.is_sign_positive());
///
/// assert!(!F128::NEG_ZERO.is_sign_positive());
/// assert!(!F128::MIN.is_sign_positive());
/// assert!(!F128::NEG_INFINITY.is_sign_positive());
/// ```
#[inline]
pub const fn is_sign_positive(self) -> bool {
(self.to_bits() & SIGN_MASK) == 0
}
/// Returns [`true`] if the number has a negative sign, including −0,
/// −∞, and NaN with a negative sign bit.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::NEG_ZERO.is_sign_negative());
/// assert!(F128::MIN.is_sign_negative());
/// assert!(F128::NEG_INFINITY.is_sign_negative());
///
/// assert!(!F128::ZERO.is_sign_negative());
/// assert!(!F128::MAX.is_sign_negative());
/// assert!(!F128::INFINITY.is_sign_negative());
/// ```
#[inline]
pub const fn is_sign_negative(self) -> bool {
(self.to_bits() & SIGN_MASK) != 0
}
/// Returns the maximum of two numbers, ignoring NaN.
///
/// If one of the arguments is NaN, then the other argument is returned.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert_eq!(F128::ZERO.max(F128::ONE), F128::ONE);
/// ```
#[inline]
pub const fn max(self, other: F128) -> F128 {
if self.is_nan() || matches!(partial_cmp(&self, &other), Some(Ordering::Less)) {
other
} else {
self
}
}
/// Returns the minimum of two numbers, ignoring NaN.
///
/// If one of the arguments is NaN, then the other argument is returned.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert_eq!(F128::ZERO.min(F128::ONE), F128::ZERO);
/// ```
#[inline]
pub const fn min(self, other: F128) -> F128 {
if self.is_nan() || matches!(partial_cmp(&self, &other), Some(Ordering::Greater)) {
other
} else {
self
}
}
/// Clamps the value within the specified bounds.
///
/// Returns `min` if `self` < `min`, `max` if
/// `self` > `max`, or `self` otherwise.
///
/// Note that this method returns NaN if the initial value is NaN.
///
/// # Panics
///
/// Panics if `min` > `max`, `min` is NaN, or `max` is NaN.
///
/// # Examples
///
/// ```
/// use fixed::F128;
/// assert_eq!(F128::MIN.clamp(F128::NEG_ONE, F128::ONE), F128::NEG_ONE);
/// assert_eq!(F128::ZERO.clamp(F128::NEG_ONE, F128::ONE), F128::ZERO);
/// assert_eq!(F128::MAX.clamp(F128::NEG_ONE, F128::ONE), F128::ONE);
/// assert!(F128::NAN.clamp(F128::NEG_ONE, F128::ONE).is_nan());
/// ```
#[inline]
pub const fn clamp(mut self, min: F128, max: F128) -> F128 {
match partial_cmp(&min, &max) {
Some(Ordering::Less) | Some(Ordering::Equal) => {}
_ => panic!("need min <= max"),
}
if matches!(partial_cmp(&self, &min), Some(Ordering::Less)) {
self = min;
}
if matches!(partial_cmp(&self, &max), Some(Ordering::Greater)) {
self = max;
}
self
}
/// Returns the ordering between `self` and `other`.
///
/// Unlike the [`PartialOrd`] implementation, this method always returns an
/// order in the following sequence:
///
/// * NaN with the sign bit set
/// * −∞
/// * negative normal numbers
/// * negative subnormal numbers
/// * −0
/// * +0
/// * positive subnormal numbers
/// * positive normal numbers
/// * +∞
/// * NaN with the sign bit cleared
///
/// # Example
///
/// ```rust
/// use core::cmp::Ordering;
/// use fixed::F128;
///
/// let neg_nan = F128::NAN.copysign(F128::NEG_ONE);
/// let pos_nan = F128::NAN.copysign(F128::ONE);
/// let neg_inf = F128::NEG_INFINITY;
/// let pos_inf = F128::INFINITY;
/// let neg_zero = F128::NEG_ZERO;
/// let pos_zero = F128::ZERO;
///
/// assert_eq!(neg_nan.total_cmp(&neg_inf), Ordering::Less);
/// assert_eq!(pos_nan.total_cmp(&pos_inf), Ordering::Greater);
/// assert_eq!(neg_zero.total_cmp(&pos_zero), Ordering::Less);
/// ```
#[inline]
pub const fn total_cmp(&self, other: &F128) -> Ordering {
let a = self.to_bits();
let b = other.to_bits();
match (self.is_sign_negative(), other.is_sign_negative()) {
(false, false) => cmp_bits(a, b),
(true, true) => cmp_bits(b, a),
(false, true) => Ordering::Greater,
(true, false) => Ordering::Less,
}
}
}
const fn cmp_bits(a: u128, b: u128) -> Ordering {
if a < b {
Ordering::Less
} else if a == b {
Ordering::Equal
} else {
Ordering::Greater
}
}
impl PartialEq for F128 {
#[inline]
fn eq(&self, other: &F128) -> bool {
if self.is_nan() || other.is_nan() {
return false;
}
let a = self.to_bits();
let b = other.to_bits();
// handle zero
if ((a | b) & !SIGN_MASK) == 0 {
return true;
}
a == b
}
}
impl PartialOrd for F128 {
#[inline]
fn partial_cmp(&self, other: &F128) -> Option<Ordering> {
partial_cmp(self, other)
}
}
#[inline]
const fn partial_cmp(a: &F128, b: &F128) -> Option<Ordering> {
if a.is_nan() || b.is_nan() {
return None;
}
let a_bits = a.to_bits();
let b_bits = b.to_bits();
// handle zero
if ((a_bits | b_bits) & !SIGN_MASK) == 0 {
return Some(Ordering::Equal);
}
match (a.is_sign_negative(), b.is_sign_negative()) {
(false, false) => Some(cmp_bits(a_bits, b_bits)),
(true, true) => Some(cmp_bits(b_bits, a_bits)),
(false, true) => Some(Ordering::Greater),
(true, false) => Some(Ordering::Less),
}
}
impl Hash for F128 {
#[inline]
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
let mut bits = self.to_bits();
if bits == F128::NEG_ZERO.to_bits() {
bits = 0;
}
bits.hash(state);
}
}
impl Neg for F128 {
type Output = F128;
#[inline]
fn neg(self) -> F128 {
F128::from_bits(self.to_bits() ^ SIGN_MASK)
}
}
macro_rules! from_float {
($f:ident, $u:ident) => {
impl From<$f> for F128 {
fn from(src: $f) -> F128 {
const PREC_S: u32 = $f::MANTISSA_DIGITS;
const EXP_BITS_S: u32 = $u::BITS - PREC_S;
const EXP_BIAS_S: u32 = (1 << (EXP_BITS_S - 1)) - 1;
const SIGN_MASK_S: $u = 1 << ($u::BITS - 1);
const EXP_MASK_S: $u = ((1 << EXP_BITS_S) - 1) << (PREC_S - 1);
const MANT_MASK_S: $u = (1 << (PREC_S - 1)) - 1;
let b = src.to_bits();
let sign_bit_s = b & SIGN_MASK_S;
let exp_bits_s = b & EXP_MASK_S;
let mant_bits_s = b & MANT_MASK_S;
let sign_bit = u128::from(sign_bit_s) << (u128::BITS - $u::BITS);
if exp_bits_s == EXP_MASK_S {
if mant_bits_s == 0 {
// infinity
return F128::from_bits(sign_bit | EXP_MASK);
}
// NaN; set most significant mantissa bit
let mant_bits =
(u128::from(mant_bits_s) << (PREC - PREC_S)) | (1 << (PREC - 2));
return F128::from_bits(sign_bit | EXP_MASK | mant_bits);
}
if exp_bits_s == 0 {
// subnormal
// Example: if for f64 mantissa == 0b1011 == 11, then it has 60
// leading zeros, and 64 - 60 == 4 significant bits. The value is
//
// 0b1011 × 2^(-1021 - 53) == 0b1.011 × 2^(-1021 - 53 + 4 - 1)
//
// In F128, this is normal, with
// * mantissa == (1011 << ((113 - 1) - (4 - 1))) & MANT_MASK_128
// == (1011 << (113 - 4)) & MANT_MASK_128
// == (1011 << (113 - 64 + 60)) & MANT_MASK_128
// * unbiased exp == -1021 - 53 + 4 - 1
// == -1021 - 53 - 1 + 64 - 60
if mant_bits_s == 0 {
return F128::from_bits(sign_bit);
}
let lz = mant_bits_s.leading_zeros();
let mant_bits = (u128::from(mant_bits_s) << (PREC - $u::BITS + lz)) & MANT_MASK;
let unbiased_exp =
$f::MIN_EXP - PREC_S as i32 - 1 + $u::BITS as i32 - lz as i32;
let exp_bits = ((unbiased_exp + EXP_BIAS as i32) as u128) << (PREC - 1);
return F128::from_bits(sign_bit | exp_bits | mant_bits);
}
let mant_bits = u128::from(mant_bits_s) << (PREC - PREC_S);
let dbias = (EXP_BIAS - EXP_BIAS_S) as u128;
let exp_bits = (u128::from(exp_bits_s >> (PREC_S - 1)) + dbias) << (PREC - 1);
F128::from_bits(sign_bit | exp_bits | mant_bits)
}
}
};
}
from_float! { f64, u64 }
from_float! { f32, u32 }
from_float! { f16, u16 }
from_float! { bf16, u16 }
/*
```rust
use core::{cmp::Ord, convert::TryFrom};
use rug::{
float::{Constant, Round},
Assign, Float, Integer,
};
fn decimal_string(val: &Float, prec: i32) -> String {
let log10 = val.clone().log10();
let floor_log10 = log10.to_i32_saturating_round(Round::Down).unwrap();
let shift = u32::try_from(prec - 1 - floor_log10).unwrap();
let val = val.clone() * Integer::from(Integer::u_pow_u(10, shift));
let int = val.to_integer_round(Round::Down).unwrap().0;
let padding = "0".repeat(usize::try_from(-floor_log10.min(0)).unwrap());
let mut s = format!("{}{}", padding, int);
s.insert(1, '.');
s
}
fn hex_bits(bits: u128) -> String {
let mut s = format!("0x{:016X}", bits);
for i in 0..7 {
s.insert(6 + 5 * i, '_');
}
s
}
fn print(doc: &str, name: &str, val: Float) {
println!();
println!(" /// {} = {}…", doc, decimal_string(&val, 6));
println!(" // {} = {}...", name, decimal_string(&val, 40));
let round = Float::with_val(113, &val);
let sign_bit = if round.is_sign_negative() {
1u128 << 127
} else {
0
};
let unbiased_exp = round.get_exp().unwrap();
assert!(-16_381 <= unbiased_exp && unbiased_exp <= 16_384);
let exp_bits = u128::from((unbiased_exp + 16_382).unsigned_abs()) << 112;
let unshifted_mant = round.get_significand().unwrap();
let mant = unshifted_mant.clone() >> (unshifted_mant.significant_bits() - 113);
let mant_128 = mant.to_u128_wrapping();
assert_eq!(mant_128 >> 112, 1);
let mant_bits = mant_128 & ((1 << 112) - 1);
println!(
" pub const {name}: F128 = F128::from_bits({});",
hex_bits(sign_bit | exp_bits | mant_bits)
);
}
fn float<T>(t: T) -> Float
where
Float: Assign<T>,
{
Float::with_val(1000, t)
}
fn main() {
println!("/// Basic mathematical constants.");
println!("pub mod consts {{");
println!(" use crate::F128;");
print("Archimedes’ constant, π", "PI", float(Constant::Pi));
print("A turn, τ", "TAU", float(Constant::Pi) * 2);
print("π/2", "FRAC_PI_2", float(Constant::Pi) / 2);
print("π/3", "FRAC_PI_3", float(Constant::Pi) / 3);
print("π/4", "FRAC_PI_4", float(Constant::Pi) / 4);
print("π/6", "FRAC_PI_6", float(Constant::Pi) / 6);
print("π/8", "FRAC_PI_8", float(Constant::Pi) / 8);
print("1/π", "FRAC_1_PI", 1 / float(Constant::Pi));
print("2/π", "FRAC_2_PI", 2 / float(Constant::Pi));
print("2/√π", "FRAC_2_SQRT_PI", 2 / float(Constant::Pi).sqrt());
print("√2", "SQRT_2", float(2).sqrt());
print("1/√2", "FRAC_1_SQRT_2", float(0.5).sqrt());
print("Euler’s number, e", "E", float(1).exp());
print("log<sub>2</sub> 10", "LOG2_10", float(10).log2());
print("log<sub>2</sub> e", "LOG2_E", float(1).exp().log2());
print("log<sub>10</sub> 2", "LOG10_2", float(2).log10());
print("log<sub>10</sub> e", "LOG10_E", float(1).exp().log10());
print("ln 2", "LN_2", float(2).ln());
print("ln 10", "LN_10", float(10).ln());
println!("}}");
}
```
*/
/// Basic mathematical constants.
pub mod consts {
use crate::F128;
/// Archimedes’ constant, π = 3.14159…
// PI = 3.141592653589793238462643383279502884197...
pub const PI: F128 = F128::from_bits(0x4000_921F_B544_42D1_8469_898C_C517_01B8);
/// A turn, τ = 6.28318…
// TAU = 6.283185307179586476925286766559005768394...
pub const TAU: F128 = F128::from_bits(0x4001_921F_B544_42D1_8469_898C_C517_01B8);
/// π/2 = 1.57079…
// FRAC_PI_2 = 1.570796326794896619231321691639751442098...
pub const FRAC_PI_2: F128 = F128::from_bits(0x3FFF_921F_B544_42D1_8469_898C_C517_01B8);
/// π/3 = 1.04719…
// FRAC_PI_3 = 1.047197551196597746154214461093167628065...
pub const FRAC_PI_3: F128 = F128::from_bits(0x3FFF_0C15_2382_D736_5846_5BB3_2E0F_567B);
/// π/4 = 0.785398…
// FRAC_PI_4 = 0.7853981633974483096156608458198757210492...
pub const FRAC_PI_4: F128 = F128::from_bits(0x3FFE_921F_B544_42D1_8469_898C_C517_01B8);
/// π/6 = 0.523598…
// FRAC_PI_6 = 0.5235987755982988730771072305465838140328...
pub const FRAC_PI_6: F128 = F128::from_bits(0x3FFE_0C15_2382_D736_5846_5BB3_2E0F_567B);
/// π/8 = 0.392699…
// FRAC_PI_8 = 0.3926990816987241548078304229099378605246...
pub const FRAC_PI_8: F128 = F128::from_bits(0x3FFD_921F_B544_42D1_8469_898C_C517_01B8);
/// 1/π = 0.318309…
// FRAC_1_PI = 0.3183098861837906715377675267450287240689...
pub const FRAC_1_PI: F128 = F128::from_bits(0x3FFD_45F3_06DC_9C88_2A53_F84E_AFA3_EA6A);
/// 2/π = 0.636619…
// FRAC_2_PI = 0.6366197723675813430755350534900574481378...
pub const FRAC_2_PI: F128 = F128::from_bits(0x3FFE_45F3_06DC_9C88_2A53_F84E_AFA3_EA6A);
/// 2/√π = 1.12837…
// FRAC_2_SQRT_PI = 1.128379167095512573896158903121545171688...
pub const FRAC_2_SQRT_PI: F128 = F128::from_bits(0x3FFF_20DD_7504_29B6_D11A_E3A9_14FE_D7FE);
/// √2 = 1.41421…
// SQRT_2 = 1.414213562373095048801688724209698078569...
pub const SQRT_2: F128 = F128::from_bits(0x3FFF_6A09_E667_F3BC_C908_B2FB_1366_EA95);
/// 1/√2 = 0.707106…
// FRAC_1_SQRT_2 = 0.7071067811865475244008443621048490392848...
pub const FRAC_1_SQRT_2: F128 = F128::from_bits(0x3FFE_6A09_E667_F3BC_C908_B2FB_1366_EA95);
/// Euler’s number, e = 2.71828…
// E = 2.718281828459045235360287471352662497757...
pub const E: F128 = F128::from_bits(0x4000_5BF0_A8B1_4576_9535_5FB8_AC40_4E7A);
/// log<sub>2</sub> 10 = 3.32192…
// LOG2_10 = 3.321928094887362347870319429489390175864...
pub const LOG2_10: F128 = F128::from_bits(0x4000_A934_F097_9A37_15FC_9257_EDFE_9B60);
/// log<sub>2</sub> e = 1.44269…
// LOG2_E = 1.442695040888963407359924681001892137426...
pub const LOG2_E: F128 = F128::from_bits(0x3FFF_7154_7652_B82F_E177_7D0F_FDA0_D23A);
/// log<sub>10</sub> 2 = 0.301029…
// LOG10_2 = 0.3010299956639811952137388947244930267681...
pub const LOG10_2: F128 = F128::from_bits(0x3FFD_3441_3509_F79F_EF31_1F12_B358_16F9);
/// log<sub>10</sub> e = 0.434294…
// LOG10_E = 0.4342944819032518276511289189166050822943...
pub const LOG10_E: F128 = F128::from_bits(0x3FFD_BCB7_B152_6E50_E32A_6AB7_555F_5A68);
/// ln 2 = 0.693147…
// LN_2 = 0.6931471805599453094172321214581765680755...
pub const LN_2: F128 = F128::from_bits(0x3FFE_62E4_2FEF_A39E_F357_93C7_6730_07E6);
/// ln 10 = 2.30258…
// LN_10 = 2.302585092994045684017991454684364207601...
pub const LN_10: F128 = F128::from_bits(0x4000_26BB_1BBB_5551_582D_D4AD_AC57_05A6);
sourcepub const fn to_bits(self) -> u128
pub const fn to_bits(self) -> u128
Raw transmutation to u128
.
Examples
use fixed::F128;
assert_eq!(F128::ONE.to_bits(), 0x3FFF_u128 << 112);
assert_ne!(F128::ONE.to_bits(), 1u128);
Examples found in repository?
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
pub const NEG_ONE: F128 = F128::from_bits(SIGN_MASK | F128::ONE.to_bits());
/// Smallest positive subnormal number.
///
/// Equal to 2<sup>[`MIN_EXP`] − [`MANTISSA_DIGITS`]</sup>.
///
/// [`MANTISSA_DIGITS`]: Self::MANTISSA_DIGITS
/// [`MIN_EXP`]: Self::MIN_EXP
pub const MIN_POSITIVE_SUB: F128 = F128::from_bits(1);
/// Smallest positive normal number.
///
/// Equal to 2<sup>[`MIN_EXP`] − 1</sup>.
///
/// [`MIN_EXP`]: Self::MIN_EXP
pub const MIN_POSITIVE: F128 = F128::from_bits(MANT_MASK + 1);
/// Largest finite number.
///
/// Equal to
/// (1 − 2<sup>−[`MANTISSA_DIGITS`]</sup>) 2<sup>[`MAX_EXP`]</sup>.
///
/// [`MANTISSA_DIGITS`]: Self::MANTISSA_DIGITS
/// [`MAX_EXP`]: Self::MAX_EXP
pub const MAX: F128 = F128::from_bits(EXP_MASK - 1);
/// Smallest finite number (−[`MAX`]).
///
/// [`MAX`]: Self::MAX
pub const MIN: F128 = F128::from_bits(SIGN_MASK | F128::MAX.to_bits());
/// Infinity (∞).
pub const INFINITY: F128 = F128::from_bits(EXP_MASK);
/// Negative infinity (−∞).
pub const NEG_INFINITY: F128 = F128::from_bits(SIGN_MASK | EXP_MASK);
/// NaN.
pub const NAN: F128 = F128::from_bits(EXP_MASK | (1u128 << (PREC - 2)));
/// The radix or base of the internal representation (2).
pub const RADIX: u32 = 2;
/// Number of significant digits in base 2.
pub const MANTISSA_DIGITS: u32 = PREC;
/// Maximum <i>x</i> such that any decimal number with <i>x</i> significant
/// digits can be converted to [`F128`] and back without loss.
///
/// Equal to
/// floor(log<sub>10</sub> 2<sup>[`MANTISSA_DIGITS`] − 1</sup>).
///
/// [`MANTISSA_DIGITS`]: Self::MANTISSA_DIGITS
pub const DIGITS: u32 = 33;
/// The difference between 1 and the next larger representable number.
///
/// Equal to 2<sup>1 − [`MANTISSA_DIGITS`]</sup>.
///
/// [`MANTISSA_DIGITS`]: Self::MANTISSA_DIGITS
pub const EPSILON: F128 = F128::from_bits(((EXP_BIAS - (PREC - 1)) as u128) << (PREC - 1));
/// If <i>x</i> = `MIN_EXP`, then normal numbers
/// ≥ 0.5 × 2<sup><i>x</i></sup>.
pub const MIN_EXP: i32 = 3 - F128::MAX_EXP;
/// If <i>x</i> = `MAX_EXP`, then normal numbers
/// < 1 × 2<sup><i>x</i></sup>.
pub const MAX_EXP: i32 = EXP_BIAS as i32 + 1;
/// Minimum <i>x</i> for which 10<sup><i>x</i></sup> is in the normal range
/// of [`F128`].
///
/// Equal to ceil(log<sub>10</sub> [`MIN_POSITIVE`]).
///
/// [`MIN_POSITIVE`]: Self::MIN_POSITIVE
pub const MIN_10_EXP: i32 = -4931;
/// Maximum <i>x</i> for which 10<sup><i>x</i></sup> is in the normal range
/// of [`F128`].
///
/// Equal to floor(log<sub>10</sub> [`MAX`]).
///
/// [`MAX`]: Self::MAX
pub const MAX_10_EXP: i32 = 4932;
/// Raw transmutation from [`u128`].
///
/// # Examples
///
/// ```rust
/// use fixed::F128;
/// let infinity_bits = 0x7FFF_u128 << 112;
/// assert!(F128::from_bits(infinity_bits - 1).is_finite());
/// assert!(!F128::from_bits(infinity_bits).is_finite());
/// ```
#[inline]
pub const fn from_bits(bits: u128) -> F128 {
F128 { bits }
}
/// Raw transmutation to [`u128`].
///
/// # Examples
///
/// ```rust
/// use fixed::F128;
/// assert_eq!(F128::ONE.to_bits(), 0x3FFF_u128 << 112);
/// assert_ne!(F128::ONE.to_bits(), 1u128);
/// ```
#[inline]
pub const fn to_bits(self) -> u128 {
self.bits
}
/// Creates a number from a byte array in big-endian byte order.
#[inline]
pub const fn from_be_bytes(bytes: [u8; 16]) -> F128 {
F128::from_bits(u128::from_be_bytes(bytes))
}
/// Creates a number from a byte array in little-endian byte order.
#[inline]
pub const fn from_le_bytes(bytes: [u8; 16]) -> F128 {
F128::from_bits(u128::from_le_bytes(bytes))
}
/// Creates a number from a byte array in native-endian byte order.
#[inline]
pub const fn from_ne_bytes(bytes: [u8; 16]) -> F128 {
F128::from_bits(u128::from_ne_bytes(bytes))
}
/// Returns the memory representation of the number as a byte array in
/// big-endian byte order.
#[inline]
pub const fn to_be_bytes(self) -> [u8; 16] {
self.to_bits().to_be_bytes()
}
/// Returns the memory representation of the number as a byte array in
/// little-endian byte order.
#[inline]
pub const fn to_le_bytes(self) -> [u8; 16] {
self.to_bits().to_le_bytes()
}
/// Returns the memory representation of the number as a byte array in
/// native-endian byte order.
#[inline]
pub const fn to_ne_bytes(self) -> [u8; 16] {
self.to_bits().to_ne_bytes()
}
/// Returns [`true`] if the number is NaN.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::NAN.is_nan());
///
/// assert!(!F128::ONE.is_nan());
/// assert!(!F128::INFINITY.is_nan());
/// assert!(!F128::NEG_INFINITY.is_nan());
/// ```
#[inline]
pub const fn is_nan(self) -> bool {
(self.to_bits() & !SIGN_MASK) > EXP_MASK
}
/// Returns [`true`] if the number is infinite.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::INFINITY.is_infinite());
/// assert!(F128::NEG_INFINITY.is_infinite());
///
/// assert!(!F128::ONE.is_infinite());
/// assert!(!F128::NAN.is_infinite());
/// ```
#[inline]
pub const fn is_infinite(self) -> bool {
(self.to_bits() & !SIGN_MASK) == EXP_MASK
}
/// Returns [`true`] if the number is neither infinite nor NaN.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::ONE.is_finite());
/// assert!(F128::MAX.is_finite());
///
/// assert!(!F128::INFINITY.is_finite());
/// assert!(!F128::NEG_INFINITY.is_finite());
/// assert!(!F128::NAN.is_finite());
/// ```
#[inline]
pub const fn is_finite(self) -> bool {
(self.to_bits() & EXP_MASK) != EXP_MASK
}
/// Returns [`true`] if the number is zero.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::ZERO.is_zero());
/// assert!(F128::NEG_ZERO.is_zero());
///
/// assert!(!F128::MIN_POSITIVE_SUB.is_zero());
/// assert!(!F128::NAN.is_zero());
/// ```
#[inline]
pub const fn is_zero(self) -> bool {
(self.to_bits() & !SIGN_MASK) == 0
}
/// Returns [`true`] if the number is subnormal.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::MIN_POSITIVE_SUB.is_subnormal());
///
/// assert!(!F128::ZERO.is_subnormal());
/// assert!(!F128::MIN_POSITIVE.is_subnormal());
/// ```
#[inline]
pub const fn is_subnormal(self) -> bool {
let abs = self.to_bits() & !SIGN_MASK;
0 < abs && abs < F128::MIN_POSITIVE.to_bits()
}
/// Returns [`true`] if the number is neither zero, infinite, subnormal, or NaN.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::MIN.is_normal());
/// assert!(F128::MIN_POSITIVE.is_normal());
/// assert!(F128::MAX.is_normal());
///
/// assert!(!F128::ZERO.is_normal());
/// assert!(!F128::MIN_POSITIVE_SUB.is_normal());
/// assert!(!F128::INFINITY.is_normal());
/// assert!(!F128::NAN.is_normal());
/// ```
#[inline]
pub const fn is_normal(self) -> bool {
let abs = self.to_bits() & !SIGN_MASK;
F128::MIN_POSITIVE.to_bits() <= abs && abs <= F128::MAX.to_bits()
}
/// Returns the floating point category of the number.
///
/// If only one property is going to be tested, it is generally faster to
/// use the specific predicate instead.
///
/// # Example
///
/// ```rust
/// use core::num::FpCategory;
/// use fixed::F128;
///
/// assert_eq!(F128::ZERO.classify(), FpCategory::Zero);
/// assert_eq!(F128::MIN_POSITIVE_SUB.classify(), FpCategory::Subnormal);
/// assert_eq!(F128::MIN_POSITIVE.classify(), FpCategory::Normal);
/// assert_eq!(F128::INFINITY.classify(), FpCategory::Infinite);
/// assert_eq!(F128::NAN.classify(), FpCategory::Nan);
/// ```
#[inline]
pub const fn classify(self) -> FpCategory {
let exp = self.to_bits() & EXP_MASK;
let mant = self.to_bits() & MANT_MASK;
if exp == 0 {
if mant == 0 {
FpCategory::Zero
} else {
FpCategory::Subnormal
}
} else if exp == EXP_MASK {
if mant == 0 {
FpCategory::Infinite
} else {
FpCategory::Nan
}
} else {
FpCategory::Normal
}
}
/// Returns the absolute value of the number.
///
/// The only difference possible between the input value and the returned
/// value is in the sign bit, which is always cleared in the return value.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// // -0 == +0, but -0 bits != +0 bits
/// assert_eq!(F128::NEG_ZERO, F128::ZERO);
/// assert_ne!(F128::NEG_ZERO.to_bits(), F128::ZERO.to_bits());
/// assert_eq!(F128::NEG_ZERO.abs().to_bits(), F128::ZERO.to_bits());
///
/// assert_eq!(F128::NEG_INFINITY.abs(), F128::INFINITY);
/// assert_eq!(F128::MIN.abs(), F128::MAX);
///
/// assert!(F128::NAN.abs().is_nan());
/// ```
#[inline]
pub const fn abs(self) -> F128 {
F128::from_bits(self.to_bits() & !SIGN_MASK)
}
/// Returns a number that represents the sign of the input value.
///
/// * 1 if the number is positive, +0, or +∞
/// * −1 if the number is negative, −0, or −∞
/// * NaN if the number is NaN
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert_eq!(F128::ONE.signum(), F128::ONE);
/// assert_eq!(F128::INFINITY.signum(), F128::ONE);
/// assert_eq!(F128::NEG_ZERO.signum(), F128::NEG_ONE);
/// assert_eq!(F128::MIN.signum(), F128::NEG_ONE);
///
/// assert!(F128::NAN.signum().is_nan());
/// ```
#[inline]
pub const fn signum(self) -> F128 {
if self.is_nan() {
self
} else if self.is_sign_positive() {
F128::ONE
} else {
F128::NEG_ONE
}
}
/// Returns a number composed of the magnitude of `self` and the sign of `sign`.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert_eq!(F128::ONE.copysign(F128::NEG_ZERO), F128::NEG_ONE);
/// assert_eq!(F128::ONE.copysign(F128::ZERO), F128::ONE);
/// assert_eq!(F128::NEG_ONE.copysign(F128::NEG_INFINITY), F128::NEG_ONE);
/// assert_eq!(F128::NEG_ONE.copysign(F128::INFINITY), F128::ONE);
///
/// assert!(F128::NAN.copysign(F128::ONE).is_nan());
/// assert!(F128::NAN.copysign(F128::ONE).is_sign_positive());
/// assert!(F128::NAN.copysign(F128::NEG_ONE).is_sign_negative());
/// ```
#[inline]
pub const fn copysign(self, sign: F128) -> F128 {
F128::from_bits((self.to_bits() & !SIGN_MASK) | (sign.to_bits() & SIGN_MASK))
}
/// Returns [`true`] if the number has a positive sign, including +0, +∞,
/// and NaN without a negative sign bit.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::ZERO.is_sign_positive());
/// assert!(F128::MAX.is_sign_positive());
/// assert!(F128::INFINITY.is_sign_positive());
///
/// assert!(!F128::NEG_ZERO.is_sign_positive());
/// assert!(!F128::MIN.is_sign_positive());
/// assert!(!F128::NEG_INFINITY.is_sign_positive());
/// ```
#[inline]
pub const fn is_sign_positive(self) -> bool {
(self.to_bits() & SIGN_MASK) == 0
}
/// Returns [`true`] if the number has a negative sign, including −0,
/// −∞, and NaN with a negative sign bit.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::NEG_ZERO.is_sign_negative());
/// assert!(F128::MIN.is_sign_negative());
/// assert!(F128::NEG_INFINITY.is_sign_negative());
///
/// assert!(!F128::ZERO.is_sign_negative());
/// assert!(!F128::MAX.is_sign_negative());
/// assert!(!F128::INFINITY.is_sign_negative());
/// ```
#[inline]
pub const fn is_sign_negative(self) -> bool {
(self.to_bits() & SIGN_MASK) != 0
}
/// Returns the maximum of two numbers, ignoring NaN.
///
/// If one of the arguments is NaN, then the other argument is returned.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert_eq!(F128::ZERO.max(F128::ONE), F128::ONE);
/// ```
#[inline]
pub const fn max(self, other: F128) -> F128 {
if self.is_nan() || matches!(partial_cmp(&self, &other), Some(Ordering::Less)) {
other
} else {
self
}
}
/// Returns the minimum of two numbers, ignoring NaN.
///
/// If one of the arguments is NaN, then the other argument is returned.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert_eq!(F128::ZERO.min(F128::ONE), F128::ZERO);
/// ```
#[inline]
pub const fn min(self, other: F128) -> F128 {
if self.is_nan() || matches!(partial_cmp(&self, &other), Some(Ordering::Greater)) {
other
} else {
self
}
}
/// Clamps the value within the specified bounds.
///
/// Returns `min` if `self` < `min`, `max` if
/// `self` > `max`, or `self` otherwise.
///
/// Note that this method returns NaN if the initial value is NaN.
///
/// # Panics
///
/// Panics if `min` > `max`, `min` is NaN, or `max` is NaN.
///
/// # Examples
///
/// ```
/// use fixed::F128;
/// assert_eq!(F128::MIN.clamp(F128::NEG_ONE, F128::ONE), F128::NEG_ONE);
/// assert_eq!(F128::ZERO.clamp(F128::NEG_ONE, F128::ONE), F128::ZERO);
/// assert_eq!(F128::MAX.clamp(F128::NEG_ONE, F128::ONE), F128::ONE);
/// assert!(F128::NAN.clamp(F128::NEG_ONE, F128::ONE).is_nan());
/// ```
#[inline]
pub const fn clamp(mut self, min: F128, max: F128) -> F128 {
match partial_cmp(&min, &max) {
Some(Ordering::Less) | Some(Ordering::Equal) => {}
_ => panic!("need min <= max"),
}
if matches!(partial_cmp(&self, &min), Some(Ordering::Less)) {
self = min;
}
if matches!(partial_cmp(&self, &max), Some(Ordering::Greater)) {
self = max;
}
self
}
/// Returns the ordering between `self` and `other`.
///
/// Unlike the [`PartialOrd`] implementation, this method always returns an
/// order in the following sequence:
///
/// * NaN with the sign bit set
/// * −∞
/// * negative normal numbers
/// * negative subnormal numbers
/// * −0
/// * +0
/// * positive subnormal numbers
/// * positive normal numbers
/// * +∞
/// * NaN with the sign bit cleared
///
/// # Example
///
/// ```rust
/// use core::cmp::Ordering;
/// use fixed::F128;
///
/// let neg_nan = F128::NAN.copysign(F128::NEG_ONE);
/// let pos_nan = F128::NAN.copysign(F128::ONE);
/// let neg_inf = F128::NEG_INFINITY;
/// let pos_inf = F128::INFINITY;
/// let neg_zero = F128::NEG_ZERO;
/// let pos_zero = F128::ZERO;
///
/// assert_eq!(neg_nan.total_cmp(&neg_inf), Ordering::Less);
/// assert_eq!(pos_nan.total_cmp(&pos_inf), Ordering::Greater);
/// assert_eq!(neg_zero.total_cmp(&pos_zero), Ordering::Less);
/// ```
#[inline]
pub const fn total_cmp(&self, other: &F128) -> Ordering {
let a = self.to_bits();
let b = other.to_bits();
match (self.is_sign_negative(), other.is_sign_negative()) {
(false, false) => cmp_bits(a, b),
(true, true) => cmp_bits(b, a),
(false, true) => Ordering::Greater,
(true, false) => Ordering::Less,
}
}
}
const fn cmp_bits(a: u128, b: u128) -> Ordering {
if a < b {
Ordering::Less
} else if a == b {
Ordering::Equal
} else {
Ordering::Greater
}
}
impl PartialEq for F128 {
#[inline]
fn eq(&self, other: &F128) -> bool {
if self.is_nan() || other.is_nan() {
return false;
}
let a = self.to_bits();
let b = other.to_bits();
// handle zero
if ((a | b) & !SIGN_MASK) == 0 {
return true;
}
a == b
}
}
impl PartialOrd for F128 {
#[inline]
fn partial_cmp(&self, other: &F128) -> Option<Ordering> {
partial_cmp(self, other)
}
}
#[inline]
const fn partial_cmp(a: &F128, b: &F128) -> Option<Ordering> {
if a.is_nan() || b.is_nan() {
return None;
}
let a_bits = a.to_bits();
let b_bits = b.to_bits();
// handle zero
if ((a_bits | b_bits) & !SIGN_MASK) == 0 {
return Some(Ordering::Equal);
}
match (a.is_sign_negative(), b.is_sign_negative()) {
(false, false) => Some(cmp_bits(a_bits, b_bits)),
(true, true) => Some(cmp_bits(b_bits, a_bits)),
(false, true) => Some(Ordering::Greater),
(true, false) => Some(Ordering::Less),
}
}
impl Hash for F128 {
#[inline]
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
let mut bits = self.to_bits();
if bits == F128::NEG_ZERO.to_bits() {
bits = 0;
}
bits.hash(state);
}
}
impl Neg for F128 {
type Output = F128;
#[inline]
fn neg(self) -> F128 {
F128::from_bits(self.to_bits() ^ SIGN_MASK)
}
sourcepub const fn from_be_bytes(bytes: [u8; 16]) -> F128
pub const fn from_be_bytes(bytes: [u8; 16]) -> F128
Creates a number from a byte array in big-endian byte order.
sourcepub const fn from_le_bytes(bytes: [u8; 16]) -> F128
pub const fn from_le_bytes(bytes: [u8; 16]) -> F128
Creates a number from a byte array in little-endian byte order.
sourcepub const fn from_ne_bytes(bytes: [u8; 16]) -> F128
pub const fn from_ne_bytes(bytes: [u8; 16]) -> F128
Creates a number from a byte array in native-endian byte order.
sourcepub const fn to_be_bytes(self) -> [u8; 16]
pub const fn to_be_bytes(self) -> [u8; 16]
Returns the memory representation of the number as a byte array in big-endian byte order.
sourcepub const fn to_le_bytes(self) -> [u8; 16]
pub const fn to_le_bytes(self) -> [u8; 16]
Returns the memory representation of the number as a byte array in little-endian byte order.
sourcepub const fn to_ne_bytes(self) -> [u8; 16]
pub const fn to_ne_bytes(self) -> [u8; 16]
Returns the memory representation of the number as a byte array in native-endian byte order.
sourcepub const fn is_nan(self) -> bool
pub const fn is_nan(self) -> bool
Returns true
if the number is NaN.
Example
use fixed::F128;
assert!(F128::NAN.is_nan());
assert!(!F128::ONE.is_nan());
assert!(!F128::INFINITY.is_nan());
assert!(!F128::NEG_INFINITY.is_nan());
Examples found in repository?
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
pub const fn signum(self) -> F128 {
if self.is_nan() {
self
} else if self.is_sign_positive() {
F128::ONE
} else {
F128::NEG_ONE
}
}
/// Returns a number composed of the magnitude of `self` and the sign of `sign`.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert_eq!(F128::ONE.copysign(F128::NEG_ZERO), F128::NEG_ONE);
/// assert_eq!(F128::ONE.copysign(F128::ZERO), F128::ONE);
/// assert_eq!(F128::NEG_ONE.copysign(F128::NEG_INFINITY), F128::NEG_ONE);
/// assert_eq!(F128::NEG_ONE.copysign(F128::INFINITY), F128::ONE);
///
/// assert!(F128::NAN.copysign(F128::ONE).is_nan());
/// assert!(F128::NAN.copysign(F128::ONE).is_sign_positive());
/// assert!(F128::NAN.copysign(F128::NEG_ONE).is_sign_negative());
/// ```
#[inline]
pub const fn copysign(self, sign: F128) -> F128 {
F128::from_bits((self.to_bits() & !SIGN_MASK) | (sign.to_bits() & SIGN_MASK))
}
/// Returns [`true`] if the number has a positive sign, including +0, +∞,
/// and NaN without a negative sign bit.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::ZERO.is_sign_positive());
/// assert!(F128::MAX.is_sign_positive());
/// assert!(F128::INFINITY.is_sign_positive());
///
/// assert!(!F128::NEG_ZERO.is_sign_positive());
/// assert!(!F128::MIN.is_sign_positive());
/// assert!(!F128::NEG_INFINITY.is_sign_positive());
/// ```
#[inline]
pub const fn is_sign_positive(self) -> bool {
(self.to_bits() & SIGN_MASK) == 0
}
/// Returns [`true`] if the number has a negative sign, including −0,
/// −∞, and NaN with a negative sign bit.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert!(F128::NEG_ZERO.is_sign_negative());
/// assert!(F128::MIN.is_sign_negative());
/// assert!(F128::NEG_INFINITY.is_sign_negative());
///
/// assert!(!F128::ZERO.is_sign_negative());
/// assert!(!F128::MAX.is_sign_negative());
/// assert!(!F128::INFINITY.is_sign_negative());
/// ```
#[inline]
pub const fn is_sign_negative(self) -> bool {
(self.to_bits() & SIGN_MASK) != 0
}
/// Returns the maximum of two numbers, ignoring NaN.
///
/// If one of the arguments is NaN, then the other argument is returned.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert_eq!(F128::ZERO.max(F128::ONE), F128::ONE);
/// ```
#[inline]
pub const fn max(self, other: F128) -> F128 {
if self.is_nan() || matches!(partial_cmp(&self, &other), Some(Ordering::Less)) {
other
} else {
self
}
}
/// Returns the minimum of two numbers, ignoring NaN.
///
/// If one of the arguments is NaN, then the other argument is returned.
///
/// # Example
///
/// ```rust
/// use fixed::F128;
///
/// assert_eq!(F128::ZERO.min(F128::ONE), F128::ZERO);
/// ```
#[inline]
pub const fn min(self, other: F128) -> F128 {
if self.is_nan() || matches!(partial_cmp(&self, &other), Some(Ordering::Greater)) {
other
} else {
self
}
}
/// Clamps the value within the specified bounds.
///
/// Returns `min` if `self` < `min`, `max` if
/// `self` > `max`, or `self` otherwise.
///
/// Note that this method returns NaN if the initial value is NaN.
///
/// # Panics
///
/// Panics if `min` > `max`, `min` is NaN, or `max` is NaN.
///
/// # Examples
///
/// ```
/// use fixed::F128;
/// assert_eq!(F128::MIN.clamp(F128::NEG_ONE, F128::ONE), F128::NEG_ONE);
/// assert_eq!(F128::ZERO.clamp(F128::NEG_ONE, F128::ONE), F128::ZERO);
/// assert_eq!(F128::MAX.clamp(F128::NEG_ONE, F128::ONE), F128::ONE);
/// assert!(F128::NAN.clamp(F128::NEG_ONE, F128::ONE).is_nan());
/// ```
#[inline]
pub const fn clamp(mut self, min: F128, max: F128) -> F128 {
match partial_cmp(&min, &max) {
Some(Ordering::Less) | Some(Ordering::Equal) => {}
_ => panic!("need min <= max"),
}
if matches!(partial_cmp(&self, &min), Some(Ordering::Less)) {
self = min;
}
if matches!(partial_cmp(&self, &max), Some(Ordering::Greater)) {
self = max;
}
self
}
/// Returns the ordering between `self` and `other`.
///
/// Unlike the [`PartialOrd`] implementation, this method always returns an
/// order in the following sequence:
///
/// * NaN with the sign bit set
/// * −∞
/// * negative normal numbers
/// * negative subnormal numbers
/// * −0
/// * +0
/// * positive subnormal numbers
/// * positive normal numbers
/// * +∞
/// * NaN with the sign bit cleared
///
/// # Example
///
/// ```rust
/// use core::cmp::Ordering;
/// use fixed::F128;
///
/// let neg_nan = F128::NAN.copysign(F128::NEG_ONE);
/// let pos_nan = F128::NAN.copysign(F128::ONE);
/// let neg_inf = F128::NEG_INFINITY;
/// let pos_inf = F128::INFINITY;
/// let neg_zero = F128::NEG_ZERO;
/// let pos_zero = F128::ZERO;
///
/// assert_eq!(neg_nan.total_cmp(&neg_inf), Ordering::Less);
/// assert_eq!(pos_nan.total_cmp(&pos_inf), Ordering::Greater);
/// assert_eq!(neg_zero.total_cmp(&pos_zero), Ordering::Less);
/// ```
#[inline]
pub const fn total_cmp(&self, other: &F128) -> Ordering {
let a = self.to_bits();
let b = other.to_bits();
match (self.is_sign_negative(), other.is_sign_negative()) {
(false, false) => cmp_bits(a, b),
(true, true) => cmp_bits(b, a),
(false, true) => Ordering::Greater,
(true, false) => Ordering::Less,
}
}
}
const fn cmp_bits(a: u128, b: u128) -> Ordering {
if a < b {
Ordering::Less
} else if a == b {
Ordering::Equal
} else {
Ordering::Greater
}
}
impl PartialEq for F128 {
#[inline]
fn eq(&self, other: &F128) -> bool {
if self.is_nan() || other.is_nan() {
return false;
}
let a = self.to_bits();
let b = other.to_bits();
// handle zero
if ((a | b) & !SIGN_MASK) == 0 {
return true;
}
a == b
}
}
impl PartialOrd for F128 {
#[inline]
fn partial_cmp(&self, other: &F128) -> Option<Ordering> {
partial_cmp(self, other)
}
}
#[inline]
const fn partial_cmp(a: &F128, b: &F128) -> Option<Ordering> {
if a.is_nan() || b.is_nan() {
return None;
}
let a_bits = a.to_bits();
let b_bits = b.to_bits();
// handle zero
if ((a_bits | b_bits) & !SIGN_MASK) == 0 {
return Some(Ordering::Equal);
}
match (a.is_sign_negative(), b.is_sign_negative()) {
(false, false) => Some(cmp_bits(a_bits, b_bits)),
(true, true) => Some(cmp_bits(b_bits, a_bits)),
(false, true) => Some(Ordering::Greater),
(true, false) => Some(Ordering::Less),
}
}
sourcepub const fn is_infinite(self) -> bool
pub const fn is_infinite(self) -> bool
sourcepub const fn is_subnormal(self) -> bool
pub const fn is_subnormal(self) -> bool
sourcepub const fn is_normal(self) -> bool
pub const fn is_normal(self) -> bool
Returns true
if the number is neither zero, infinite, subnormal, or NaN.
Example
use fixed::F128;
assert!(F128::MIN.is_normal());
assert!(F128::MIN_POSITIVE.is_normal());
assert!(F128::MAX.is_normal());
assert!(!F128::ZERO.is_normal());
assert!(!F128::MIN_POSITIVE_SUB.is_normal());
assert!(!F128::INFINITY.is_normal());
assert!(!F128::NAN.is_normal());
sourcepub const fn classify(self) -> FpCategory
pub const fn classify(self) -> FpCategory
Returns the floating point category of the number.
If only one property is going to be tested, it is generally faster to use the specific predicate instead.
Example
use core::num::FpCategory;
use fixed::F128;
assert_eq!(F128::ZERO.classify(), FpCategory::Zero);
assert_eq!(F128::MIN_POSITIVE_SUB.classify(), FpCategory::Subnormal);
assert_eq!(F128::MIN_POSITIVE.classify(), FpCategory::Normal);
assert_eq!(F128::INFINITY.classify(), FpCategory::Infinite);
assert_eq!(F128::NAN.classify(), FpCategory::Nan);
sourcepub const fn abs(self) -> F128
pub const fn abs(self) -> F128
Returns the absolute value of the number.
The only difference possible between the input value and the returned value is in the sign bit, which is always cleared in the return value.
Example
use fixed::F128;
// -0 == +0, but -0 bits != +0 bits
assert_eq!(F128::NEG_ZERO, F128::ZERO);
assert_ne!(F128::NEG_ZERO.to_bits(), F128::ZERO.to_bits());
assert_eq!(F128::NEG_ZERO.abs().to_bits(), F128::ZERO.to_bits());
assert_eq!(F128::NEG_INFINITY.abs(), F128::INFINITY);
assert_eq!(F128::MIN.abs(), F128::MAX);
assert!(F128::NAN.abs().is_nan());
sourcepub const fn signum(self) -> F128
pub const fn signum(self) -> F128
Returns a number that represents the sign of the input value.
- 1 if the number is positive, +0, or +∞
- −1 if the number is negative, −0, or −∞
- NaN if the number is NaN
Example
use fixed::F128;
assert_eq!(F128::ONE.signum(), F128::ONE);
assert_eq!(F128::INFINITY.signum(), F128::ONE);
assert_eq!(F128::NEG_ZERO.signum(), F128::NEG_ONE);
assert_eq!(F128::MIN.signum(), F128::NEG_ONE);
assert!(F128::NAN.signum().is_nan());
sourcepub const fn copysign(self, sign: F128) -> F128
pub const fn copysign(self, sign: F128) -> F128
Returns a number composed of the magnitude of self
and the sign of sign
.
Example
use fixed::F128;
assert_eq!(F128::ONE.copysign(F128::NEG_ZERO), F128::NEG_ONE);
assert_eq!(F128::ONE.copysign(F128::ZERO), F128::ONE);
assert_eq!(F128::NEG_ONE.copysign(F128::NEG_INFINITY), F128::NEG_ONE);
assert_eq!(F128::NEG_ONE.copysign(F128::INFINITY), F128::ONE);
assert!(F128::NAN.copysign(F128::ONE).is_nan());
assert!(F128::NAN.copysign(F128::ONE).is_sign_positive());
assert!(F128::NAN.copysign(F128::NEG_ONE).is_sign_negative());
sourcepub const fn is_sign_positive(self) -> bool
pub const fn is_sign_positive(self) -> bool
Returns true
if the number has a positive sign, including +0, +∞,
and NaN without a negative sign bit.
Example
use fixed::F128;
assert!(F128::ZERO.is_sign_positive());
assert!(F128::MAX.is_sign_positive());
assert!(F128::INFINITY.is_sign_positive());
assert!(!F128::NEG_ZERO.is_sign_positive());
assert!(!F128::MIN.is_sign_positive());
assert!(!F128::NEG_INFINITY.is_sign_positive());
sourcepub const fn is_sign_negative(self) -> bool
pub const fn is_sign_negative(self) -> bool
Returns true
if the number has a negative sign, including −0,
−∞, and NaN with a negative sign bit.
Example
use fixed::F128;
assert!(F128::NEG_ZERO.is_sign_negative());
assert!(F128::MIN.is_sign_negative());
assert!(F128::NEG_INFINITY.is_sign_negative());
assert!(!F128::ZERO.is_sign_negative());
assert!(!F128::MAX.is_sign_negative());
assert!(!F128::INFINITY.is_sign_negative());
Examples found in repository?
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
pub const fn total_cmp(&self, other: &F128) -> Ordering {
let a = self.to_bits();
let b = other.to_bits();
match (self.is_sign_negative(), other.is_sign_negative()) {
(false, false) => cmp_bits(a, b),
(true, true) => cmp_bits(b, a),
(false, true) => Ordering::Greater,
(true, false) => Ordering::Less,
}
}
}
const fn cmp_bits(a: u128, b: u128) -> Ordering {
if a < b {
Ordering::Less
} else if a == b {
Ordering::Equal
} else {
Ordering::Greater
}
}
impl PartialEq for F128 {
#[inline]
fn eq(&self, other: &F128) -> bool {
if self.is_nan() || other.is_nan() {
return false;
}
let a = self.to_bits();
let b = other.to_bits();
// handle zero
if ((a | b) & !SIGN_MASK) == 0 {
return true;
}
a == b
}
}
impl PartialOrd for F128 {
#[inline]
fn partial_cmp(&self, other: &F128) -> Option<Ordering> {
partial_cmp(self, other)
}
}
#[inline]
const fn partial_cmp(a: &F128, b: &F128) -> Option<Ordering> {
if a.is_nan() || b.is_nan() {
return None;
}
let a_bits = a.to_bits();
let b_bits = b.to_bits();
// handle zero
if ((a_bits | b_bits) & !SIGN_MASK) == 0 {
return Some(Ordering::Equal);
}
match (a.is_sign_negative(), b.is_sign_negative()) {
(false, false) => Some(cmp_bits(a_bits, b_bits)),
(true, true) => Some(cmp_bits(b_bits, a_bits)),
(false, true) => Some(Ordering::Greater),
(true, false) => Some(Ordering::Less),
}
}
sourcepub const fn max(self, other: F128) -> F128
pub const fn max(self, other: F128) -> F128
Returns the maximum of two numbers, ignoring NaN.
If one of the arguments is NaN, then the other argument is returned.
Example
use fixed::F128;
assert_eq!(F128::ZERO.max(F128::ONE), F128::ONE);
sourcepub const fn min(self, other: F128) -> F128
pub const fn min(self, other: F128) -> F128
Returns the minimum of two numbers, ignoring NaN.
If one of the arguments is NaN, then the other argument is returned.
Example
use fixed::F128;
assert_eq!(F128::ZERO.min(F128::ONE), F128::ZERO);
sourcepub const fn clamp(self, min: F128, max: F128) -> F128
pub const fn clamp(self, min: F128, max: F128) -> F128
Clamps the value within the specified bounds.
Returns min
if self
< min
, max
if
self
> max
, or self
otherwise.
Note that this method returns NaN if the initial value is NaN.
Panics
Panics if min
> max
, min
is NaN, or max
is NaN.
Examples
use fixed::F128;
assert_eq!(F128::MIN.clamp(F128::NEG_ONE, F128::ONE), F128::NEG_ONE);
assert_eq!(F128::ZERO.clamp(F128::NEG_ONE, F128::ONE), F128::ZERO);
assert_eq!(F128::MAX.clamp(F128::NEG_ONE, F128::ONE), F128::ONE);
assert!(F128::NAN.clamp(F128::NEG_ONE, F128::ONE).is_nan());
sourcepub const fn total_cmp(&self, other: &F128) -> Ordering
pub const fn total_cmp(&self, other: &F128) -> Ordering
Returns the ordering between self
and other
.
Unlike the PartialOrd
implementation, this method always returns an
order in the following sequence:
- NaN with the sign bit set
- −∞
- negative normal numbers
- negative subnormal numbers
- −0
- +0
- positive subnormal numbers
- positive normal numbers
- +∞
- NaN with the sign bit cleared
Example
use core::cmp::Ordering;
use fixed::F128;
let neg_nan = F128::NAN.copysign(F128::NEG_ONE);
let pos_nan = F128::NAN.copysign(F128::ONE);
let neg_inf = F128::NEG_INFINITY;
let pos_inf = F128::INFINITY;
let neg_zero = F128::NEG_ZERO;
let pos_zero = F128::ZERO;
assert_eq!(neg_nan.total_cmp(&neg_inf), Ordering::Less);
assert_eq!(pos_nan.total_cmp(&pos_inf), Ordering::Greater);
assert_eq!(neg_zero.total_cmp(&pos_zero), Ordering::Less);
Trait Implementations§
source§impl<Frac: LeEqU128> CheckedCast<F128> for FixedI128<Frac>
impl<Frac: LeEqU128> CheckedCast<F128> for FixedI128<Frac>
source§fn checked_cast(self) -> Option<F128>
fn checked_cast(self) -> Option<F128>
source§impl<Frac: LeEqU16> CheckedCast<F128> for FixedI16<Frac>
impl<Frac: LeEqU16> CheckedCast<F128> for FixedI16<Frac>
source§fn checked_cast(self) -> Option<F128>
fn checked_cast(self) -> Option<F128>
source§impl<Frac: LeEqU32> CheckedCast<F128> for FixedI32<Frac>
impl<Frac: LeEqU32> CheckedCast<F128> for FixedI32<Frac>
source§fn checked_cast(self) -> Option<F128>
fn checked_cast(self) -> Option<F128>
source§impl<Frac: LeEqU64> CheckedCast<F128> for FixedI64<Frac>
impl<Frac: LeEqU64> CheckedCast<F128> for FixedI64<Frac>
source§fn checked_cast(self) -> Option<F128>
fn checked_cast(self) -> Option<F128>
source§impl<Frac: LeEqU8> CheckedCast<F128> for FixedI8<Frac>
impl<Frac: LeEqU8> CheckedCast<F128> for FixedI8<Frac>
source§fn checked_cast(self) -> Option<F128>
fn checked_cast(self) -> Option<F128>
source§impl<Frac: LeEqU128> CheckedCast<F128> for FixedU128<Frac>
impl<Frac: LeEqU128> CheckedCast<F128> for FixedU128<Frac>
source§fn checked_cast(self) -> Option<F128>
fn checked_cast(self) -> Option<F128>
source§impl<Frac: LeEqU16> CheckedCast<F128> for FixedU16<Frac>
impl<Frac: LeEqU16> CheckedCast<F128> for FixedU16<Frac>
source§fn checked_cast(self) -> Option<F128>
fn checked_cast(self) -> Option<F128>
source§impl<Frac: LeEqU32> CheckedCast<F128> for FixedU32<Frac>
impl<Frac: LeEqU32> CheckedCast<F128> for FixedU32<Frac>
source§fn checked_cast(self) -> Option<F128>
fn checked_cast(self) -> Option<F128>
source§impl<Frac: LeEqU64> CheckedCast<F128> for FixedU64<Frac>
impl<Frac: LeEqU64> CheckedCast<F128> for FixedU64<Frac>
source§fn checked_cast(self) -> Option<F128>
fn checked_cast(self) -> Option<F128>
source§impl<Frac: LeEqU8> CheckedCast<F128> for FixedU8<Frac>
impl<Frac: LeEqU8> CheckedCast<F128> for FixedU8<Frac>
source§fn checked_cast(self) -> Option<F128>
fn checked_cast(self) -> Option<F128>
source§impl<Frac: LeEqU128> CheckedCast<FixedI128<Frac>> for F128
impl<Frac: LeEqU128> CheckedCast<FixedI128<Frac>> for F128
source§fn checked_cast(self) -> Option<FixedI128<Frac>>
fn checked_cast(self) -> Option<FixedI128<Frac>>
source§impl<Frac: LeEqU16> CheckedCast<FixedI16<Frac>> for F128
impl<Frac: LeEqU16> CheckedCast<FixedI16<Frac>> for F128
source§fn checked_cast(self) -> Option<FixedI16<Frac>>
fn checked_cast(self) -> Option<FixedI16<Frac>>
source§impl<Frac: LeEqU32> CheckedCast<FixedI32<Frac>> for F128
impl<Frac: LeEqU32> CheckedCast<FixedI32<Frac>> for F128
source§fn checked_cast(self) -> Option<FixedI32<Frac>>
fn checked_cast(self) -> Option<FixedI32<Frac>>
source§impl<Frac: LeEqU64> CheckedCast<FixedI64<Frac>> for F128
impl<Frac: LeEqU64> CheckedCast<FixedI64<Frac>> for F128
source§fn checked_cast(self) -> Option<FixedI64<Frac>>
fn checked_cast(self) -> Option<FixedI64<Frac>>
source§impl<Frac: LeEqU8> CheckedCast<FixedI8<Frac>> for F128
impl<Frac: LeEqU8> CheckedCast<FixedI8<Frac>> for F128
source§fn checked_cast(self) -> Option<FixedI8<Frac>>
fn checked_cast(self) -> Option<FixedI8<Frac>>
source§impl<Frac: LeEqU128> CheckedCast<FixedU128<Frac>> for F128
impl<Frac: LeEqU128> CheckedCast<FixedU128<Frac>> for F128
source§fn checked_cast(self) -> Option<FixedU128<Frac>>
fn checked_cast(self) -> Option<FixedU128<Frac>>
source§impl<Frac: LeEqU16> CheckedCast<FixedU16<Frac>> for F128
impl<Frac: LeEqU16> CheckedCast<FixedU16<Frac>> for F128
source§fn checked_cast(self) -> Option<FixedU16<Frac>>
fn checked_cast(self) -> Option<FixedU16<Frac>>
source§impl<Frac: LeEqU32> CheckedCast<FixedU32<Frac>> for F128
impl<Frac: LeEqU32> CheckedCast<FixedU32<Frac>> for F128
source§fn checked_cast(self) -> Option<FixedU32<Frac>>
fn checked_cast(self) -> Option<FixedU32<Frac>>
source§impl<Frac: LeEqU64> CheckedCast<FixedU64<Frac>> for F128
impl<Frac: LeEqU64> CheckedCast<FixedU64<Frac>> for F128
source§fn checked_cast(self) -> Option<FixedU64<Frac>>
fn checked_cast(self) -> Option<FixedU64<Frac>>
source§impl<Frac: LeEqU8> CheckedCast<FixedU8<Frac>> for F128
impl<Frac: LeEqU8> CheckedCast<FixedU8<Frac>> for F128
source§fn checked_cast(self) -> Option<FixedU8<Frac>>
fn checked_cast(self) -> Option<FixedU8<Frac>>
source§impl FromFixed for F128
impl FromFixed for F128
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number.
Rounding is to the nearest, with ties rounded to even.
Panics
When debug assertions are enabled, panics if the value
does not fit. When debug assertions are not enabled,
the wrapped value can be returned, but it is not
considered a breaking change if in the future it
panics; if wrapping is required use
wrapping_from_fixed
instead.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to a floating-point
number if it fits, otherwise returns None
.
Rounding is to the nearest, with ties rounded to even.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number, saturating if it does not fit.
Rounding is to the nearest, with ties rounded to even.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number, wrapping if it does not fit.
Rounding is to the nearest, with ties rounded to even.
source§fn overflowing_from_fixed<F: Fixed>(src: F) -> (Self, bool)
fn overflowing_from_fixed<F: Fixed>(src: F) -> (Self, bool)
source§fn unwrapped_from_fixed<F: Fixed>(src: F) -> Self
fn unwrapped_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number, panicking if it does not fit.
Rounding is to the nearest, with ties rounded to even.
Panics
Panics if the value does not fit, even when debug assertions are not enabled.
source§impl LosslessTryFrom<i16> for F128
impl LosslessTryFrom<i16> for F128
source§impl LosslessTryFrom<i32> for F128
impl LosslessTryFrom<i32> for F128
source§impl LosslessTryFrom<i64> for F128
impl LosslessTryFrom<i64> for F128
source§impl LosslessTryFrom<i8> for F128
impl LosslessTryFrom<i8> for F128
source§impl LosslessTryFrom<u16> for F128
impl LosslessTryFrom<u16> for F128
source§impl LosslessTryFrom<u32> for F128
impl LosslessTryFrom<u32> for F128
source§impl LosslessTryFrom<u64> for F128
impl LosslessTryFrom<u64> for F128
source§impl LosslessTryFrom<u8> for F128
impl LosslessTryFrom<u8> for F128
source§impl<Frac: LeEqU128> LossyFrom<FixedI128<Frac>> for F128
impl<Frac: LeEqU128> LossyFrom<FixedI128<Frac>> for F128
source§fn lossy_from(src: FixedI128<Frac>) -> F128
fn lossy_from(src: FixedI128<Frac>) -> F128
Converts a fixed-point number to a floating-point number.
This conversion never fails (infallible) but may lose precision (lossy). Rounding is to the nearest, with ties rounded to even.
source§impl<Frac: LeEqU16> LossyFrom<FixedI16<Frac>> for F128
impl<Frac: LeEqU16> LossyFrom<FixedI16<Frac>> for F128
source§fn lossy_from(src: FixedI16<Frac>) -> F128
fn lossy_from(src: FixedI16<Frac>) -> F128
Converts a fixed-point number to a floating-point number.
This conversion never fails (infallible) but may lose precision (lossy). Rounding is to the nearest, with ties rounded to even.
source§impl<Frac: LeEqU32> LossyFrom<FixedI32<Frac>> for F128
impl<Frac: LeEqU32> LossyFrom<FixedI32<Frac>> for F128
source§fn lossy_from(src: FixedI32<Frac>) -> F128
fn lossy_from(src: FixedI32<Frac>) -> F128
Converts a fixed-point number to a floating-point number.
This conversion never fails (infallible) but may lose precision (lossy). Rounding is to the nearest, with ties rounded to even.
source§impl<Frac: LeEqU64> LossyFrom<FixedI64<Frac>> for F128
impl<Frac: LeEqU64> LossyFrom<FixedI64<Frac>> for F128
source§fn lossy_from(src: FixedI64<Frac>) -> F128
fn lossy_from(src: FixedI64<Frac>) -> F128
Converts a fixed-point number to a floating-point number.
This conversion never fails (infallible) but may lose precision (lossy). Rounding is to the nearest, with ties rounded to even.
source§impl<Frac: LeEqU8> LossyFrom<FixedI8<Frac>> for F128
impl<Frac: LeEqU8> LossyFrom<FixedI8<Frac>> for F128
source§fn lossy_from(src: FixedI8<Frac>) -> F128
fn lossy_from(src: FixedI8<Frac>) -> F128
Converts a fixed-point number to a floating-point number.
This conversion never fails (infallible) but may lose precision (lossy). Rounding is to the nearest, with ties rounded to even.
source§impl<Frac: LeEqU128> LossyFrom<FixedU128<Frac>> for F128
impl<Frac: LeEqU128> LossyFrom<FixedU128<Frac>> for F128
source§fn lossy_from(src: FixedU128<Frac>) -> F128
fn lossy_from(src: FixedU128<Frac>) -> F128
Converts a fixed-point number to a floating-point number.
This conversion never fails (infallible) but may lose precision (lossy). Rounding is to the nearest, with ties rounded to even.
source§impl<Frac: LeEqU16> LossyFrom<FixedU16<Frac>> for F128
impl<Frac: LeEqU16> LossyFrom<FixedU16<Frac>> for F128
source§fn lossy_from(src: FixedU16<Frac>) -> F128
fn lossy_from(src: FixedU16<Frac>) -> F128
Converts a fixed-point number to a floating-point number.
This conversion never fails (infallible) but may lose precision (lossy). Rounding is to the nearest, with ties rounded to even.
source§impl<Frac: LeEqU32> LossyFrom<FixedU32<Frac>> for F128
impl<Frac: LeEqU32> LossyFrom<FixedU32<Frac>> for F128
source§fn lossy_from(src: FixedU32<Frac>) -> F128
fn lossy_from(src: FixedU32<Frac>) -> F128
Converts a fixed-point number to a floating-point number.
This conversion never fails (infallible) but may lose precision (lossy). Rounding is to the nearest, with ties rounded to even.
source§impl<Frac: LeEqU64> LossyFrom<FixedU64<Frac>> for F128
impl<Frac: LeEqU64> LossyFrom<FixedU64<Frac>> for F128
source§fn lossy_from(src: FixedU64<Frac>) -> F128
fn lossy_from(src: FixedU64<Frac>) -> F128
Converts a fixed-point number to a floating-point number.
This conversion never fails (infallible) but may lose precision (lossy). Rounding is to the nearest, with ties rounded to even.
source§impl<Frac: LeEqU8> LossyFrom<FixedU8<Frac>> for F128
impl<Frac: LeEqU8> LossyFrom<FixedU8<Frac>> for F128
source§fn lossy_from(src: FixedU8<Frac>) -> F128
fn lossy_from(src: FixedU8<Frac>) -> F128
Converts a fixed-point number to a floating-point number.
This conversion never fails (infallible) but may lose precision (lossy). Rounding is to the nearest, with ties rounded to even.
source§impl LossyFrom<i128> for F128
impl LossyFrom<i128> for F128
source§fn lossy_from(src: i128) -> F128
fn lossy_from(src: i128) -> F128
Converts an integer to a floating-point number.
This conversion never fails (infallible) but may lose precision (lossy). Rounding is to the nearest, with ties rounded to even.
source§impl LossyFrom<i16> for F128
impl LossyFrom<i16> for F128
source§fn lossy_from(src: i16) -> F128
fn lossy_from(src: i16) -> F128
Converts an integer to a floating-point number.
This conversion never fails (infallible) and actually does not lose precision (lossless).
source§impl LossyFrom<i32> for F128
impl LossyFrom<i32> for F128
source§fn lossy_from(src: i32) -> F128
fn lossy_from(src: i32) -> F128
Converts an integer to a floating-point number.
This conversion never fails (infallible) and actually does not lose precision (lossless).
source§impl LossyFrom<i64> for F128
impl LossyFrom<i64> for F128
source§fn lossy_from(src: i64) -> F128
fn lossy_from(src: i64) -> F128
Converts an integer to a floating-point number.
This conversion never fails (infallible) and actually does not lose precision (lossless).
source§impl LossyFrom<i8> for F128
impl LossyFrom<i8> for F128
source§fn lossy_from(src: i8) -> F128
fn lossy_from(src: i8) -> F128
Converts an integer to a floating-point number.
This conversion never fails (infallible) and actually does not lose precision (lossless).
source§impl LossyFrom<isize> for F128
impl LossyFrom<isize> for F128
source§fn lossy_from(src: isize) -> F128
fn lossy_from(src: isize) -> F128
Converts an integer to a floating-point number.
This conversion never fails (infallible) but may lose precision (lossy). Rounding is to the nearest, with ties rounded to even.
source§impl LossyFrom<u128> for F128
impl LossyFrom<u128> for F128
source§fn lossy_from(src: u128) -> F128
fn lossy_from(src: u128) -> F128
Converts an integer to a floating-point number.
This conversion never fails (infallible) but may lose precision (lossy). Rounding is to the nearest, with ties rounded to even.
source§impl LossyFrom<u16> for F128
impl LossyFrom<u16> for F128
source§fn lossy_from(src: u16) -> F128
fn lossy_from(src: u16) -> F128
Converts an integer to a floating-point number.
This conversion never fails (infallible) and actually does not lose precision (lossless).
source§impl LossyFrom<u32> for F128
impl LossyFrom<u32> for F128
source§fn lossy_from(src: u32) -> F128
fn lossy_from(src: u32) -> F128
Converts an integer to a floating-point number.
This conversion never fails (infallible) and actually does not lose precision (lossless).
source§impl LossyFrom<u64> for F128
impl LossyFrom<u64> for F128
source§fn lossy_from(src: u64) -> F128
fn lossy_from(src: u64) -> F128
Converts an integer to a floating-point number.
This conversion never fails (infallible) and actually does not lose precision (lossless).
source§impl LossyFrom<u8> for F128
impl LossyFrom<u8> for F128
source§fn lossy_from(src: u8) -> F128
fn lossy_from(src: u8) -> F128
Converts an integer to a floating-point number.
This conversion never fails (infallible) and actually does not lose precision (lossless).
source§impl LossyFrom<usize> for F128
impl LossyFrom<usize> for F128
source§fn lossy_from(src: usize) -> F128
fn lossy_from(src: usize) -> F128
Converts an integer to a floating-point number.
This conversion never fails (infallible) but may lose precision (lossy). Rounding is to the nearest, with ties rounded to even.
source§impl<Frac: LeEqU128> OverflowingCast<F128> for FixedI128<Frac>
impl<Frac: LeEqU128> OverflowingCast<F128> for FixedI128<Frac>
source§fn overflowing_cast(self) -> (F128, bool)
fn overflowing_cast(self) -> (F128, bool)
source§impl<Frac: LeEqU16> OverflowingCast<F128> for FixedI16<Frac>
impl<Frac: LeEqU16> OverflowingCast<F128> for FixedI16<Frac>
source§fn overflowing_cast(self) -> (F128, bool)
fn overflowing_cast(self) -> (F128, bool)
source§impl<Frac: LeEqU32> OverflowingCast<F128> for FixedI32<Frac>
impl<Frac: LeEqU32> OverflowingCast<F128> for FixedI32<Frac>
source§fn overflowing_cast(self) -> (F128, bool)
fn overflowing_cast(self) -> (F128, bool)
source§impl<Frac: LeEqU64> OverflowingCast<F128> for FixedI64<Frac>
impl<Frac: LeEqU64> OverflowingCast<F128> for FixedI64<Frac>
source§fn overflowing_cast(self) -> (F128, bool)
fn overflowing_cast(self) -> (F128, bool)
source§impl<Frac: LeEqU8> OverflowingCast<F128> for FixedI8<Frac>
impl<Frac: LeEqU8> OverflowingCast<F128> for FixedI8<Frac>
source§fn overflowing_cast(self) -> (F128, bool)
fn overflowing_cast(self) -> (F128, bool)
source§impl<Frac: LeEqU128> OverflowingCast<F128> for FixedU128<Frac>
impl<Frac: LeEqU128> OverflowingCast<F128> for FixedU128<Frac>
source§fn overflowing_cast(self) -> (F128, bool)
fn overflowing_cast(self) -> (F128, bool)
source§impl<Frac: LeEqU16> OverflowingCast<F128> for FixedU16<Frac>
impl<Frac: LeEqU16> OverflowingCast<F128> for FixedU16<Frac>
source§fn overflowing_cast(self) -> (F128, bool)
fn overflowing_cast(self) -> (F128, bool)
source§impl<Frac: LeEqU32> OverflowingCast<F128> for FixedU32<Frac>
impl<Frac: LeEqU32> OverflowingCast<F128> for FixedU32<Frac>
source§fn overflowing_cast(self) -> (F128, bool)
fn overflowing_cast(self) -> (F128, bool)
source§impl<Frac: LeEqU64> OverflowingCast<F128> for FixedU64<Frac>
impl<Frac: LeEqU64> OverflowingCast<F128> for FixedU64<Frac>
source§fn overflowing_cast(self) -> (F128, bool)
fn overflowing_cast(self) -> (F128, bool)
source§impl<Frac: LeEqU8> OverflowingCast<F128> for FixedU8<Frac>
impl<Frac: LeEqU8> OverflowingCast<F128> for FixedU8<Frac>
source§fn overflowing_cast(self) -> (F128, bool)
fn overflowing_cast(self) -> (F128, bool)
source§impl<Frac: LeEqU128> OverflowingCast<FixedI128<Frac>> for F128
impl<Frac: LeEqU128> OverflowingCast<FixedI128<Frac>> for F128
source§fn overflowing_cast(self) -> (FixedI128<Frac>, bool)
fn overflowing_cast(self) -> (FixedI128<Frac>, bool)
source§impl<Frac: LeEqU16> OverflowingCast<FixedI16<Frac>> for F128
impl<Frac: LeEqU16> OverflowingCast<FixedI16<Frac>> for F128
source§fn overflowing_cast(self) -> (FixedI16<Frac>, bool)
fn overflowing_cast(self) -> (FixedI16<Frac>, bool)
source§impl<Frac: LeEqU32> OverflowingCast<FixedI32<Frac>> for F128
impl<Frac: LeEqU32> OverflowingCast<FixedI32<Frac>> for F128
source§fn overflowing_cast(self) -> (FixedI32<Frac>, bool)
fn overflowing_cast(self) -> (FixedI32<Frac>, bool)
source§impl<Frac: LeEqU64> OverflowingCast<FixedI64<Frac>> for F128
impl<Frac: LeEqU64> OverflowingCast<FixedI64<Frac>> for F128
source§fn overflowing_cast(self) -> (FixedI64<Frac>, bool)
fn overflowing_cast(self) -> (FixedI64<Frac>, bool)
source§impl<Frac: LeEqU8> OverflowingCast<FixedI8<Frac>> for F128
impl<Frac: LeEqU8> OverflowingCast<FixedI8<Frac>> for F128
source§fn overflowing_cast(self) -> (FixedI8<Frac>, bool)
fn overflowing_cast(self) -> (FixedI8<Frac>, bool)
source§impl<Frac: LeEqU128> OverflowingCast<FixedU128<Frac>> for F128
impl<Frac: LeEqU128> OverflowingCast<FixedU128<Frac>> for F128
source§fn overflowing_cast(self) -> (FixedU128<Frac>, bool)
fn overflowing_cast(self) -> (FixedU128<Frac>, bool)
source§impl<Frac: LeEqU16> OverflowingCast<FixedU16<Frac>> for F128
impl<Frac: LeEqU16> OverflowingCast<FixedU16<Frac>> for F128
source§fn overflowing_cast(self) -> (FixedU16<Frac>, bool)
fn overflowing_cast(self) -> (FixedU16<Frac>, bool)
source§impl<Frac: LeEqU32> OverflowingCast<FixedU32<Frac>> for F128
impl<Frac: LeEqU32> OverflowingCast<FixedU32<Frac>> for F128
source§fn overflowing_cast(self) -> (FixedU32<Frac>, bool)
fn overflowing_cast(self) -> (FixedU32<Frac>, bool)
source§impl<Frac: LeEqU64> OverflowingCast<FixedU64<Frac>> for F128
impl<Frac: LeEqU64> OverflowingCast<FixedU64<Frac>> for F128
source§fn overflowing_cast(self) -> (FixedU64<Frac>, bool)
fn overflowing_cast(self) -> (FixedU64<Frac>, bool)
source§impl<Frac: LeEqU8> OverflowingCast<FixedU8<Frac>> for F128
impl<Frac: LeEqU8> OverflowingCast<FixedU8<Frac>> for F128
source§fn overflowing_cast(self) -> (FixedU8<Frac>, bool)
fn overflowing_cast(self) -> (FixedU8<Frac>, bool)
source§impl PartialEq<F128> for F128
impl PartialEq<F128> for F128
source§impl<Frac: Unsigned> PartialEq<FixedI128<Frac>> for F128
impl<Frac: Unsigned> PartialEq<FixedI128<Frac>> for F128
source§impl<Frac: Unsigned> PartialEq<FixedI16<Frac>> for F128
impl<Frac: Unsigned> PartialEq<FixedI16<Frac>> for F128
source§impl<Frac: Unsigned> PartialEq<FixedI32<Frac>> for F128
impl<Frac: Unsigned> PartialEq<FixedI32<Frac>> for F128
source§impl<Frac: Unsigned> PartialEq<FixedI64<Frac>> for F128
impl<Frac: Unsigned> PartialEq<FixedI64<Frac>> for F128
source§impl<Frac: Unsigned> PartialEq<FixedI8<Frac>> for F128
impl<Frac: Unsigned> PartialEq<FixedI8<Frac>> for F128
source§impl<Frac: Unsigned> PartialEq<FixedU128<Frac>> for F128
impl<Frac: Unsigned> PartialEq<FixedU128<Frac>> for F128
source§impl<Frac: Unsigned> PartialEq<FixedU16<Frac>> for F128
impl<Frac: Unsigned> PartialEq<FixedU16<Frac>> for F128
source§impl<Frac: Unsigned> PartialEq<FixedU32<Frac>> for F128
impl<Frac: Unsigned> PartialEq<FixedU32<Frac>> for F128
source§impl<Frac: Unsigned> PartialEq<FixedU64<Frac>> for F128
impl<Frac: Unsigned> PartialEq<FixedU64<Frac>> for F128
source§impl<Frac: Unsigned> PartialEq<FixedU8<Frac>> for F128
impl<Frac: Unsigned> PartialEq<FixedU8<Frac>> for F128
source§impl PartialOrd<F128> for F128
impl PartialOrd<F128> for F128
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<F128> for FixedI128<Frac>
impl<Frac: Unsigned> PartialOrd<F128> for FixedI128<Frac>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<F128> for FixedI16<Frac>
impl<Frac: Unsigned> PartialOrd<F128> for FixedI16<Frac>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<F128> for FixedI32<Frac>
impl<Frac: Unsigned> PartialOrd<F128> for FixedI32<Frac>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<F128> for FixedI64<Frac>
impl<Frac: Unsigned> PartialOrd<F128> for FixedI64<Frac>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<F128> for FixedI8<Frac>
impl<Frac: Unsigned> PartialOrd<F128> for FixedI8<Frac>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<F128> for FixedU128<Frac>
impl<Frac: Unsigned> PartialOrd<F128> for FixedU128<Frac>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<F128> for FixedU16<Frac>
impl<Frac: Unsigned> PartialOrd<F128> for FixedU16<Frac>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<F128> for FixedU32<Frac>
impl<Frac: Unsigned> PartialOrd<F128> for FixedU32<Frac>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<F128> for FixedU64<Frac>
impl<Frac: Unsigned> PartialOrd<F128> for FixedU64<Frac>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<F128> for FixedU8<Frac>
impl<Frac: Unsigned> PartialOrd<F128> for FixedU8<Frac>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<FixedI128<Frac>> for F128
impl<Frac: Unsigned> PartialOrd<FixedI128<Frac>> for F128
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<FixedI16<Frac>> for F128
impl<Frac: Unsigned> PartialOrd<FixedI16<Frac>> for F128
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<FixedI32<Frac>> for F128
impl<Frac: Unsigned> PartialOrd<FixedI32<Frac>> for F128
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<FixedI64<Frac>> for F128
impl<Frac: Unsigned> PartialOrd<FixedI64<Frac>> for F128
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<FixedI8<Frac>> for F128
impl<Frac: Unsigned> PartialOrd<FixedI8<Frac>> for F128
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<FixedU128<Frac>> for F128
impl<Frac: Unsigned> PartialOrd<FixedU128<Frac>> for F128
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<FixedU16<Frac>> for F128
impl<Frac: Unsigned> PartialOrd<FixedU16<Frac>> for F128
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<FixedU32<Frac>> for F128
impl<Frac: Unsigned> PartialOrd<FixedU32<Frac>> for F128
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<FixedU64<Frac>> for F128
impl<Frac: Unsigned> PartialOrd<FixedU64<Frac>> for F128
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: Unsigned> PartialOrd<FixedU8<Frac>> for F128
impl<Frac: Unsigned> PartialOrd<FixedU8<Frac>> for F128
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<Frac: LeEqU128> SaturatingCast<F128> for FixedI128<Frac>
impl<Frac: LeEqU128> SaturatingCast<F128> for FixedI128<Frac>
source§fn saturating_cast(self) -> F128
fn saturating_cast(self) -> F128
source§impl<Frac: LeEqU16> SaturatingCast<F128> for FixedI16<Frac>
impl<Frac: LeEqU16> SaturatingCast<F128> for FixedI16<Frac>
source§fn saturating_cast(self) -> F128
fn saturating_cast(self) -> F128
source§impl<Frac: LeEqU32> SaturatingCast<F128> for FixedI32<Frac>
impl<Frac: LeEqU32> SaturatingCast<F128> for FixedI32<Frac>
source§fn saturating_cast(self) -> F128
fn saturating_cast(self) -> F128
source§impl<Frac: LeEqU64> SaturatingCast<F128> for FixedI64<Frac>
impl<Frac: LeEqU64> SaturatingCast<F128> for FixedI64<Frac>
source§fn saturating_cast(self) -> F128
fn saturating_cast(self) -> F128
source§impl<Frac: LeEqU8> SaturatingCast<F128> for FixedI8<Frac>
impl<Frac: LeEqU8> SaturatingCast<F128> for FixedI8<Frac>
source§fn saturating_cast(self) -> F128
fn saturating_cast(self) -> F128
source§impl<Frac: LeEqU128> SaturatingCast<F128> for FixedU128<Frac>
impl<Frac: LeEqU128> SaturatingCast<F128> for FixedU128<Frac>
source§fn saturating_cast(self) -> F128
fn saturating_cast(self) -> F128
source§impl<Frac: LeEqU16> SaturatingCast<F128> for FixedU16<Frac>
impl<Frac: LeEqU16> SaturatingCast<F128> for FixedU16<Frac>
source§fn saturating_cast(self) -> F128
fn saturating_cast(self) -> F128
source§impl<Frac: LeEqU32> SaturatingCast<F128> for FixedU32<Frac>
impl<Frac: LeEqU32> SaturatingCast<F128> for FixedU32<Frac>
source§fn saturating_cast(self) -> F128
fn saturating_cast(self) -> F128
source§impl<Frac: LeEqU64> SaturatingCast<F128> for FixedU64<Frac>
impl<Frac: LeEqU64> SaturatingCast<F128> for FixedU64<Frac>
source§fn saturating_cast(self) -> F128
fn saturating_cast(self) -> F128
source§impl<Frac: LeEqU8> SaturatingCast<F128> for FixedU8<Frac>
impl<Frac: LeEqU8> SaturatingCast<F128> for FixedU8<Frac>
source§fn saturating_cast(self) -> F128
fn saturating_cast(self) -> F128
source§impl<Frac: LeEqU128> SaturatingCast<FixedI128<Frac>> for F128
impl<Frac: LeEqU128> SaturatingCast<FixedI128<Frac>> for F128
source§fn saturating_cast(self) -> FixedI128<Frac>
fn saturating_cast(self) -> FixedI128<Frac>
source§impl<Frac: LeEqU16> SaturatingCast<FixedI16<Frac>> for F128
impl<Frac: LeEqU16> SaturatingCast<FixedI16<Frac>> for F128
source§fn saturating_cast(self) -> FixedI16<Frac>
fn saturating_cast(self) -> FixedI16<Frac>
source§impl<Frac: LeEqU32> SaturatingCast<FixedI32<Frac>> for F128
impl<Frac: LeEqU32> SaturatingCast<FixedI32<Frac>> for F128
source§fn saturating_cast(self) -> FixedI32<Frac>
fn saturating_cast(self) -> FixedI32<Frac>
source§impl<Frac: LeEqU64> SaturatingCast<FixedI64<Frac>> for F128
impl<Frac: LeEqU64> SaturatingCast<FixedI64<Frac>> for F128
source§fn saturating_cast(self) -> FixedI64<Frac>
fn saturating_cast(self) -> FixedI64<Frac>
source§impl<Frac: LeEqU8> SaturatingCast<FixedI8<Frac>> for F128
impl<Frac: LeEqU8> SaturatingCast<FixedI8<Frac>> for F128
source§fn saturating_cast(self) -> FixedI8<Frac>
fn saturating_cast(self) -> FixedI8<Frac>
source§impl<Frac: LeEqU128> SaturatingCast<FixedU128<Frac>> for F128
impl<Frac: LeEqU128> SaturatingCast<FixedU128<Frac>> for F128
source§fn saturating_cast(self) -> FixedU128<Frac>
fn saturating_cast(self) -> FixedU128<Frac>
source§impl<Frac: LeEqU16> SaturatingCast<FixedU16<Frac>> for F128
impl<Frac: LeEqU16> SaturatingCast<FixedU16<Frac>> for F128
source§fn saturating_cast(self) -> FixedU16<Frac>
fn saturating_cast(self) -> FixedU16<Frac>
source§impl<Frac: LeEqU32> SaturatingCast<FixedU32<Frac>> for F128
impl<Frac: LeEqU32> SaturatingCast<FixedU32<Frac>> for F128
source§fn saturating_cast(self) -> FixedU32<Frac>
fn saturating_cast(self) -> FixedU32<Frac>
source§impl<Frac: LeEqU64> SaturatingCast<FixedU64<Frac>> for F128
impl<Frac: LeEqU64> SaturatingCast<FixedU64<Frac>> for F128
source§fn saturating_cast(self) -> FixedU64<Frac>
fn saturating_cast(self) -> FixedU64<Frac>
source§impl<Frac: LeEqU8> SaturatingCast<FixedU8<Frac>> for F128
impl<Frac: LeEqU8> SaturatingCast<FixedU8<Frac>> for F128
source§fn saturating_cast(self) -> FixedU8<Frac>
fn saturating_cast(self) -> FixedU8<Frac>
source§impl ToFixed for F128
impl ToFixed for F128
source§fn to_fixed<F: Fixed>(self) -> F
fn to_fixed<F: Fixed>(self) -> F
Converts a floating-point number to a fixed-point number.
Rounding is to the nearest, with ties rounded to even.
Panics
Panics if self
is not finite.
When debug assertions are enabled, also panics if the value does not
fit. When debug assertions are not enabled, the wrapped value can be
returned, but it is not considered a breaking change if in the future
it panics; if wrapping is required use wrapping_to_fixed
instead.
source§fn checked_to_fixed<F: Fixed>(self) -> Option<F>
fn checked_to_fixed<F: Fixed>(self) -> Option<F>
Converts a floating-point number to a fixed-point
number if it fits, otherwise returns None
.
Rounding is to the nearest, with ties rounded to even.