From: Robert Griesemer Date: Tue, 3 May 2011 00:23:18 +0000 (-0700) Subject: go spec: clarify semantics of integer division X-Git-Tag: weekly.2011-05-22~165 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=bb7eb4002e54720e829cc9e2344252741411ccd0;p=gostls13.git go spec: clarify semantics of integer division Fixes #1764. R=rsc, r, iant, ken2 CC=golang-dev https://golang.org/cl/4431082 --- diff --git a/doc/go_spec.html b/doc/go_spec.html index e8f7894dbc..0a8a598b73 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -2808,15 +2808,18 @@ s += " and good bye" String addition creates a new string by concatenating the operands.

-For integer values, / and % satisfy the following relationship: +For two integer values x and y, the integer quotient +q = x / y and remainder r = x % y satisfy the following +relationships:

-(a / b) * b + a % b == a
+x = q*y + r  and  |r| < |y|
 

-with (a / b) truncated towards zero. +with x / y truncated towards zero +("truncated division").

@@ -2827,6 +2830,20 @@ with (a / b) truncated towards zero.
 -5    -3       1        -2
 
+

+As an exception to this rule, if the dividend x is the most +negative value for the int type of x, the quotient +q = x / -1 is equal to x (and r = 0). +

+ +
+			 x, q
+int8                     -128
+int16                  -32768
+int32             -2147483648
+int64    -9223372036854775808
+
+

If the divisor is zero, a run-time panic occurs. If the dividend is positive and the divisor is a constant power of 2,