algebraeon_nzq/integer/
mod.rs1use crate::natural::*;
2use crate::traits::*;
3use algebraeon_sets::structure::*;
4use malachite_base::num::basic::traits::{One, Two, Zero};
5use std::{
6 ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Rem, Sub, SubAssign},
7 str::FromStr,
8};
9
10#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct Integer(malachite_nz::integer::Integer);
12
13impl Integer {
14 pub(crate) fn from_malachite(value: malachite_nz::integer::Integer) -> Self {
15 Self(value)
16 }
17 pub(crate) fn to_malachite(self) -> malachite_nz::integer::Integer {
18 self.0
19 }
20 pub(crate) fn to_malachite_ref(&self) -> &malachite_nz::integer::Integer {
21 &self.0
22 }
23}
24
25impl std::fmt::Display for Integer {
26 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27 self.0.fmt(f)
28 }
29}
30
31impl From<u8> for Integer {
32 fn from(value: u8) -> Self {
33 Self(malachite_nz::integer::Integer::from(value))
34 }
35}
36impl From<u16> for Integer {
37 fn from(value: u16) -> Self {
38 Self(malachite_nz::integer::Integer::from(value))
39 }
40}
41impl From<u32> for Integer {
42 fn from(value: u32) -> Self {
43 Self(malachite_nz::integer::Integer::from(value))
44 }
45}
46impl From<u64> for Integer {
47 fn from(value: u64) -> Self {
48 Self(malachite_nz::integer::Integer::from(value))
49 }
50}
51impl From<u128> for Integer {
52 fn from(value: u128) -> Self {
53 Self(malachite_nz::integer::Integer::from(value))
54 }
55}
56impl From<usize> for Integer {
57 fn from(value: usize) -> Self {
58 Self(malachite_nz::integer::Integer::from(value))
59 }
60}
61impl From<i8> for Integer {
62 fn from(value: i8) -> Self {
63 Self(malachite_nz::integer::Integer::from(value))
64 }
65}
66impl From<i16> for Integer {
67 fn from(value: i16) -> Self {
68 Self(malachite_nz::integer::Integer::from(value))
69 }
70}
71impl From<i32> for Integer {
72 fn from(value: i32) -> Self {
73 Self(malachite_nz::integer::Integer::from(value))
74 }
75}
76impl From<i64> for Integer {
77 fn from(value: i64) -> Self {
78 Self(malachite_nz::integer::Integer::from(value))
79 }
80}
81impl From<i128> for Integer {
82 fn from(value: i128) -> Self {
83 Self(malachite_nz::integer::Integer::from(value))
84 }
85}
86impl From<isize> for Integer {
87 fn from(value: isize) -> Self {
88 Self(malachite_nz::integer::Integer::from(value))
89 }
90}
91impl From<Natural> for Integer {
92 fn from(value: Natural) -> Self {
93 Self(malachite_nz::integer::Integer::from(value.to_malachite()))
94 }
95}
96impl From<&Natural> for Integer {
97 fn from(value: &Natural) -> Self {
98 Self(malachite_nz::integer::Integer::from(
99 value.to_malachite_ref(),
100 ))
101 }
102}
103
104impl Into<f64> for Integer {
105 fn into(self) -> f64 {
106 if self < Integer::ZERO {
107 -<Self as Into<f64>>::into(-self)
108 } else {
109 let limbs = self.to_malachite().into_twos_complement_limbs_asc();
110 let mut flt = 0.0;
111 for (i, k) in limbs.into_iter().enumerate() {
112 flt += (k as f64) * (2.0 as f64).powf(i as f64 * 64.0);
113 }
114 flt
115 }
116 }
117}
118impl Into<f64> for &Integer {
119 fn into(self) -> f64 {
120 self.clone().into()
121 }
122}
123
124impl FromStr for Integer {
125 type Err = ();
126
127 fn from_str(s: &str) -> Result<Self, Self::Err> {
128 Ok(Self(malachite_nz::integer::Integer::from_str(s)?))
129 }
130}
131
132impl Integer {
133 pub const ZERO: Self = Self(malachite_nz::integer::Integer::ZERO);
134 pub const ONE: Self = Self(malachite_nz::integer::Integer::ONE);
135 pub const TWO: Self = Self(malachite_nz::integer::Integer::TWO);
136}
137
138impl PartialEq<Natural> for Integer {
139 fn eq(&self, other: &Natural) -> bool {
140 self.0.eq(other.to_malachite_ref())
141 }
142}
143impl PartialEq<&Natural> for Integer {
144 fn eq(&self, other: &&Natural) -> bool {
145 self.eq(*other)
146 }
147}
148impl PartialOrd<Natural> for Integer {
149 fn partial_cmp(&self, other: &Natural) -> Option<std::cmp::Ordering> {
150 self.0.partial_cmp(other.to_malachite_ref())
151 }
152}
153impl PartialOrd<&Natural> for Integer {
154 fn partial_cmp(&self, other: &&Natural) -> Option<std::cmp::Ordering> {
155 self.partial_cmp(*other)
156 }
157}
158
159impl AddAssign<Integer> for Integer {
160 fn add_assign(&mut self, rhs: Integer) {
161 self.0.add_assign(rhs.0)
162 }
163}
164impl AddAssign<&Integer> for Integer {
165 fn add_assign(&mut self, rhs: &Integer) {
166 self.0.add_assign(&rhs.0)
167 }
168}
169
170impl SubAssign<Integer> for Integer {
171 fn sub_assign(&mut self, rhs: Integer) {
172 self.0.sub_assign(rhs.0)
173 }
174}
175impl SubAssign<&Integer> for Integer {
176 fn sub_assign(&mut self, rhs: &Integer) {
177 self.0.sub_assign(&rhs.0)
178 }
179}
180
181impl MulAssign<Integer> for Integer {
182 fn mul_assign(&mut self, rhs: Integer) {
183 self.0.mul_assign(rhs.0)
184 }
185}
186impl MulAssign<&Integer> for Integer {
187 fn mul_assign(&mut self, rhs: &Integer) {
188 self.0.mul_assign(&rhs.0)
189 }
190}
191
192impl Neg for Integer {
193 type Output = Integer;
194
195 fn neg(self) -> Self::Output {
196 Integer(self.0.neg())
197 }
198}
199impl Neg for &Integer {
200 type Output = Integer;
201
202 fn neg(self) -> Self::Output {
203 Integer((&self.0).neg())
204 }
205}
206
207impl Add<Integer> for Integer {
208 type Output = Integer;
209
210 fn add(self, rhs: Integer) -> Self::Output {
211 Integer(self.0.add(rhs.0))
212 }
213}
214impl Add<&Integer> for Integer {
215 type Output = Integer;
216
217 fn add(self, rhs: &Integer) -> Self::Output {
218 Integer(self.0.add(&rhs.0))
219 }
220}
221impl Add<Integer> for &Integer {
222 type Output = Integer;
223
224 fn add(self, rhs: Integer) -> Self::Output {
225 Integer((&self.0).add(rhs.0))
226 }
227}
228impl Add<&Integer> for &Integer {
229 type Output = Integer;
230
231 fn add(self, rhs: &Integer) -> Self::Output {
232 Integer((&self.0).add(&rhs.0))
233 }
234}
235
236impl Sub<Integer> for Integer {
237 type Output = Integer;
238
239 fn sub(self, rhs: Integer) -> Self::Output {
240 Integer(self.0.sub(rhs.0))
241 }
242}
243impl Sub<&Integer> for Integer {
244 type Output = Integer;
245
246 fn sub(self, rhs: &Integer) -> Self::Output {
247 Integer(self.0.sub(&rhs.0))
248 }
249}
250impl Sub<Integer> for &Integer {
251 type Output = Integer;
252
253 fn sub(self, rhs: Integer) -> Self::Output {
254 Integer((&self.0).sub(rhs.0))
255 }
256}
257impl Sub<&Integer> for &Integer {
258 type Output = Integer;
259
260 fn sub(self, rhs: &Integer) -> Self::Output {
261 Integer((&self.0).sub(&rhs.0))
262 }
263}
264
265impl Mul<Integer> for Integer {
266 type Output = Integer;
267
268 fn mul(self, rhs: Integer) -> Self::Output {
269 Integer(self.0.mul(rhs.0))
270 }
271}
272impl Mul<&Integer> for Integer {
273 type Output = Integer;
274
275 fn mul(self, rhs: &Integer) -> Self::Output {
276 Integer(self.0.mul(&rhs.0))
277 }
278}
279impl Mul<Integer> for &Integer {
280 type Output = Integer;
281
282 fn mul(self, rhs: Integer) -> Self::Output {
283 Integer((&self.0).mul(rhs.0))
284 }
285}
286impl Mul<&Integer> for &Integer {
287 type Output = Integer;
288
289 fn mul(self, rhs: &Integer) -> Self::Output {
290 Integer((&self.0).mul(&rhs.0))
291 }
292}
293
294impl Rem<Integer> for Integer {
295 type Output = Integer;
296
297 fn rem(self, rhs: Integer) -> Self::Output {
298 use malachite_base::num::arithmetic::traits::Mod;
299 Integer(self.0.mod_op(rhs.0))
300 }
301}
302impl Rem<&Integer> for Integer {
303 type Output = Integer;
304
305 fn rem(self, rhs: &Integer) -> Self::Output {
306 use malachite_base::num::arithmetic::traits::Mod;
307 Integer(self.0.mod_op(&rhs.0))
308 }
309}
310impl Rem<Integer> for &Integer {
311 type Output = Integer;
312
313 fn rem(self, rhs: Integer) -> Self::Output {
314 use malachite_base::num::arithmetic::traits::Mod;
315 Integer((&self.0).mod_op(rhs.0))
316 }
317}
318impl Rem<&Integer> for &Integer {
319 type Output = Integer;
320
321 fn rem(self, rhs: &Integer) -> Self::Output {
322 use malachite_base::num::arithmetic::traits::Mod;
323 Integer((&self.0).mod_op(&rhs.0))
324 }
325}
326
327impl Div<Integer> for Integer {
328 type Output = Integer;
329
330 fn div(self, rhs: Integer) -> Self::Output {
331 Integer(self.0.div(rhs.0))
332 }
333}
334impl Div<&Integer> for Integer {
335 type Output = Integer;
336
337 fn div(self, rhs: &Integer) -> Self::Output {
338 Integer(self.0.div(&rhs.0))
339 }
340}
341impl Div<Integer> for &Integer {
342 type Output = Integer;
343
344 fn div(self, rhs: Integer) -> Self::Output {
345 Integer((&self.0).div(rhs.0))
346 }
347}
348impl Div<&Integer> for &Integer {
349 type Output = Integer;
350
351 fn div(self, rhs: &Integer) -> Self::Output {
352 Integer((&self.0).div(&rhs.0))
353 }
354}
355
356impl DivMod<Integer> for Integer {
357 type DivOutput = Integer;
358
359 type ModOutput = Integer;
360
361 fn div_mod(self, other: Integer) -> (Self::DivOutput, Self::ModOutput) {
362 use malachite_base::num::arithmetic::traits::DivMod;
363 let (q, r) = self.0.div_mod(other.0);
364 (Integer(q), Integer(r))
365 }
366}
367impl DivMod<&Integer> for Integer {
368 type DivOutput = Integer;
369
370 type ModOutput = Integer;
371
372 fn div_mod(self, other: &Integer) -> (Self::DivOutput, Self::ModOutput) {
373 use malachite_base::num::arithmetic::traits::DivMod;
374 let (q, r) = self.0.div_mod(&other.0);
375 (Integer(q), Integer(r))
376 }
377}
378impl DivMod<Integer> for &Integer {
379 type DivOutput = Integer;
380
381 type ModOutput = Integer;
382
383 fn div_mod(self, other: Integer) -> (Self::DivOutput, Self::ModOutput) {
384 use malachite_base::num::arithmetic::traits::DivMod;
385 let (q, r) = (&self.0).div_mod(other.0);
386 (Integer(q), Integer(r))
387 }
388}
389impl DivMod<&Integer> for &Integer {
390 type DivOutput = Integer;
391
392 type ModOutput = Integer;
393
394 fn div_mod(self, other: &Integer) -> (Self::DivOutput, Self::ModOutput) {
395 use malachite_base::num::arithmetic::traits::DivMod;
396 let (q, r) = (&self.0).div_mod(&other.0);
397 (Integer(q), Integer(r))
398 }
399}
400
401impl Integer {
402 pub fn abs(self) -> Integer {
403 use malachite_base::num::arithmetic::traits::Abs;
404 Self(self.0.abs())
405 }
406
407 pub fn abs_ref(&self) -> Integer {
408 use malachite_base::num::arithmetic::traits::Abs;
409 Self((&self.0).abs())
410 }
411
412 pub fn unsigned_abs(self) -> Natural {
413 use malachite_base::num::arithmetic::traits::UnsignedAbs;
414 Natural::from_malachite(self.0.unsigned_abs())
415 }
416
417 pub fn unsigned_abs_ref(&self) -> Natural {
418 use malachite_base::num::arithmetic::traits::UnsignedAbs;
419 Natural::from_malachite((&self.0).unsigned_abs())
420 }
421}
422
423impl MetaType for Integer {
424 type Structure = CannonicalStructure<Integer>;
425
426 fn structure() -> std::rc::Rc<Self::Structure> {
427 CannonicalStructure::new().into()
428 }
429}