@@ -30,7 +30,7 @@ pub type Rational64 = Ratio<i64>;
3030/// Alias for arbitrary precision rationals.
3131pub type BigRational = Ratio < BigInt > ;
3232
33- impl < T : Copy + Num + Ord >
33+ impl < T : Copy + Integer + Ord >
3434 Ratio < T > {
3535 /// Create a ratio representing the integer `t`.
3636 #[ inline( always) ]
@@ -57,7 +57,7 @@ impl<T: Copy + Num + Ord>
5757
5858 /// Put self into lowest terms, with denom > 0.
5959 fn reduce(&mut self) {
60- let g : T = gcd( self.numer, self.denom);
60+ let g : T = self.numer.gcd(& self.denom);
6161
6262 self.numer /= g;
6363 self.denom /= g;
@@ -76,34 +76,6 @@ impl<T: Copy + Num + Ord>
7676 }
7777}
7878
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-
10779/* Comparisons */
10880
10981// 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)
133105
134106/* Arithmetic */
135107// a/b * c/d = (a*c)/(b*d)
136- impl<T: Copy + Num + Ord>
108+ impl<T: Copy + Integer + Ord>
137109 Mul<Ratio<T>,Ratio<T>> for Ratio<T> {
138110 #[inline]
139111 fn mul(&self, rhs: &Ratio<T>) -> Ratio<T> {
@@ -142,7 +114,7 @@ impl<T: Copy + Num + Ord>
142114}
143115
144116// (a/b) / (c/d) = (a*d)/(b*c)
145- impl<T: Copy + Num + Ord>
117+ impl<T: Copy + Integer + Ord>
146118 Div<Ratio<T>,Ratio<T>> for Ratio<T> {
147119 #[inline]
148120 fn div(&self, rhs: &Ratio<T>) -> Ratio<T> {
@@ -153,7 +125,7 @@ impl<T: Copy + Num + Ord>
153125// Abstracts the a/b `op` c/d = (a*d `op` b*d) / (b*d) pattern
154126macro_rules! arith_impl {
155127 (impl $imp:ident, $method:ident) => {
156- impl<T: Copy + Num + Ord>
128+ impl<T: Copy + Integer + Ord>
157129 $imp<Ratio<T>,Ratio<T>> for Ratio<T> {
158130 #[inline]
159131 fn $method(&self, rhs: &Ratio<T>) -> Ratio<T> {
@@ -173,16 +145,16 @@ arith_impl!(impl Sub, sub)
173145// a/b % c/d = (a*d % b*c)/(b*d)
174146arith_impl!(impl Rem, rem)
175147
176- impl<T: Copy + Num + Ord>
148+ impl<T: Copy + Integer + Ord>
177149 Neg<Ratio<T>> for Ratio<T> {
178150 #[inline]
179151 fn neg(&self) -> Ratio<T> {
180- Ratio::new_raw(-self.numer, self.denom)
152+ Ratio::new_raw(-self.numer, self.denom.clone() )
181153 }
182154}
183155
184156/* Constants */
185- impl<T: Copy + Num + Ord>
157+ impl<T: Copy + Integer + Ord>
186158 Zero for Ratio<T> {
187159 #[inline]
188160 fn zero() -> Ratio<T> {
@@ -195,19 +167,19 @@ impl<T: Copy + Num + Ord>
195167 }
196168}
197169
198- impl<T: Copy + Num + Ord>
170+ impl<T: Copy + Integer + Ord>
199171 One for Ratio<T> {
200172 #[inline]
201173 fn one() -> Ratio<T> {
202174 Ratio::new_raw(One::one(), One::one())
203175 }
204176}
205177
206- impl<T: Copy + Num + Ord>
178+ impl<T: Copy + Integer + Ord>
207179 Num for Ratio<T> {}
208180
209181/* Utils */
210- impl<T: Copy + Num + Ord>
182+ impl<T: Copy + Integer + Ord>
211183 Round for Ratio<T> {
212184
213185 fn floor(&self) -> Ratio<T> {
@@ -245,7 +217,7 @@ impl<T: Copy + Num + Ord>
245217 }
246218}
247219
248- impl<T: Copy + Num + Ord> Fractional for Ratio<T> {
220+ impl<T: Copy + Integer + Ord> Fractional for Ratio<T> {
249221 #[inline]
250222 fn recip(&self) -> Ratio<T> {
251223 Ratio::new_raw(self.denom, self.numer)
@@ -266,7 +238,7 @@ impl<T: ToStrRadix> ToStrRadix for Ratio<T> {
266238 }
267239}
268240
269- impl<T: FromStr + Copy + Num + Ord>
241+ impl<T: FromStr + Copy + Integer + Ord>
270242 FromStr for Ratio<T> {
271243 /// Parses `numer/denom`.
272244 fn from_str(s: &str) -> Option<Ratio<T>> {
@@ -283,7 +255,7 @@ impl<T: FromStr + Copy + Num + Ord>
283255 }
284256 }
285257}
286- impl<T: FromStrRadix + Copy + Num + Ord>
258+ impl<T: FromStrRadix + Copy + Integer + Ord>
287259 FromStrRadix for Ratio<T> {
288260 /// Parses `numer/denom` where the numbers are in base `radix`.
289261 fn from_str_radix(s: &str, radix: uint) -> Option<Ratio<T>> {
@@ -316,17 +288,6 @@ mod test {
316288 pub static _3_2: Rational = Ratio { numer: 3, denom: 2};
317289 pub static _neg1_2: Rational = Ratio { numer: -1, denom: 2};
318290
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- }
330291
331292 #[test]
332293 fn test_test_constants() {
0 commit comments