问题描述
今天在工作中,我与一位同事进行了有趣的讨论.当他发生以下事情时,他感到惊讶:
Today at work I had an interesting discussion with one of my coworkers. He was surprised when he had the following happen to him:
assert(-1 % 10 == -1) //Expecting 9
因此,当他来问我这个问题时,我告诉他很好,这是有道理的.将-1除以10时,您得到0,剩下的-1.但是他的论点是模运算符应该坚持永远是积极的"模型.我做了一点研究,发现他指的模数看起来像这样:
So when he came to ask me about it, I told him "well, that makes sense. When you divide -1 by 10, you get 0 with -1 remaining. His argument however was that the modulus operator is supposed to hold true to the "always positive" model. I did a little research and found that the modulus he was referring to looks like this:
a = n * q + r
a = n * q + r
I 使用的定义似乎是模数的Knuth版本,即:
The definition I was using, however, appears to be the Knuth version of modulus, which is:
r = a-n * q
r = a - n * q
所以,我的问题是,为什么它以FORTRAN标准(以及后来的C标准)结尾而使模数运算符截断为0?在我看来,称其为模数"而不是余数"是不恰当的(在数学上,答案实际上应该是9).这与硬件的划分方式有关吗?
So, my question is why it ended up in the FORTRAN standard (and subsequently the C-standard) to have the modulus operator truncate toward 0? It seems like a misnomer to me to call it "modulus" and not "remainder" (In math, the answer really should be 9). Is this related to how hardware is doing the division?
供参考:
- 有关Modulus的维基百科
- 模数"运算符上的MSDN条目
(是的,我意识到它是针对VS2003的...我目前仍坚持使用它.Sadface) - 模运算符更改
- 不要假设正余数...
- Wikipedia on Modulus
- MSDN entry on the "modulus" operator
(Yes, i realize its for VS2003...I'm stuck with it currently. Sadface) - Modulus operator changes
- Don't assume positive remainder...
推荐答案
这篇关于为什么将运算符%称为“模数"?而不是“剩余"运算符操作员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!