SE-0225: Adding isEven, isOdd, isMultiple to BinaryInteger

Since the % operator has different semantics in different languages (see eg Swift's vs Python's % when lhs is negative) and the common pitfall of testing for oddness by writing x % 2 == 1 rather than x % 2 != 0 is common enough to appear in several learning-Swift-resources as well as in Swift's std lib docs, I think it's clear that people does not quickly understand the semantics of % (in language L) correctly, so implementing this functionality correctly is not trivial, and neither is quickly understanding the implications of eg x % 2 == 1. Will it be true for x = -3?

That depends on what language we're using, In Python:
(-3 % 2 == 1) == True.
But in Swift:
(-3 % 2 == 1) == false.

And stack overflow is full of x % 2 == 1, for example here.

Thus I'm mildly in favor of adding the proposed functions.

(I would be more in favor of actually removing %, replacing it with a more complete/proper set of remainder functions, accompanying truncated, floored and euclidean division. Though I realize removing % will probably never happen.)

8 Likes