Skip to content

Commit 141ed79

Browse files
committed
Simplify proposed solution section now that implementation is available
1 parent 457633a commit 141ed79

File tree

1 file changed

+5
-29
lines changed

1 file changed

+5
-29
lines changed

proposals/nnnn-binaryinteger-iseven-isodd-ismultiple.md

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Testing the parity of integers is also relatively common in sample code and educ
7979
This functionality will also eliminate the need to use the remainder operator or bitwise AND when querying the divisibility of an integer.
8080

8181
**Correctness:** It isn't [uncommon](https://github.com/apple/swift/blob/master/stdlib/public/core/RangeReplaceableCollection.swift#L1090) to see tests for oddness written as `value % 2 == 1` in Swift, but this is incorrect for negative odd values. The semantics of the `%` operator vary between programming languages, such as Ruby and Python, which can be surprising.
82+
8283
```
8384
// Swift:
8485
7 % 2 == 1 // true
@@ -106,37 +107,12 @@ Add two computed properties, `isEven` and `isOdd`, and a function `isMultiple` t
106107
// On protocol BinaryInteger
107108

108109
@_transparent
109-
/// A Boolean value indicating whether this value is even.
110-
///
111-
/// An integer is even if it is a multiple of two.
112-
public var isEven: Bool {
113-
return _lowWord % 2 == 0
114-
}
110+
public var isEven: Bool { return _lowWord % 2 == 0 }
115111

116112
@_transparent
117-
/// A Boolean value indicating whether this value is odd.
118-
///
119-
/// An integer is odd if it is not a multiple of two.
120-
public var isOdd: Bool {
121-
return !isEven
122-
}
123-
124-
@inlinable
125-
/// Returns a Boolean value that indicates whether the integer is a
126-
/// multiple of another integer.
127-
///
128-
/// For two integers a and b, b is a multiple of a if b = na for some integer n.
129-
///
130-
/// - Note: 0 is a multiple of every integer.
131-
///
132-
/// - Parameter divisor: The integer to test.
133-
/// - Returns: `true` if `self` is a multiple of `divisor`;
134-
/// otherwise, `false`.
135-
public func isMultiple(of divisor: Self) -> Bool {
136-
guard self != 0 else { return true }
137-
guard divisor != 0 else { return false }
138-
return self % divisor == 0
139-
}
113+
public var isOdd: Bool { return !isEven }
114+
115+
func isMultiple(of other: Self) -> Bool
140116
```
141117

142118
## Detailed design

0 commit comments

Comments
 (0)