3

I know that division by zero is not allowed in math, but can I use modulo by zero and what answer should I get?

For example

10%0 = ?
5%0 = ?
ST3
  • 8,826
  • 3
  • 68
  • 92
user3394586
  • 191
  • 1
  • 1
  • 6

4 Answers4

10

The standard defines it as "undefined".

In nearly all processors, the modulo is performed by the same functionality as divide. In modern larger processors, it's an instruction (x86 for example). Most often, this instruction will cause a trap when dividing by zero, and this applies whether the code is "intending to use the modulo" or "quotient" part.

It is undefined so that processors and compilers have the freedom to implement what they fancy - e.g. if the processor just returns whatever came in as the input, that's also allowed, or if it causes the entire OS to crash, that's "fine" too by the standard.

In summary, modulo of zero is just as bad as divide by zero.

(Note that typically, floating point divide by zero does NOT trap [by default], and produces a infinity value, except if the value divided is also zero, in which case you get "not a number")

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
7

In C/C++ it is Undefined behaviour, you can get various of results depending on compiler or even different instances of same program.

C11dr §6.5.5

The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined

ST3
  • 8,826
  • 3
  • 68
  • 92
4

The result is UB

C11dr §6.5.5 "The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined."

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 2
    Generally speaking is good to keep in mind that any mathematical expression that doesn't make sense invokes undefined behaviour in both C and C++. Since you can't divide by `0` in the math world, here it is your UB. – user2485710 Mar 08 '14 at 22:26
1

Mathematically speaking you should get infinite, which is the correct answer... programmatically any sane compiler will warn you of division by zero as internally the modulo operator (at least in C/C++) is translated in a division (in most implementations). So the answer to your question is that you would get a floating point exception, in both cases.

jtimz
  • 324
  • 3
  • 14
  • oh but you will, at least libc throws floating point exception as does Microsoft's implementation. – jtimz Mar 08 '14 at 22:15
  • so you say `try {int a = 8 / 0;} catch(...) {}` is a valid code? – ST3 Mar 08 '14 at 22:16
  • On MS C++ compiler, you do get an error: `cannot mod by 0` on compile time – inixsoftware Mar 08 '14 at 22:16
  • @inixsoftware that's because internally most implementations translate the modulo operator as a division... which is why the exception is thrown. – jtimz Mar 08 '14 at 22:17
  • 1
    The standard `try/catch` does not work for processor exceptions (traps). It will work if you use the Microsoft specific "Structured Exception Handling" `__try` and `__except` - but only in Windows systems. – Mats Petersson Mar 08 '14 at 22:20
  • @ST3 well if by valid you mean that it's compilable it is but it will not run correctly and it will produce an exception. – jtimz Mar 08 '14 at 22:20
  • agree, valid is wrong word, I meant that this won't catch an exception. @Mats comment and answer are really good, take a look at them – ST3 Mar 08 '14 at 22:26
  • 3
    @jtimz, I don't agree that mathematically speaking, x % 0 should be infinite. What's your reasoning? – eerorika Mar 08 '14 at 22:28
  • 1
    @user2079303 good point, mathematically speaking `x % 0` should be x. – ST3 Mar 08 '14 at 22:32
  • Well, say you have a%n then the modulo is defined (by Knuth) as r = a -nq = a - n*floor(a/n). Now if n = 0 then we have r to be equal to -inf. That's my reasoning. You can find the formulas and more info [here](http://en.wikipedia.org/wiki/Modulo_operation) – jtimz Mar 08 '14 at 22:38
  • @ST3 Concur that `x % 0` should be `x`. But like other corner cases in math vs. programming, `power(0,0)` comes to mind, there is not a strong consensus on the result. So the spec says "we'll agree to disagree" in other words: UB. – chux - Reinstate Monica Mar 08 '14 at 22:39
  • 1
    @jimz, if `n = 0`, then `r = a - n*floor(a/n)` becomes `r = a - 0*floor(a/0)`. Since, when `n` approaches `0`, `a / n` approaches `inf` (or `-inf` depending on sign of `a`), we have `0 * floor(inf) = 0 * inf` (assuming `floor(inf) = inf`) which is mathematically not defined. If we bend rules a bit and ignored that `0 * inf` isn't defined, and ignored the floor, we could reduce `n` away and get `r = a - a = 0`. Which is incidentally what `fmod` approaches with very small denominator values. So by my reasoning, `x % 0` should not be defined at all, mathematically. – eerorika Mar 08 '14 at 22:58