This means when (b - a) is positive, the result of % should also be positive. Thus, the usual calculation would be:Īnd, as mentioned, I want that to work for all x. In other words, my source data runs from a to b and repeats indefinitely, so I want to calculate the remainder of x when divided by b-a, and add that to a to see where it lands.
Now, given an x-coordinate, I want to find out what value it corresponds to when wrapped around. Suppose I have a linearly repeating structure-perhaps a game board that wraps around “Pac-Man style”, or a tiled image, or something sinusoidal.
I started thinking about what circumstances might lead me to try a negative divisor, in order to determine what effect I would want. In my experience, when using the % operator the second number (divisor) has always been positive, and I have always wanted a positive result (remainder) irrespective of what sign the first number (dividend) has. So if I were to suggest something with respect to the ergonomics of integer remainders, it would be to add euclidean division and remainder operations.Ĭould you please clarify, to make sure we have the correct background for a potential proposal, is it floored or euclidean?Īll right, I have given this question serious consideration. Neither the % operator nor the quotientAndRemainder(dividingBy:) do what I need in these cases, because they can produce negative remainders. Rust implemented special functions for euclidean division and remainder and it might be worthwhile to refer to the RFC for that change which gives some more rationale.Īlso, from the related recent post I mentioned above: To my knowledge, Swift doesn't use any of those two definitions. It is argued that the E- and F-definitions are superior to all other ones in regularity and useful mathematical properties and hence deserve serious consideration as the standard convention at the applications and language level. There is a paper whose abstract states that (That said, I tend to agree that such a breaking change now is probably too dangerous.) Everyone does it differently, and I suspect that most people don't have any strong expectations about what % should do with negative numbers anyway. There is no "standard" behaviour for the modulo operator when it comes to negative numbers, as the table on the Wikipedia page illustrates. The current behavior of % matches the overwhelming majority of programming languages.
Sorry, to revive this thread, but it was just brought to my attention through the "even and odd integers" thread. It might well be possible that "%" does more harm than good, and I actually don't see a strong connection to "/" - but imho the evolution process is to tiring for breaking changes like redefining an operator, so I'd strongly prefer to keep this separated from the addition of needed functionality. The square root, for example, might even be used more often than mod, but it does not seem common to have an operator for it (and Swift even decided to make it a method, instead of the common sqrt(doubleValue). Operators save some keystrokes, but they are also less discoverable than methods.
#C programming modulus operator floating point exception mod
Operators are somewhat more privileged than regular functions, so why should a less useful algorithm deserve what mod isn't granted?īut for me, "%" has no strong connection to remainder or modulo - which "mod" has.Īs operators in Swift can't contain regular characters, methods on the various integer types are my preferred choice, and I don't care much what "%" does. Afaics, no one here questions that either of the two missing modulo variants is more useful than %, so It may feel "unfair" not to have an operator for it: