dashu_ratio/third_party/
num_traits.rs1use crate::rbig::{RBig, Relaxed};
4use dashu_base::{Abs, DivEuclid, ParseError, RemEuclid, Sign};
5use num_traits_v02 as num_traits;
6
7macro_rules! impl_num_traits {
8 ($t:ty) => {
9 impl num_traits::Zero for $t {
10 #[inline]
11 fn zero() -> Self {
12 <$t>::ZERO
13 }
14 #[inline]
15 fn is_zero(&self) -> bool {
16 <$t>::is_zero(self)
17 }
18 }
19
20 impl num_traits::One for $t {
21 #[inline]
22 fn one() -> Self {
23 <$t>::ONE
24 }
25 #[inline]
26 fn is_one(&self) -> bool {
27 <$t>::is_one(self)
28 }
29 }
30
31 impl num_traits::Num for $t {
32 type FromStrRadixErr = ParseError;
33 #[inline]
34 fn from_str_radix(src: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
35 <$t>::from_str_radix(src, radix)
36 }
37 }
38
39 impl num_traits::Signed for $t {
40 #[inline]
41 fn abs(&self) -> Self {
42 Abs::abs(self.clone())
43 }
44
45 #[inline]
46 fn abs_sub(&self, other: &Self) -> Self {
47 Abs::abs(self - other)
48 }
49
50 #[inline]
51 fn signum(&self) -> Self {
52 <$t>::signum(self)
53 }
54
55 #[inline]
56 fn is_positive(&self) -> bool {
57 !self.is_zero() && self.sign() == Sign::Positive
58 }
59
60 #[inline]
61 fn is_negative(&self) -> bool {
62 self.sign() == Sign::Negative
63 }
64 }
65
66 impl num_traits::Euclid for $t {
67 #[inline]
68 fn div_euclid(&self, v: &Self) -> Self {
69 DivEuclid::div_euclid(self, v).into()
70 }
71 #[inline]
72 fn rem_euclid(&self, v: &Self) -> Self {
73 RemEuclid::rem_euclid(self, v)
74 }
75 }
76
77 impl num_traits::Pow<usize> for $t {
78 type Output = $t;
79 #[inline]
80 fn pow(self, rhs: usize) -> $t {
81 <$t>::pow(&self, rhs)
82 }
83 }
84 impl num_traits::Pow<usize> for &$t {
85 type Output = $t;
86 #[inline]
87 fn pow(self, rhs: usize) -> $t {
88 <$t>::pow(self, rhs)
89 }
90 }
91 };
92}
93impl_num_traits!(RBig);
94impl_num_traits!(Relaxed);
95
96macro_rules! impl_from_primitive_int {
97 ($t:ty, $method:ident) => {
98 #[inline]
99 fn $method(n: $t) -> Option<Self> {
100 Some(Self::from(n))
101 }
102 };
103}
104
105impl num_traits::FromPrimitive for RBig {
106 impl_from_primitive_int!(i8, from_i8);
107 impl_from_primitive_int!(i16, from_i16);
108 impl_from_primitive_int!(i32, from_i32);
109 impl_from_primitive_int!(i64, from_i64);
110 impl_from_primitive_int!(i128, from_i128);
111 impl_from_primitive_int!(isize, from_isize);
112 impl_from_primitive_int!(u8, from_u8);
113 impl_from_primitive_int!(u16, from_u16);
114 impl_from_primitive_int!(u32, from_u32);
115 impl_from_primitive_int!(u64, from_u64);
116 impl_from_primitive_int!(u128, from_u128);
117 impl_from_primitive_int!(usize, from_usize);
118
119 #[inline]
120 fn from_f32(f: f32) -> Option<Self> {
121 Self::try_from(f).ok()
122 }
123 #[inline]
124 fn from_f64(f: f64) -> Option<Self> {
125 Self::try_from(f).ok()
126 }
127}
128
129impl num_traits::FromPrimitive for Relaxed {
130 impl_from_primitive_int!(i8, from_i8);
131 impl_from_primitive_int!(i16, from_i16);
132 impl_from_primitive_int!(i32, from_i32);
133 impl_from_primitive_int!(i64, from_i64);
134 impl_from_primitive_int!(i128, from_i128);
135 impl_from_primitive_int!(isize, from_isize);
136 impl_from_primitive_int!(u8, from_u8);
137 impl_from_primitive_int!(u16, from_u16);
138 impl_from_primitive_int!(u32, from_u32);
139 impl_from_primitive_int!(u64, from_u64);
140 impl_from_primitive_int!(u128, from_u128);
141 impl_from_primitive_int!(usize, from_usize);
142
143 #[inline]
144 fn from_f32(f: f32) -> Option<Self> {
145 Self::try_from(f).ok()
146 }
147 #[inline]
148 fn from_f64(f: f64) -> Option<Self> {
149 Self::try_from(f).ok()
150 }
151}
152
153macro_rules! impl_to_primitive_int {
154 ($t:ty, $method:ident) => {
155 #[inline]
156 fn $method(&self) -> Option<$t> {
157 num_traits::ToPrimitive::$method(&self.to_int().value())
158 }
159 };
160}
161
162impl num_traits::ToPrimitive for RBig {
163 impl_to_primitive_int!(i8, to_i8);
164 impl_to_primitive_int!(i16, to_i16);
165 impl_to_primitive_int!(i32, to_i32);
166 impl_to_primitive_int!(i64, to_i64);
167 impl_to_primitive_int!(i128, to_i128);
168 impl_to_primitive_int!(isize, to_isize);
169 impl_to_primitive_int!(u8, to_u8);
170 impl_to_primitive_int!(u16, to_u16);
171 impl_to_primitive_int!(u32, to_u32);
172 impl_to_primitive_int!(u64, to_u64);
173 impl_to_primitive_int!(u128, to_u128);
174 impl_to_primitive_int!(usize, to_usize);
175
176 #[inline]
177 fn to_f32(&self) -> Option<f32> {
178 Some(self.to_f32().value())
179 }
180 #[inline]
181 fn to_f64(&self) -> Option<f64> {
182 Some(self.to_f64().value())
183 }
184}
185
186impl num_traits::ToPrimitive for Relaxed {
187 impl_to_primitive_int!(i8, to_i8);
188 impl_to_primitive_int!(i16, to_i16);
189 impl_to_primitive_int!(i32, to_i32);
190 impl_to_primitive_int!(i64, to_i64);
191 impl_to_primitive_int!(i128, to_i128);
192 impl_to_primitive_int!(isize, to_isize);
193 impl_to_primitive_int!(u8, to_u8);
194 impl_to_primitive_int!(u16, to_u16);
195 impl_to_primitive_int!(u32, to_u32);
196 impl_to_primitive_int!(u64, to_u64);
197 impl_to_primitive_int!(u128, to_u128);
198 impl_to_primitive_int!(usize, to_usize);
199
200 #[inline]
201 fn to_f32(&self) -> Option<f32> {
202 Some(self.to_f32().value())
203 }
204 #[inline]
205 fn to_f64(&self) -> Option<f64> {
206 Some(self.to_f64().value())
207 }
208}