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

Projects
None yet
4 participants
@dotaheor

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

@gopherbot gopherbot added the Proposal label Jan 14, 2018

@ianlancetaylor

This comment has been minimized.

Show comment
Hide comment
@ianlancetaylor
Contributor

ianlancetaylor commented Jan 14, 2018

@mvdan mvdan changed the title from proposal: the integer division exception rule should be moved to the integer overflow section to 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

This comment has been minimized.

Show comment
Hide comment
@griesemer

griesemer Jan 17, 2018

Contributor

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

Contributor

griesemer commented Jan 17, 2018

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

@griesemer griesemer removed the Proposal label Jan 17, 2018

@gopherbot gopherbot added the Proposal label Jan 17, 2018

@griesemer griesemer modified the milestones: Proposal, Go1.11 Jan 17, 2018

@griesemer griesemer added Documentation and removed Proposal labels Jan 17, 2018

@griesemer

This comment has been minimized.

Show comment
Hide comment
@griesemer

griesemer Jan 17, 2018

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.

Contributor

griesemer commented Jan 17, 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.

See the upcoming CL. Thanks.

@gopherbot

This comment has been minimized.

Show comment
Hide comment
@gopherbot

gopherbot Jan 17, 2018

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

gopherbot commented Jan 17, 2018

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

@gopherbot gopherbot closed this in 18d527b Jan 17, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment