Skip to content

Commit

Permalink
Merge #47
Browse files Browse the repository at this point in the history
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 <giacomofenzi@outlook.com>
Co-authored-by: Josh Stone <cuviper@gmail.com>
  • Loading branch information
3 people committed Apr 29, 2022
2 parents a7d40a1 + dee9a82 commit 42c22de
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -8,7 +8,7 @@ categories = ["algorithms", "science", "no-std"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/rust-num/num-integer"
name = "num-integer"
version = "0.1.44"
version = "0.1.45"
readme = "README.md"
build = "build.rs"
exclude = ["/bors.toml", "/ci/*", "/.github/*"]
Expand Down
9 changes: 9 additions & 0 deletions RELEASES.md
@@ -1,3 +1,12 @@
# Release 0.1.45 (2022-04-29)

- [`Integer::is_multiple_of` now handles a 0 argument without panicking][47]
for primitive integers.

**Contributors**: @cuviper, @WizardOfMenlo

[47]: https://github.com/rust-num/num-integer/pull/47

# Release 0.1.44 (2020-10-29)

- [The "i128" feature now bypasses compiler probing][35]. The build script
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Expand Up @@ -540,6 +540,9 @@ macro_rules! impl_integer_for_isize {
/// Returns `true` if the number is a multiple of `other`.
#[inline]
fn is_multiple_of(&self, other: &Self) -> bool {
if other.is_zero() {
return self.is_zero();
}
*self % *other == 0
}

Expand Down Expand Up @@ -915,6 +918,9 @@ macro_rules! impl_integer_for_usize {
/// Returns `true` if the number is a multiple of `other`.
#[inline]
fn is_multiple_of(&self, other: &Self) -> bool {
if other.is_zero() {
return self.is_zero();
}
*self % *other == 0
}

Expand Down Expand Up @@ -1011,9 +1017,14 @@ macro_rules! impl_integer_for_usize {

#[test]
fn test_is_multiple_of() {
assert!((0 as $T).is_multiple_of(&(0 as $T)));
assert!((6 as $T).is_multiple_of(&(6 as $T)));
assert!((6 as $T).is_multiple_of(&(3 as $T)));
assert!((6 as $T).is_multiple_of(&(1 as $T)));

assert!(!(42 as $T).is_multiple_of(&(5 as $T)));
assert!(!(5 as $T).is_multiple_of(&(3 as $T)));
assert!(!(42 as $T).is_multiple_of(&(0 as $T)));
}

#[test]
Expand Down

0 comments on commit 42c22de

Please sign in to comment.