简体   繁体   English

在Go中int和int64之间进行转换时获得不同的输出; 是由于处理器架构造成的吗?

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

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 < intCounter; b++ {

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

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

            slcTestNums = append(slcTestNums, int64Random)
    }

When I run this on my Mac (amd64, darwin) I get output like: 当我在Mac(amd64,darwin)上运行此命令时,输出如下:

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: 当我在Pi(手臂,Linux)上运行此命令时,我得到如下输出:

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: 如果在Pi上将int64Random更改为= rand.Int63()并重新编译,则输出如下:

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

...which more closely matches what the Mac is getting. ...与Mac的功能更加接近。 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? 为什么int64(rand.Int())生成int64范围内的数字而不是保留int范围内的数字,而是更改要存储在其中的变量的类型? Am I missing the Go documentation that mentions this behavior? 我是否缺少提到此行为的Go文档?

According to https://golang.org/doc/go1.1 根据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. 该语言允许实现选择int类型和uint类型是32位还是64位。 Previous Go implementations made int and uint 32 bits on all systems. 先前的Go实现在所有系统上都将int和uint设置为32位。 Both the gc and gccgo implementations now make int and uint 64 bits on 64-bit platforms such as AMD64/x86-64 现在,gc和gccgo实现都可以在64位平台(例如AMD64 / x86-64)上将int和uint设置为64位

rand.Int() returns int. rand.Int()返回int。 On amd64 it will be 64 bits, on ARM it will be 32 bits 在amd64上它将是64位,在ARM上将是32位

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM