同一个 Go 程序产生不同的输出结果

huangapple go评论70阅读模式
英文:

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 &quot;fmt&quot;

func main() {
	var max int = 0
	for i := 0; i &lt; 1000000; i++ {
		var len int = GetCollatzSeqLen(i)
		if len &gt; max {
			max = len
		}
	}

	fmt.Println(max)

}

func GetCollatzSeqLen(n int) int {
	var len int = 1
	for n &gt; 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 &quot;fmt&quot;

func main() {
	var max int64 = 0
	for i := int64(0); i &lt; 1000000; i++ {
		var len int64 = GetCollatzSeqLen(i)
		if len &gt; max {
			max = len
		}
	}

	fmt.Println(max)

}

func GetCollatzSeqLen(n int64) int64 {
	var len int64 = 1
	for n &gt; 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 (
	&quot;fmt&quot;
	&quot;runtime&quot;
	&quot;strconv&quot;
)

func main() {
	fmt.Println(
		&quot;For &quot;+runtime.GOARCH+&quot; the implementation-specific size of int is&quot;,
		strconv.IntSize, &quot;bits.&quot;,
	)
}

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>

huangapple
  • 本文由 发表于 2014年11月9日 23:01:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/26829562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定