ebi/
traits_external_implemenations.rs

1// ============ implementations ============
2
3use fraction::BigFraction;
4use num::{BigInt, BigUint, Float, One as NumOne, Signed as NumSigned, Zero as NumZero};
5
6use crate::{fraction::EPSILON, traits::{Infinite, One, Signed, Zero}};
7
8impl One for BigInt {
9    fn one() -> Self {
10        <BigInt as NumOne>::one()
11    }
12
13    fn is_one(&self) -> bool {
14        <BigInt as NumOne>::is_one(&self)
15    }
16}
17
18impl Zero for BigInt {
19    fn zero() -> Self {
20        <BigInt as NumZero>::zero()
21    }
22
23    fn is_zero(&self) -> bool {
24        <BigInt as NumZero>::is_zero(&self)
25    }
26}
27
28impl Signed for BigInt {
29    fn abs(&self) -> Self {
30        <BigInt as NumSigned>::abs(&self)
31    }
32
33    fn is_positive(&self) -> bool {
34        <BigInt as NumSigned>::is_positive(&self)
35    }
36
37    fn is_negative(&self) -> bool {
38        <BigInt as NumSigned>::is_negative(&self)
39    }
40
41    fn is_not_negative(&self) -> bool {
42        !Signed::is_negative(self)
43    }
44
45    fn is_not_positive(&self) -> bool {
46        !Signed::is_positive(self)
47    }
48}
49
50impl Infinite for BigInt {
51    fn is_infinite(&self) -> bool {
52        false
53    }
54}
55
56impl Zero for BigUint {
57    fn zero() -> Self {
58        num::Zero::zero()
59    }
60
61    fn is_zero(&self) -> bool {
62        num::Zero::is_zero(self)
63    }
64}
65
66impl One for BigUint {
67    fn one() -> Self {
68        num::One::one()
69    }
70
71    fn is_one(&self) -> bool {
72        num::One::is_one(self)
73    }
74}
75
76impl One for f64 {
77    fn one() -> Self {
78        1.0
79    }
80
81    fn is_one(&self) -> bool {
82        (self - 1.0).abs() - &EPSILON < 0.0
83    }
84}
85
86impl Zero for f64 {
87    fn zero() -> Self {
88        0.0
89    }
90
91    fn is_zero(&self) -> bool {
92        <f64 as Signed>::abs(&self) - &EPSILON < 0.0
93    }
94}
95
96impl Signed for f64 {
97    fn abs(&self) -> Self {
98        <f64 as Float>::abs(*self)
99    }
100
101    fn is_positive(&self) -> bool {
102        *self != 0f64 && self > &EPSILON
103    }
104
105    fn is_negative(&self) -> bool {
106        *self != 0f64 && self < &-EPSILON
107    }
108
109    fn is_not_negative(&self) -> bool {
110        self > &-EPSILON
111    }
112
113    fn is_not_positive(&self) -> bool {
114        self < &EPSILON
115    }
116}
117
118impl Infinite for f64 {
119    fn is_infinite(&self) -> bool {
120        Float::is_infinite(*self)
121    }
122}
123
124impl Zero for BigFraction {
125    fn zero() -> Self {
126        num::Zero::zero()
127    }
128
129    fn is_zero(&self) -> bool {
130        num::Zero::is_zero(self)
131    }
132}
133
134impl One for BigFraction {
135    fn one() -> Self {
136        num::One::one()
137    }
138
139    fn is_one(&self) -> bool {
140        num::One::is_one(self)
141    }
142}
143
144macro_rules! ttype_signed {
145    ($t:ident) => {
146        impl Zero for $t {
147            fn zero() -> Self {
148                0
149            }
150
151            fn is_zero(&self) -> bool {
152                num::Zero::is_zero(self)
153            }
154        }
155
156        impl One for $t {
157            fn one() -> Self {
158                1
159            }
160
161            fn is_one(&self) -> bool {
162                num::One::is_one(self)
163            }
164        }
165
166        impl Signed for $t {
167            fn abs(&self) -> Self {
168                NumSigned::abs(&self)
169            }
170
171            fn is_positive(&self) -> bool {
172                NumSigned::is_positive(self)
173            }
174
175            fn is_negative(&self) -> bool {
176                NumSigned::is_negative(self)
177            }
178
179            fn is_not_negative(&self) -> bool {
180                !Signed::is_negative(self)
181            }
182
183            fn is_not_positive(&self) -> bool {
184                !Signed::is_positive(self)
185            }
186        }
187
188        impl Infinite for $t {
189            fn is_infinite(&self) -> bool {
190                false
191            }
192        }
193    };
194}
195
196macro_rules! ttype {
197    ($t:ident) => {
198        impl Zero for $t {
199            fn zero() -> Self {
200                0
201            }
202
203            fn is_zero(&self) -> bool {
204                num::Zero::is_zero(self)
205            }
206        }
207
208        impl One for $t {
209            fn one() -> Self {
210                1
211            }
212
213            fn is_one(&self) -> bool {
214                num::One::is_one(self)
215            }
216        }
217
218        impl Signed for $t {
219            fn abs(&self) -> Self {
220                *self
221            }
222
223            fn is_positive(&self) -> bool {
224                *self > 0
225            }
226
227            fn is_negative(&self) -> bool {
228                false
229            }
230
231            fn is_not_negative(&self) -> bool {
232                !self.is_negative()
233            }
234
235            fn is_not_positive(&self) -> bool {
236                !self.is_positive()
237            }
238        }
239    };
240}
241
242ttype!(usize);
243ttype!(u128);
244ttype!(u16);
245ttype!(u32);
246ttype!(u64);
247ttype!(u8);
248ttype_signed!(i128);
249ttype_signed!(i16);
250ttype_signed!(i32);
251ttype_signed!(i64);
252ttype_signed!(i8);