diff --git a/src/float.rs b/src/float.rs index ce1b4acf..9725541b 100644 --- a/src/float.rs +++ b/src/float.rs @@ -1840,6 +1840,35 @@ pub trait Float: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> { /// assert!(abs_difference < 1e-10); /// ``` fn integer_decode(self) -> (u64, i16, i8); + + /// Returns a number composed of the magnitude of `self` and the sign of + /// `sign`. + /// + /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise + /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of + /// `sign` is returned. + /// + /// # Examples + /// + /// ``` + /// use num_traits::Float; + /// + /// let f = 3.5_f32; + /// + /// assert_eq!(f.copysign(0.42), 3.5_f32); + /// assert_eq!(f.copysign(-0.42), -3.5_f32); + /// assert_eq!((-f).copysign(0.42), 3.5_f32); + /// assert_eq!((-f).copysign(-0.42), -3.5_f32); + /// + /// assert!(f32::nan().copysign(1.0).is_nan()); + /// ``` + fn copysign(self, sign: Self) -> Self { + if self.is_sign_negative() == sign.is_sign_negative() { + self + } else { + self.neg() + } + } } #[cfg(feature = "std")] @@ -1916,6 +1945,7 @@ macro_rules! float_impl_std { Self::asinh(self) -> Self; Self::acosh(self) -> Self; Self::atanh(self) -> Self; + Self::copysign(self, sign: Self) -> Self; } } }; @@ -2095,6 +2125,7 @@ impl Float for f64 { libm::atanh as atanh(self) -> Self; libm::fmax as max(self, other: Self) -> Self; libm::fmin as min(self, other: Self) -> Self; + libm::copysign as copysign(self, sign: Self) -> Self; } } @@ -2243,4 +2274,28 @@ mod tests { check::<f32>(1e-6); check::<f64>(1e-12); } + + #[cfg(any(feature = "std", feature = "libm"))] + #[test] + fn copysign() { + use float::Float; + test_copysign_generic(2.0_f32, -2.0_f32, f32::nan()); + test_copysign_generic(2.0_f64, -2.0_f64, f64::nan()); + } + + #[cfg(any(feature = "std", feature = "libm"))] + fn test_copysign_generic<F: float::Float + core::fmt::Debug>(p: F, n: F, nan: F) { + assert!(p.is_sign_positive()); + assert!(n.is_sign_negative()); + assert!(nan.is_nan()); + + assert_eq!(p, p.copysign(p)); + assert_eq!(p.neg(), p.copysign(n)); + + assert_eq!(n, n.copysign(n)); + assert_eq!(n.neg(), n.copysign(p)); + + assert!(nan.copysign(p).is_sign_positive()); + assert!(nan.copysign(n).is_sign_negative()); + } }