1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use crate::{
    fbig::FBig,
    repr::{Context, Repr, Word},
    round::Round,
};
use core::ops::{Mul, MulAssign, Neg};
use dashu_base::{Abs, Sign, Signed};
use dashu_int::IBig;

impl<R: Round, const B: Word> FBig<R, B> {
    /// Get the sign of the number. Zero value has a positive sign.
    ///
    /// # Examples
    ///
    /// ```
    /// # use dashu_base::{ParseError, Sign};
    /// # use dashu_float::DBig;
    /// assert_eq!(DBig::ZERO.sign(), Sign::Positive);
    /// assert_eq!(DBig::from_str_native("-1.234")?.sign(), Sign::Negative);
    /// # Ok::<(), ParseError>(())
    /// ```
    #[inline]
    pub const fn sign(&self) -> Sign {
        self.repr.sign()
    }

    /// A number representing the sign of `self`.
    ///
    /// * [FBig::ONE] if the number is positive (including `inf`)
    /// * [FBig::ZERO] if the number is zero
    /// * [FBig::NEG_ONE] if the number is negative (including `-inf`)
    ///
    /// # Examples
    /// ```
    /// # use dashu_base::ParseError;
    /// # use dashu_float::DBig;
    /// assert_eq!(DBig::from_str_native("2.01")?.signum(), DBig::ONE);
    /// assert_eq!(DBig::from_str_native("-1.234")?.signum(), DBig::NEG_ONE);
    /// # Ok::<(), ParseError>(())
    /// ```
    pub const fn signum(&self) -> Self {
        let significand = if self.repr.significand.is_zero() && self.repr.exponent != 0 {
            if self.repr.exponent > 0 {
                IBig::ONE
            } else {
                IBig::NEG_ONE
            }
        } else {
            self.repr.significand.signum()
        };
        let repr = Repr {
            significand,
            exponent: 0,
        };
        Self::new(repr, Context::new(1))
    }
}

impl<const B: Word> Neg for Repr<B> {
    type Output = Self;
    #[inline]
    fn neg(mut self) -> Self::Output {
        self.significand = -self.significand;
        self
    }
}

impl<R: Round, const B: Word> Neg for FBig<R, B> {
    type Output = Self;
    #[inline]
    fn neg(mut self) -> Self::Output {
        self.repr.significand = -self.repr.significand;
        self
    }
}

impl<R: Round, const B: Word> Neg for &FBig<R, B> {
    type Output = FBig<R, B>;
    #[inline]
    fn neg(self) -> Self::Output {
        self.clone().neg()
    }
}

impl<R: Round, const B: Word> Abs for FBig<R, B> {
    type Output = Self;
    fn abs(mut self) -> Self::Output {
        self.repr.significand = self.repr.significand.abs();
        self
    }
}

impl<R: Round, const B: Word> Mul<FBig<R, B>> for Sign {
    type Output = FBig<R, B>;
    #[inline]
    fn mul(self, mut rhs: FBig<R, B>) -> Self::Output {
        rhs.repr.significand *= self;
        rhs
    }
}

impl<R: Round, const B: Word> Mul<Sign> for FBig<R, B> {
    type Output = FBig<R, B>;
    #[inline]
    fn mul(mut self, rhs: Sign) -> Self::Output {
        self.repr.significand *= rhs;
        self
    }
}

impl<R: Round, const B: Word> MulAssign<Sign> for FBig<R, B> {
    #[inline]
    fn mul_assign(&mut self, rhs: Sign) {
        self.repr.significand *= rhs;
    }
}

impl<R: Round, const B: Word> Signed for FBig<R, B> {
    #[inline]
    fn sign(&self) -> Sign {
        self.repr.sign()
    }
}