Skip to content

Commit e664781

Browse files
committed
auto merge of #8610 : kballard/rust/mod_floor, r=alexcrichton
`mod_floor()` was incorrectly defined for uint types as `a / b` instead of `a % b`.
2 parents bf90634 + ac3bc9c commit e664781

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/libstd/num/uint_macros.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ impl Integer for $T {
238238

239239
/// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
240240
#[inline]
241-
fn mod_floor(&self, other: &$T) -> $T { *self / *other }
241+
fn mod_floor(&self, other: &$T) -> $T { *self % *other }
242242

243-
/// Calculates `div_floor` and `modulo_floor` simultaneously
243+
/// Calculates `div_floor` and `mod_floor` simultaneously
244244
#[inline]
245245
fn div_mod_floor(&self, other: &$T) -> ($T,$T) {
246246
(*self / *other, *self % *other)
@@ -458,6 +458,19 @@ mod tests {
458458
assert_eq!((3 as $T).clamp(&(2 as $T), &(4 as $T)), 3 as $T);
459459
}
460460

461+
#[test]
462+
fn test_div_mod_floor() {
463+
assert_eq!((10 as $T).div_floor(&(3 as $T)), 3 as $T);
464+
assert_eq!((10 as $T).mod_floor(&(3 as $T)), 1 as $T);
465+
assert_eq!((10 as $T).div_mod_floor(&(3 as $T)), (3 as $T, 1 as $T));
466+
assert_eq!((5 as $T).div_floor(&(5 as $T)), 1 as $T);
467+
assert_eq!((5 as $T).mod_floor(&(5 as $T)), 0 as $T);
468+
assert_eq!((5 as $T).div_mod_floor(&(5 as $T)), (1 as $T, 0 as $T));
469+
assert_eq!((3 as $T).div_floor(&(7 as $T)), 0 as $T);
470+
assert_eq!((3 as $T).mod_floor(&(7 as $T)), 3 as $T);
471+
assert_eq!((3 as $T).div_mod_floor(&(7 as $T)), (0 as $T, 3 as $T));
472+
}
473+
461474
#[test]
462475
fn test_gcd() {
463476
assert_eq!((10 as $T).gcd(&2), 2 as $T);

0 commit comments

Comments
 (0)