Bläddra i källkod

Implement is_sign_* and signum methods in terms of libm.

Alex S 3 år sedan
förälder
incheckning
59a17538b1
1 ändrade filer med 32 tillägg och 0 borttagningar
  1. 32 0
      src/float.rs

+ 32 - 0
src/float.rs

@@ -835,6 +835,22 @@ impl FloatCore for f32 {
     fn fract(self) -> Self {
         self - libm::truncf(self)
     }
+
+    #[cfg(all(not(feature = "std"), feature = "libm"))]
+    #[inline]
+    fn is_sign_negative(self) -> bool {
+        libm::copysignf(1.0f32, self) == -1.0f32
+    }
+
+    #[cfg(all(not(feature = "std"), feature = "libm"))]
+    #[inline]
+    fn signum(self) -> Self {
+        if self.is_nan() {
+            FloatCore::nan()
+        } else {
+            libm::copysignf(1.0f32, self)
+        }
+    }
 }
 
 impl FloatCore for f64 {
@@ -927,6 +943,22 @@ impl FloatCore for f64 {
     fn fract(self) -> Self {
         self - libm::trunc(self)
     }
+
+    #[cfg(all(not(feature = "std"), feature = "libm"))]
+    #[inline]
+    fn is_sign_negative(self) -> bool {
+        libm::copysign(1.0f64, self) == -1.0f64
+    }
+
+    #[cfg(all(not(feature = "std"), feature = "libm"))]
+    #[inline]
+    fn signum(self) -> Self {
+        if self.is_nan() {
+            FloatCore::nan()
+        } else {
+            libm::copysign(1.0f64, self)
+        }
+    }
 }
 
 // FIXME: these doctests aren't actually helpful, because they're using and