@@ -393,6 +393,15 @@ impl f32 {
393
393
pub const MAX_10_EXP : i32 = 38 ;
394
394
395
395
/// Not a Number (NaN).
396
+ ///
397
+ /// Note that IEEE-745 doesn't define just a single NaN value;
398
+ /// a plethora of bit patterns are considered to be NaN.
399
+ /// Furthermore, the standard makes a difference
400
+ /// between a "signaling" and a "quiet" NaN,
401
+ /// and allows inspecting its "payload" (the unspecified bits in the bit pattern).
402
+ /// This constant isn't guaranteed to equal to any specific NaN bitpattern,
403
+ /// and the stability of its representation over Rust versions
404
+ /// and target platforms isn't guaranteed.
396
405
#[ stable( feature = "assoc_int_consts" , since = "1.43.0" ) ]
397
406
pub const NAN : f32 = 0.0_f32 / 0.0_f32 ;
398
407
/// Infinity (∞).
@@ -402,7 +411,7 @@ impl f32 {
402
411
#[ stable( feature = "assoc_int_consts" , since = "1.43.0" ) ]
403
412
pub const NEG_INFINITY : f32 = -1.0_f32 / 0.0_f32 ;
404
413
405
- /// Returns `true` if this value is ` NaN` .
414
+ /// Returns `true` if this value is NaN.
406
415
///
407
416
/// ```
408
417
/// let nan = f32::NAN;
@@ -455,7 +464,7 @@ impl f32 {
455
464
( self == f32:: INFINITY ) | ( self == f32:: NEG_INFINITY )
456
465
}
457
466
458
- /// Returns `true` if this number is neither infinite nor ` NaN` .
467
+ /// Returns `true` if this number is neither infinite nor NaN.
459
468
///
460
469
/// ```
461
470
/// let f = 7.0f32;
@@ -506,7 +515,7 @@ impl f32 {
506
515
}
507
516
508
517
/// Returns `true` if the number is neither zero, infinite,
509
- /// [subnormal], or ` NaN` .
518
+ /// [subnormal], or NaN.
510
519
///
511
520
/// ```
512
521
/// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
@@ -622,8 +631,12 @@ impl f32 {
622
631
}
623
632
}
624
633
625
- /// Returns `true` if `self` has a positive sign, including `+0.0`, `NaN`s with
626
- /// positive sign bit and positive infinity.
634
+ /// Returns `true` if `self` has a positive sign, including `+0.0`, NaNs with
635
+ /// positive sign bit and positive infinity. Note that IEEE-745 doesn't assign any
636
+ /// meaning to the sign bit in case of a NaN, and as Rust doesn't guarantee that
637
+ /// the bit pattern of NaNs are conserved over arithmetic operations, the result of
638
+ /// `is_sign_positive` on a NaN might produce an unexpected result in some cases.
639
+ /// See [explanation of NaN as a special value](f32) for more info.
627
640
///
628
641
/// ```
629
642
/// let f = 7.0_f32;
@@ -640,8 +653,12 @@ impl f32 {
640
653
!self . is_sign_negative ( )
641
654
}
642
655
643
- /// Returns `true` if `self` has a negative sign, including `-0.0`, `NaN`s with
644
- /// negative sign bit and negative infinity.
656
+ /// Returns `true` if `self` has a negative sign, including `-0.0`, NaNs with
657
+ /// negative sign bit and negative infinity. Note that IEEE-745 doesn't assign any
658
+ /// meaning to the sign bit in case of a NaN, and as Rust doesn't guarantee that
659
+ /// the bit pattern of NaNs are conserved over arithmetic operations, the result of
660
+ /// `is_sign_negative` on a NaN might produce an unexpected result in some cases.
661
+ /// See [explanation of NaN as a special value](f32) for more info.
645
662
///
646
663
/// ```
647
664
/// let f = 7.0f32;
@@ -713,47 +730,47 @@ impl f32 {
713
730
self * ( value / 180.0f32 )
714
731
}
715
732
716
- /// Returns the maximum of the two numbers.
733
+ /// Returns the maximum of the two numbers, ignoring NaN .
717
734
///
718
- /// Follows the IEEE-754 2008 semantics for maxNum, except for handling of signaling NaNs.
719
- /// This matches the behavior of libm’s fmax.
735
+ /// If one of the arguments is NaN, then the other argument is returned.
736
+ /// This follows the IEEE-754 2008 semantics for maxNum, except for handling of signaling NaNs;
737
+ /// this function handles all NaNs the same way and avoids maxNum's problems with associativity.
738
+ /// This also matches the behavior of libm’s fmax.
720
739
///
721
740
/// ```
722
741
/// let x = 1.0f32;
723
742
/// let y = 2.0f32;
724
743
///
725
744
/// assert_eq!(x.max(y), y);
726
745
/// ```
727
- ///
728
- /// If one of the arguments is NaN, then the other argument is returned.
729
746
#[ must_use = "this returns the result of the comparison, without modifying either input" ]
730
747
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
731
748
#[ inline]
732
749
pub fn max ( self , other : f32 ) -> f32 {
733
750
intrinsics:: maxnumf32 ( self , other)
734
751
}
735
752
736
- /// Returns the minimum of the two numbers.
753
+ /// Returns the minimum of the two numbers, ignoring NaN .
737
754
///
738
- /// Follows the IEEE-754 2008 semantics for minNum, except for handling of signaling NaNs.
739
- /// This matches the behavior of libm’s fmin.
755
+ /// If one of the arguments is NaN, then the other argument is returned.
756
+ /// This follows the IEEE-754 2008 semantics for minNum, except for handling of signaling NaNs;
757
+ /// this function handles all NaNs the same way and avoids minNum's problems with associativity.
758
+ /// This also matches the behavior of libm’s fmin.
740
759
///
741
760
/// ```
742
761
/// let x = 1.0f32;
743
762
/// let y = 2.0f32;
744
763
///
745
764
/// assert_eq!(x.min(y), x);
746
765
/// ```
747
- ///
748
- /// If one of the arguments is NaN, then the other argument is returned.
749
766
#[ must_use = "this returns the result of the comparison, without modifying either input" ]
750
767
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
751
768
#[ inline]
752
769
pub fn min ( self , other : f32 ) -> f32 {
753
770
intrinsics:: minnumf32 ( self , other)
754
771
}
755
772
756
- /// Returns the maximum of the two numbers, propagating NaNs .
773
+ /// Returns the maximum of the two numbers, propagating NaN .
757
774
///
758
775
/// This returns NaN when *either* argument is NaN, as opposed to
759
776
/// [`f32::max`] which only returns NaN when *both* arguments are NaN.
@@ -770,6 +787,9 @@ impl f32 {
770
787
/// If one of the arguments is NaN, then NaN is returned. Otherwise this returns the greater
771
788
/// of the two numbers. For this operation, -0.0 is considered to be less than +0.0.
772
789
/// Note that this follows the semantics specified in IEEE 754-2019.
790
+ ///
791
+ /// Also note that "propagation" of NaNs here doesn't necessarily mean that the bitpattern of a NaN
792
+ /// operand is conserved; see [explanation of NaN as a special value](f32) for more info.
773
793
#[ must_use = "this returns the result of the comparison, without modifying either input" ]
774
794
#[ unstable( feature = "float_minimum_maximum" , issue = "91079" ) ]
775
795
#[ inline]
@@ -785,7 +805,7 @@ impl f32 {
785
805
}
786
806
}
787
807
788
- /// Returns the minimum of the two numbers, propagating NaNs .
808
+ /// Returns the minimum of the two numbers, propagating NaN .
789
809
///
790
810
/// This returns NaN when *either* argument is NaN, as opposed to
791
811
/// [`f32::min`] which only returns NaN when *both* arguments are NaN.
@@ -802,6 +822,9 @@ impl f32 {
802
822
/// If one of the arguments is NaN, then NaN is returned. Otherwise this returns the lesser
803
823
/// of the two numbers. For this operation, -0.0 is considered to be less than +0.0.
804
824
/// Note that this follows the semantics specified in IEEE 754-2019.
825
+ ///
826
+ /// Also note that "propagation" of NaNs here doesn't necessarily mean that the bitpattern of a NaN
827
+ /// operand is conserved; see [explanation of NaN as a special value](f32) for more info.
805
828
#[ must_use = "this returns the result of the comparison, without modifying either input" ]
806
829
#[ unstable( feature = "float_minimum_maximum" , issue = "91079" ) ]
807
830
#[ inline]
@@ -1009,6 +1032,9 @@ impl f32 {
1009
1032
/// Return the memory representation of this floating point number as a byte array in
1010
1033
/// big-endian (network) byte order.
1011
1034
///
1035
+ /// See [`from_bits`](Self::from_bits) for some discussion of the
1036
+ /// portability of this operation (there are almost no issues).
1037
+ ///
1012
1038
/// # Examples
1013
1039
///
1014
1040
/// ```
@@ -1027,6 +1053,9 @@ impl f32 {
1027
1053
/// Return the memory representation of this floating point number as a byte array in
1028
1054
/// little-endian byte order.
1029
1055
///
1056
+ /// See [`from_bits`](Self::from_bits) for some discussion of the
1057
+ /// portability of this operation (there are almost no issues).
1058
+ ///
1030
1059
/// # Examples
1031
1060
///
1032
1061
/// ```
@@ -1051,6 +1080,9 @@ impl f32 {
1051
1080
/// [`to_be_bytes`]: f32::to_be_bytes
1052
1081
/// [`to_le_bytes`]: f32::to_le_bytes
1053
1082
///
1083
+ /// See [`from_bits`](Self::from_bits) for some discussion of the
1084
+ /// portability of this operation (there are almost no issues).
1085
+ ///
1054
1086
/// # Examples
1055
1087
///
1056
1088
/// ```
@@ -1075,6 +1107,9 @@ impl f32 {
1075
1107
1076
1108
/// Create a floating point value from its representation as a byte array in big endian.
1077
1109
///
1110
+ /// See [`from_bits`](Self::from_bits) for some discussion of the
1111
+ /// portability of this operation (there are almost no issues).
1112
+ ///
1078
1113
/// # Examples
1079
1114
///
1080
1115
/// ```
@@ -1091,6 +1126,9 @@ impl f32 {
1091
1126
1092
1127
/// Create a floating point value from its representation as a byte array in little endian.
1093
1128
///
1129
+ /// See [`from_bits`](Self::from_bits) for some discussion of the
1130
+ /// portability of this operation (there are almost no issues).
1131
+ ///
1094
1132
/// # Examples
1095
1133
///
1096
1134
/// ```
@@ -1114,6 +1152,9 @@ impl f32 {
1114
1152
/// [`from_be_bytes`]: f32::from_be_bytes
1115
1153
/// [`from_le_bytes`]: f32::from_le_bytes
1116
1154
///
1155
+ /// See [`from_bits`](Self::from_bits) for some discussion of the
1156
+ /// portability of this operation (there are almost no issues).
1157
+ ///
1117
1158
/// # Examples
1118
1159
///
1119
1160
/// ```
0 commit comments