Cannot use -a (type int) as type float64 in function argument

6,217 views
Skip to first unread message

Berker Peksağ

unread,
Oct 29, 2010, 6:35:23 PM10/29/10
to golang-nuts
Hi everyone,

I'm getting this error:

Cannot use -a (type int) as type float64 in function argument

Code:

package main

import (
"fmt"
"math"
)

var (
a = 53
b = math.Pow(2, -a)
)

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

But this works:

package main

import (
"fmt"
"math"
)

var (
b = math.Pow(2, -53)
)

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

Cory Mainwaring

unread,
Oct 29, 2010, 6:47:43 PM10/29/10
to Berker Peksağ, golang-nuts
-53 is a constant, and thus has no type, it is an arbitrary precision number (implementation may vary on how arbitrary) and can be implicitly converted to any type. a := 53 creates an int, because that's the symantics of the language, add a decimal point to get a float, and wrap 53 in float64(53) to get a float64.

Conversions in Go are explicit, so int cannot be implicitly converted to float64 like in C. If you want a to be a float64, you have to tell the compiler you want that by A) giving it a number that requires float64 precision or B) converting the 53 int-necessary value to a float64 by using a type-cast.


2010/10/29 Berker Peksağ <berker...@gmail.com>

Ian Lance Taylor

unread,
Oct 29, 2010, 6:54:34 PM10/29/10
to Berker Peksağ, golang-nuts
Berker Peksağ <berker...@gmail.com> writes:

> I'm getting this error:
>
> Cannot use -a (type int) as type float64 in function argument

> var (


> a = 53
> b = math.Pow(2, -a)
> )

> var (


> b = math.Pow(2, -53)
> )

This is correct. You didn't declare a type for a, and the initializer
is integral, so it gets the type "int". Pasisng a variable of type int
to a function which expects a value of type float64 requires a type
conversion.

In the second case, you are passing a constant. Constants are untyped
by default, and may be assigned to a value of any type which can
represent the constant.

This code would work if you changed a from a var to a const.

Ian

Sonia Keys

unread,
Oct 29, 2010, 7:05:28 PM10/29/10
to golang-nuts
True. Variables and functions are statically typed in Go. math.Pow
requires float64s, no way around it.

A constant, starting as a literal, doesn't have complete type
information, so the compiler is free to make it fit as needed. In
your second example, the compiler can see that the literal -53 needs
to be a float64 and so it compiles it that way and the program works.

In the first example though, in the line a = 53, the compiler does the
simplest thing and makes a an int. Now it is trapped when it gets to
the math.Pow function. It needs a float64 but has already made a an
int. Go does no silent conversions in this case. The compiler gives
you the error.

The fix is to give the compiler a heads up in the declaration of a.
Change the line to,

a float64 = 53

and it compiles and runs as expected.

All this is explained in the Tutorial, Effective Go, the FAQ, and of
course, the Language Specification. Read.
Reply all
Reply to author
Forward
0 new messages