libm/math/
copysign.rs

1/// Sign of Y, magnitude of X (f16)
2///
3/// Constructs a number with the magnitude (absolute value) of its
4/// first argument, `x`, and the sign of its second argument, `y`.
5#[cfg(f16_enabled)]
6#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
7pub fn copysignf16(x: f16, y: f16) -> f16 {
8    super::generic::copysign(x, y)
9}
10
11/// Sign of Y, magnitude of X (f32)
12///
13/// Constructs a number with the magnitude (absolute value) of its
14/// first argument, `x`, and the sign of its second argument, `y`.
15#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
16pub fn copysignf(x: f32, y: f32) -> f32 {
17    super::generic::copysign(x, y)
18}
19
20/// Sign of Y, magnitude of X (f64)
21///
22/// Constructs a number with the magnitude (absolute value) of its
23/// first argument, `x`, and the sign of its second argument, `y`.
24#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
25pub fn copysign(x: f64, y: f64) -> f64 {
26    super::generic::copysign(x, y)
27}
28
29/// Sign of Y, magnitude of X (f128)
30///
31/// Constructs a number with the magnitude (absolute value) of its
32/// first argument, `x`, and the sign of its second argument, `y`.
33#[cfg(f128_enabled)]
34#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
35pub fn copysignf128(x: f128, y: f128) -> f128 {
36    super::generic::copysign(x, y)
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42    use crate::support::Float;
43
44    fn spec_test<F: Float>(f: impl Fn(F, F) -> F) {
45        assert_biteq!(f(F::ZERO, F::ZERO), F::ZERO);
46        assert_biteq!(f(F::NEG_ZERO, F::ZERO), F::ZERO);
47        assert_biteq!(f(F::ZERO, F::NEG_ZERO), F::NEG_ZERO);
48        assert_biteq!(f(F::NEG_ZERO, F::NEG_ZERO), F::NEG_ZERO);
49
50        assert_biteq!(f(F::ONE, F::ONE), F::ONE);
51        assert_biteq!(f(F::NEG_ONE, F::ONE), F::ONE);
52        assert_biteq!(f(F::ONE, F::NEG_ONE), F::NEG_ONE);
53        assert_biteq!(f(F::NEG_ONE, F::NEG_ONE), F::NEG_ONE);
54
55        assert_biteq!(f(F::INFINITY, F::INFINITY), F::INFINITY);
56        assert_biteq!(f(F::NEG_INFINITY, F::INFINITY), F::INFINITY);
57        assert_biteq!(f(F::INFINITY, F::NEG_INFINITY), F::NEG_INFINITY);
58        assert_biteq!(f(F::NEG_INFINITY, F::NEG_INFINITY), F::NEG_INFINITY);
59
60        // Not required but we expect it
61        assert_biteq!(f(F::NAN, F::NAN), F::NAN);
62        assert_biteq!(f(F::NEG_NAN, F::NAN), F::NAN);
63        assert_biteq!(f(F::NAN, F::NEG_NAN), F::NEG_NAN);
64        assert_biteq!(f(F::NEG_NAN, F::NEG_NAN), F::NEG_NAN);
65    }
66
67    #[test]
68    #[cfg(f16_enabled)]
69    fn spec_tests_f16() {
70        spec_test::<f16>(copysignf16);
71    }
72
73    #[test]
74    fn spec_tests_f32() {
75        spec_test::<f32>(copysignf);
76    }
77
78    #[test]
79    fn spec_tests_f64() {
80        spec_test::<f64>(copysign);
81    }
82
83    #[test]
84    #[cfg(f128_enabled)]
85    fn spec_tests_f128() {
86        spec_test::<f128>(copysignf128);
87    }
88}