From 41eaa97372ef0ede1f9f2657d2c6752bbf191edb Mon Sep 17 00:00:00 2001 From: gifnksm Date: Tue, 14 May 2013 21:25:53 +0900 Subject: [PATCH] libstd: `Rational` requires `Integer` as type bounds instead of `Num` --- src/libstd/num/rational.rs | 67 ++++++++------------------------------ 1 file changed, 14 insertions(+), 53 deletions(-) diff --git a/src/libstd/num/rational.rs b/src/libstd/num/rational.rs index 9b92b7241b9..c9596291981 100644 --- a/src/libstd/num/rational.rs +++ b/src/libstd/num/rational.rs @@ -30,7 +30,7 @@ pub type Rational64 = Ratio; /// Alias for arbitrary precision rationals. pub type BigRational = Ratio; -impl +impl Ratio { /// Create a ratio representing the integer `t`. #[inline(always)] @@ -57,7 +57,7 @@ impl /// Put self into lowest terms, with denom > 0. fn reduce(&mut self) { - let g : T = gcd(self.numer, self.denom); + let g : T = self.numer.gcd(&self.denom); self.numer /= g; self.denom /= g; @@ -76,34 +76,6 @@ impl } } -/** -Compute the greatest common divisor of two numbers, via Euclid's algorithm. - -The result can be negative. -*/ -#[inline] -pub fn gcd_raw(n: T, m: T) -> T { - let mut m = m, n = n; - while m != Zero::zero() { - let temp = m; - m = n % temp; - n = temp; - } - n -} - -/** -Compute the greatest common divisor of two numbers, via Euclid's algorithm. - -The result is always positive. -*/ -#[inline] -pub fn gcd(n: T, m: T) -> T { - let g = gcd_raw(n, m); - if g < Zero::zero() { -g } - else { g } -} - /* Comparisons */ // 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) /* Arithmetic */ // a/b * c/d = (a*c)/(b*d) -impl +impl Mul,Ratio> for Ratio { #[inline] fn mul(&self, rhs: &Ratio) -> Ratio { @@ -142,7 +114,7 @@ impl } // (a/b) / (c/d) = (a*d)/(b*c) -impl +impl Div,Ratio> for Ratio { #[inline] fn div(&self, rhs: &Ratio) -> Ratio { @@ -153,7 +125,7 @@ impl // Abstracts the a/b `op` c/d = (a*d `op` b*d) / (b*d) pattern macro_rules! arith_impl { (impl $imp:ident, $method:ident) => { - impl + impl $imp,Ratio> for Ratio { #[inline] fn $method(&self, rhs: &Ratio) -> Ratio { @@ -173,16 +145,16 @@ arith_impl!(impl Sub, sub) // a/b % c/d = (a*d % b*c)/(b*d) arith_impl!(impl Rem, rem) -impl +impl Neg> for Ratio { #[inline] fn neg(&self) -> Ratio { - Ratio::new_raw(-self.numer, self.denom) + Ratio::new_raw(-self.numer, self.denom.clone()) } } /* Constants */ -impl +impl Zero for Ratio { #[inline] fn zero() -> Ratio { @@ -195,7 +167,7 @@ impl } } -impl +impl One for Ratio { #[inline] fn one() -> Ratio { @@ -203,11 +175,11 @@ impl } } -impl +impl Num for Ratio {} /* Utils */ -impl +impl Round for Ratio { fn floor(&self) -> Ratio { @@ -245,7 +217,7 @@ impl } } -impl Fractional for Ratio { +impl Fractional for Ratio { #[inline] fn recip(&self) -> Ratio { Ratio::new_raw(self.denom, self.numer) @@ -266,7 +238,7 @@ impl ToStrRadix for Ratio { } } -impl +impl FromStr for Ratio { /// Parses `numer/denom`. fn from_str(s: &str) -> Option> { @@ -283,7 +255,7 @@ impl } } } -impl +impl FromStrRadix for Ratio { /// Parses `numer/denom` where the numbers are in base `radix`. fn from_str_radix(s: &str, radix: uint) -> Option> { @@ -316,17 +288,6 @@ mod test { pub static _3_2: Rational = Ratio { numer: 3, denom: 2}; pub static _neg1_2: Rational = Ratio { numer: -1, denom: 2}; - #[test] - fn test_gcd() { - assert_eq!(gcd(10,2),2); - assert_eq!(gcd(10,3),1); - assert_eq!(gcd(0,3),3); - assert_eq!(gcd(3,3),3); - - assert_eq!(gcd(3,-3), 3); - assert_eq!(gcd(-6,3), 3); - assert_eq!(gcd(-4,-2), 2); - } #[test] fn test_test_constants() {