操作无效:float64类型的移位

jul*_*enc 6 bit-shift go

<<在Golang中使用shift运算符面临一个奇怪的问题.在我的最终代码中,移位值将是两个整数的绝对值.但是,Go包只定义了值的Abs函数float64,所以我需要转换参数来使用它,然后将结果转换回来uint.

最后,这个值将被用作float64参数,因此我将其转换回来float64.

问题是返回值的转换似乎不像我预期的那样工作......

var test float64

// all the following lines are working as expected
test = float64(1 << 10)
test = float64(1 << uint(10))
test = float64(1 << uint(float64(11-1)))
test = float64(1 << uint(-float64(1-11)))

// but this one does not: error at compilation
test = float64(1 << uint(math.Abs(10)))
Run Code Online (Sandbox Code Playgroud)

我收到的错误是:

invalid operation: 1 << uint(math.Abs(10)) (shift of type float64)
Run Code Online (Sandbox Code Playgroud)

但是,似乎只有施法操作才有效:

var test = uint(math.Abs(10))
fmt.Println(reflect.Kind(test))
// uint32
Run Code Online (Sandbox Code Playgroud)

这是一个Golang问题吗?我没有在规格中找到的行为?我根本不懂的正常行为?

这是一个游乐场:http://play.golang.org/p/36a8r8CCYL

Ain*_*r-G 11

规格:

shift表达式中的右操作数必须具有无符号整数类型,或者是可以转换为无符号整数类型的无类型常量.如果非常量移位表达式的左操作数是无类型常量,则常量的类型是仅在其左操作数替换移位表达式时的类型.

所以float64(1 << uint(math.Abs(10)))基本上是相同的float64(1) << uint(math.Abs(10)),它产生一个错误,因为一个不简单地移动一个浮点数.