kaspa_math/
int.rs

1use core::fmt::{self, Display};
2use core::ops::{Add, Div, Mul, Sub};
3
4#[derive(Copy, Clone, Debug)]
5pub struct SignedInteger<T> {
6    abs: T,
7    negative: bool,
8}
9
10impl<T> From<T> for SignedInteger<T> {
11    #[inline]
12    fn from(u: T) -> Self {
13        Self { abs: u, negative: false }
14    }
15}
16impl<T: From<u64>> SignedInteger<T> {
17    #[inline]
18    pub fn positive_u64(u: u64) -> Self {
19        Self { abs: T::from(u), negative: false }
20    }
21}
22
23impl<T: Display> Display for SignedInteger<T> {
24    #[inline]
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        if self.negative {
27            write!(f, "-")?;
28        }
29        write!(f, "{}", self.abs)
30    }
31}
32
33impl<T: Copy> SignedInteger<T> {
34    #[inline]
35    pub const fn abs(&self) -> T {
36        self.abs
37    }
38
39    #[inline]
40    pub const fn negative(&self) -> bool {
41        self.negative
42    }
43}
44
45impl<T: Sub<Output = T> + Add<Output = T> + Ord> Sub for SignedInteger<T> {
46    type Output = Self;
47    #[inline]
48    #[track_caller]
49    fn sub(self, other: Self) -> Self::Output {
50        match (self.negative, other.negative) {
51            (false, false) | (true, true) => {
52                if self.abs < other.abs {
53                    Self { negative: !self.negative, abs: other.abs - self.abs }
54                } else {
55                    Self { negative: self.negative, abs: self.abs - other.abs }
56                }
57            }
58            (false, true) | (true, false) => Self { negative: self.negative, abs: self.abs + other.abs },
59        }
60    }
61}
62
63impl<T: Mul<Output = T>> Mul for SignedInteger<T> {
64    type Output = Self;
65    #[inline]
66    #[track_caller]
67    fn mul(self, rhs: Self) -> Self::Output {
68        Self { negative: self.negative ^ rhs.negative, abs: self.abs * rhs.abs }
69    }
70}
71
72impl<T: Div<Output = T>> Div for SignedInteger<T> {
73    type Output = Self;
74    #[inline]
75    #[track_caller]
76    fn div(self, rhs: Self) -> Self::Output {
77        Self { negative: self.negative ^ rhs.negative, abs: self.abs / rhs.abs }
78    }
79}
80
81impl<T: PartialEq + PartialEq<u64>> PartialEq for SignedInteger<T> {
82    fn eq(&self, other: &Self) -> bool {
83        if self.abs == 0 && other.abs == 0 {
84            // neg/pos zeros are considered equal
85            return true;
86        }
87        self.negative == other.negative && self.abs == other.abs
88    }
89}
90
91impl<T: PartialEq + PartialEq<u64>> Eq for SignedInteger<T> {}
92
93impl<T: PartialOrd + PartialEq<u64>> PartialOrd for SignedInteger<T> {
94    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
95        if self.abs == 0 && other.abs == 0 {
96            // neg/pos zeros are considered equal
97            return Some(std::cmp::Ordering::Equal);
98        }
99        match (self.negative, other.negative) {
100            (false, false) => self.abs.partial_cmp(&other.abs),
101            (true, true) => other.abs.partial_cmp(&self.abs),
102            (true, false) => Some(std::cmp::Ordering::Less),
103            (false, true) => Some(std::cmp::Ordering::Greater),
104        }
105    }
106}
107
108#[cfg(test)]
109mod tests {
110    use crate::{int::SignedInteger, Uint192};
111
112    fn from_u64(val: u64) -> SignedInteger<Uint192> {
113        SignedInteger::from(Uint192::from_u64(val))
114    }
115
116    #[test]
117    fn test_partial_eq() {
118        assert_eq!(from_u64(0), SignedInteger::from(Uint192::ZERO));
119        assert_eq!(from_u64(0), from_u64(10) - from_u64(10));
120        assert_eq!(from_u64(0), from_u64(10) - from_u64(20) - from_u64(10) * (from_u64(0) - from_u64(1))); // 0 == 10 - 20 -(-10)
121        assert_eq!(from_u64(0) - from_u64(1000), from_u64(0) - from_u64(1000)); // -1000 = -1000
122        assert_eq!(from_u64(1000), from_u64(1000));
123    }
124
125    #[test]
126    fn test_partial_cmp() {
127        // Test cases related to 0 and equality
128        assert!(from_u64(0) >= from_u64(10) - from_u64(20) - from_u64(10) * (from_u64(0) - from_u64(1))); // pos 0 >= neg 0
129        assert!(from_u64(0) <= from_u64(10) - from_u64(20) - from_u64(10) * (from_u64(0) - from_u64(1))); // pos 0 <= neg 0
130
131        // Test all possible neg/pos combinations
132        assert!(from_u64(100) > from_u64(0) - from_u64(1000)); // pos > neg
133        assert!(from_u64(0) - from_u64(100) < from_u64(10)); // neg < pos
134        assert!(from_u64(0) - from_u64(1000) < from_u64(0) - from_u64(100)); // -1000 < -100
135        assert!(from_u64(0) - from_u64(1000) <= from_u64(0) - from_u64(1000)); // -1000 <= -1000
136        assert!(from_u64(0) - from_u64(1000) >= from_u64(0) - from_u64(1000)); // -1000 >= -1000
137        assert!(from_u64(1000) > from_u64(100));
138        assert!(from_u64(100) < from_u64(1000));
139        assert!(from_u64(1000) >= from_u64(1000));
140        assert!(from_u64(100) <= from_u64(100));
141    }
142}