dashu_int/
sign.rs

1//! Operators on the sign of [IBig].
2
3use crate::{
4    ibig::IBig,
5    ops::{Abs, UnsignedAbs},
6    ubig::UBig,
7};
8use core::ops::{Mul, MulAssign, Neg};
9use dashu_base::{Sign, Signed};
10
11impl IBig {
12    /// A number representing the sign of `self`.
13    ///
14    /// * [IBig::ONE] if the number is positive
15    /// * [IBig::ZERO] if the number is zero
16    /// * [IBig::NEG_ONE] if the number is negative
17    ///
18    /// # Examples
19    /// ```
20    /// # use dashu_int::IBig;
21    /// assert_eq!(IBig::from(-500).signum(), IBig::from(-1));
22    /// ```
23    #[inline]
24    pub const fn signum(&self) -> IBig {
25        IBig(self.0.signum())
26    }
27}
28
29impl Neg for UBig {
30    type Output = IBig;
31
32    #[inline]
33    fn neg(self) -> IBig {
34        IBig(self.0.neg())
35    }
36}
37
38impl Neg for IBig {
39    type Output = IBig;
40
41    #[inline]
42    fn neg(self) -> IBig {
43        IBig(self.0.neg())
44    }
45}
46
47impl Neg for &UBig {
48    type Output = IBig;
49
50    #[inline]
51    fn neg(self) -> IBig {
52        IBig(self.0.clone().neg())
53    }
54}
55
56impl Neg for &IBig {
57    type Output = IBig;
58
59    #[inline]
60    fn neg(self) -> IBig {
61        IBig(self.0.clone().neg())
62    }
63}
64
65impl Abs for IBig {
66    type Output = IBig;
67
68    #[inline]
69    fn abs(self) -> IBig {
70        IBig(self.0.with_sign(Sign::Positive))
71    }
72}
73
74impl Abs for &IBig {
75    type Output = IBig;
76
77    #[inline]
78    fn abs(self) -> IBig {
79        IBig(self.0.clone().with_sign(Sign::Positive))
80    }
81}
82
83impl UnsignedAbs for IBig {
84    type Output = UBig;
85
86    #[inline]
87    fn unsigned_abs(self) -> UBig {
88        UBig(self.0.with_sign(Sign::Positive))
89    }
90}
91
92impl UnsignedAbs for &IBig {
93    type Output = UBig;
94
95    #[inline]
96    fn unsigned_abs(self) -> UBig {
97        UBig(self.0.clone().with_sign(Sign::Positive))
98    }
99}
100
101impl Mul<Sign> for UBig {
102    type Output = IBig;
103
104    #[inline]
105    fn mul(self, rhs: Sign) -> Self::Output {
106        IBig(self.0.with_sign(rhs))
107    }
108}
109
110impl Mul<UBig> for Sign {
111    type Output = IBig;
112
113    #[inline]
114    fn mul(self, rhs: UBig) -> Self::Output {
115        IBig(rhs.0.with_sign(self))
116    }
117}
118
119impl Mul<Sign> for IBig {
120    type Output = IBig;
121
122    #[inline]
123    fn mul(self, rhs: Sign) -> Self::Output {
124        let sign = self.sign() * rhs;
125        IBig(self.0.with_sign(sign))
126    }
127}
128
129impl Mul<IBig> for Sign {
130    type Output = IBig;
131
132    #[inline]
133    fn mul(self, rhs: IBig) -> Self::Output {
134        let sign = self * rhs.sign();
135        IBig(rhs.0.with_sign(sign))
136    }
137}
138
139impl MulAssign<Sign> for IBig {
140    #[inline]
141    fn mul_assign(&mut self, rhs: Sign) {
142        *self = core::mem::take(self) * rhs;
143    }
144}
145
146impl Signed for IBig {
147    #[inline]
148    fn sign(&self) -> Sign {
149        self.0.sign()
150    }
151}