@@ -264,15 +264,15 @@ impl Mul<BigUint, BigUint> for BigUint {
264
264
265
265
impl Quot < BigUint , BigUint > for BigUint {
266
266
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 ;
269
269
}
270
270
}
271
271
272
272
impl Rem < BigUint , BigUint > for BigUint {
273
273
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 ;
276
276
}
277
277
}
278
278
@@ -302,14 +302,14 @@ impl ToStrRadix for BigUint {
302
302
fn convert_base ( n : BigUint , base : uint ) -> ~[ BigDigit ] {
303
303
let divider = BigUint :: from_uint ( base) ;
304
304
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;
310
310
}
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 ] ;
313
313
}
314
314
return result;
315
315
}
@@ -384,7 +384,16 @@ pub impl BigUint {
384
384
385
385
fn abs ( & self ) -> BigUint { copy * self }
386
386
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 ) {
388
397
if other. is_zero ( ) { fail ! ( ) }
389
398
if self . is_zero ( ) { return ( Zero :: zero ( ) , Zero :: zero ( ) ) ; }
390
399
if * other == One :: one ( ) { return ( copy * self , Zero :: zero ( ) ) ; }
@@ -402,17 +411,17 @@ pub impl BigUint {
402
411
shift += 1 ;
403
412
}
404
413
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) ;
406
415
return ( d, m >> shift) ;
407
416
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;
410
419
let mut d = Zero :: zero :: < BigUint > ( ) ;
411
420
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) ;
414
423
let mut prod = b * d0;
415
- while prod > r {
424
+ while prod > m {
416
425
d0 -= d_unit;
417
426
prod -= b_unit;
418
427
}
@@ -422,9 +431,9 @@ pub impl BigUint {
422
431
}
423
432
n = 1 ;
424
433
d += d0;
425
- r -= prod;
434
+ m -= prod;
426
435
}
427
- return ( d, r ) ;
436
+ return ( d, m ) ;
428
437
}
429
438
430
439
fn div_estimate ( a : & BigUint , b : & BigUint , n : uint )
@@ -455,16 +464,8 @@ pub impl BigUint {
455
464
}
456
465
}
457
466
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)
468
469
}
469
470
470
471
fn is_zero ( & self ) -> bool { self . data . is_empty ( ) }
@@ -739,15 +740,15 @@ impl Mul<BigInt, BigInt> for BigInt {
739
740
740
741
impl Quot < BigInt , BigInt > for BigInt {
741
742
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 ;
744
745
}
745
746
}
746
747
747
748
impl Rem < BigInt , BigInt > for BigInt {
748
749
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 ;
751
752
}
752
753
}
753
754
@@ -841,7 +842,16 @@ pub impl BigInt {
841
842
BigInt::from_biguint(Plus, copy self.data)
842
843
}
843
844
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) {
845
855
// m.sign == other.sign
846
856
let (d_ui, m_ui) = self.data.quot_rem(&other.data);
847
857
let d = BigInt::from_biguint(Plus, d_ui),
@@ -863,18 +873,9 @@ pub impl BigInt {
863
873
}
864
874
}
865
875
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) {
876
877
// 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);
878
879
let q = BigInt::from_biguint(Plus, q_ui);
879
880
let r = BigInt::from_biguint(Plus, r_ui);
880
881
match (self.sign, other.sign) {
@@ -1151,9 +1152,9 @@ mod biguint_tests {
1151
1152
] ;
1152
1153
1153
1154
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 ] ) ]
1157
1158
= & [
1158
1159
( & [ 1 ] , & [ 2 ] , & [ ] , & [ 1 ] ) ,
1159
1160
( & [ 1 , 1 ] , & [ 2 ] , & [ -1 /2 +1 ] , & [ 1 ] ) ,
@@ -1517,9 +1518,9 @@ mod bigint_tests {
1517
1518
];
1518
1519
1519
1520
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])]
1523
1524
= &[
1524
1525
(&[ 1], &[ 2], &[], &[1]),
1525
1526
(&[ 1, 1], &[ 2], &[-1/2+1], &[1]),
@@ -1556,9 +1557,9 @@ mod bigint_tests {
1556
1557
}
1557
1558
1558
1559
#[test]
1559
- fn test_quot_rem () {
1560
+ fn test_div_mod () {
1560
1561
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);
1562
1563
if m.is_not_zero() {
1563
1564
assert!(m.sign == b.sign);
1564
1565
}
@@ -1607,9 +1608,9 @@ mod bigint_tests {
1607
1608
1608
1609
1609
1610
#[test]
1610
- fn test_quotrem () {
1611
+ fn test_quot_rem () {
1611
1612
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);
1613
1614
if r.is_not_zero() {
1614
1615
assert!(r.sign == a.sign);
1615
1616
}
0 commit comments