diff options
| author | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2013-04-22 01:58:53 +1000 |
|---|---|---|
| committer | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2013-04-22 01:58:53 +1000 |
| commit | 01eb5e8ad39a57e9fc31569983c1b9d5905a7e58 (patch) | |
| tree | 5523ef76c48368ff911e1f7399d2f79f73ba43fb /src/libstd/num | |
| parent | 2104cd69d491c622a70208a7b70a4d948fc30d05 (diff) | |
| download | rust-01eb5e8ad39a57e9fc31569983c1b9d5905a7e58.tar.gz rust-01eb5e8ad39a57e9fc31569983c1b9d5905a7e58.zip | |
Rename Div operator trait to Quot and Modulo operator trait to Rem
Diffstat (limited to 'src/libstd/num')
| -rw-r--r-- | src/libstd/num/bigint.rs | 64 | ||||
| -rw-r--r-- | src/libstd/num/complex.rs | 32 | ||||
| -rw-r--r-- | src/libstd/num/rational.rs | 41 |
3 files changed, 63 insertions, 74 deletions
diff --git a/src/libstd/num/bigint.rs b/src/libstd/num/bigint.rs index ec5d2cded8d..08c65d190bf 100644 --- a/src/libstd/num/bigint.rs +++ b/src/libstd/num/bigint.rs @@ -262,16 +262,16 @@ impl Mul<BigUint, BigUint> for BigUint { } } -impl Div<BigUint, BigUint> for BigUint { - fn div(&self, other: &BigUint) -> BigUint { - let (d, _) = self.divmod(other); +impl Quot<BigUint, BigUint> for BigUint { + fn quot(&self, other: &BigUint) -> BigUint { + let (d, _) = self.quot_rem(other); return d; } } -impl Modulo<BigUint, BigUint> for BigUint { - fn modulo(&self, other: &BigUint) -> BigUint { - let (_, m) = self.divmod(other); +impl Rem<BigUint, BigUint> for BigUint { + fn rem(&self, other: &BigUint) -> BigUint { + let (_, m) = self.quot_rem(other); return m; } } @@ -304,7 +304,7 @@ impl ToStrRadix for BigUint { let mut result = ~[]; let mut r = n; while r > divider { - let (d, r0) = r.divmod(÷r); + let (d, r0) = r.quot_rem(÷r); result += [r0.to_uint() as BigDigit]; r = d; } @@ -384,7 +384,7 @@ pub impl BigUint { fn abs(&self) -> BigUint { copy *self } - fn divmod(&self, other: &BigUint) -> (BigUint, BigUint) { + fn quot_rem(&self, other: &BigUint) -> (BigUint, BigUint) { if other.is_zero() { fail!() } if self.is_zero() { return (Zero::zero(), Zero::zero()); } if *other == One::one() { return (copy *self, Zero::zero()); } @@ -402,10 +402,10 @@ pub impl BigUint { shift += 1; } assert!(shift < BigDigit::bits); - let (d, m) = divmod_inner(self << shift, other << shift); + let (d, m) = quot_rem_inner(self << shift, other << shift); return (d, m >> shift); - fn divmod_inner(a: BigUint, b: BigUint) -> (BigUint, BigUint) { + fn quot_rem_inner(a: BigUint, b: BigUint) -> (BigUint, BigUint) { let mut r = a; let mut d = Zero::zero::<BigUint>(); let mut n = 1; @@ -464,7 +464,7 @@ pub impl BigUint { return r; } fn quotrem(&self, other: &BigUint) -> (BigUint, BigUint) { - self.divmod(other) + self.quot_rem(other) } fn is_zero(&self) -> bool { self.data.is_empty() } @@ -737,16 +737,16 @@ impl Mul<BigInt, BigInt> for BigInt { } } -impl Div<BigInt, BigInt> for BigInt { - fn div(&self, other: &BigInt) -> BigInt { - let (d, _) = self.divmod(other); +impl Quot<BigInt, BigInt> for BigInt { + fn quot(&self, other: &BigInt) -> BigInt { + let (d, _) = self.quot_rem(other); return d; } } -impl Modulo<BigInt, BigInt> for BigInt { - fn modulo(&self, other: &BigInt) -> BigInt { - let (_, m) = self.divmod(other); +impl Rem<BigInt, BigInt> for BigInt { + fn rem(&self, other: &BigInt) -> BigInt { + let (_, m) = self.quot_rem(other); return m; } } @@ -841,9 +841,9 @@ pub impl BigInt { BigInt::from_biguint(Plus, copy self.data) } - fn divmod(&self, other: &BigInt) -> (BigInt, BigInt) { + fn quot_rem(&self, other: &BigInt) -> (BigInt, BigInt) { // m.sign == other.sign - let (d_ui, m_ui) = self.data.divmod(&other.data); + let (d_ui, m_ui) = self.data.quot_rem(&other.data); let d = BigInt::from_biguint(Plus, d_ui), m = BigInt::from_biguint(Plus, m_ui); match (self.sign, other.sign) { @@ -1150,7 +1150,7 @@ mod biguint_tests { (&[ 0, 0, 1], &[ 0, 0, 0, 1], &[0, 0, 0, 0, 0, 1]) ]; - static divmod_quadruples: &'static [(&'static [BigDigit], + static quot_rem_quadruples: &'static [(&'static [BigDigit], &'static [BigDigit], &'static [BigDigit], &'static [BigDigit])] @@ -1174,7 +1174,7 @@ mod biguint_tests { assert!(b * a == c); } - for divmod_quadruples.each |elm| { + for quot_rem_quadruples.each |elm| { let (aVec, bVec, cVec, dVec) = *elm; let a = BigUint::from_slice(aVec); let b = BigUint::from_slice(bVec); @@ -1187,7 +1187,7 @@ mod biguint_tests { } #[test] - fn test_divmod() { + fn test_quot_rem() { for mul_triples.each |elm| { let (aVec, bVec, cVec) = *elm; let a = BigUint::from_slice(aVec); @@ -1195,21 +1195,21 @@ mod biguint_tests { let c = BigUint::from_slice(cVec); if a.is_not_zero() { - assert!(c.divmod(&a) == (b, Zero::zero())); + assert!(c.quot_rem(&a) == (b, Zero::zero())); } if b.is_not_zero() { - assert!(c.divmod(&b) == (a, Zero::zero())); + assert!(c.quot_rem(&b) == (a, Zero::zero())); } } - for divmod_quadruples.each |elm| { + for quot_rem_quadruples.each |elm| { let (aVec, bVec, cVec, dVec) = *elm; let a = BigUint::from_slice(aVec); let b = BigUint::from_slice(bVec); let c = BigUint::from_slice(cVec); let d = BigUint::from_slice(dVec); - if b.is_not_zero() { assert!(a.divmod(&b) == (c, d)); } + if b.is_not_zero() { assert!(a.quot_rem(&b) == (c, d)); } } } @@ -1516,7 +1516,7 @@ mod bigint_tests { (&[ 0, 0, 1], &[ 0, 0, 0, 1], &[0, 0, 0, 0, 0, 1]) ]; - static divmod_quadruples: &'static [(&'static [BigDigit], + static quot_rem_quadruples: &'static [(&'static [BigDigit], &'static [BigDigit], &'static [BigDigit], &'static [BigDigit])] @@ -1543,7 +1543,7 @@ mod bigint_tests { assert!((-b) * a == -c); } - for divmod_quadruples.each |elm| { + for quot_rem_quadruples.each |elm| { let (aVec, bVec, cVec, dVec) = *elm; let a = BigInt::from_slice(Plus, aVec); let b = BigInt::from_slice(Plus, bVec); @@ -1556,9 +1556,9 @@ mod bigint_tests { } #[test] - fn test_divmod() { + fn test_quot_rem() { fn check_sub(a: &BigInt, b: &BigInt, ans_d: &BigInt, ans_m: &BigInt) { - let (d, m) = a.divmod(b); + let (d, m) = a.quot_rem(b); if m.is_not_zero() { assert!(m.sign == b.sign); } @@ -1592,7 +1592,7 @@ mod bigint_tests { if b.is_not_zero() { check(&c, &b, &a, &Zero::zero()); } } - for divmod_quadruples.each |elm| { + for quot_rem_quadruples.each |elm| { let (aVec, bVec, cVec, dVec) = *elm; let a = BigInt::from_slice(Plus, aVec); let b = BigInt::from_slice(Plus, bVec); @@ -1635,7 +1635,7 @@ mod bigint_tests { if b.is_not_zero() { check(&c, &b, &a, &Zero::zero()); } } - for divmod_quadruples.each |elm| { + for quot_rem_quadruples.each |elm| { let (aVec, bVec, cVec, dVec) = *elm; let a = BigInt::from_slice(Plus, aVec); let b = BigInt::from_slice(Plus, bVec); diff --git a/src/libstd/num/complex.rs b/src/libstd/num/complex.rs index 949850f3ca6..ef7fa397d7f 100644 --- a/src/libstd/num/complex.rs +++ b/src/libstd/num/complex.rs @@ -32,8 +32,7 @@ pub type Complex = Cmplx<float>; pub type Complex32 = Cmplx<f32>; pub type Complex64 = Cmplx<f64>; -impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> - Cmplx<T> { +impl<T: Copy + Num> Cmplx<T> { /// Create a new Cmplx #[inline] pub fn new(re: T, im: T) -> Cmplx<T> { @@ -80,24 +79,21 @@ impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> /* arithmetic */ // (a + i b) + (c + i d) == (a + c) + i (b + d) -impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> - Add<Cmplx<T>, Cmplx<T>> for Cmplx<T> { +impl<T: Copy + Num> Add<Cmplx<T>, Cmplx<T>> for Cmplx<T> { #[inline] fn add(&self, other: &Cmplx<T>) -> Cmplx<T> { Cmplx::new(self.re + other.re, self.im + other.im) } } // (a + i b) - (c + i d) == (a - c) + i (b - d) -impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> - Sub<Cmplx<T>, Cmplx<T>> for Cmplx<T> { +impl<T: Copy + Num> Sub<Cmplx<T>, Cmplx<T>> for Cmplx<T> { #[inline] fn sub(&self, other: &Cmplx<T>) -> Cmplx<T> { Cmplx::new(self.re - other.re, self.im - other.im) } } // (a + i b) * (c + i d) == (a*c - b*d) + i (a*d + b*c) -impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> - Mul<Cmplx<T>, Cmplx<T>> for Cmplx<T> { +impl<T: Copy + Num> Mul<Cmplx<T>, Cmplx<T>> for Cmplx<T> { #[inline] fn mul(&self, other: &Cmplx<T>) -> Cmplx<T> { Cmplx::new(self.re*other.re - self.im*other.im, @@ -107,18 +103,16 @@ impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> // (a + i b) / (c + i d) == [(a + i b) * (c - i d)] / (c*c + d*d) // == [(a*c + b*d) / (c*c + d*d)] + i [(b*c - a*d) / (c*c + d*d)] -impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> - Div<Cmplx<T>, Cmplx<T>> for Cmplx<T> { +impl<T: Copy + Num> Quot<Cmplx<T>, Cmplx<T>> for Cmplx<T> { #[inline] - fn div(&self, other: &Cmplx<T>) -> Cmplx<T> { + fn quot(&self, other: &Cmplx<T>) -> Cmplx<T> { let norm_sqr = other.norm_sqr(); Cmplx::new((self.re*other.re + self.im*other.im) / norm_sqr, (self.im*other.re - self.re*other.im) / norm_sqr) } } -impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> - Neg<Cmplx<T>> for Cmplx<T> { +impl<T: Copy + Num> Neg<Cmplx<T>> for Cmplx<T> { #[inline] fn neg(&self) -> Cmplx<T> { Cmplx::new(-self.re, -self.im) @@ -126,16 +120,14 @@ impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> } /* constants */ -impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T> + Zero> - Zero for Cmplx<T> { +impl<T: Copy + Num> Zero for Cmplx<T> { #[inline] fn zero() -> Cmplx<T> { Cmplx::new(Zero::zero(), Zero::zero()) } } -impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T> + Zero + One> - One for Cmplx<T> { +impl<T: Copy + Num> One for Cmplx<T> { #[inline] fn one() -> Cmplx<T> { Cmplx::new(One::one(), Zero::zero()) @@ -143,7 +135,7 @@ impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T> + Zero + One> } /* string conversions */ -impl<T: ToStr + Zero + Ord + Neg<T>> ToStr for Cmplx<T> { +impl<T: ToStr + Num + Ord> ToStr for Cmplx<T> { fn to_str(&self) -> ~str { if self.im < Zero::zero() { fmt!("%s-%si", self.re.to_str(), (-self.im).to_str()) @@ -153,7 +145,7 @@ impl<T: ToStr + Zero + Ord + Neg<T>> ToStr for Cmplx<T> { } } -impl<T: ToStrRadix + Zero + Ord + Neg<T>> ToStrRadix for Cmplx<T> { +impl<T: ToStrRadix + Num + Ord> ToStrRadix for Cmplx<T> { fn to_str_radix(&self, radix: uint) -> ~str { if self.im < Zero::zero() { fmt!("%s-%si", self.re.to_str_radix(radix), (-self.im).to_str_radix(radix)) @@ -280,7 +272,7 @@ mod test { } } #[test] - fn test_div() { + fn test_quot() { assert_eq!(_neg1_1i / _0_1i, _1_1i); for all_consts.each |&c| { if c != Zero::zero() { diff --git a/src/libstd/num/rational.rs b/src/libstd/num/rational.rs index f15b382dcd3..2098429833d 100644 --- a/src/libstd/num/rational.rs +++ b/src/libstd/num/rational.rs @@ -33,7 +33,7 @@ pub type Rational64 = Ratio<i64>; /// Alias for arbitrary precision rationals. pub type BigRational = Ratio<BigInt>; -impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq> +impl<T: Copy + Num + Ord> Ratio<T> { /// Create a ratio representing the integer `t`. #[inline(always)] @@ -51,7 +51,7 @@ impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq> #[inline(always)] pub fn new(numer: T, denom: T) -> Ratio<T> { if denom == Zero::zero() { - fail!(~"divide by 0"); + fail!(~"quotient of 0"); } let mut ret = Ratio::new_raw(numer, denom); ret.reduce(); @@ -85,7 +85,7 @@ Compute the greatest common divisor of two numbers, via Euclid's algorithm. The result can be negative. */ #[inline] -pub fn gcd_raw<T: Modulo<T,T> + Zero + Eq>(n: T, m: T) -> T { +pub fn gcd_raw<T: Num>(n: T, m: T) -> T { let mut m = m, n = n; while m != Zero::zero() { let temp = m; @@ -101,7 +101,7 @@ Compute the greatest common divisor of two numbers, via Euclid's algorithm. The result is always positive. */ #[inline] -pub fn gcd<T: Modulo<T,T> + Neg<T> + Zero + Ord + Eq>(n: T, m: T) -> T { +pub fn gcd<T: Num + Ord>(n: T, m: T) -> T { let g = gcd_raw(n, m); if g < Zero::zero() { -g } else { g } @@ -136,7 +136,7 @@ cmp_impl!(impl TotalOrd, cmp -> cmp::Ordering) /* Arithmetic */ // a/b * c/d = (a*c)/(b*d) -impl<T: Copy + Mul<T,T> + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq> +impl<T: Copy + Num + Ord> Mul<Ratio<T>,Ratio<T>> for Ratio<T> { #[inline] fn mul(&self, rhs: &Ratio<T>) -> Ratio<T> { @@ -145,10 +145,10 @@ impl<T: Copy + Mul<T,T> + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + E } // (a/b) / (c/d) = (a*d)/(b*c) -impl<T: Copy + Mul<T,T> + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq> - Div<Ratio<T>,Ratio<T>> for Ratio<T> { +impl<T: Copy + Num + Ord> + Quot<Ratio<T>,Ratio<T>> for Ratio<T> { #[inline] - fn div(&self, rhs: &Ratio<T>) -> Ratio<T> { + fn quot(&self, rhs: &Ratio<T>) -> Ratio<T> { Ratio::new(self.numer * rhs.denom, self.denom * rhs.numer) } } @@ -156,9 +156,7 @@ impl<T: Copy + Mul<T,T> + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + E // 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<T: Copy + - Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Modulo<T,T> + Neg<T> + - Zero + One + Ord + Eq> + impl<T: Copy + Num + Ord> $imp<Ratio<T>,Ratio<T>> for Ratio<T> { #[inline] fn $method(&self, rhs: &Ratio<T>) -> Ratio<T> { @@ -176,9 +174,9 @@ arith_impl!(impl Add, add) arith_impl!(impl Sub, sub) // a/b % c/d = (a*d % b*c)/(b*d) -arith_impl!(impl Modulo, modulo) +arith_impl!(impl Rem, rem) -impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq> +impl<T: Copy + Num + Ord> Neg<Ratio<T>> for Ratio<T> { #[inline] fn neg(&self) -> Ratio<T> { @@ -187,7 +185,7 @@ impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq> } /* Constants */ -impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq> +impl<T: Copy + Num + Ord> Zero for Ratio<T> { #[inline] fn zero() -> Ratio<T> { @@ -195,7 +193,7 @@ impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq> } } -impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq> +impl<T: Copy + Num + Ord> One for Ratio<T> { #[inline] fn one() -> Ratio<T> { @@ -204,8 +202,7 @@ impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq> } /* Utils */ -impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Modulo<T,T> + Neg<T> + - Zero + One + Ord + Eq> +impl<T: Copy + Num + Ord> Round for Ratio<T> { fn round(&self, mode: num::RoundMode) -> Ratio<T> { match mode { @@ -256,7 +253,7 @@ impl<T: ToStrRadix> ToStrRadix for Ratio<T> { } } -impl<T: FromStr + Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq> +impl<T: FromStr + Copy + Num + Ord> FromStr for Ratio<T> { /// Parses `numer/denom`. fn from_str(s: &str) -> Option<Ratio<T>> { @@ -273,7 +270,7 @@ impl<T: FromStr + Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq } } } -impl<T: FromStrRadix + Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq> +impl<T: FromStrRadix + Copy + Num + Ord> FromStrRadix for Ratio<T> { /// Parses `numer/denom` where the numbers are in base `radix`. fn from_str_radix(s: &str, radix: uint) -> Option<Ratio<T>> { @@ -386,14 +383,14 @@ mod test { } #[test] - fn test_div() { + fn test_quot() { assert_eq!(_1 / _1_2, _2); assert_eq!(_3_2 / _1_2, _1 + _2); assert_eq!(_1 / _neg1_2, _neg1_2 + _neg1_2 + _neg1_2 + _neg1_2); } #[test] - fn test_modulo() { + fn test_rem() { assert_eq!(_3_2 % _1, _1_2); assert_eq!(_2 % _neg1_2, _0); assert_eq!(_1_2 % _2, _1_2); @@ -415,7 +412,7 @@ mod test { } #[test] #[should_fail] - fn test_div_0() { + fn test_quot_0() { let _a = _1 / _0; } } |
