You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The above content is in the "Integer operators" section,
but I think it should be moved to the "Integer overflow" section.
And it is not an exception. It is just a normal overflow,
the "wrap around" behavior is the same as other overflows.
I think it can just be shown as one line example to show how "wrap around" works.
The benefit of the proposal is to make people know int8( -128)/int(-1) is exactly an overflow.
The text was updated successfully, but these errors were encountered:
mvdan
changed the title
proposal: the integer division exception rule should be moved to the integer overflow section
proposal: spec: the integer division exception rule should be moved to the integer overflow section
Jan 14, 2018
@dotaheor After considering the suggested change, I am proposing a compromise: As currently formulated, the case minint / -1 is indeed an exception to the specified rule which states that q = x / y, rounded towards 0. By that rule alone, minint / -1 should be -minint. Since we explicitly give the rules for integer division w/o correction for overflow, and since there's exactly only case for each integer type only, it is easier to explain this as an exception (rather than making it complicated for all divisions by bringing in overflow).
That said, I take your point that the underlying cause is integer overflow. I added a respective phrase to the section and I also mentioned that integer division may overflow in the overflow section.
In Go spec:
The above content is in the "Integer operators" section,
but I think it should be moved to the "Integer overflow" section.
And it is not an exception. It is just a normal overflow,
the "wrap around" behavior is the same as other overflows.
I think it can just be shown as one line example to show how "wrap around" works.
The benefit of the proposal is to make people know
int8( -128)/int(-1)
is exactly an overflow.The text was updated successfully, but these errors were encountered: