Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix is_multiple_of to account for 0 #47

Merged
merged 4 commits into from Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -543,6 +543,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 @@ -885,6 +888,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 @@ -981,9 +987,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)));
WizardOfMenlo marked this conversation as resolved.
Show resolved Hide resolved

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