dsa99349 2014-11-09 15:01
浏览 9
已采纳

来自同一个Go程序的不同输出

Here is my Go code: 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

}

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

  • drygauost253590142 2014-11-09 15:06
    关注

    It's because of the implementation-specific size of int, 32 or 64 bits. Use int64 for consistent results. For example,

    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
    
    }
    

    Output:

    525
    

    Playground: http://play.golang.org/p/0Cdic16edP


    The Go Programming Language Specification

    Numeric types

     int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
     int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807) 

    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:

     uint     either 32 or 64 bits
     int      same size as uint 

    To see the implementation-specific size of int, run this program.

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

    Output:

    For amd64 the implementation-specific size of int is 64 bits.
    

    On Go Playground: http://play.golang.org/p/7O6dEdgDNd

    For amd64p32 the implementation-specific size of int is 32 bits.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?