injective_math/fp_decimal/
comparison.rs1use crate::fp_decimal::FPDecimal;
2use std::cmp::Ordering;
3
4impl Ord for FPDecimal {
5 fn cmp(&self, other: &FPDecimal) -> Ordering {
6 if self.sign == other.sign {
7 let mut ordering = self.num.cmp(&other.num);
8
9 if self.sign == 0 {
10 ordering = ordering.reverse()
11 }
12
13 return ordering;
14 }
15
16 if self.sign == 1 {
17 Ordering::Greater
18 } else {
19 Ordering::Less
20 }
21 }
22}
23
24impl PartialEq for FPDecimal {
25 fn eq(&self, other: &Self) -> bool {
26 self.num == other.num && self.sign == other.sign
27 }
28}
29
30impl PartialOrd for FPDecimal {
31 fn partial_cmp(&self, other: &FPDecimal) -> Option<Ordering> {
32 Some(self.cmp(other))
33 }
34
35 fn lt(&self, other: &Self) -> bool {
36 if self.sign != other.sign {
37 return self.sign < other.sign;
38 }
39
40 if self.sign == 0 {
41 return self.num >= other.num;
42 }
43
44 self.num < other.num
45 }
46
47 fn le(&self, other: &Self) -> bool {
48 if self.sign != other.sign {
49 return self.sign < other.sign;
50 }
51
52 if self.sign == 0 {
53 return self.num >= other.num;
54 }
55
56 self.num <= other.num
57 }
58
59 fn gt(&self, other: &Self) -> bool {
60 if self.sign != other.sign {
61 return self.sign > other.sign;
62 }
63
64 if self.sign == 0 {
65 return self.num < other.num;
66 }
67
68 self.num > other.num
69 }
70
71 fn ge(&self, other: &Self) -> bool {
72 if self.sign != other.sign {
73 return self.sign > other.sign;
74 }
75
76 if self.sign == 0 {
77 return self.num <= other.num;
78 }
79
80 self.num >= other.num
81 }
82}
83
84impl FPDecimal {
85 pub fn maximum(&self, other: &Self) -> FPDecimal {
86 match self.cmp(other) {
87 Ordering::Greater | Ordering::Equal => *self,
88 Ordering::Less => *other,
89 }
90 }
91
92 pub fn minimum(&self, other: &Self) -> FPDecimal {
93 match self.cmp(other) {
94 Ordering::Less | Ordering::Equal => *self,
95 Ordering::Greater => *other,
96 }
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use crate::FPDecimal;
103 use std::str::FromStr;
104
105 #[test]
106 fn test_is_greater() {
107 let mut is_greater = FPDecimal::from_str("1.0").unwrap() > FPDecimal::from_str("-5.0").unwrap();
108 assert!(is_greater);
109
110 is_greater = FPDecimal::from_str("1.0").unwrap() > FPDecimal::from_str("0.99999").unwrap();
111 assert!(is_greater);
112
113 is_greater = FPDecimal::from_str("-1.0").unwrap() > FPDecimal::from_str("-2.0").unwrap();
114 assert!(is_greater);
115
116 is_greater = FPDecimal::from_str("4.0").unwrap() > FPDecimal::from_str("1.0").unwrap();
117 assert!(is_greater);
118 }
119
120 #[test]
121 fn test_is_equal() {
122 let mut is_equal = FPDecimal::from_str("1.0").unwrap() == FPDecimal::from_str("1.0").unwrap();
123 assert!(is_equal);
124
125 is_equal = FPDecimal::from_str("1.0").unwrap() == FPDecimal::from_str("-1.0").unwrap();
126 assert!(!is_equal);
127 }
128
129 #[test]
130 fn test_is_not_equal() {
131 let mut is_not_equal = FPDecimal::from_str("1.0").unwrap() != FPDecimal::from_str("1.0").unwrap();
132 assert!(!is_not_equal);
133
134 is_not_equal = FPDecimal::from_str("1.0").unwrap() != FPDecimal::from_str("-1.0").unwrap();
135 assert!(is_not_equal);
136 }
137
138 #[test]
139 fn test_is_not_greater() {
140 let mut is_greater = FPDecimal::from_str("-5.0").unwrap() > FPDecimal::from_str("1.0").unwrap();
141 assert!(!is_greater);
142
143 is_greater = FPDecimal::from_str("0.99999").unwrap() > FPDecimal::from_str("1.0").unwrap();
144 assert!(!is_greater);
145
146 is_greater = FPDecimal::from_str("-2.0").unwrap() > FPDecimal::from_str("-1.0").unwrap();
147 assert!(!is_greater);
148
149 is_greater = FPDecimal::from_str("1.0").unwrap() > FPDecimal::from_str("4.0").unwrap();
150 assert!(!is_greater);
151
152 is_greater = FPDecimal::from_str("1.0").unwrap() > FPDecimal::from_str("1.0").unwrap();
153 assert!(!is_greater);
154 }
155
156 #[test]
157 fn test_is_greater_equal() {
158 let mut is_greater_equal = FPDecimal::from_str("1.0").unwrap() >= FPDecimal::from_str("-5.0").unwrap();
159 assert!(is_greater_equal);
160
161 is_greater_equal = FPDecimal::from_str("1.0").unwrap() >= FPDecimal::from_str("0.99999").unwrap();
162 assert!(is_greater_equal);
163
164 is_greater_equal = FPDecimal::from_str("-1.0").unwrap() >= FPDecimal::from_str("-2.0").unwrap();
165 assert!(is_greater_equal);
166
167 is_greater_equal = FPDecimal::from_str("4.0").unwrap() >= FPDecimal::from_str("1.0").unwrap();
168 assert!(is_greater_equal);
169
170 is_greater_equal = FPDecimal::from_str("-2.3").unwrap() >= FPDecimal::from_str("-2.3").unwrap();
171 assert!(is_greater_equal);
172
173 is_greater_equal = FPDecimal::from_str("2.3").unwrap() >= FPDecimal::from_str("2.3").unwrap();
174 assert!(is_greater_equal);
175
176 is_greater_equal = FPDecimal::from_str("0.0").unwrap() >= FPDecimal::from_str("0.0").unwrap();
177 assert!(is_greater_equal);
178 }
179
180 #[test]
181 fn test_is_not_greater_equal() {
182 let mut is_greater_equal = FPDecimal::from_str("-5.0").unwrap() >= FPDecimal::from_str("1.0").unwrap();
183 assert!(!is_greater_equal);
184
185 is_greater_equal = FPDecimal::from_str("0.99999").unwrap() >= FPDecimal::from_str("1.0").unwrap();
186 assert!(!is_greater_equal);
187
188 is_greater_equal = FPDecimal::from_str("-2.0").unwrap() >= FPDecimal::from_str("-1.0").unwrap();
189 assert!(!is_greater_equal);
190
191 is_greater_equal = FPDecimal::from_str("1.0").unwrap() >= FPDecimal::from_str("4.0").unwrap();
192 assert!(!is_greater_equal);
193 }
194
195 #[test]
196 fn test_is_lesser() {
197 let mut is_lesser = FPDecimal::from_str("-5.0").unwrap() < FPDecimal::from_str("1.0").unwrap();
198 assert!(is_lesser);
199
200 is_lesser = FPDecimal::from_str("0.99999").unwrap() < FPDecimal::from_str("1.0").unwrap();
201 assert!(is_lesser);
202
203 is_lesser = FPDecimal::from_str("-2.0").unwrap() < FPDecimal::from_str("-1.0").unwrap();
204 assert!(is_lesser);
205
206 is_lesser = FPDecimal::from_str("1.0").unwrap() < FPDecimal::from_str("4.0").unwrap();
207 assert!(is_lesser);
208 }
209
210 #[test]
211 fn test_is_not_lesser() {
212 let mut is_lesser = FPDecimal::from_str("1.0").unwrap() < FPDecimal::from_str("-5.0").unwrap();
213 assert!(!is_lesser);
214
215 is_lesser = FPDecimal::from_str("1.0").unwrap() < FPDecimal::from_str("0.99999").unwrap();
216 assert!(!is_lesser);
217
218 is_lesser = FPDecimal::from_str("-1.0").unwrap() < FPDecimal::from_str("-2.0").unwrap();
219 assert!(!is_lesser);
220
221 is_lesser = FPDecimal::from_str("4.0").unwrap() < FPDecimal::from_str("1.0").unwrap();
222 assert!(!is_lesser);
223
224 is_lesser = FPDecimal::from_str("1.0").unwrap() < FPDecimal::from_str("1.0").unwrap();
225 assert!(!is_lesser);
226 }
227
228 #[test]
229 fn test_is_lesser_equal() {
230 let mut is_lesser_equal = FPDecimal::from_str("-5.0").unwrap() <= FPDecimal::from_str("1.0").unwrap();
231 assert!(is_lesser_equal);
232
233 is_lesser_equal = FPDecimal::from_str("0.99999").unwrap() <= FPDecimal::from_str("1.0").unwrap();
234 assert!(is_lesser_equal);
235
236 is_lesser_equal = FPDecimal::from_str("-2.0").unwrap() <= FPDecimal::from_str("-1.0").unwrap();
237 assert!(is_lesser_equal);
238
239 is_lesser_equal = FPDecimal::from_str("1.0").unwrap() <= FPDecimal::from_str("4.0").unwrap();
240 assert!(is_lesser_equal);
241
242 is_lesser_equal = FPDecimal::from_str("-2.3").unwrap() <= FPDecimal::from_str("-2.3").unwrap();
243 assert!(is_lesser_equal);
244
245 is_lesser_equal = FPDecimal::from_str("2.3").unwrap() <= FPDecimal::from_str("2.3").unwrap();
246 assert!(is_lesser_equal);
247
248 is_lesser_equal = FPDecimal::from_str("0.0").unwrap() <= FPDecimal::from_str("0.0").unwrap();
249 assert!(is_lesser_equal);
250 }
251
252 #[test]
253 fn test_is_not_lesser_equal() {
254 let mut is_lesser_equal = FPDecimal::from_str("1.0").unwrap() <= FPDecimal::from_str("-5.0").unwrap();
255 assert!(!is_lesser_equal);
256
257 is_lesser_equal = FPDecimal::from_str("1.0").unwrap() <= FPDecimal::from_str("0.99999").unwrap();
258 assert!(!is_lesser_equal);
259
260 is_lesser_equal = FPDecimal::from_str("-1.0").unwrap() <= FPDecimal::from_str("-2.0").unwrap();
261 assert!(!is_lesser_equal);
262
263 is_lesser_equal = FPDecimal::from_str("4.0").unwrap() <= FPDecimal::from_str("1.0").unwrap();
264 assert!(!is_lesser_equal);
265 }
266
267 #[test]
268 fn test_maximum_of_values() {
269 let lhs = FPDecimal::from_str("-1.0").unwrap();
270 let rhs = FPDecimal::from_str("1.0").unwrap();
271 assert_eq!(rhs, lhs.maximum(&rhs));
272
273 let lhs = FPDecimal::from_str("1.0").unwrap();
274 let rhs = FPDecimal::from_str("1.0").unwrap();
275 assert_eq!(lhs, lhs.maximum(&rhs));
276
277 let lhs = FPDecimal::from_str("1.0").unwrap();
278 let rhs = FPDecimal::from_str("-1.0").unwrap();
279 assert_eq!(lhs, lhs.maximum(&rhs));
280 }
281
282 #[test]
283 fn test_minimum_of_values() {
284 let lhs = FPDecimal::from_str("-1.0").unwrap();
285 let rhs = FPDecimal::from_str("1.0").unwrap();
286 assert_eq!(lhs, lhs.minimum(&rhs));
287
288 let lhs = FPDecimal::from_str("1.0").unwrap();
289 let rhs = FPDecimal::from_str("1.0").unwrap();
290 assert_eq!(rhs, lhs.minimum(&rhs));
291
292 let lhs = FPDecimal::from_str("1.0").unwrap();
293 let rhs = FPDecimal::from_str("-1.0").unwrap();
294 assert_eq!(rhs, lhs.minimum(&rhs));
295 }
296}