Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pow operator produces incorrect Go translation #10

Open
ns-cweber opened this issue Jan 9, 2017 · 3 comments
Open

Pow operator produces incorrect Go translation #10

ns-cweber opened this issue Jan 9, 2017 · 3 comments

Comments

@ns-cweber
Copy link

Python's pow operator (x**2) produces math.Pow(x, 2) in Go; however, in Go, math.Pow() only accepts floats, but in Python, ** handles ints as well as floats. This means the Python program:

x = 2
print(x**2)

Translates to

// package decl and imports elided
func main() {
	x := 2 // int
	fmt.Println(math.Pow(x, 2))
}

Which fails to compile with error: ./foo.go:10: cannot use x (type int) as type float64 in argument to math.Pow

@alehander92
Copy link
Collaborator

What would be idiomatic in Go: to call bigint Exp ? The other posibillity is to generate a PowInt function for cases like this.

@ns-cweber
Copy link
Author

I would inline int(math.Pow(float64(a), float64(b)), or a custom PowInt() if math.Pow() isn't very performant for integers. I would use bigint if I was concerned about overflow. I'm not sure how you generalize this for all applications--maybe you just always use bigint and take the perf hit in all cases?

@alehander92
Copy link
Collaborator

That would be the alpha versions choice, however later versions might add support for type annotations which could signify bigint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants