ANSI C 或 ISO C 是否指定 -5 % 10 应该是什么?

发布于 2024-09-16 20:43:16 字数 74 浏览 1 评论 0原文

我似乎记得 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹过旳痕迹 2024-09-23 20:43:17

C89,不完全(§3.3.5/6)。它可以是 -5 或 5,因为 -5 / 10 可以返回 0 或 -1(% 是根据涉及 /的线性方程定义的*+):

当整数相除并且除法不精确时,如果两个操作数都为正,则 / 运算符的结果是小于代数商的最大整数和 %< /code> 运算符为正。 如果任一操作数为负/运算符的结果是小于代数商的最大整数还是大于代数商的最小整数执行-定义,与%运算符结果的符号相同。如果商a/b可表示,则表达式(a/b)*b + a%b应等于a


C99,是的(§6.5.5/6),结果必须是-5:

整数相除时,/ 运算符的结果是代数商,舍去小数部分。88) 如果商 a/b< /code> 是可表示的,表达式 (a/b)*b + a%b 应等于 a

88) 这通常称为“向零截断”。


类似地,在 C++98 中,结果是实现定义的 (§5.6/4),遵循 C89 的定义,但提到舍入-零规则是首选,

...如果两个操作数均为非负,则余数为非负;如果不是,则余数的符号是​​实现定义的74)

74) 根据正在进行的 ISO C 修订工作,整数除法的首选算法遵循 ISO Fortran 标准 ISO/IEC 1539:1991 中定义的规则,其中商始终四舍五入为零。

事实上,它成为 C++0x 中的标准规则 (§5.6/4):

... 对于整数操作数,/ 运算符生成代数商,并丢弃任何小数部分;82 ...

82) 这通常称为向零截断。

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 +):

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.

C99, yes (§6.5.5/6), the result must be -5:

When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded.88) If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.

88) This is often called "truncation toward zero".


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,

... If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined74).

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.

and indeed it becomes the standard rule in C++0x (§5.6/4):

... For integral operands the / operator yields the algebraic quotient with any fractional part discarded;82 ...

82) This is often called truncation towards zero.

难得心□动 2024-09-23 20:43:17

为 KennyTM 的答案添加一些细节:如果 C 标准调用某种定义的实现,那么该实现需要来记录它所做的选择。通常这会在编译器或库文档中(手册页、帮助手册、印刷文档、CD 小册子:-)
任何声称符合 C89 或更高版本的实现都必须在某处提供此功能。
尝试寻找这样的文档。以 gcc 为例,它位于 gcc-info 中:

4 C 实现定义的行为

<小时>

需要符合 ISO C 的实施来记录其
每个指定区域的行为选择
“实现定义”。下面列出了所有此类区域,以及
带有 ISO/IEC 9899:1990 和 ISO/IEC 中的章节编号
9899:1999 标准。有些领域仅在一个领域中实现定义
标准版本。

一些选择取决于外部确定的平台 ABI
GCC 遵循的(包括标准字符编码);这些都是
下面列为“由 ABI 确定”。 *注意二进制兼容性:
兼容性和“http://gcc.gnu.org/readings.html”。一些选择
记录在预处理器手册中。 *笔记
实现定义的行为:(cpp)实现定义的行为。
有些选择是由库和操作系统(或其他
编译独立环境时的环境);参考
他们的文档了解详细信息。

  • 菜单:

  • 翻译实现::

  • 环境实现::
  • 标识符实现::
  • 字符实现::
  • 整数实现::
  • 浮点实现::
  • 数组和指针实现::
  • 提示实施::
  • 结构联合枚举和位域实现::
  • 限定符实现::
  • 声明符实现::
  • 语句实现::
  • 预处理指令实现::
  • 库函数实现::
  • 架构实现::
  • 特定于区域设置的行为实现::

To add a little detail to KennyTM's answer: If the C Standards call something implementation defined then that implementation is required to document the choice it makes. Usually this would be in the compiler or library documentation (man page, help manual, printed docs, CD booklet :-)
Any implementation claiming conformance to C89 or later must provide this somewhere.
Try looking for such a document. In the case of gcc for example, this is in the gcc-info:

4 C Implementation-defined behavior


A conforming implementation of ISO C is required to document its
choice of behavior in each of the areas that are designated
"implementation defined". The following lists all such areas, along
with the section numbers from the ISO/IEC 9899:1990 and ISO/IEC
9899:1999 standards. Some areas are only implementation-defined in one
version of the standard.

Some choices depend on the externally determined ABI for the platform
(including standard character encodings) which GCC follows; these are
listed as "determined by ABI" below. *Note Binary Compatibility:
Compatibility, and `http://gcc.gnu.org/readings.html'. Some choices
are documented in the preprocessor manual. *Note
Implementation-defined behavior: (cpp)Implementation-defined behavior.
Some choices are made by the library and operating system (or other
environment when compiling for a freestanding environment); refer to
their documentation for details.

  • Menu:

  • Translation implementation::

  • Environment implementation::
  • Identifiers implementation::
  • Characters implementation::
  • Integers implementation::
  • Floating point implementation::
  • Arrays and pointers implementation::
  • Hints implementation::
  • Structures unions enumerations and bit-fields implementation::
  • Qualifiers implementation::
  • Declarators implementation::
  • Statements implementation::
  • Preprocessing directives implementation::
  • Library functions implementation::
  • Architecture implementation::
  • Locale-specific behavior implementation::
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文