dashu_ratio/
sign.rs

1use core::ops::{Mul, Neg};
2use dashu_base::{Abs, Sign, Signed};
3use dashu_int::UBig;
4
5use crate::{
6    rbig::{RBig, Relaxed},
7    repr::Repr,
8};
9
10impl RBig {
11    /// Get the sign of the number. Zero value has a positive sign.
12    ///
13    /// # Examples
14    ///
15    /// ```
16    /// # use dashu_base::Sign;
17    /// # use dashu_ratio::RBig;
18    /// assert_eq!(RBig::ZERO.sign(), Sign::Positive);
19    /// assert_eq!(RBig::ONE.sign(), Sign::Positive);
20    /// assert_eq!(RBig::NEG_ONE.sign(), Sign::Negative);
21    /// ```
22    #[inline]
23    pub const fn sign(&self) -> Sign {
24        self.0.numerator.sign()
25    }
26
27    /// A number representing the sign of `self`.
28    ///
29    /// * [RBig::ONE] if the number is positive (including `inf`)
30    /// * [RBig::ZERO] if the number is zero
31    /// * [RBig::NEG_ONE] if the number is negative (including `-inf`)
32    ///
33    /// # Examples
34    ///
35    /// ```
36    /// # use dashu_ratio::RBig;
37    ///
38    /// let r = RBig::from_parts((-10).into(), 5u8.into());
39    /// assert_eq!(r.signum(), RBig::NEG_ONE);
40    /// ```
41    #[inline]
42    pub const fn signum(&self) -> Self {
43        RBig(Repr {
44            numerator: self.0.numerator.signum(),
45            denominator: UBig::ONE,
46        })
47    }
48}
49
50impl Relaxed {
51    /// Get the sign of the number. Zero value has a positive sign.
52    ///
53    /// See [RBig::sign] for details.
54    #[inline]
55    pub const fn sign(&self) -> Sign {
56        self.0.numerator.sign()
57    }
58
59    /// A number representing the sign of `self`.
60    ///
61    /// See [RBig::signum] for details.
62    #[inline]
63    pub const fn signum(&self) -> Self {
64        Relaxed(Repr {
65            numerator: self.0.numerator.signum(),
66            denominator: UBig::ONE,
67        })
68    }
69}
70
71impl Repr {
72    #[inline]
73    pub fn neg(mut self) -> Repr {
74        self.numerator = -self.numerator;
75        self
76    }
77
78    #[inline]
79    pub fn abs(mut self) -> Repr {
80        if self.numerator.sign() == Sign::Negative {
81            self.numerator = -self.numerator
82        }
83        self
84    }
85}
86
87impl Neg for RBig {
88    type Output = RBig;
89    #[inline]
90    fn neg(self) -> Self::Output {
91        RBig(self.0.neg())
92    }
93}
94
95impl Neg for &RBig {
96    type Output = RBig;
97    #[inline]
98    fn neg(self) -> Self::Output {
99        RBig(self.0.clone().neg())
100    }
101}
102
103impl Neg for Relaxed {
104    type Output = Relaxed;
105    #[inline]
106    fn neg(self) -> Self::Output {
107        Relaxed(self.0.neg())
108    }
109}
110
111impl Neg for &Relaxed {
112    type Output = Relaxed;
113    #[inline]
114    fn neg(self) -> Self::Output {
115        Relaxed(self.0.clone().neg())
116    }
117}
118
119impl Mul<Repr> for Sign {
120    type Output = Repr;
121    #[inline]
122    fn mul(self, mut rhs: Repr) -> Repr {
123        rhs.numerator *= self;
124        rhs
125    }
126}
127impl Mul<Sign> for Repr {
128    type Output = Repr;
129    #[inline]
130    fn mul(mut self, rhs: Sign) -> Repr {
131        self.numerator *= rhs;
132        self
133    }
134}
135
136impl Mul<Sign> for RBig {
137    type Output = RBig;
138    #[inline]
139    fn mul(mut self, rhs: Sign) -> RBig {
140        self.0.numerator *= rhs;
141        self
142    }
143}
144
145impl Mul<Sign> for Relaxed {
146    type Output = Relaxed;
147    #[inline]
148    fn mul(mut self, rhs: Sign) -> Self::Output {
149        self.0.numerator *= rhs;
150        self
151    }
152}
153
154impl Signed for RBig {
155    #[inline]
156    fn sign(&self) -> Sign {
157        self.0.numerator.sign()
158    }
159}
160
161impl Signed for Relaxed {
162    #[inline]
163    fn sign(&self) -> Sign {
164        self.0.numerator.sign()
165    }
166}
167
168impl Abs for Repr {
169    type Output = Self;
170    #[inline]
171    fn abs(self) -> Self::Output {
172        let Repr {
173            numerator,
174            denominator,
175        } = self;
176        Repr {
177            numerator: numerator.abs(),
178            denominator,
179        }
180    }
181}
182
183impl Abs for RBig {
184    type Output = Self;
185    #[inline]
186    fn abs(self) -> Self::Output {
187        RBig(self.0.abs())
188    }
189}
190
191impl Abs for Relaxed {
192    type Output = Self;
193    #[inline]
194    fn abs(self) -> Self::Output {
195        Relaxed(self.0.abs())
196    }
197}