C语言 “mod”和“remainder”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13683563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:39:58  来源:igfitidea点击:

What's the difference between “mod” and “remainder”?

cmathoperators

提问by songhir

My friend said that there are differences between "mod" and "remainder".

我的朋友说“mod”和“remainder”之间存在差异。

If so, what are those differences in C and C++? Does '%' mean either "mod" or "rem" in C?

如果是这样,C 和 C++ 中的那些区别是什么?'%' 在 C 中是指“mod”还是“rem”?

回答by David Schwartz

There is a difference between modulus and remainder. For example:

模数和余数之间存在差异。例如:

-21mod 4is 3because -21 + 4 x 6is 3.

-21mod43因为-21 + 4 x 63

But -21divided by 4gives -5with a remainder of -1.

但是-21除以4给出-5了剩余-1

For positive values, there is no difference.

对于正值,没有区别。

回答by chux - Reinstate Monica

Does '%' mean either "mod" or "rem" in C?

'%' 在 C 中是指“mod”还是“rem”?

In C, %is the remainder1.

在 C 中,%余数1

..., the result of the /operator is the algebraic quotient with any fractional part discarded ... (This is often called "truncation toward zero".) C11dr §6.5.5 6

The operands of the %operator shall have integer type. C11dr §6.5.5 2

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... C11dr §6.5.5 5

...,/运算符的结果是任何小数部分都被丢弃的代数商 ...(这通常称为“向零截断”。)C11dr §6.5.5 6

运算%符的操作数应为整数类型。C11dr §6.5.5 2

/运算符的结果是第一个操作数除以第二个操作数的商;%运算符的结果是余数... C11dr §6.5.5 5



What's the difference between “mod” and “remainder”?

“mod”和“remainder”有什么区别?

C does not define "mod", such as the integer modulus function used in Euclidean divisionor other modulo. "Euclidean mod" differs from C's a%boperation when ais negative.

C 没有定义“mod”,例如欧几里德除法中使用的整数模函数或其他 moduloa%ba为负时,“欧几里得模”与 C 的运算不同。

 // a % b
 7 %  3 -->  1  
 7 % -3 -->  1  
-7 %  3 --> -1  
-7 % -3 --> -1   

Modulo as Euclidean division

模作为欧几里德除法

 7 modulo  3 -->  1  
 7 modulo -3 -->  1  
-7 modulo  3 -->  2  
-7 modulo -3 -->  2   


Candidate modulo code:

候选模代码:

int modulo_Euclidean(int a, int b) {
  int m = a % b;
  if (m < 0) {
    // m += (b < 0) ? -b : b; // avoid this form: it is UB when b == INT_MIN
    m = (b < 0) ? m - b : m + b;
  }
  return m;
}


Note about floating point: double fmod(double x, double y), even though called "fmod", it is not the same as Euclidean division "mod", but similar to C integer remainder:

关于浮点数的注意事项:double fmod(double x, double y),尽管称为“fmod”,但它与欧几里德除法“mod”不同,但类似于 C 整数余数:

The fmodfunctions compute the floating-point remainder of x/y. C11dr §7.12.10.1 2

这些fmod函数计算 的浮点余数x/y。C11dr §7.12.10.1 2

fmod( 7,  3) -->  1.0  
fmod( 7, -3) -->  1.0  
fmod(-7,  3) --> -1.0  
fmod(-7, -3) --> -1.0   


Disambiguation: C also has a similar named function double modf(double value, double *iptr)which breaks the argument value into integral and fractional parts, each of which has the same type and sign as the argument. This has little to do with the "mod" discussion here except name similarity.

消歧:C 也有一个类似的命名函数double modf(double value, double *iptr),它将参数值分解为整数和小数部分,每个部分与参数具有相同的类型和符号。除了名称相似性之外,这与此处的“mod”讨论几乎没有关系。



1Prior to C99, C's definition of %was still the remainderfrom division, yet then /allowed negative quotients to round down rather than "truncation toward zero". See Why do you get different values for integer division in C89?. Thus with some pre-C99 compilation, %code can act just like the Euclidean division "mod". The above modulo_Euclidean()will work with this alternate old-school remainder too.

1在 C99 之前,C 的定义%仍然是除法的余数,但/允许负商向下舍入而不是“向零截断”。请参阅为什么在 C89 中得到不同的整数除法值?. 因此,通过一些 C99 之前的编译,%代码可以像欧几里得除法“mod”一样工作。以上modulo_Euclidean()也适用于这个替代的老派剩余部分。

回答by user487158

Modulus, in modular arithmetic as you're referring, is the value left over or remaining value after arithmetic division. This is commonly known as remainder. % is formally the remainder operator in C / C++. Example:

在您所指的模算术中,模数是算术除法后剩余的值或剩余值。这通常称为余数。% 在形式上是 C/C++ 中的余数运算符。例子:

7 % 3 = 1  // dividend % divisor = remainder

What's left for discussion is how to treat negative inputs to this % operation. Modern C and C++ produce a signed remainder value for this operation where the sign of the result always matches the dividend inputwithout regard to the sign of the divisor input.

剩下要讨论的是如何处理这个 % 操作的负输入。现代 C 和 C++ 为该运算生成有符号余数值,其中结果的符号始终与被除数输入匹配,而不考虑除数输入的符号。

回答by theEpsilon

In C and C++ and many languages, %is the remainder NOT the modulus operator.

在 C 和 C++ 以及许多语言中,%余数不是模运算符。

For example in the operation -21 / 4the integer part is -5and the decimal part is -.25. The remainder is the fractional part times the divisor, so our remainder is -1. JavaScript uses the remainder operator and confirms this

例如运算-21 / 4中整数部分为-5,小数部分为-.25。余数是小数部分乘以除数,所以我们的余数是-1。JavaScript 使用余数运算符并确认这一点

console.log(-21 % 4 == -1);

The modulus operator is like you had a "clock". Imagine a circle with the values 0, 1, 2, and 3 at the 12 o'clock, 3 o'clock, 6 o'clock, and 9 o'clock positions respectively. Stepping quotient times around the clock clock-wise lands us on the result of our modulus operation, or, in our example with a negative quotient, counter-clockwise, yielding 3.

模数运算符就像你有一个“时钟”。想象一个圆圈,分别在 12 点钟、3 点钟、6 点钟和 9 点钟位置具有值 0、1、2 和 3。顺时针步进商时间使我们得到模数运算的结果,或者,在我们的示例中,逆时针为负商,产生 3。

Note:Modulus is always the same sign as the divisor and remainder the same sign as the quotient. Adding the divisor and the remainder when the remainder at least one is negative yields the modulus.

注意:模数总是与除数同号,余数与商同号。当余数至少为负时,将除数和余数相加得到模数。

回答by shub sharma

In mathematics the result of the modulo operation is the remainder of the Euclidean division. However, other conventions are possible. Computers and calculators have various ways of storing and representing numbers; thus their definition of the modulo operation depends on the programming language and/or the underlying hardware.

在数学中,模运算的结果是欧几里得除法的余数。然而,其他约定也是可能的。计算机和计算器有多种存储和表示数字的方式;因此,他们对模运算的定义取决于编程语言和/或底层硬件。

 7 modulo  3 -->  1  
 7 modulo -3 --> -2 
-7 modulo  3 -->  2  
-7 modulo -3 --> -1