Why is X % 0 an invalid expression?
I always thought X % 0 should equal X. Since you can't divide by zero, shouldn't the answer naturally be the remainder, X (everything left over)?
Why is X % 0 an invalid expression?
I always thought X % 0 should equal X. Since you can't divide by zero, shouldn't the answer naturally be the remainder, X (everything left over)?
The C++ Standard(2003) says in §5.6/4,
[...] If the second operand of / or % is zero the behavior is undefined; [...]
That is, following expressions invoke undefined-behavior(UB):
X / 0; //UB
X % 0; //UB
Note also that -5 % 2 is NOT equal to -(5 % 2) (as Petar seems to suggest in his comment to his answer). It's implementation-defined. The spec says (§5.6/4),
[...] If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined.
a/b + a%b is a for all a and b where the quotient is defined (the behaviour is undefined otherwise). The change happened in C99 and C++11 (maybe already in C++03 with TR1, don't know). Would be nice, if you could also tag the question as C, as they are the same in this respect (it was a C question which duplicated this).INT_MIN % -1 was defined, although it throws an exception on many platforms. In C11, x%y is defined only if x/y is, and it was never safe to assume INT_MIN % -1 to evaluate.This answer is not for the mathematician. This answer attempts to give motivation (at the cost of mathematical precision).
Mathematicians: See here.
Programmers: Remember that division by 0 is undefined. Therefore, mod, which relies on division, is also undefined.
This represents division for positive X and D; it's made up of the integral part and fractional part:
(X / D) = integer + fraction
= floor(X / D) + (X % D) / D
Rearranging, you get:
(X % D) = D * (X / D) - D * floor(X / D)
Substituting 0 for D:
(X % 0) = 0 * (X / 0) - 0 * floor(X / 0)
Since division by 0 is undefined:
(X % 0) = 0 * undefined - 0 * floor(undefined)
= undefined - undefined
= undefined
(X % 0) = 0 * (w/e) and just call it zero?0 * (w/e) isn't always 0. If w/e is a real number (which includes integers), then it's 0. If not, regular multiplication doesn't give us an answer, i.e., the answer is undefined.X % D is by definition a number 0 <= R < D, such that there exists Q so that
X = D*Q + R
So if D = 0, no such number can exists (because 0 <= R < 0)
x % y is implementation defined if x < 0. -5 % 2 happens to be -1 on my system.X = D*Q + R works for any Q when D = 0, with X = R as the OP wanted. It's the 0 <= R < 0 that's impossible to satisfy. Your answer seems to imply that it's the other way round, though I might just be reading it wrong.-5 % 2 is NOT -(5 % 2) in fact. It's implementation-defined. The spec says, If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-definedAnother way that might be conceptually easy to understand the issue:
Ignoring for the moment the issue of argument sign, a % b could easily be re-written as a - ((a / b) * b). The expression a / b is undefined if b is zero, so in that case the overall expression must be too.
In the end, modulus is effectively a divisive operation, so if a / b is undefined, it's not unreasonable to expect a % b to be as well.
X % Y gives a result in the integer [ 0, Y ) range. X % 0 would have to give a result greater or equal to zero, and less than zero.
x % y is implementation defined if x < 0. -5 % 2 happens to be -1 on my system.Erm... I think the mathematical rationalisations here are a bit iffy given that |x mod y| <= |y| and it therefore makes perfect sense to use the squeeze theorem to ‘patch it up’ by defining x mod 0 = 0.
It's true that on your approach to y = 0 you'll encounter more and more discontinuities, infinitely many in fact, but they get smaller and smaller and the question of whether it's continuous at y = 0 is different from whether it should have a value there anyway.
The only true answer here is ‘because the standard says so’.