4

If I type:

int main() { return 0 % 0; }

I get back an error:

error C2124: divide or mod by zero

What is the reason behind this? Isn't the answer zero?

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
user541686
  • 205,094
  • 128
  • 528
  • 886
  • 6
    Why do you think it would be 0? – David G Oct 15 '13 at 00:18
  • @0x499602D2: Because 0 % anything is zero... – user541686 Oct 15 '13 at 00:19
  • 8
    Anything % 0 is undefined. – David G Oct 15 '13 at 00:20
  • @0x499602D2: Well, division is undefined because 0 × N = 0 has an infinite number of solutions for N. But doesn't 0 × N + M = 0 has only *one* solution for M, which is zero, don't you agree? – user541686 Oct 15 '13 at 00:21
  • I do agree, but I'm not sure I understand the specifics for why this particular expression is undefined. – David G Oct 15 '13 at 00:23
  • 3
    Isn't this is more of a math question rather than a programming one? – user2802841 Oct 15 '13 at 00:23
  • @user2802841: I'm not sure, is it generally undefined in math too? I only came across it when my code crashed, I don't remember ever having seen this in any math book before. – user541686 Oct 15 '13 at 00:24
  • 3
    Modulo is the remainder of the division, simple as that. If no division is possible, the modulo isn't either. Your pseudo alternative to the modulo function doesn't apply to what actually happens. – Havenard Oct 15 '13 at 00:24
  • 1
    @Havenard: With regard to unsigned integers, defining `x%0 == 0` would constitute an exception to the principle that `x%y < y`. With regard to floating-point values, I think `fmod(x,0)` should have been defined as zero, given that, mathematically speaking, for any x, the limit as (y->0) of fmod(x,y) is zero. – supercat Oct 30 '13 at 22:34
  • 1
    Besides, although `0/0` is undefined because there is no unique value `x` for which `0*x==0`, there is exactly one possible value of y such that `0*x+y == 0`. – supercat Oct 30 '13 at 22:42
  • Not sure what you're saying but a modulo returning 0 means the remainder of the division is 0. Its impossible to affirm that the remainder of a division by 0 is 0 if a division by 0 is mathematically undefined to begin with. I understand for some applications it is convenient to think that the result of a modulo, or even a division by 0, is 0, but that is not correct. – Havenard Oct 30 '13 at 22:47
  • Possible duplicate of [How to divide by zero without error](http://stackoverflow.com/questions/17001417/how-to-divide-by-zero-without-error) – Gnemlock Jan 20 '17 at 07:02
  • For reference regarding the mathematics behind the operation: https://math.stackexchange.com/questions/516251/why-is-n-mod-0-undefined – Andrew Oct 04 '17 at 21:44
  • @Andrew: Wait just kidding, that's n % 0 not 0 % 0... – user541686 Oct 04 '17 at 22:25

3 Answers3

13

In mathematics, x mod 0 is undefined, hence the error.

David G
  • 94,763
  • 41
  • 167
  • 253
  • Okay I admit I didn't know that. (I'd never even thought about it before.) Guess I'll go ask on Math.SE... thanks. – user541686 Oct 15 '13 at 00:26
  • 8
    0x499602D2, you are too categorical. One convention is that x (mod 0) is x. The rationale is as follows: In modular arithmetic, z ≡ x (mod y) means that there is some integer k such that z - x = k * y. If y = 0, then we get z - x = 0 or z = x. – Peter John Acklam Apr 03 '15 at 18:56
5

From C++ standard, section 5.5:

If during the evaluation of an expression the result is not mathematically defined or not in the range of representable mathematical values for its type, the behavior is undefined. [...] Treatment of division by zero, forming a remainder using a zero divider, and all floating point exceptions vary among machines, and is usually adjustable by a library function.

Since remainder of a division by zero is mathematically undefined regardless of the number being divided, the answer is undefined according to the C++ standard.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

The mod function is effectively the same as the integer division function, except that it gives you the remainder, rather than the quotient. You can't divide by zero...

(BTW, as an aside, 0/0 is not even infinity, it's indeterminate.)

jwismar
  • 12,164
  • 3
  • 32
  • 44