go/types: misleading error message in const expression #11825
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:
There may be a more serious underlying problem. For a test case x.src:
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.
Correction: With the latest gotype, go/types, we get:
which is a) the correct expression, and b) likely the correct value (it's 1.0000000517753564e+40, or 1e40 after rounding to 24bits).
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.
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.