什么是用C模运算规则? [英] What are the rules for modular arithmetic in C?

查看:179
本文介绍了什么是用C模运算规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在前面班,我被教导说, N%Ð= R ,并把它作为 N =ð* Q + R ,其中 D 是除数,是商,而 - [R 是(注意余数不能为负)的余数。

In earlier classes, I was taught that n % d = r and to think about it as n = d*q + r, where d is the divisor, q is the quotient, and r is the remainder (noting that the remainder can never be negative).

因此​​,例如, -111mod11 10 ,因为 -111 = -11 * -11 + 10 (而不是 -111 = -11 * 10 -1 ,看到怎么说会给我们一个负余)。

So for instance, -111 mod 11 is 10, because -111 = -11*-11 + 10 (as opposed to -111 = -11*10 -1, seeing as how that would give us a negative remainder).

然而,印刷的 -111%11 1 是结果,而不是<$时的结果C $ C> 10 。为什么?这不是技术上的错误?

However, when printing the results of -111 % 11, -1 is the result and not 10. Why? Isn't this technically wrong?

推荐答案

答案很简单:

标准保证(A / B)* B + A%B 等于 A

在C99,除法运算的结果 / 将截断向零。 运算符的结果是肯定的,在这种情况下, 1

In C99, the result of division / will truncated toward zero. The result of % operator will be certain, in this case, -1.

在C89,除法运算的结果 / 可截断负的操作数两种方式。因此,运算符的结果是机器相关的为好。

In C89, the result of division / can be truncated either way for negative operands. So the result of % operator is machine-dependent as well.

龙答:

从C99 6.5.5

From C99 6.5.5

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.

6当整数划分的/操作的结果是代数商与任何
  小数部分丢弃。如果商A / B重新presentable,前pression
  (A / B)* B + A%B应等于一个;否则,这两个A / B的行为和%b为
  不确定的。

6 When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a; otherwise, the behavior of both a/b and a%b is undefined.

和在同一页脚注解释如何 / 的作品,它说:

And the footnote on the same page to explain how / works, it says:

这通常被称为''截断向零'。

This is often called ‘‘truncation toward zero’’.

根据此规则, -111 / 11 只能是 -10 ,而不是1自(A / b)* b + A%b 必须等于 A ,我们有 - 111%11 1

According to this rule, -111 / 11 can only be -10, not 1. Since (a/b)*b + a%b must be equal to a, we have -111 % 11 is -1.

不过,K&安培; R第2.5章给出了不同的答案:

However, K&R Chapter 2.5 gives a different answer:

对于截断/方向,结果为%的符号是依赖于机器的负的操作数,上溢或下溢采取的行动。

The direction of truncation for / and the sign of the result for % are machine-dependent for negative operands, as is the action taken on overflow or underflow.

根据这一点,无论是 1 10 可以是一个合法的结果。

According to this, either -1 or 10 can be a legal result.

究其原因,C89 3.3.5:

The reason is in C89 3.3.5:

当整数分割和分割是不精确的,如果这两个操作数是正的/操作的结果比代数商较少的最大整数并%操作的结果是肯定的。如果操作数是否定的,则/操作的结果是否大于代数商或最小整数比代数商更大更少的最大整数是实现定义,因为是%运算的结果的符号。如果商A / B重新presentable,前pression(A / B)* B + A%B应等于一。

When integers are divided and the division is inexact, if both operands are positive the result of the / operator is the largest integer less than the algebraic quotient and the result of the % operator is positive. If either operand is negative, whether the result of the / operator is the largest integer less than the algebraic quotient or the smallest integer greater than the algebraic quotient is implementation-defined, as is the sign of the result of the % operator. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a .

原来是从C89更改C99。

It turns out to be a change from C89 to C99.

C99 6.5.5基本原理提供了一些历史原因:

C99 Rationale 6.5.5 provides some historical reasons:

在C89,涉及负面操作数可以在一个实现定义的方式向上或向下四舍五入整数的除法;目的是避免在运行时code招致开销检查特殊情况和实施的具体行为。用Fortran,然而,其结果将总是向零截断,与架空似乎是可以接受的数字编程社区。因此,C99现在需要类似的行为,这将有利于$ C $的c移植从的Fortran到C.表在本文件的§7.20.6.2示出所需的语义。

In C89, division of integers involving negative operands could round upward or downward in an implementation-defined manner; the intent was to avoid incurring overhead in run-time code to check for special cases and enforce specific behavior. In Fortran, however, the result will always truncate toward zero, and the overhead seems to be acceptable to the numeric programming community. Therefore, C99 now requires similar behavior, which should facilitate porting of code from Fortran to C. The table in §7.20.6.2 of this document illustrates the required semantics.

和这里的表§7.20.6.2:

And here's the table in §7.20.6.2:

numer denom quot rem
 7      3    2    1
–7      3   –2   –1
 7     –3   –2    1
–7     –3    2   –1

这篇关于什么是用C模运算规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆