-
Notifications
You must be signed in to change notification settings - Fork 18k
go/types: misleading error message in const expression #11825
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
Comments
There may be a more serious underlying problem. For a test case x.src:
we get
that is the traced constant value is not exact. However, in http://play.golang.org/p/1nxEHRZf5P we get the exact result
Suspicion: Premature rounding after unary/binary operations. |
Simpler:
produces
vs http://play.golang.org/p/jYgihTQe8f
|
Correction: With the latest gotype, go/types, we get:
gotype reports:
which is a) the correct expression, and b) likely the correct value (it's 1.0000000517753564e+40, or 1e40 after rounding to 24bits). Similarly,
prints
which is approx. 1e30. Per the spec (http://tip.golang.org/ref/spec#Conversions):
Thus internally, we need to continue in full precision (or in case of go/types, exact) after the intermediate value was rounded to float32 (in this case). Consequently, we get spurious values. Should decide if error messages should report in a different more readable format. |
Possible test case: http://play.golang.org/p/lTVEGyl3oR |
Might still be worth fixing, if easy. |
With the pending CL https://go-review.googlesource.com/#/c/17360/ we now get better error messages. For the original example we get:
|
This appears to be fixed (or the error was incorrectly analyzed). For the following program:
The corresponding test case for go/types is (the trace built-in is available for testing only):
With the current go/types (uses big.Rat for constant arithmetic) we get:
-1/9007199254740992 is -1.1102230246251565404236316680908203125 × 10^-16 which is -1.11022e-16 or -2**-53 which is correct (http://www.wolframalpha.com/input/?i=-1%2F9007199254740992). With the pending CL https://go-review.googlesource.com/#/c/17360/ (uses big.Float for constant arithmetic):
matching cmd/compile output exactly. Or in other words: The incorrect expression in the error message reported in the beginning of this issue was fixed. The other discrepancies in output are due to the fact that go/types, when based on rational arithmetic, is doing exact (algebraic) math (no rounding). When using go/types based on floating-point arithmetic (doing rounding), we get the same result as cmd/compile. Closing as resolved. |
package a
var a = 1e20/complex64(1e-20)
gotype reports:
x.go:2:9: 1e20 (constant 15474250801184809498242605942063297429153775616/1547425 of type complex64) overflows complex64
The culprit is not 1e20. The error message needs to be investigated.
The text was updated successfully, but these errors were encountered: