Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: spec: the integer division exception rule should be moved to the integer overflow section #23443

Closed
dotaheor opened this issue Jan 14, 2018 · 4 comments

Comments

@dotaheor
Copy link

dotaheor commented Jan 14, 2018

In Go spec:

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.

@gopherbot gopherbot added this to the Proposal milestone Jan 14, 2018
@ianlancetaylor
Copy link
Contributor

CC @griesemer

@mvdan 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
@griesemer griesemer self-assigned this Jan 17, 2018
@griesemer
Copy link
Contributor

Fair enough. I don't think this needs to be a proposal. Thanks for the suggestion.

@griesemer
Copy link
Contributor

@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.

See the upcoming CL. Thanks.

@gopherbot
Copy link

Change https://golang.org/cl/87975 mentions this issue: spec: mention that special case for integer division is due to overflow

@golang golang locked and limited conversation to collaborators Jan 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants