Skip to content

Commit 5f843ee

Browse files
committed
Examples for Integer trait methods.
1 parent 0582421 commit 5f843ee

File tree

1 file changed

+90
-18
lines changed

1 file changed

+90
-18
lines changed

src/libnum/integer.rs

Lines changed: 90 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! Integer trait and functions
11+
//! Integer trait and functions.
1212
1313
pub trait Integer: Num + PartialOrd
1414
+ Div<Self, Self>
1515
+ 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.
2317
///
2418
/// # Examples
2519
///
@@ -61,25 +55,103 @@ pub trait Integer: Num + PartialOrd
6155
/// ~~~
6256
fn mod_floor(&self, other: &Self) -> Self;
6357

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+
/// ~~~
7067
fn gcd(&self, other: &Self) -> Self;
7168

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+
/// ~~~
7378
fn lcm(&self, other: &Self) -> Self;
7479

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+
/// ~~~
7689
fn divides(&self, other: &Self) -> bool;
7790

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+
/// ~~~
79100
fn is_even(&self) -> bool;
80101

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+
/// ~~~
82111
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+
}
83155
}
84156

85157
/// Simultaneous integer division and modulus

0 commit comments

Comments
 (0)