numera/number/integer/nz/integer.rs
1// numera::number::integer::nz::integer
2//
3//!
4//
5
6use crate::number::integer::{nz::*, Integer};
7use devela::paste;
8
9macro_rules! impl_nonzero_integer {
10 // $t: base name type
11 // $b: bitsize
12 // $inner:
13 (many: $($t:ident + $b:literal + $inner:ident),+) => {
14 $( impl_nonzero_integer![$t + $b + $inner]; )+
15 };
16 ($t:ident + $b:literal + $inner:ident) => { paste! {
17 /// # Methods for all integers
18 impl [<$t$b>] {
19 /// Returns `true` if this integer is even.
20 #[inline]
21 #[must_use]
22 pub const fn is_even(&self) -> bool {
23 self.0.get() & 1 == 0
24 }
25 /// Returns `true` if this integer is odd.
26 #[inline]
27 #[must_use]
28 pub const fn is_odd(&self) -> bool {
29 !self.is_even()
30 }
31
32 /// Returns `true` if this integer is a multiple of the `other`.
33 #[inline]
34 #[must_use]
35 pub const fn is_multiple_of(&self, other: &Self) -> bool {
36 self.0.get() % other.0.get() == 0
37 }
38 /// Returns `true` if this integer is a divisor of the `other`.
39 #[inline]
40 #[must_use]
41 pub const fn is_divisor_of(&self, other: &Self) -> bool {
42 other.is_multiple_of(self)
43 }
44
45 // /// Returns `true` if `self` and `other` are relative primes,
46 // /// which means they have only 1 as their only common divisor.
47 // ///
48 // /// # Notation
49 // /// $a \perp b$.
50 // #[inline]
51 // #[must_use]
52 // pub const fn is_coprime(&self, other: &Self) -> bool {
53 // self.gcd(other).0 == Self::ONE.0 // FIX
54 // }
55
56 /// Returns the number of digits in base 10.
57 #[inline]
58 #[must_use]
59 pub const fn digits(&self) -> usize {
60 self.0.ilog10() as usize + 1
61 }
62 }
63
64 /// # Methods that are open to the result being a different kind of integer.
65 impl [<$t$b>] {
66 // TODO: NEED open_neg
67 //
68 // /// Calculates the *Greatest Common Divisor* of this integer and `other`.
69 // #[inline]
70 // #[must_use]
71 // pub const fn open_gcd(&self, other: &Self) -> [<PositiveInteger$b>] {
72 // let pself = [<PositiveInteger$b
73 // let (mut a, mut b) = (self.0, other.0);
74 // while b != 0 {
75 // let temp = b;
76 // b = a % b;
77 // a = temp;
78 // }
79 // Self(a)
80 // }
81 }
82
83 impl Integer for [<$t$b>] {
84 #[inline]
85 fn integer_is_even(&self) -> bool {
86 self.is_even()
87 }
88 #[inline]
89 fn integer_is_multiple_of(&self, other: &Self) -> bool {
90 self.is_multiple_of(other)
91 }
92
93
94 /// Returns always `None`, since negative numbers can't be prime.
95 #[inline]
96 fn integer_is_prime(&self) -> Option<bool> {
97 None
98 }
99 /// Returns always `None`, since the result must be a positive number.
100 #[inline]
101 fn integer_gcd(&self, _other: &Self) -> Option<Self> {
102 None
103 }
104 /// Returns always `None`, since the result must be a non-negative number.
105 #[inline]
106 fn integer_lcm(&self, _other: &Self) -> Option<Self> {
107 None
108 }
109
110 #[inline]
111 fn integer_digits(&self) -> usize {
112 self.digits()
113 }
114 }
115 }};
116}
117
118impl_nonzero_integer![
119 many: NegativeInteger + 8 + NonZeroU8,
120 NegativeInteger + 16 + NonZeroU16,
121 NegativeInteger + 32 + NonZeroU32,
122 NegativeInteger + 64 + NonZeroU64,
123 NegativeInteger + 128 + NonZeroU128
124];