Movatterモバイル変換


[0]ホーム

URL:


This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 119a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-12-20


614. Results of integer/ and%

Section:7.6.5  [expr.mul]    Status:CD1    Submitter:Gabriel Dos Reis    Date:15 January 2007

[Voted into the WP at the September, 2008 meeting as part ofpaper N2757.]

The current Standard leaves it implementation-defined whetherinteger division rounds the result toward 0 or toward negativeinfinity and thus whether the result of% may be negative.C99, apparently reflecting (nearly?) unanimous hardware practice, hasadopted the rule that integer division rounds toward 0, thus requiringthat the result of-1 % 5 be-1. Should the C++Standard follow suit?

On a related note, doesINT_MIN % -1 invoke undefinedbehavior? The% operator is defined in terms of the/ operator, andINT_MIN / -1 overflows, which byClause 7 [expr] paragraph 5 causes undefined behavior;however, that is not the “result” of the%operation, so it's not clear. The wording of 7.6.5 [expr.mul] paragraph 4appears to allow% to cause undefined behavioronly when the second operand is 0.

Proposed resolution (August, 2008):

Change 7.6.5 [expr.mul] paragraph 4 as follows:

The binary/ operator yields the quotient, and thebinary% operator yields the remainder from the divisionof the first expression by the second. If the second operand of/ or% is zero the behavior is undefined;otherwise(a/b)*b + a%b is equal toa. If bothoperands are nonnegative then the remainder is nonnegative; ifnot, the sign of the remainder isimplementation-defined. [Footnote: According to workunderway toward the revision of ISO C, the preferred algorithmfor integer division follows the rules defined in the ISO Fortranstandard, ISO/IEC 1539:1991, in which the quotient is alwaysrounded toward zero. —end footnote]. Forintegral operands, the/ operator yields thealgebraic quotient with any fractional part discarded;[Footnote: This is often called “truncation towardszero.” —end footnote] if the quotienta/b is representable in the type of the result,(a/b)*b + a%b is equal toa.

[Drafting note: see C99 6.5.5 paragraph 6.]




[8]ページ先頭

©2009-2026 Movatter.jp