Skip to main content

use_arithmetic/
parity.rs

1/// Returns whether `value` is evenly divisible by two.
2///
3/// # Examples
4///
5/// ```rust
6/// use use_arithmetic::is_even;
7///
8/// assert!(is_even(12));
9/// assert!(!is_even(7));
10/// ```
11#[must_use]
12pub const fn is_even(value: u64) -> bool {
13    value.is_multiple_of(2)
14}
15
16/// Returns whether `value` leaves a remainder of one when divided by two.
17///
18/// # Examples
19///
20/// ```rust
21/// use use_arithmetic::is_odd;
22///
23/// assert!(is_odd(7));
24/// assert!(!is_odd(12));
25/// ```
26#[must_use]
27pub const fn is_odd(value: u64) -> bool {
28    !is_even(value)
29}
30
31#[cfg(test)]
32mod tests {
33    use super::{is_even, is_odd};
34
35    #[test]
36    fn classifies_zero_and_positive_values() {
37        assert!(is_even(0));
38        assert!(is_even(12));
39        assert!(!is_even(7));
40        assert!(is_odd(7));
41        assert!(!is_odd(12));
42    }
43}