Even and Odd Integers

Would be interesting to see how many of the x % 2 == 1 are behaving as expected if the use case is generalized to handle negative xs.

People need to learn about the common pitfall of using % without being aware of the (at least three) different variants and which one is implemented in the language (see eg Swift’s vs Python’s %, using negative lhs).

(I don’t have a strong opinion about whether isEven should be added or not, but I do think Swift’s std lib should provide the alternative of flooring division quotient and remainder, since Swift’s % (truncating) is practically useless for negative lhs.)

2 Likes