Skip to content

Commit a117cf0

Browse files
committed
libstd: correct bigint's quot/rem, div/modulo
1 parent 8205f73 commit a117cf0

File tree

1 file changed

+57
-56
lines changed

1 file changed

+57
-56
lines changed

src/libstd/num/bigint.rs

+57-56
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,15 @@ impl Mul<BigUint, BigUint> for BigUint {
264264

265265
impl Quot<BigUint, BigUint> for BigUint {
266266
fn quot(&self, other: &BigUint) -> BigUint {
267-
let (d, _) = self.quot_rem(other);
268-
return d;
267+
let (q, _) = self.quot_rem(other);
268+
return q;
269269
}
270270
}
271271

272272
impl Rem<BigUint, BigUint> for BigUint {
273273
fn rem(&self, other: &BigUint) -> BigUint {
274-
let (_, m) = self.quot_rem(other);
275-
return m;
274+
let (_, r) = self.quot_rem(other);
275+
return r;
276276
}
277277
}
278278

@@ -302,14 +302,14 @@ impl ToStrRadix for BigUint {
302302
fn convert_base(n: BigUint, base: uint) -> ~[BigDigit] {
303303
let divider = BigUint::from_uint(base);
304304
let mut result = ~[];
305-
let mut r = n;
306-
while r > divider {
307-
let (d, r0) = r.quot_rem(&divider);
308-
result += [r0.to_uint() as BigDigit];
309-
r = d;
305+
let mut m = n;
306+
while m > divider {
307+
let (d, m0) = m.div_mod(&divider);
308+
result += [m0.to_uint() as BigDigit];
309+
m = d;
310310
}
311-
if r.is_not_zero() {
312-
result += [r.to_uint() as BigDigit];
311+
if m.is_not_zero() {
312+
result += [m.to_uint() as BigDigit];
313313
}
314314
return result;
315315
}
@@ -384,7 +384,16 @@ pub impl BigUint {
384384

385385
fn abs(&self) -> BigUint { copy *self }
386386

387-
fn quot_rem(&self, other: &BigUint) -> (BigUint, BigUint) {
387+
fn div(&self, other: &BigUint) -> BigUint {
388+
let (d, _) = self.div_mod(other);
389+
return d;
390+
}
391+
fn modulo(&self, other: &BigUint) -> BigUint {
392+
let (_, m) = self.div_mod(other);
393+
return m;
394+
}
395+
396+
fn div_mod(&self, other: &BigUint) -> (BigUint, BigUint) {
388397
if other.is_zero() { fail!() }
389398
if self.is_zero() { return (Zero::zero(), Zero::zero()); }
390399
if *other == One::one() { return (copy *self, Zero::zero()); }
@@ -402,17 +411,17 @@ pub impl BigUint {
402411
shift += 1;
403412
}
404413
assert!(shift < BigDigit::bits);
405-
let (d, m) = quot_rem_inner(self << shift, other << shift);
414+
let (d, m) = div_mod_inner(self << shift, other << shift);
406415
return (d, m >> shift);
407416

408-
fn quot_rem_inner(a: BigUint, b: BigUint) -> (BigUint, BigUint) {
409-
let mut r = a;
417+
fn div_mod_inner(a: BigUint, b: BigUint) -> (BigUint, BigUint) {
418+
let mut m = a;
410419
let mut d = Zero::zero::<BigUint>();
411420
let mut n = 1;
412-
while r >= b {
413-
let mut (d0, d_unit, b_unit) = div_estimate(&r, &b, n);
421+
while m >= b {
422+
let mut (d0, d_unit, b_unit) = div_estimate(&m, &b, n);
414423
let mut prod = b * d0;
415-
while prod > r {
424+
while prod > m {
416425
d0 -= d_unit;
417426
prod -= b_unit;
418427
}
@@ -422,9 +431,9 @@ pub impl BigUint {
422431
}
423432
n = 1;
424433
d += d0;
425-
r -= prod;
434+
m -= prod;
426435
}
427-
return (d, r);
436+
return (d, m);
428437
}
429438

430439
fn div_estimate(a: &BigUint, b: &BigUint, n: uint)
@@ -455,16 +464,8 @@ pub impl BigUint {
455464
}
456465
}
457466

458-
fn quot(&self, other: &BigUint) -> BigUint {
459-
let (q, _) = self.quotrem(other);
460-
return q;
461-
}
462-
fn rem(&self, other: &BigUint) -> BigUint {
463-
let (_, r) = self.quotrem(other);
464-
return r;
465-
}
466-
fn quotrem(&self, other: &BigUint) -> (BigUint, BigUint) {
467-
self.quot_rem(other)
467+
fn quot_rem(&self, other: &BigUint) -> (BigUint, BigUint) {
468+
self.div_mod(other)
468469
}
469470

470471
fn is_zero(&self) -> bool { self.data.is_empty() }
@@ -739,15 +740,15 @@ impl Mul<BigInt, BigInt> for BigInt {
739740

740741
impl Quot<BigInt, BigInt> for BigInt {
741742
fn quot(&self, other: &BigInt) -> BigInt {
742-
let (d, _) = self.quot_rem(other);
743-
return d;
743+
let (q, _) = self.quot_rem(other);
744+
return q;
744745
}
745746
}
746747

747748
impl Rem<BigInt, BigInt> for BigInt {
748749
fn rem(&self, other: &BigInt) -> BigInt {
749-
let (_, m) = self.quot_rem(other);
750-
return m;
750+
let (_, r) = self.quot_rem(other);
751+
return r;
751752
}
752753
}
753754

@@ -841,7 +842,16 @@ pub impl BigInt {
841842
BigInt::from_biguint(Plus, copy self.data)
842843
}
843844
844-
fn quot_rem(&self, other: &BigInt) -> (BigInt, BigInt) {
845+
fn div(&self, other: &BigInt) -> BigInt {
846+
let (d, _) = self.div_mod(other);
847+
return d;
848+
}
849+
fn modulo(&self, other: &BigInt) -> BigInt {
850+
let (_, m) = self.div_mod(other);
851+
return m;
852+
}
853+
854+
fn div_mod(&self, other: &BigInt) -> (BigInt, BigInt) {
845855
// m.sign == other.sign
846856
let (d_ui, m_ui) = self.data.quot_rem(&other.data);
847857
let d = BigInt::from_biguint(Plus, d_ui),
@@ -863,18 +873,9 @@ pub impl BigInt {
863873
}
864874
}
865875
866-
fn quot(&self, other: &BigInt) -> BigInt {
867-
let (q, _) = self.quotrem(other);
868-
return q;
869-
}
870-
fn rem(&self, other: &BigInt) -> BigInt {
871-
let (_, r) = self.quotrem(other);
872-
return r;
873-
}
874-
875-
fn quotrem(&self, other: &BigInt) -> (BigInt, BigInt) {
876+
fn quot_rem(&self, other: &BigInt) -> (BigInt, BigInt) {
876877
// r.sign == self.sign
877-
let (q_ui, r_ui) = self.data.quotrem(&other.data);
878+
let (q_ui, r_ui) = self.data.div_mod(&other.data);
878879
let q = BigInt::from_biguint(Plus, q_ui);
879880
let r = BigInt::from_biguint(Plus, r_ui);
880881
match (self.sign, other.sign) {
@@ -1151,9 +1152,9 @@ mod biguint_tests {
11511152
];
11521153

11531154
static quot_rem_quadruples: &'static [(&'static [BigDigit],
1154-
&'static [BigDigit],
1155-
&'static [BigDigit],
1156-
&'static [BigDigit])]
1155+
&'static [BigDigit],
1156+
&'static [BigDigit],
1157+
&'static [BigDigit])]
11571158
= &[
11581159
(&[ 1], &[ 2], &[], &[1]),
11591160
(&[ 1, 1], &[ 2], &[-1/2+1], &[1]),
@@ -1517,9 +1518,9 @@ mod bigint_tests {
15171518
];
15181519
15191520
static quot_rem_quadruples: &'static [(&'static [BigDigit],
1520-
&'static [BigDigit],
1521-
&'static [BigDigit],
1522-
&'static [BigDigit])]
1521+
&'static [BigDigit],
1522+
&'static [BigDigit],
1523+
&'static [BigDigit])]
15231524
= &[
15241525
(&[ 1], &[ 2], &[], &[1]),
15251526
(&[ 1, 1], &[ 2], &[-1/2+1], &[1]),
@@ -1556,9 +1557,9 @@ mod bigint_tests {
15561557
}
15571558
15581559
#[test]
1559-
fn test_quot_rem() {
1560+
fn test_div_mod() {
15601561
fn check_sub(a: &BigInt, b: &BigInt, ans_d: &BigInt, ans_m: &BigInt) {
1561-
let (d, m) = a.quot_rem(b);
1562+
let (d, m) = a.div_mod(b);
15621563
if m.is_not_zero() {
15631564
assert!(m.sign == b.sign);
15641565
}
@@ -1607,9 +1608,9 @@ mod bigint_tests {
16071608
16081609
16091610
#[test]
1610-
fn test_quotrem() {
1611+
fn test_quot_rem() {
16111612
fn check_sub(a: &BigInt, b: &BigInt, ans_q: &BigInt, ans_r: &BigInt) {
1612-
let (q, r) = a.quotrem(b);
1613+
let (q, r) = a.quot_rem(b);
16131614
if r.is_not_zero() {
16141615
assert!(r.sign == a.sign);
16151616
}

0 commit comments

Comments
 (0)