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)?
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
isundefined
. Therefore,mod
, which relies on division, is alsoundefined
.This represents division for positive
X
andD
; it's made up of the integral part and fractional part:Rearranging, you get:
Substituting
0
forD
:Since division by
0
isundefined
: