Skip to content

Commit 59a1753

Browse files
committed
Implement is_sign_* and signum methods in terms of libm.
1 parent 92298b2 commit 59a1753

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/float.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,22 @@ impl FloatCore for f32 {
835835
fn fract(self) -> Self {
836836
self - libm::truncf(self)
837837
}
838+
839+
#[cfg(all(not(feature = "std"), feature = "libm"))]
840+
#[inline]
841+
fn is_sign_negative(self) -> bool {
842+
libm::copysignf(1.0f32, self) == -1.0f32
843+
}
844+
845+
#[cfg(all(not(feature = "std"), feature = "libm"))]
846+
#[inline]
847+
fn signum(self) -> Self {
848+
if self.is_nan() {
849+
FloatCore::nan()
850+
} else {
851+
libm::copysignf(1.0f32, self)
852+
}
853+
}
838854
}
839855

840856
impl FloatCore for f64 {
@@ -927,6 +943,22 @@ impl FloatCore for f64 {
927943
fn fract(self) -> Self {
928944
self - libm::trunc(self)
929945
}
946+
947+
#[cfg(all(not(feature = "std"), feature = "libm"))]
948+
#[inline]
949+
fn is_sign_negative(self) -> bool {
950+
libm::copysign(1.0f64, self) == -1.0f64
951+
}
952+
953+
#[cfg(all(not(feature = "std"), feature = "libm"))]
954+
#[inline]
955+
fn signum(self) -> Self {
956+
if self.is_nan() {
957+
FloatCore::nan()
958+
} else {
959+
libm::copysign(1.0f64, self)
960+
}
961+
}
930962
}
931963

932964
// FIXME: these doctests aren't actually helpful, because they're using and

0 commit comments

Comments
 (0)