@@ -1146,29 +1146,35 @@ impl f64 {
11461146 /// representation into the `f64` type, similar to the
11471147 /// `transmute` function.
11481148 ///
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.
11501154 ///
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.
11531156 ///
11541157 /// # Examples
11551158 ///
11561159 /// ```
11571160 /// #![feature(float_bits_conv)]
11581161 /// use std::f64;
1159- /// let v = f64::from_bits(0x4029000000000000).unwrap() ;
1162+ /// let v = f64::from_bits(0x4029000000000000);
11601163 /// let difference = (v - 12.5).abs();
11611164 /// assert!(difference <= 1e-5);
11621165 /// // 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);
11641168 /// ```
11651169 #[ unstable( feature = "float_bits_conv" , reason = "recently added" , issue = "40470" ) ]
11661170 #[ inline]
1167- pub fn from_bits ( v : u64 ) -> Result < Self , ( ) > {
1171+ pub fn from_bits ( v : u64 ) -> Self {
11681172 match v {
1173+ // sNaN limits source:
1174+ // https://www.doc.ic.ac.uk/~eedwards/compsys/float/nan.html
11691175 0x7FF0000000000001 ... 0x7FF7FFFFFFFFFFFF |
1170- 0xFFF0000000000001 ... 0xFFF7FFFFFFFFFFFF => Err ( ( ) ) ,
1171- _ => Ok ( unsafe { :: mem:: transmute ( v) } ) ,
1176+ 0xFFF0000000000001 ... 0xFFF7FFFFFFFFFFFF => :: f64 :: NAN ,
1177+ _ => unsafe { :: mem:: transmute ( v) } ,
11721178 }
11731179 }
11741180}
@@ -1815,9 +1821,9 @@ mod tests {
18151821 assert_eq ! ( ( 12.5f64 ) . to_bits( ) , 0x4029000000000000 ) ;
18161822 assert_eq ! ( ( 1337f64 ) . to_bits( ) , 0x4094e40000000000 ) ;
18171823 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 ) ;
18221828 }
18231829}
0 commit comments