nyar_number/decimal/
arith.rs

1use super::*;
2
3impl Zero for NyarDecimal {
4    fn zero() -> Self {
5        Self { sign: Sign::Plus, digits: NyarDigits::zero(), scale: 0 }
6    }
7
8    fn is_zero(&self) -> bool {
9        todo!()
10    }
11}
12impl One for NyarDecimal {
13    fn one() -> Self {
14        Self { sign: Sign::Plus, digits: NyarDigits::one(), scale: 0 }
15    }
16}
17
18impl Neg for NyarDecimal {
19    type Output = Self;
20
21    fn neg(self) -> Self::Output {
22        Self { sign: -self.sign, digits: self.digits, scale: self.scale }
23    }
24}
25
26impl Add for NyarDecimal {
27    type Output = Self;
28
29    fn add(self, rhs: Self) -> Self::Output {
30        self.delegate().add(rhs.delegate()).into()
31    }
32}
33impl Sub for NyarDecimal {
34    type Output = Self;
35
36    fn sub(self, rhs: Self) -> Self::Output {
37        self.delegate().sub(rhs.delegate()).into()
38    }
39}
40
41impl Mul for NyarDecimal {
42    type Output = Self;
43
44    fn mul(self, rhs: Self) -> Self::Output {
45        self.delegate().mul(rhs.delegate()).into()
46    }
47}
48
49impl Div for NyarDecimal {
50    type Output = Self;
51
52    fn div(self, rhs: Self) -> Self::Output {
53        self.delegate().div(rhs.delegate()).into()
54    }
55}
56
57impl Rem for NyarDecimal {
58    type Output = Self;
59
60    fn rem(self, rhs: Self) -> Self::Output {
61        self.delegate().rem(rhs.delegate()).into()
62    }
63}
64
65impl Signed for NyarDecimal {
66    fn abs(&self) -> Self {
67        Self { sign: Sign::Plus, digits: self.digits.clone(), scale: self.scale }
68    }
69
70    fn abs_sub(&self, other: &Self) -> Self {
71        self.delegate().abs_sub(&other.delegate()).into()
72    }
73
74    fn signum(&self) -> Self {
75        self.delegate().signum().into()
76    }
77
78    fn is_positive(&self) -> bool {
79        matches!(self.sign, Sign::Plus)
80    }
81
82    fn is_negative(&self) -> bool {
83        matches!(self.sign, Sign::Minus)
84    }
85}
86impl NyarDecimal {
87    ///
88    ///
89    /// # Arguments
90    ///
91    /// * `rhs`:
92    ///
93    /// returns: NyarReal
94    ///
95    /// # Examples
96    ///
97    /// ```
98    /// ```
99    pub fn safe_div(&self, rhs: Self) -> NyarReal {
100        if rhs.is_zero() {
101            NyarReal::infinity(self.sign)
102        }
103        else {
104            NyarReal::Decimal(self.delegate().div(rhs.delegate()).into())
105        }
106    }
107    ///
108    ///
109    /// # Arguments
110    ///
111    /// * `rhs`:
112    ///
113    /// returns: NyarReal
114    ///
115    /// # Examples
116    ///
117    /// ```
118    /// ```
119    pub fn safe_rem(&self, rhs: Self) -> NyarReal {
120        if rhs.is_zero() {
121            NyarReal::infinity(self.sign)
122        }
123        else {
124            NyarReal::Decimal(self.delegate().rem(rhs.delegate()).into())
125        }
126    }
127}