Getting different output when casting between int and int64 in Go; is it due to processor architecture?

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

Getting different output when casting between int and int64 in Go; is it due to processor architecture?

问题

我正在使用一个应用程序的一小部分来测试一些预期行为,但是根据我运行它的处理器不同,它会产生不同的输出。以下是代码的相关部分:

for b := 0; b < intCounter; b++ {
    int64Random = int64(rand.Int())
    fmt.Println("int64Random is " + strconv.FormatInt(int64Random, 10))
    slcTestNums = append(slcTestNums, int64Random)
}

当我在我的 Mac (amd64, darwin) 上运行时,输出如下:

int64Random is 2991558990735723489
int64Random is 7893058381743103687
int64Random is 7672635040537837613
int64Random is 1557718564618710869
int64Random is 2107352926413218802

当我在树莓派 (arm, linux) 上运行时,输出如下:

int64Random is 1251459732
int64Random is 1316852782
int64Random is 971786136
int64Random is 1359359453
int64Random is 729066469

如果我在树莓派上将 int64Random 改为 rand.Int63() 并重新编译,输出如下:

int64Random is 7160249008355881289
int64Random is 7184347289772016444
int64Random is 9201664581141930074
int64Random is 917219239600463359
int64Random is 6015348270214295654

这更接近于 Mac 上的输出。这是因为处理器架构的不同导致了运行时的一些变化吗?为什么 int64(rand.Int()) 会生成 int64 范围的数字,而不是保持 int 范围的数字,并将其存储在变量的类型中进行更改?我是否遗漏了提到这种行为的 Go 文档?

英文:

A small part of an application I'm using to test some expected behavior is giving different output, depending on what processor I run it on. Here's the relevant part of the code:

   for b := 0; b &lt; intCounter; b++ {

            //int64Random = rand.Int63()
            int64Random = int64(rand.Int())

//CHECKING FOR SANITY
fmt.Println(&quot;int64Random is &quot; + strconv.FormatInt(int64Random, 10))

            slcTestNums = append(slcTestNums, int64Random)
    }

When I run this on my Mac (amd64, darwin) I get output like:

int64Random is 2991558990735723489
int64Random is 7893058381743103687
int64Random is 7672635040537837613
int64Random is 1557718564618710869
int64Random is 2107352926413218802

When I run this on a Pi (arm, linux) I get output like:

int64Random is 1251459732
int64Random is 1316852782
int64Random is 971786136
int64Random is 1359359453
int64Random is 729066469

If on the Pi I change the int64Random to = rand.Int63() and recompile, I get output like:

int64Random is 7160249008355881289
int64Random is 7184347289772016444
int64Random is 9201664581141930074
int64Random is 917219239600463359
int64Random is 6015348270214295654

...which more closely matches what the Mac is getting. Is this because of something that is changed at runtime due to the processor architecture? Why is int64(rand.Int()) generating int64-ranged numbers instead of keeping an int-ranged number, but changing the type for the variable it's being stored in? Am I missing the Go documentation that mentions this behavior?

答案1

得分: 1

根据https://golang.org/doc/go1.1的说明:

> 该语言允许实现选择int类型和uint类型是32位还是64位。之前的Go实现在所有系统上都将int和uint设为32位。现在,gc和gccgo实现在64位平台(如AMD64/x86-64)上将int和uint设为64位。

rand.Int()返回int类型。在amd64上,它将是64位,在ARM上将是32位。

英文:

According to https://golang.org/doc/go1.1

> The language allows the implementation to choose whether the int type
> and uint types are 32 or 64 bits. Previous Go implementations made int
> and uint 32 bits on all systems. Both the gc and gccgo implementations
> now make int and uint 64 bits on 64-bit platforms such as AMD64/x86-64

rand.Int() returns int. On amd64 it will be 64 bits, on ARM it will be 32 bits

huangapple
  • 本文由 发表于 2015年7月24日 04:02:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/31597007.html
匿名

发表评论

匿名网友

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

确定