在 Go 中转换 int 和 int64 时得到不同的输出;是由于处理器架构吗?
Getting different output when casting between int and int64 in Go; is it due to processor architecture?
我用来测试某些预期行为的应用程序的一小部分会给出不同的输出,这取决于我 运行 它所使用的处理器。这是代码的相关部分:
for b := 0; b < intCounter; b++ {
//int64Random = rand.Int63()
int64Random = int64(rand.Int())
//CHECKING FOR SANITY
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
当我在 Pi(arm,linux)上 运行 时,我得到如下输出:
int64Random is 1251459732
int64Random is 1316852782
int64Random is 971786136
int64Random is 1359359453
int64Random is 729066469
如果在 Pi 上我将 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 文档?
根据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 整数。在 amd64 上它将是 64 位,在 ARM 上它将是 32 位
我用来测试某些预期行为的应用程序的一小部分会给出不同的输出,这取决于我 运行 它所使用的处理器。这是代码的相关部分:
for b := 0; b < intCounter; b++ {
//int64Random = rand.Int63()
int64Random = int64(rand.Int())
//CHECKING FOR SANITY
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
当我在 Pi(arm,linux)上 运行 时,我得到如下输出:
int64Random is 1251459732
int64Random is 1316852782
int64Random is 971786136
int64Random is 1359359453
int64Random is 729066469
如果在 Pi 上我将 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 文档?
根据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 整数。在 amd64 上它将是 64 位,在 ARM 上它将是 32 位