@@ -1146,29 +1146,35 @@ impl f64 {
1146
1146
/// representation into the `f64` type, similar to the
1147
1147
/// `transmute` function.
1148
1148
///
1149
- /// Note that this function is distinct from casting.
1149
+ /// There is only one difference to a bare `transmute`:
1150
+ /// Due to the implications onto Rust's safety promises being
1151
+ /// uncertain, if the representation of a signaling NaN "sNaN" float
1152
+ /// is passed to the function, a quiet NaN will be returned
1153
+ /// instead.
1150
1154
///
1151
- /// Returns `Err(())` if the representation of a signaling NaN "sNaN"
1152
- /// float, is passed to the function.
1155
+ /// Note that this function is distinct from casting.
1153
1156
///
1154
1157
/// # Examples
1155
1158
///
1156
1159
/// ```
1157
1160
/// #![feature(float_bits_conv)]
1158
1161
/// use std::f64;
1159
- /// let v = f64::from_bits(0x4029000000000000).unwrap() ;
1162
+ /// let v = f64::from_bits(0x4029000000000000);
1160
1163
/// let difference = (v - 12.5).abs();
1161
1164
/// assert!(difference <= 1e-5);
1162
1165
/// // Example for a signaling NaN value:
1163
- /// assert_eq!(f64::from_bits(0x7FF0000000000001), Err(()));
1166
+ /// let snan = 0x7FF0000000000001;
1167
+ /// assert_ne!(f64::from_bits(snan).to_bits(), snan);
1164
1168
/// ```
1165
1169
#[ unstable( feature = "float_bits_conv" , reason = "recently added" , issue = "40470" ) ]
1166
1170
#[ inline]
1167
- pub fn from_bits ( v : u64 ) -> Result < Self , ( ) > {
1171
+ pub fn from_bits ( v : u64 ) -> Self {
1168
1172
match v {
1173
+ // sNaN limits source:
1174
+ // https://www.doc.ic.ac.uk/~eedwards/compsys/float/nan.html
1169
1175
0x7FF0000000000001 ... 0x7FF7FFFFFFFFFFFF |
1170
- 0xFFF0000000000001 ... 0xFFF7FFFFFFFFFFFF => Err ( ( ) ) ,
1171
- _ => Ok ( unsafe { :: mem:: transmute ( v) } ) ,
1176
+ 0xFFF0000000000001 ... 0xFFF7FFFFFFFFFFFF => :: f64 :: NAN ,
1177
+ _ => unsafe { :: mem:: transmute ( v) } ,
1172
1178
}
1173
1179
}
1174
1180
}
@@ -1815,9 +1821,9 @@ mod tests {
1815
1821
assert_eq ! ( ( 12.5f64 ) . to_bits( ) , 0x4029000000000000 ) ;
1816
1822
assert_eq ! ( ( 1337f64 ) . to_bits( ) , 0x4094e40000000000 ) ;
1817
1823
assert_eq ! ( ( -14.25f64 ) . to_bits( ) , 0xc02c800000000000 ) ;
1818
- assert_approx_eq ! ( f64 :: from_bits( 0x3ff0000000000000 ) . unwrap ( ) , 1.0 ) ;
1819
- assert_approx_eq ! ( f64 :: from_bits( 0x4029000000000000 ) . unwrap ( ) , 12.5 ) ;
1820
- assert_approx_eq ! ( f64 :: from_bits( 0x4094e40000000000 ) . unwrap ( ) , 1337.0 ) ;
1821
- assert_approx_eq ! ( f64 :: from_bits( 0xc02c800000000000 ) . unwrap ( ) , -14.25 ) ;
1824
+ assert_approx_eq ! ( f64 :: from_bits( 0x3ff0000000000000 ) , 1.0 ) ;
1825
+ assert_approx_eq ! ( f64 :: from_bits( 0x4029000000000000 ) , 12.5 ) ;
1826
+ assert_approx_eq ! ( f64 :: from_bits( 0x4094e40000000000 ) , 1337.0 ) ;
1827
+ assert_approx_eq ! ( f64 :: from_bits( 0xc02c800000000000 ) , -14.25 ) ;
1822
1828
}
1823
1829
}
0 commit comments