@@ -30,7 +30,7 @@ pub fn sub(x: T, y: T) -> T { x - y }
30
30
#[ inline( always) ]
31
31
pub fn mul ( x : T , y : T ) -> T { x * y }
32
32
#[ inline( always) ]
33
- pub fn quot ( x : T , y : T ) -> T { x / y }
33
+ pub fn div ( x : T , y : T ) -> T { x / y }
34
34
35
35
///
36
36
/// Returns the remainder of y / x.
@@ -201,16 +201,11 @@ impl Mul<T,T> for T {
201
201
fn mul(&self, other: &T) -> T { *self * *other }
202
202
}
203
203
204
- #[cfg(stage0, notest)]
204
+ #[cfg(notest)]
205
205
impl Div<T,T> for T {
206
- #[inline(always)]
207
- fn div(&self, other: &T) -> T { *self / *other }
208
- }
209
- #[cfg(not(stage0),notest)]
210
- impl Quot<T,T> for T {
211
206
///
212
- /// Returns the integer quotient , truncated towards 0. As this behaviour reflects
213
- /// the underlying machine implementation it is more efficient than `Natural::div `.
207
+ /// Integer division , truncated towards 0. As this behaviour reflects the underlying
208
+ /// machine implementation it is more efficient than `Integer::div_floor `.
214
209
///
215
210
/// # Examples
216
211
///
@@ -227,7 +222,7 @@ impl Quot<T,T> for T {
227
222
/// ~~~
228
223
///
229
224
#[inline(always)]
230
- fn quot (&self, other: &T) -> T { *self / *other }
225
+ fn div (&self, other: &T) -> T { *self / *other }
231
226
}
232
227
233
228
#[cfg(stage0,notest)]
@@ -307,51 +302,51 @@ impl Integer for T {
307
302
/// # Examples
308
303
///
309
304
/// ~~~
310
- /// assert!(( 8).div ( 3) == 2);
311
- /// assert!(( 8).div (-3) == -3);
312
- /// assert!((-8).div ( 3) == -3);
313
- /// assert!((-8).div (-3) == 2);
305
+ /// assert!(( 8).div_floor ( 3) == 2);
306
+ /// assert!(( 8).div_floor (-3) == -3);
307
+ /// assert!((-8).div_floor ( 3) == -3);
308
+ /// assert!((-8).div_floor (-3) == 2);
314
309
///
315
- /// assert!(( 1).div ( 2) == 0);
316
- /// assert!(( 1).div (-2) == -1);
317
- /// assert!((-1).div ( 2) == -1);
318
- /// assert!((-1).div (-2) == 0);
310
+ /// assert!(( 1).div_floor ( 2) == 0);
311
+ /// assert!(( 1).div_floor (-2) == -1);
312
+ /// assert!((-1).div_floor ( 2) == -1);
313
+ /// assert!((-1).div_floor (-2) == 0);
319
314
/// ~~~
320
315
///
321
316
#[inline(always)]
322
- fn div (&self, other: &T) -> T {
317
+ fn div_floor (&self, other: &T) -> T {
323
318
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
324
319
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
325
- match self.quot_rem (other) {
326
- (q , r) if (r > 0 && *other < 0)
327
- || (r < 0 && *other > 0) => q - 1,
328
- (q , _) => q ,
320
+ match self.div_rem (other) {
321
+ (d , r) if (r > 0 && *other < 0)
322
+ || (r < 0 && *other > 0) => d - 1,
323
+ (d , _) => d ,
329
324
}
330
325
}
331
326
332
327
///
333
328
/// Integer modulo, satisfying:
334
329
///
335
330
/// ~~~
336
- /// assert!(n.div (d) * d + n.modulo (d) == n)
331
+ /// assert!(n.div_floor (d) * d + n.mod_floor (d) == n)
337
332
/// ~~~
338
333
///
339
334
/// # Examples
340
335
///
341
336
/// ~~~
342
- /// assert!(( 8).modulo ( 3) == 2);
343
- /// assert!(( 8).modulo (-3) == -1);
344
- /// assert!((-8).modulo ( 3) == 1);
345
- /// assert!((-8).modulo (-3) == -2);
337
+ /// assert!(( 8).mod_floor ( 3) == 2);
338
+ /// assert!(( 8).mod_floor (-3) == -1);
339
+ /// assert!((-8).mod_floor ( 3) == 1);
340
+ /// assert!((-8).mod_floor (-3) == -2);
346
341
///
347
- /// assert!(( 1).modulo ( 2) == 1);
348
- /// assert!(( 1).modulo (-2) == -1);
349
- /// assert!((-1).modulo ( 2) == 1);
350
- /// assert!((-1).modulo (-2) == -1);
342
+ /// assert!(( 1).mod_floor ( 2) == 1);
343
+ /// assert!(( 1).mod_floor (-2) == -1);
344
+ /// assert!((-1).mod_floor ( 2) == 1);
345
+ /// assert!((-1).mod_floor (-2) == -1);
351
346
/// ~~~
352
347
///
353
348
#[inline(always)]
354
- fn modulo (&self, other: &T) -> T {
349
+ fn mod_floor (&self, other: &T) -> T {
355
350
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
356
351
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
357
352
match *self % *other {
@@ -361,21 +356,21 @@ impl Integer for T {
361
356
}
362
357
}
363
358
364
- /// Calculates `div ` and `modulo ` simultaneously
359
+ /// Calculates `div_floor ` and `mod_floor ` simultaneously
365
360
#[inline(always)]
366
- fn div_mod (&self, other: &T) -> (T,T) {
361
+ fn div_mod_floor (&self, other: &T) -> (T,T) {
367
362
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
368
363
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
369
- match self.quot_rem (other) {
370
- (q , r) if (r > 0 && *other < 0)
371
- || (r < 0 && *other > 0) => (q - 1, r + *other),
372
- (q , r) => (q , r),
364
+ match self.div_rem (other) {
365
+ (d , r) if (r > 0 && *other < 0)
366
+ || (r < 0 && *other > 0) => (d - 1, r + *other),
367
+ (d , r) => (d , r),
373
368
}
374
369
}
375
370
376
- /// Calculates `quot ` (`\` ) and `rem` (`%`) simultaneously
371
+ /// Calculates `div ` (`\` ) and `rem` (`%`) simultaneously
377
372
#[inline(always)]
378
- fn quot_rem (&self, other: &T) -> (T,T) {
373
+ fn div_rem (&self, other: &T) -> (T,T) {
379
374
(*self / *other, *self % *other)
380
375
}
381
376
@@ -599,42 +594,42 @@ mod tests {
599
594
}
600
595
601
596
#[test]
602
- fn test_quot_rem () {
603
- fn test_nd_qr (nd: (T,T), qr: (T,T)) {
597
+ fn test_div_rem () {
598
+ fn test_nd_dr (nd: (T,T), qr: (T,T)) {
604
599
let (n,d) = nd;
605
- let separate_quot_rem = (n / d, n % d);
606
- let combined_quot_rem = n.quot_rem (&d);
600
+ let separate_div_rem = (n / d, n % d);
601
+ let combined_div_rem = n.div_rem (&d);
607
602
608
- assert_eq!(separate_quot_rem , qr);
609
- assert_eq!(combined_quot_rem , qr);
603
+ assert_eq!(separate_div_rem , qr);
604
+ assert_eq!(combined_div_rem , qr);
610
605
611
- test_division_rule(nd, separate_quot_rem );
612
- test_division_rule(nd, combined_quot_rem );
606
+ test_division_rule(nd, separate_div_rem );
607
+ test_division_rule(nd, combined_div_rem );
613
608
}
614
609
615
- test_nd_qr (( 8, 3), ( 2, 2));
616
- test_nd_qr (( 8, -3), (-2, 2));
617
- test_nd_qr ((-8, 3), (-2, -2));
618
- test_nd_qr ((-8, -3), ( 2, -2));
610
+ test_nd_dr (( 8, 3), ( 2, 2));
611
+ test_nd_dr (( 8, -3), (-2, 2));
612
+ test_nd_dr ((-8, 3), (-2, -2));
613
+ test_nd_dr ((-8, -3), ( 2, -2));
619
614
620
- test_nd_qr (( 1, 2), ( 0, 1));
621
- test_nd_qr (( 1, -2), ( 0, 1));
622
- test_nd_qr ((-1, 2), ( 0, -1));
623
- test_nd_qr ((-1, -2), ( 0, -1));
615
+ test_nd_dr (( 1, 2), ( 0, 1));
616
+ test_nd_dr (( 1, -2), ( 0, 1));
617
+ test_nd_dr ((-1, 2), ( 0, -1));
618
+ test_nd_dr ((-1, -2), ( 0, -1));
624
619
}
625
620
626
621
#[test]
627
- fn test_div_mod () {
622
+ fn test_div_mod_floor () {
628
623
fn test_nd_dm(nd: (T,T), dm: (T,T)) {
629
624
let (n,d) = nd;
630
- let separate_div_mod = (n.div (&d), n.modulo (&d));
631
- let combined_div_mod = n.div_mod (&d);
625
+ let separate_div_mod_floor = (n.div_floor (&d), n.mod_floor (&d));
626
+ let combined_div_mod_floor = n.div_mod_floor (&d);
632
627
633
- assert_eq!(separate_div_mod , dm);
634
- assert_eq!(combined_div_mod , dm);
628
+ assert_eq!(separate_div_mod_floor , dm);
629
+ assert_eq!(combined_div_mod_floor , dm);
635
630
636
- test_division_rule(nd, separate_div_mod );
637
- test_division_rule(nd, combined_div_mod );
631
+ test_division_rule(nd, separate_div_mod_floor );
632
+ test_division_rule(nd, combined_div_mod_floor );
638
633
}
639
634
640
635
test_nd_dm(( 8, 3), ( 2, 2));
0 commit comments