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

math: MaxUint64 doesn't return correct result but an error? #38723

Closed
IIvyPy opened this issue Apr 28, 2020 · 6 comments
Closed

math: MaxUint64 doesn't return correct result but an error? #38723

IIvyPy opened this issue Apr 28, 2020 · 6 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@IIvyPy
Copy link

IIvyPy commented Apr 28, 2020

What version of Go are you using (go version)?

$ go@1.12

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

$ amd64

What did you do?

I just use the const in math packge: math.MaxUint64

What did you expect to see?

18446744073709551615

What did you see instead?

./main.go:169:29: constant 18446744073709551615 overflows uint

Notice that "1<<64" in "math.MaxUint64 = 1<<64 - 1" may cause overflow, perhas you can use "math.MaxUint64 = ^uint64(0)" instead.

@randall77
Copy link
Contributor

Show us your code. This code works fine:

package main

import "math"

func main() {
	var x uint = math.MaxUint64
	_ = x
}

The definition of MaxUint64 as 1<<64-1 has no issues with overflow. It is a constant expression, which in Go is evaluated with arbitrary precision/range.

@andybons andybons added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Apr 28, 2020
@andybons andybons changed the title math.MaxUint64 doesn't return correct result but an error? math: MaxUint64 doesn't return correct result but an error? Apr 28, 2020
@andybons andybons added this to the Unplanned milestone Apr 28, 2020
@IIvyPy
Copy link
Author

IIvyPy commented Apr 29, 2020

Show us your code. This code works fine:

package main

import "math"

func main() {
	var x uint = math.MaxUint64
	_ = x
}

The definition of MaxUint64 as 1<<64-1 has no issues with overflow. It is a constant expression, which in Go is evaluated with arbitrary precision/range.

Your code works well.
Here is my code.

package main

import "math"
func main() {
 	x := math.MaxUint64
	_ = x
 }

@zhangfannie
Copy link
Contributor

Is this related to untyped constant? The untyped constant math.MaxUint64 cannot infer the type from x that has no type. So it is given the default type int, so it overflows.

@bmkessler
Copy link
Contributor

Yes, the second example is due to the type inference with the untyped constant.

x := math.MaxUint64

infers that x is an int and it overflows.

Also, the first example

var x uint = math.MaxUint64

still overflows on any architecture where uint is 32-bit, e.g. arm.

If you want to assign math.MaxUint64 to a variable you need to store it in a uint64

@randall77
Copy link
Contributor

Closing, this is working as intended.
As @bmkessler says, explicitly type your variable instead of using :=.

@ianlancetaylor
Copy link
Contributor

See also #19621, #23086, #23087, #23163.

@golang golang locked and limited conversation to collaborators Apr 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

7 participants