<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">FYI, here is the original question originating on the WG14 list.<div><br></div><div>Regards,</div><div>Craig<br><div><br><div>Begin forwarded message:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Helvetica" size="3" color="#000000" style="font: 12.0px Helvetica; color: #000000"><b>From: </b></font><font face="Helvetica" size="3" style="font: 12.0px Helvetica">David Svoboda <<a href="mailto:svoboda@cert.org">svoboda@cert.org</a>></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Helvetica" size="3" color="#000000" style="font: 12.0px Helvetica; color: #000000"><b>Date: </b></font><font face="Helvetica" size="3" style="font: 12.0px Helvetica">October 23, 2008 11:32:44 AM MDT</font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Helvetica" size="3" color="#000000" style="font: 12.0px Helvetica; color: #000000"><b>To: </b></font><font face="Helvetica" size="3" style="font: 12.0px Helvetica">"<a href="mailto:sc22wg14@open-std.org">sc22wg14@open-std.org</a>" <<a href="mailto:sc22wg14@open-std.org">sc22wg14@open-std.org</a>></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Helvetica" size="3" color="#000000" style="font: 12.0px Helvetica; color: #000000"><b>Cc: </b></font><font face="Helvetica" size="3" style="font: 12.0px Helvetica">David Svoboda <<a href="mailto:svoboda@cert.org">svoboda@cert.org</a>></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Helvetica" size="3" color="#000000" style="font: 12.0px Helvetica; color: #000000"><b>Subject: </b></font><font face="Helvetica" size="3" style="font: 12.0px Helvetica"><b>(SC22WG14.11572) What is INT_MIN % -1?</b></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><br></div> </div><div>Does the C standard dictate what INT_MIN % -1 should evaluate to?<br><br>The best citation I can find is section 6.5.5, paragraph 6:<br><br> When integers are divided, the result of the / operator is the<br> algebraic quotient with any fractional part discarded.) If the<br> quotient a/b is representable, the expression (a/b)*b + a%b shall<br> equal a.<br><br>So a reasonable interpretation would be that INT_MIN % -1 = 0.<br><br>But I think another reasonable interpration exists. On<br>two's-complement machines, INT_MIN / -1 is mathematically equivalent<br>to INT_MAX + 1, which cannot be represented as a signed<br>int. Consequently the condition in the second statement in the above<br>quotation fails, and so the above quotation leaves INT_MIN % -1 in the<br>realm of undefined behavior.<br><br>On MSVC++, taking the modulo of INT_MIN by -1 yields the value 0. But<br>on gcc/Linux, taking the modulo of INT_MIN by -1 produces a<br>floating-point exception.<br><br><br>So what is going on here? Is there another section of the C standard<br>that defines INT_MIN % -1? Or is one of the above interpretations<br>better than the other? Or are both reasonable, and INT_MIN % -1<br>really is undefined for two's-complement machines?<br><br>Put another way, is MSVC++ non-complaiant with the standard in this<br>facet? Is GCC? Or are they both standards-compliant here?<br><br>-- <br>David Svoboda <<a href="mailto:svoboda@cert.org">svoboda@cert.org</a>><br>Software Security Engineer<br>CERT Coordination Center<br>(412) 268-3965<br></div></blockquote></div><br></div></body></html>