|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 |
| -//! Integer trait and functions |
| 11 | +//! Integer trait and functions. |
12 | 12 |
|
13 | 13 | pub trait Integer: Num + PartialOrd
|
14 | 14 | + Div<Self, Self>
|
15 | 15 | + Rem<Self, Self> {
|
16 |
| - /// Simultaneous truncated integer division and modulus |
17 |
| - #[inline] |
18 |
| - fn div_rem(&self, other: &Self) -> (Self, Self) { |
19 |
| - (*self / *other, *self % *other) |
20 |
| - } |
21 |
| - |
22 |
| - /// Floored integer division |
| 16 | + /// Floored integer division. |
23 | 17 | ///
|
24 | 18 | /// # Examples
|
25 | 19 | ///
|
@@ -61,25 +55,103 @@ pub trait Integer: Num + PartialOrd
|
61 | 55 | /// ~~~
|
62 | 56 | fn mod_floor(&self, other: &Self) -> Self;
|
63 | 57 |
|
64 |
| - /// Simultaneous floored integer division and modulus |
65 |
| - fn div_mod_floor(&self, other: &Self) -> (Self, Self) { |
66 |
| - (self.div_floor(other), self.mod_floor(other)) |
67 |
| - } |
68 |
| - |
69 |
| - /// Greatest Common Divisor (GCD) |
| 58 | + /// Greatest Common Divisor (GCD). |
| 59 | + /// |
| 60 | + /// # Examples |
| 61 | + /// |
| 62 | + /// ~~~ |
| 63 | + /// # use num::Integer; |
| 64 | + /// assert_eq!(6i.gcd(&8), 2); |
| 65 | + /// assert_eq!(7i.gcd(&3), 1); |
| 66 | + /// ~~~ |
70 | 67 | fn gcd(&self, other: &Self) -> Self;
|
71 | 68 |
|
72 |
| - /// Lowest Common Multiple (LCM) |
| 69 | + /// Lowest Common Multiple (LCM). |
| 70 | + /// |
| 71 | + /// # Examples |
| 72 | + /// |
| 73 | + /// ~~~ |
| 74 | + /// # use num::Integer; |
| 75 | + /// assert_eq!(7i.lcm(&3), 21); |
| 76 | + /// assert_eq!(2i.lcm(&4), 4); |
| 77 | + /// ~~~ |
73 | 78 | fn lcm(&self, other: &Self) -> Self;
|
74 | 79 |
|
75 |
| - /// Returns `true` if `other` divides evenly into `self` |
| 80 | + /// Returns `true` if `other` divides evenly into `self`. |
| 81 | + /// |
| 82 | + /// # Examples |
| 83 | + /// |
| 84 | + /// ~~~ |
| 85 | + /// # use num::Integer; |
| 86 | + /// assert_eq!(9i.divides(&3), true); |
| 87 | + /// assert_eq!(3i.divides(&9), false); |
| 88 | + /// ~~~ |
76 | 89 | fn divides(&self, other: &Self) -> bool;
|
77 | 90 |
|
78 |
| - /// Returns `true` if the number is even |
| 91 | + /// Returns `true` if the number is even. |
| 92 | + /// |
| 93 | + /// # Examples |
| 94 | + /// |
| 95 | + /// ~~~ |
| 96 | + /// # use num::Integer; |
| 97 | + /// assert_eq!(3i.is_even(), false); |
| 98 | + /// assert_eq!(4i.is_even(), true); |
| 99 | + /// ~~~ |
79 | 100 | fn is_even(&self) -> bool;
|
80 | 101 |
|
81 |
| - /// Returns `true` if the number is odd |
| 102 | + /// Returns `true` if the number is odd. |
| 103 | + /// |
| 104 | + /// # Examples |
| 105 | + /// |
| 106 | + /// ~~~ |
| 107 | + /// # use num::Integer; |
| 108 | + /// assert_eq!(3i.is_odd(), true); |
| 109 | + /// assert_eq!(4i.is_odd(), false); |
| 110 | + /// ~~~ |
82 | 111 | fn is_odd(&self) -> bool;
|
| 112 | + |
| 113 | + /// Simultaneous truncated integer division and modulus. |
| 114 | + /// Returns `(quotient, remainder)`. |
| 115 | + /// |
| 116 | + /// # Examples |
| 117 | + /// |
| 118 | + /// ~~~ |
| 119 | + /// # use num::Integer; |
| 120 | + /// assert_eq!(( 8i).div_rem( &3), ( 2, 2)); |
| 121 | + /// assert_eq!(( 8i).div_rem(&-3), (-2, 2)); |
| 122 | + /// assert_eq!((-8i).div_rem( &3), (-2, -2)); |
| 123 | + /// assert_eq!((-8i).div_rem(&-3), ( 2, -2)); |
| 124 | + /// |
| 125 | + /// assert_eq!(( 1i).div_rem( &2), ( 0, 1)); |
| 126 | + /// assert_eq!(( 1i).div_rem(&-2), ( 0, 1)); |
| 127 | + /// assert_eq!((-1i).div_rem( &2), ( 0, -1)); |
| 128 | + /// assert_eq!((-1i).div_rem(&-2), ( 0, -1)); |
| 129 | + /// ~~~ |
| 130 | + #[inline] |
| 131 | + fn div_rem(&self, other: &Self) -> (Self, Self) { |
| 132 | + (*self / *other, *self % *other) |
| 133 | + } |
| 134 | + |
| 135 | + /// Simultaneous floored integer division and modulus. |
| 136 | + /// Returns `(quotient, remainder)`. |
| 137 | + /// |
| 138 | + /// # Examples |
| 139 | + /// |
| 140 | + /// ~~~ |
| 141 | + /// # use num::Integer; |
| 142 | + /// assert_eq!(( 8i).div_mod_floor( &3), ( 2, 2)); |
| 143 | + /// assert_eq!(( 8i).div_mod_floor(&-3), (-3, -1)); |
| 144 | + /// assert_eq!((-8i).div_mod_floor( &3), (-3, 1)); |
| 145 | + /// assert_eq!((-8i).div_mod_floor(&-3), ( 2, -2)); |
| 146 | + /// |
| 147 | + /// assert_eq!(( 1i).div_mod_floor( &2), ( 0, 1)); |
| 148 | + /// assert_eq!(( 1i).div_mod_floor(&-2), (-1, -1)); |
| 149 | + /// assert_eq!((-1i).div_mod_floor( &2), (-1, 1)); |
| 150 | + /// assert_eq!((-1i).div_mod_floor(&-2), ( 0, -1)); |
| 151 | + /// ~~~ |
| 152 | + fn div_mod_floor(&self, other: &Self) -> (Self, Self) { |
| 153 | + (self.div_floor(other), self.mod_floor(other)) |
| 154 | + } |
83 | 155 | }
|
84 | 156 |
|
85 | 157 | /// Simultaneous integer division and modulus
|
|
0 commit comments