Invalid operation: shift of type float64

julienc

I'm facing a strange issue using the shift operator << in Golang. In my final code, the shift value will be the absolute value of two integers. However, the Go package only defines the Abs function for float64 values, so I will need to cast the parameters to use it, and then cast the result back to uint.

Finally, this value will be used as a float64 parameter, so I cast it back to float64 after that.

The problem is that the return value's cast does not seem to work as I expected...

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)))

The error I'm receiving is:

invalid operation: 1 << uint(math.Abs(10)) (shift of type float64)

However, it seems that the cast operation alone works:

var test = uint(math.Abs(10))
fmt.Println(reflect.Kind(test))
// uint32

Is it a Golang issue ? A behaviour I did not find in the specifications ? A normal behaviour I simply don't understand ?

Here is a playground: http://play.golang.org/p/36a8r8CCYL

Ainar-G

From the spec:

The right operand in a shift expression must have unsigned integer type or be an untyped constant that can be converted to unsigned integer type. If the left operand of a non-constant shift expression is an untyped constant, the type of the constant is what it would be if the shift expression were replaced by its left operand alone.

So float64(1 << uint(math.Abs(10))) is basically the same as float64(1) << uint(math.Abs(10)), which produces an error, since one does not simply shift a float.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Webgl readPixels() invalid operation: invalid format/type combination

From Dev

How to force python float operation on float32 rather than float64?

From Dev

Julia: converting column type from Integer to Float64 in a DataFrame

From Dev

Why must I convert an integer to a float64 to type match?

From Dev

Other ways of verifying reflect.Type for int and float64

From Dev

How to fix MatMul Op has type float64 that does not match type float32 TypeError?

From Dev

Input 'b' of 'MatMul' Op has type float32 that does not match type float64 of argument 'a'

From Dev

WebGL: INVALID_OPERATION: vertexAttribPointer: stride or offset not valid for type

From Dev

Go: invalid operation - type *map[key]value does not support indexing

From Dev

Left shift operation

From Dev

Shift Operation in C++

From Dev

Bitwise shift operation choice

From Dev

Convert numpy array type and values from Float64 to Float32

From Dev

Bit shift compile error from template instantiation with float type

From Dev

pandas comparison raises TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]

From Dev

type switch from int to float64 after merge creating error

From Dev

Python 'float64' cannot be converted to a MySQL type but in manual query thats no problem

From Dev

Set attribute of type "Class" to define it as Int32, String, Float64

From Dev

Shift bit-wise operation

From Dev

Left shift operation on char array

From Dev

Invalid operation of opcode and operands

From Dev

Invalid argument to operation ++/--

From Dev

Invalid Floating Point Operation

From Dev

E: Invalid operation instal

From Dev

Invalid Operation with Decimal

From Dev

E: Invalid operation instal

From Dev

OpenGL Invalid Operation

From Dev

Invalid argument to operation ++/--

From Dev

Ctrl + Shift + e to type é

Related Related

  1. 1

    Webgl readPixels() invalid operation: invalid format/type combination

  2. 2

    How to force python float operation on float32 rather than float64?

  3. 3

    Julia: converting column type from Integer to Float64 in a DataFrame

  4. 4

    Why must I convert an integer to a float64 to type match?

  5. 5

    Other ways of verifying reflect.Type for int and float64

  6. 6

    How to fix MatMul Op has type float64 that does not match type float32 TypeError?

  7. 7

    Input 'b' of 'MatMul' Op has type float32 that does not match type float64 of argument 'a'

  8. 8

    WebGL: INVALID_OPERATION: vertexAttribPointer: stride or offset not valid for type

  9. 9

    Go: invalid operation - type *map[key]value does not support indexing

  10. 10

    Left shift operation

  11. 11

    Shift Operation in C++

  12. 12

    Bitwise shift operation choice

  13. 13

    Convert numpy array type and values from Float64 to Float32

  14. 14

    Bit shift compile error from template instantiation with float type

  15. 15

    pandas comparison raises TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]

  16. 16

    type switch from int to float64 after merge creating error

  17. 17

    Python 'float64' cannot be converted to a MySQL type but in manual query thats no problem

  18. 18

    Set attribute of type "Class" to define it as Int32, String, Float64

  19. 19

    Shift bit-wise operation

  20. 20

    Left shift operation on char array

  21. 21

    Invalid operation of opcode and operands

  22. 22

    Invalid argument to operation ++/--

  23. 23

    Invalid Floating Point Operation

  24. 24

    E: Invalid operation instal

  25. 25

    Invalid Operation with Decimal

  26. 26

    E: Invalid operation instal

  27. 27

    OpenGL Invalid Operation

  28. 28

    Invalid argument to operation ++/--

  29. 29

    Ctrl + Shift + e to type é

HotTag

Archive