Why is 0 mod 0 an error?

断了今生、忘了曾经 提交于 2019-12-10 19:03:23

问题


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?


回答1:


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




回答2:


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.




回答3:


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.)



来源:https://stackoverflow.com/questions/19371340/why-is-0-mod-0-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!