【发布时间】:2017-06-13 14:44:54
【问题描述】:
在 Go 中,我可以像这样声明一个类型化的浮点常量:
const foo float64 = 1e100
或像这样的任意位模式的浮点变量:
var bar = math.Float64frombits(0x7ff8c0c0ac0ffee1)
但这是一个错误(“const initializer… is not a constant”):
const baz = math.Float64frombits(0x7ff8c0c0ac0ffee1)
如何声明任意位模式的类型化 float const?
【问题讨论】:
-
您可以将其存储为
uint64常量并按需转换(这就是数学包内部的操作方式) -
这实际上是一个很好的建议。导出值(例如用作标记值的值)有点烦人,因为依赖包也需要导入
math,但至少它确保了常量性。
标签: go floating-point bit-manipulation constants