在一个C程序中,我尝试了以下操作(只是为了检查行为)

 x = 5 % (-3);
 y = (-5) % (3);
 z = (-5) % (-3); 

printf("%d ,%d ,%d", x, y, z); 

在gcc中输出为(2,-2,-2)我以为每次都会有积极的结果。模量可以是负的吗?有人能解释一下这种行为吗?


当前回答

根据C99标准,第6.5.5节 乘法运算符,需要以下条件:

(a / b) * b + a % b = a

结论

余数运算结果的符号 到C99,和红利是一样的。

让我们看一些例子(除数/除数):

只有股息是负的

(-3 / 2) * 2  +  -3 % 2 = -3

(-3 / 2) * 2 = -2

(-3 % 2) must be -1

当只有除数为负时

(3 / -2) * -2  +  3 % -2 = 3

(3 / -2) * -2 = 2

(3 % -2) must be 1

除数和被除数都为负

(-3 / -2) * -2  +  -3 % -2 = -3

(-3 / -2) * -2 = -2

(-3 % -2) must be -1

6.5.5 Multiplicative operators Syntax multiplicative-expression: cast-expression multiplicative-expression * cast-expression multiplicative-expression / cast-expression multiplicative-expression % cast-expression Constraints Each of the operands shall have arithmetic type. The operands of the % operator shall have integer type. Semantics The usual arithmetic conversions are performed on the operands. The result of the binary * operator is the product of the operands. 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. When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded [1]. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a. [1]: This is often called "truncation toward zero".

其他回答

根据C99标准,第6.5.5节 乘法运算符,需要以下条件:

(a / b) * b + a % b = a

结论

余数运算结果的符号 到C99,和红利是一样的。

让我们看一些例子(除数/除数):

只有股息是负的

(-3 / 2) * 2  +  -3 % 2 = -3

(-3 / 2) * 2 = -2

(-3 % 2) must be -1

当只有除数为负时

(3 / -2) * -2  +  3 % -2 = 3

(3 / -2) * -2 = 2

(3 % -2) must be 1

除数和被除数都为负

(-3 / -2) * -2  +  -3 % -2 = -3

(-3 / -2) * -2 = -2

(-3 % -2) must be -1

6.5.5 Multiplicative operators Syntax multiplicative-expression: cast-expression multiplicative-expression * cast-expression multiplicative-expression / cast-expression multiplicative-expression % cast-expression Constraints Each of the operands shall have arithmetic type. The operands of the % operator shall have integer type. Semantics The usual arithmetic conversions are performed on the operands. The result of the binary * operator is the product of the operands. 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. When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded [1]. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a. [1]: This is often called "truncation toward zero".

模运算的结果取决于分子的符号,因此y和z都是-2

这是参考资料

http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_14.html

整数的除法 介绍整数除法的函数。 这些函数在GNU C库中是多余的,因为在GNU C中 '/'运算符总是四舍五入到零。但是在其他C中 实现中,'/'可以用不同的负参数四舍五入。 Div和ldiv很有用,因为它们指定了如何舍入 商:趋于零。余数的符号和 分子。

C99要求当a/b是可表示的时:

(a/b) * b + a%b等于a

从逻辑上讲,这是有道理的。对吧?

让我们看看这会导致什么:


例A. 5/(-3) = -1

=> (-1) * (-3) + 5%(-3) = 5

这只能在5%(-3)= 2时发生。


例b (-5)/3 = -1

=> (-1) * 3 + (-5)%3 = -5

只有当(-5)%3为-2时才会发生这种情况

我认为没有必要检查数字是否为负。

求正模的一个简单函数是这个-

编辑:假设N > 0和N + N - 1 <= INT_MAX

int modulo(int x,int N){
    return (x % N + N) %N;
}

这对x的正负都成立。

原始p.s.:也正如@chux指出的那样,如果你的x和N可能分别达到INT_MAX-1和INT_MAX,只需将int替换为long long int。

如果它们也越过了long long的限制(即在LLONG_MAX附近),那么你应该分别处理正的和负的情况,在这里的其他答案中描述。

其他答案已经在C99或更高版本中解释过,涉及负操作数的整数除法总是截断为零。

注意,在C89中,结果向上舍入还是向下舍入是由实现定义的。因为(a/b) * b + a%b在所有标准中都等于a,包含负操作数的%的结果也是在C89中实现定义的。