无论是否ANSI C或ISO C指定应该是什么-5%10? [英] Does either ANSI C or ISO C specify what -5 % 10 should be?
问题描述
我似乎记得ANSI C没有指明应返回什么样的价值,当一个模运算符的操作数是负的(只是它应该是一致的)。做了以后得到指定,或者是它总是指定,我记得正确?
I seem to remember that ANSI C didn't specify what value should be returned when either operand of a modulo operator is negative (just that it should be consistent). Did it get specified later, or was it always specified and I am remembering incorrectly?
推荐答案
C89,不是完全(教派; 3.3.5 / 6)。它可以是-5或5,因为-5 / 10可以返回0或-1(%
在涉及线性方程来定义的 /
, *
和 +
)
C89, not totally (§3.3.5/6). It can be either -5 or 5, because -5 / 10 can return 0 or -1 (%
is defined in terms of a linear equation involving /
, *
and +
):
在整数分割划分不准确,如果两个操作数都是正的 /
运营商是最大的整数小于代数商和结果的结果在%
运营商是积极的。 如果操作数是负的 /
运算符的结果是否大于代数商或最小整数比代数较大者的最大整数商数为实现定义后,因为是%
运营商的结果的符号。如果商 A / B
重新presentable,前pression (A / B)* B + A%B
应等于 A
。
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 quotienta/b
is representable, the expression(a/b)*b + a%b
shall equala
.
C99,是(宗派; 6.5.5 / 6),其结果必然是-5:
C99, yes (§6.5.5/6), the result must be -5:
在整数分,在 /
运算符的结果是代数商丢弃的小数部分。 88)如果商 A / b
重新presentable,前pression (A / b)* b + A%b
应等于 A
。
When integers are divided, the result of the
/
operator is the algebraic quotient with any fractional part discarded.88) If the quotienta/b
is representable, the expression(a/b)*b + a%b
shall equala
.
<子> 88)这通常被称为截断向零。子>
同样,在C ++ 98的结果是实现定义(教派; 5.6 / 4),继C89的定义,但提到了圆向零规则是preferred,
Similarly, in C++98 the result is implementation defined (§5.6/4), following C89's definition, but mentions that the round-towards-zero rule is preferred,
...如果两个操作数都是非负则剩余部分非负;如果不是,其余的符号是实现定义 74)
... If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined74).
<分> 74)据工作向ISO C,对于整数除法的preferred算法的修订正在进行遵循在ISO Fortran标准,ISO / IEC 1539定义的规则: 1991年,在该商总是朝向零圆形。子>
74) According to work underway toward the revision of ISO C, the preferred algorithm for integer division follows the rules defined in the ISO Fortran standard, ISO/IEC 1539:1991, in which the quotient is always rounded toward zero.
而事实上它成为C ++ 0x中的标准规则(教派; 5.6 / 4):
and indeed it becomes the standard rule in C++0x (§5.6/4):
...对于整体操作数 /
运营商产生丢弃的小数部分代数商; 82 ...
... For integral operands the
/
operator yields the algebraic quotient with any fractional part discarded;82 ...
<子> 82),这通常被称为截断向零。
这篇关于无论是否ANSI C或ISO C指定应该是什么-5%10?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!