@@ -30,7 +30,7 @@ pub type Rational64 = Ratio<i64>;
30
30
/// Alias for arbitrary precision rationals.
31
31
pub type BigRational = Ratio < BigInt > ;
32
32
33
- impl < T : Copy + Num + Ord >
33
+ impl < T : Copy + Integer + Ord >
34
34
Ratio < T > {
35
35
/// Create a ratio representing the integer `t`.
36
36
#[ inline( always) ]
@@ -57,7 +57,7 @@ impl<T: Copy + Num + Ord>
57
57
58
58
/// Put self into lowest terms, with denom > 0.
59
59
fn reduce(&mut self) {
60
- let g : T = gcd( self.numer, self.denom);
60
+ let g : T = self.numer.gcd(& self.denom);
61
61
62
62
self.numer /= g;
63
63
self.denom /= g;
@@ -76,34 +76,6 @@ impl<T: Copy + Num + Ord>
76
76
}
77
77
}
78
78
79
- /**
80
- Compute the greatest common divisor of two numbers, via Euclid's algorithm.
81
-
82
- The result can be negative.
83
- */
84
- #[inline]
85
- pub fn gcd_raw<T: Num>(n: T, m: T) -> T {
86
- let mut m = m, n = n;
87
- while m != Zero::zero() {
88
- let temp = m;
89
- m = n % temp;
90
- n = temp;
91
- }
92
- n
93
- }
94
-
95
- /**
96
- Compute the greatest common divisor of two numbers, via Euclid's algorithm.
97
-
98
- The result is always positive.
99
- */
100
- #[inline]
101
- pub fn gcd<T: Num + Ord>(n: T, m: T) -> T {
102
- let g = gcd_raw(n, m);
103
- if g < Zero::zero() { -g }
104
- else { g }
105
- }
106
-
107
79
/* Comparisons */
108
80
109
81
// comparing a/b and c/d is the same as comparing a*d and b*c, so we
@@ -133,7 +105,7 @@ cmp_impl!(impl TotalOrd, cmp -> cmp::Ordering)
133
105
134
106
/* Arithmetic */
135
107
// a/b * c/d = (a*c)/(b*d)
136
- impl<T: Copy + Num + Ord>
108
+ impl<T: Copy + Integer + Ord>
137
109
Mul<Ratio<T>,Ratio<T>> for Ratio<T> {
138
110
#[inline]
139
111
fn mul(&self, rhs: &Ratio<T>) -> Ratio<T> {
@@ -142,7 +114,7 @@ impl<T: Copy + Num + Ord>
142
114
}
143
115
144
116
// (a/b) / (c/d) = (a*d)/(b*c)
145
- impl<T: Copy + Num + Ord>
117
+ impl<T: Copy + Integer + Ord>
146
118
Div<Ratio<T>,Ratio<T>> for Ratio<T> {
147
119
#[inline]
148
120
fn div(&self, rhs: &Ratio<T>) -> Ratio<T> {
@@ -153,7 +125,7 @@ impl<T: Copy + Num + Ord>
153
125
// Abstracts the a/b `op` c/d = (a*d `op` b*d) / (b*d) pattern
154
126
macro_rules! arith_impl {
155
127
(impl $imp:ident, $method:ident) => {
156
- impl<T: Copy + Num + Ord>
128
+ impl<T: Copy + Integer + Ord>
157
129
$imp<Ratio<T>,Ratio<T>> for Ratio<T> {
158
130
#[inline]
159
131
fn $method(&self, rhs: &Ratio<T>) -> Ratio<T> {
@@ -173,16 +145,16 @@ arith_impl!(impl Sub, sub)
173
145
// a/b % c/d = (a*d % b*c)/(b*d)
174
146
arith_impl!(impl Rem, rem)
175
147
176
- impl<T: Copy + Num + Ord>
148
+ impl<T: Copy + Integer + Ord>
177
149
Neg<Ratio<T>> for Ratio<T> {
178
150
#[inline]
179
151
fn neg(&self) -> Ratio<T> {
180
- Ratio::new_raw(-self.numer, self.denom)
152
+ Ratio::new_raw(-self.numer, self.denom.clone() )
181
153
}
182
154
}
183
155
184
156
/* Constants */
185
- impl<T: Copy + Num + Ord>
157
+ impl<T: Copy + Integer + Ord>
186
158
Zero for Ratio<T> {
187
159
#[inline]
188
160
fn zero() -> Ratio<T> {
@@ -195,19 +167,19 @@ impl<T: Copy + Num + Ord>
195
167
}
196
168
}
197
169
198
- impl<T: Copy + Num + Ord>
170
+ impl<T: Copy + Integer + Ord>
199
171
One for Ratio<T> {
200
172
#[inline]
201
173
fn one() -> Ratio<T> {
202
174
Ratio::new_raw(One::one(), One::one())
203
175
}
204
176
}
205
177
206
- impl<T: Copy + Num + Ord>
178
+ impl<T: Copy + Integer + Ord>
207
179
Num for Ratio<T> {}
208
180
209
181
/* Utils */
210
- impl<T: Copy + Num + Ord>
182
+ impl<T: Copy + Integer + Ord>
211
183
Round for Ratio<T> {
212
184
213
185
fn floor(&self) -> Ratio<T> {
@@ -245,7 +217,7 @@ impl<T: Copy + Num + Ord>
245
217
}
246
218
}
247
219
248
- impl<T: Copy + Num + Ord> Fractional for Ratio<T> {
220
+ impl<T: Copy + Integer + Ord> Fractional for Ratio<T> {
249
221
#[inline]
250
222
fn recip(&self) -> Ratio<T> {
251
223
Ratio::new_raw(self.denom, self.numer)
@@ -266,7 +238,7 @@ impl<T: ToStrRadix> ToStrRadix for Ratio<T> {
266
238
}
267
239
}
268
240
269
- impl<T: FromStr + Copy + Num + Ord>
241
+ impl<T: FromStr + Copy + Integer + Ord>
270
242
FromStr for Ratio<T> {
271
243
/// Parses `numer/denom`.
272
244
fn from_str(s: &str) -> Option<Ratio<T>> {
@@ -283,7 +255,7 @@ impl<T: FromStr + Copy + Num + Ord>
283
255
}
284
256
}
285
257
}
286
- impl<T: FromStrRadix + Copy + Num + Ord>
258
+ impl<T: FromStrRadix + Copy + Integer + Ord>
287
259
FromStrRadix for Ratio<T> {
288
260
/// Parses `numer/denom` where the numbers are in base `radix`.
289
261
fn from_str_radix(s: &str, radix: uint) -> Option<Ratio<T>> {
@@ -316,17 +288,6 @@ mod test {
316
288
pub static _3_2: Rational = Ratio { numer: 3, denom: 2};
317
289
pub static _neg1_2: Rational = Ratio { numer: -1, denom: 2};
318
290
319
- #[test]
320
- fn test_gcd() {
321
- assert_eq!(gcd(10,2),2);
322
- assert_eq!(gcd(10,3),1);
323
- assert_eq!(gcd(0,3),3);
324
- assert_eq!(gcd(3,3),3);
325
-
326
- assert_eq!(gcd(3,-3), 3);
327
- assert_eq!(gcd(-6,3), 3);
328
- assert_eq!(gcd(-4,-2), 2);
329
- }
330
291
331
292
#[test]
332
293
fn test_test_constants() {
0 commit comments