Though Swift provide the remainder
operator(%), however, we cannot directly this operator to calculate the negative numbers modulo/modulus operation. Here is the official doc:
The remainder operator (%) is also known as a modulo operator in other languages. However, its behavior in Swift for negative numbers means that, strictly speaking, it’s a remainder rather than a modulo operation.
Let’s check some test code:
/*
To determine the answer for a % b, the % operator calculates the following equation and returns remainder as its output:
a = (b x some multiplier) + remainder
where some multiplier is the largest number of multiples of b that will fit inside a.
*/
// 9 = (4 x 2) + 1
9 % 4 // equals 1
// -9 = (4 x -2) + -1
-9 % 4 // equals -1
We can found that -9 % 4
return -1
, rather than 3
. So Swift remainder operation (%) is actually a dividend sign rather than modulo. We have to define our own functions for getting correct modulo:
func mod(_ a: Int, _ n: Int) -> Int {
precondition(n > 0, "modulus must be positive")
let r = a % n
return r >= 0 ? r : r + n
}
Comments
Join the discussion for this article at here . Our comments is using Github Issues. All of posted comments will display at this page instantly.