Skip to content

Commit 42c22de

Browse files
bors[bot]WizardOfMenlocuviper
authored
Merge #47
47: Fix is_multiple_of to account for 0 r=cuviper a=WizardOfMenlo This pull request fixes the implementation to `is_multiple_of` to account for the case in which one of the operand is zero. The current implementation panics, while this one currently concludes that 0 is a multiple of 0 (and in fact the only one). The change is a minimal check, and an inclusion of this edge case in the tests. Co-authored-by: Giacomo Fenzi <[email protected]> Co-authored-by: Josh Stone <[email protected]>
2 parents a7d40a1 + dee9a82 commit 42c22de

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ categories = ["algorithms", "science", "no-std"]
88
license = "MIT OR Apache-2.0"
99
repository = "https://github.com/rust-num/num-integer"
1010
name = "num-integer"
11-
version = "0.1.44"
11+
version = "0.1.45"
1212
readme = "README.md"
1313
build = "build.rs"
1414
exclude = ["/bors.toml", "/ci/*", "/.github/*"]

RELEASES.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# Release 0.1.45 (2022-04-29)
2+
3+
- [`Integer::is_multiple_of` now handles a 0 argument without panicking][47]
4+
for primitive integers.
5+
6+
**Contributors**: @cuviper, @WizardOfMenlo
7+
8+
[47]: https://github.com/rust-num/num-integer/pull/47
9+
110
# Release 0.1.44 (2020-10-29)
211

312
- [The "i128" feature now bypasses compiler probing][35]. The build script

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,9 @@ macro_rules! impl_integer_for_isize {
540540
/// Returns `true` if the number is a multiple of `other`.
541541
#[inline]
542542
fn is_multiple_of(&self, other: &Self) -> bool {
543+
if other.is_zero() {
544+
return self.is_zero();
545+
}
543546
*self % *other == 0
544547
}
545548

@@ -915,6 +918,9 @@ macro_rules! impl_integer_for_usize {
915918
/// Returns `true` if the number is a multiple of `other`.
916919
#[inline]
917920
fn is_multiple_of(&self, other: &Self) -> bool {
921+
if other.is_zero() {
922+
return self.is_zero();
923+
}
918924
*self % *other == 0
919925
}
920926

@@ -1011,9 +1017,14 @@ macro_rules! impl_integer_for_usize {
10111017

10121018
#[test]
10131019
fn test_is_multiple_of() {
1020+
assert!((0 as $T).is_multiple_of(&(0 as $T)));
10141021
assert!((6 as $T).is_multiple_of(&(6 as $T)));
10151022
assert!((6 as $T).is_multiple_of(&(3 as $T)));
10161023
assert!((6 as $T).is_multiple_of(&(1 as $T)));
1024+
1025+
assert!(!(42 as $T).is_multiple_of(&(5 as $T)));
1026+
assert!(!(5 as $T).is_multiple_of(&(3 as $T)));
1027+
assert!(!(42 as $T).is_multiple_of(&(0 as $T)));
10171028
}
10181029

10191030
#[test]

0 commit comments

Comments
 (0)