From bb7eb4002e54720e829cc9e2344252741411ccd0 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 2 May 2011 17:23:18 -0700 Subject: [PATCH] go spec: clarify semantics of integer division Fixes #1764. R=rsc, r, iant, ken2 CC=golang-dev https://golang.org/cl/4431082 --- doc/go_spec.html | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) 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, -- 2.50.0