about summary refs log tree commit diff
path: root/src/libstd/f32.rs
diff options
context:
space:
mode:
authorest31 <MTest31@outlook.com>2017-01-24 18:15:11 +0100
committerest31 <MTest31@outlook.com>2017-04-18 02:43:16 +0200
commit32a43da68ae26bc3e22dbaca12ebb02ddc742cc7 (patch)
tree2b7579bac845bfc717c74ca949853d67e0275f7f /src/libstd/f32.rs
parent5516bcc4588ea6192298b4e3682eb1d09581912a (diff)
downloadrust-32a43da68ae26bc3e22dbaca12ebb02ddc742cc7.tar.gz
rust-32a43da68ae26bc3e22dbaca12ebb02ddc742cc7.zip
Add functions to safely transmute float to int
Diffstat (limited to 'src/libstd/f32.rs')
-rw-r--r--src/libstd/f32.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs
index dd831800683..8e7b7ee0923 100644
--- a/src/libstd/f32.rs
+++ b/src/libstd/f32.rs
@@ -1226,6 +1226,45 @@ impl f32 {
     pub fn atanh(self) -> f32 {
         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
     }
+
+    /// Raw transmutation to `u32`.
+    ///
+    /// Converts the `f32` into its raw memory representation,
+    /// similar to the `transmute` function.
+    ///
+    /// Note that this function is distinct from casting.
+    ///
+    /// ```
+    /// #![feature(float_bits_conv)]
+    /// assert!((1f32).to_bits() != 1f32 as u32); // to_bits() is not casting!
+    /// assert_eq!((12.5f32).to_bits(), 0x41480000);
+    ///
+    /// ```
+    #[unstable(feature = "float_bits_conv", reason = "recently added", issue = "0")]
+    #[inline]
+    pub fn to_bits(self) -> u32 {
+        unsafe { ::mem::transmute(self) }
+    }
+
+    /// Raw transmutation from `u32`.
+    ///
+    /// Converts the given `u32` containing the float's raw memory
+    /// representation into the `f32` type, similar to the
+    /// `transmute` function.
+    ///
+    /// Note that this function is distinct from casting.
+    ///
+    /// ```
+    /// #![feature(float_bits_conv)]
+    /// use std::f32;
+    /// let difference = (f32::from_bits(0x41480000) - 12.5).abs();
+    /// assert!(difference <= 1e-5);
+    /// ```
+    #[unstable(feature = "float_bits_conv", reason = "recently added", issue = "0")]
+    #[inline]
+    pub fn from_bits(v: u32) -> Self {
+        unsafe { ::mem::transmute(v) }
+    }
 }
 
 #[cfg(test)]
@@ -1870,4 +1909,16 @@ mod tests {
         assert_approx_eq!(ln_2, 2f32.ln());
         assert_approx_eq!(ln_10, 10f32.ln());
     }
+
+    #[test]
+    fn test_float_bits_conv() {
+        assert_eq!((1f32).to_bits(), 0x3f800000);
+        assert_eq!((12.5f32).to_bits(), 0x41480000);
+        assert_eq!((1337f32).to_bits(), 0x44a72000);
+        assert_eq!((-14.25f32).to_bits(), 0xc1640000);
+        assert_approx_eq!(f32::from_bits(0x3f800000), 1.0);
+        assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
+        assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
+        assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);
+    }
 }