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

cmd/compile: cannot convert (constant 10000 of type float64) to type int64 #56220

Closed
NOMORECOFFEE opened this issue Oct 13, 2022 · 2 comments
Closed
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@NOMORECOFFEE
Copy link

What version of Go are you using (go version)?

$ go version
go1.19

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

package main

import (
	"fmt"
)

func main() {
	v := int64((float64(0.03) - float64(0.02)) * 1_000_000)
	fmt.Println(v)
}

https://go.dev/play/p/V0KH5qKf6u4

What did you expect to see?

10000

What did you see instead?

cannot convert (float64(0.03) - float64(0.02)) * 1_000_000 (constant 10000 of type float64) to type int64

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Oct 13, 2022
@randall77
Copy link
Contributor

The problem is that (float64(0.03) - float64(0.02)) * 1_000_000 is not an integer. 0.03 is not representable exactly as a float64, so the total expression is not exactly an integer. That's what the compiler is complaining about.
The error message could certainly be better.
Note that (0.03 - 0.02) * 1_000_000 does work, because the compiler maintains many more significant digits than a float64 does.

@gopherbot
Copy link

Change https://go.dev/cl/442935 mentions this issue: cmd/compile: in compiler errors, print more digits for floats close to an int

@joedian joedian added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 14, 2022
@mknyszek mknyszek added this to the Go1.20 milestone Oct 19, 2022
romaindoumenc pushed a commit to TroutSoftware/go that referenced this issue Nov 3, 2022
…o an int

Error messages currently print floats with %.6g, which means that if
you tried to convert something close to, but not quite, an integer, to
an integer, the error you get looks like "cannot convert 1 to type
int", when really you want "cannot convert 0.9999999 to type int".

Add more digits to floats when printing them, to make it clear that they
aren't quite integers. This helps for errors which are the result of not
being an integer. For other errors, it won't hurt much.

Fixes golang#56220

Change-Id: I7f5873af5993114a61460ef399d15316925a15a5
Reviewed-on: https://go-review.googlesource.com/c/go/+/442935
Reviewed-by: Rob Pike <r@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
@golang golang locked and limited conversation to collaborators Oct 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants