英文:
Different output from the same Go program
问题
这是我的Go代码:http://play.golang.org/p/CDUagFZ-rk
package main
import "fmt"
func main() {
var max int = 0
for i := 0; i < 1000000; i++ {
var len int = GetCollatzSeqLen(i)
if len > max {
max = len
}
}
fmt.Println(max)
}
func GetCollatzSeqLen(n int) int {
var len int = 1
for n > 1 {
len++
if n%2 == 0 {
n = n / 2
} else {
n = 3*n + 1
}
}
return len
}
在我的本地机器上运行程序时,输出为525。当我在Go Playground上运行时,输出为476。
我想知道有什么不同之处。
英文:
Here is my Go code: http://play.golang.org/p/CDUagFZ-rk
package main
import "fmt"
func main() {
var max int = 0
for i := 0; i < 1000000; i++ {
var len int = GetCollatzSeqLen(i)
if len > max {
max = len
}
}
fmt.Println(max)
}
func GetCollatzSeqLen(n int) int {
var len int = 1
for n > 1 {
len++
if n%2 == 0 {
n = n / 2
} else {
n = 3*n + 1
}
}
return len
}
On my local machine, when I run the program, I get 525 as the output. When I run it on the Go Playground, the output is 476.
I am wondering what's different.
答案1
得分: 5
这是一个关于Go语言中整数类型的问题。在Go语言中,int
类型的大小是与具体实现相关的,可以是32位或64位。为了保持一致的结果,建议使用int64
类型。下面是一个示例代码:
package main
import "fmt"
func main() {
var max int64 = 0
for i := int64(0); i < 1000000; i++ {
var len int64 = GetCollatzSeqLen(i)
if len > max {
max = len
}
}
fmt.Println(max)
}
func GetCollatzSeqLen(n int64) int64 {
var len int64 = 1
for n > 1 {
len++
if n%2 == 0 {
n = n / 2
} else {
n = 3*n + 1
}
}
return len
}
输出结果为:
525
你还可以通过运行下面的程序来查看int
类型的具体实现大小:
package main
import (
"fmt"
"runtime"
"strconv"
)
func main() {
fmt.Println(
"For "+runtime.GOARCH+" the implementation-specific size of int is",
strconv.IntSize, "bits.",
)
}
输出结果为:
For amd64 the implementation-specific size of int is 64 bits.
希望对你有帮助!
英文:
It's because of the implementation-specific size of int
, 32 or 64 bits. Use int64
for consistent results. For example,
package main
import "fmt"
func main() {
var max int64 = 0
for i := int64(0); i < 1000000; i++ {
var len int64 = GetCollatzSeqLen(i)
if len > max {
max = len
}
}
fmt.Println(max)
}
func GetCollatzSeqLen(n int64) int64 {
var len int64 = 1
for n > 1 {
len++
if n%2 == 0 {
n = n / 2
} else {
n = 3*n + 1
}
}
return len
}
Output:
525
Playground: http://play.golang.org/p/0Cdic16edP
> The Go Programming Language Specification
>
> Numeric types
>
> <pre>
> int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
> int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807) </pre>
>
> The value of an n-bit integer is n bits wide and represented using
> two's complement arithmetic.
>
> There is also a set of predeclared numeric types with
> implementation-specific sizes:
>
> <pre>
> uint either 32 or 64 bits
> int same size as uint </pre>
To see the implementation-specific size of int
, run this program.
package main
import (
"fmt"
"runtime"
"strconv"
)
func main() {
fmt.Println(
"For "+runtime.GOARCH+" the implementation-specific size of int is",
strconv.IntSize, "bits.",
)
}
Output:
<pre>
For amd64 the implementation-specific size of int is 64 bits.
</pre>
On Go Playground: http://play.golang.org/p/7O6dEdgDNd
<pre>
For amd64p32 the implementation-specific size of int is 32 bits.
</pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论