douzhengyi5022 2015-07-23 20:02
浏览 103
已采纳

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

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:

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条回答 默认 最新

  • doujiexin1136 2015-07-23 20:34
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题