如何声明一个具有任意位模式的浮点型 Go 常量?

huangapple go评论92阅读模式
英文:

How can I declare a float-typed Go constant of arbitrary bit pattern?

问题

在Go语言中,我可以像这样声明一个具有类型的浮点常量:

const foo float64 = 1e100

或者像这样声明一个具有任意位模式的浮点变量:

var bar = math.Float64frombits(0x7ff8c0c0ac0ffee1)

但是这样会报错("const initializer... is not a constant"):

const baz = math.Float64frombits(0x7ff8c0c0ac0ffee1)

我应该如何声明一个具有任意位模式的有类型浮点常量?

英文:

In Go I can declare a typed float constant like this:

const foo float64 = 1e100

or a float variable of arbitrary bit pattern like this:

var bar = math.Float64frombits(0x7ff8c0c0ac0ffee1)

But this is an error ("const initializer… is not a constant"):

const baz = math.Float64frombits(0x7ff8c0c0ac0ffee1)

How might I declare a typed float const of arbitrary bit pattern?

答案1

得分: 3

如果你想存储位值(实质上是uint64),并将其作为float64提供给外部包使用,你可以提供一个“常量”函数,你保证该函数只返回常量值。这正是像math.NaN这样的函数的工作原理。

const uintFoo = 0x7ff8c0c0ac0ffee1

func ConstFoo() float64 {
    return math.Float64frombits(uintFoo)
}
英文:

If you want to store the bit value (which is essentially a uint64), and have it available as a float64 to external packages, you can provide a "constant" function, which you guarantee to only return the constant value. This is precisely how functions like math.NaN work.

const uintFoo = 0x7ff8c0c0ac0ffee1

func ConstFoo() float64 {
	return math.Float64frombits(uintFoo)
}

答案2

得分: 2

你对Math.Float64frombits的调用发生在运行时,而不是编译时,因此不是常量。根据effective Go页面的解释(比我更好地解释了这个问题):

在Go中,常量就是常量。它们在编译时创建,即使在函数中定义为局部变量,也只能是数字、字符串或布尔值。由于编译时的限制,定义常量的表达式必须是常量表达式,可以由编译器计算。例如,1<<3是一个常量表达式,而math.Sin(math.Pi/4)不是,因为对math.Sin的函数调用需要在运行时发生。

链接:https://golang.org/doc/effective_go.html#constants

英文:

Your call to Math.Float64frombits occurs at runtime, not compile time, and thus, is not constant. From the effective Go page (which will explain it better than I can):

> Constants in Go are just that—constant. They are created at compile
> time, even when defined as locals in functions, and can only be
> numbers, strings or booleans. Because of the compile-time restriction,
> the expressions that define them must be constant expressions,
> evaluatable by the compiler. For instance, 1<<3 is a constant
> expression, while math.Sin(math.Pi/4) is not because the function call
> to math.Sin needs to happen at run time.

Link: https://golang.org/doc/effective_go.html#constants

答案3

得分: 1

你不能在常量声明中像Float64frombits这样调用一个函数;函数调用意味着它无法在编译时完全评估,因此无法用作常量。但是,你可以将位数直接转换为浮点数值:

const myFloat float64 = 0x7ff8c0c0ac0ffee1

func main() {
    fmt.Println(myFloat)
}
英文:

You can't call a function like Float64frombits in a constant declaration; the function call means it can't be evaluated fully at compile time, so it can't be used as a constant. You can, however, just dump bits into a float value:

const myFloat float64 = 0x7ff8c0c0ac0ffee1

func main() {
	fmt.Println(myFloat)
}

huangapple
  • 本文由 发表于 2017年6月13日 22:44:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/44524774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定