【问题标题】:Why does math.Pow10(e int) returns float64 rather than int64?为什么 math.Pow10(e int) 返回 float64 而不是 int64?
【发布时间】:2017-02-13 09:07:38
【问题描述】:

既然参数eint,为什么不直接返回一个int64?这有什么特别的原因吗?

【问题讨论】:

    标签: math go


    【解决方案1】:

    2 个原因:

    首先,参数也可能为负数,在这种情况下,结果是01 之间的小数,因此float64 返回值不合理,而是需要。

    fmt.Println(math.Pow10(-1))
    

    输出(在Go Playground 上试试):

    0.1
    

    其次,几乎*math 包的每个函数都返回 float64 类型的值,添加一个不会破坏“一致性”的值。

    [*] 少数例外情况包括像 Float32frombits()llogb() 这样的偏差是合理的函数。

    【讨论】:

      猜你喜欢
      • 2018-05-14
      • 2015-06-21
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      相关资源
      最近更新 更多