aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/api
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2015-12-02 17:41:39 -0800
committerRobert Griesemer <gri@golang.org>2015-12-14 23:42:01 +0000
commitd0c17461a785a03781e4cdbd18f7f3ba04d4b9db (patch)
treed5858b492505cbb2661f5558a660ffda43f7d16d /src/cmd/api
parente568a0180a8d7c296e254d84dc5cf485695cf570 (diff)
downloadgo-d0c17461a785a03781e4cdbd18f7f3ba04d4b9db.tar.gz
go-d0c17461a785a03781e4cdbd18f7f3ba04d4b9db.zip
go/constant: switch to floating-point representation when fractions become too large
Use two internal representations for Float values (similar to what is done for Int values). Transparently switch to a big.Float representation when big.Rat values become unwieldy. This is almost never needed for real-world programs but it is trivial to create test cases that cannot be handled with rational arithmetic alone. As a consequence, the go/constant API semantics changes slightly: Until now, a value could always be represented in its "smallest" form (e.g., float values that happened to be integers would be represented as integers). Now, constant Kind depends on how the value was created, rather than its actual value. (The reason why we cannot automatically "normalize" values to their smallest form anymore is because floating-point numbers are not exact in general; and thus normalization is often not possible in the first place, or would throw away precision when it is not desired.) This has repercussions as to how constant Values are used go/types and required corresponding adjustments. Details of the changes: go/constant package: - use big.Rat and big.Float values to represent floating-point values (internal change) - changed semantic of Value.Kind accordingly - String now returns a short, human-readable form of a value (this leads to better error messages in go/types) - added ToInt, ToFloat, and ToComplex conversion functions - added ExactString to obtain an exact string form of a value go/types: - adjusted and simplified implementation of representableConst - adjusted various places where Value.Kind was expected to be "smallest" by calling the respective ToInt/Float/Complex conversion functions - enabled 5 disabled tests in stdlib_test.go that now work api checker: - print all constant values in a short human-readable form (floats are printed in floating-point form), but also print an exact form if it is different from the short form - adjusted test golden file and go.1.1.text reference file Fixes #11327. Change-Id: I492b704aae5b0238e5b7cee13e18ffce61193587 Reviewed-on: https://go-review.googlesource.com/17360 Reviewed-by: Alan Donovan <adonovan@google.com> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/cmd/api')
-rw-r--r--src/cmd/api/goapi.go9
-rw-r--r--src/cmd/api/testdata/src/pkg/p1/golden.txt2
2 files changed, 9 insertions, 2 deletions
diff --git a/src/cmd/api/goapi.go b/src/cmd/api/goapi.go
index 5d1cf05e31..8b20d123b1 100644
--- a/src/cmd/api/goapi.go
+++ b/src/cmd/api/goapi.go
@@ -680,7 +680,14 @@ func (w *Walker) emitObj(obj types.Object) {
switch obj := obj.(type) {
case *types.Const:
w.emitf("const %s %s", obj.Name(), w.typeString(obj.Type()))
- w.emitf("const %s = %s", obj.Name(), obj.Val())
+ x := obj.Val()
+ short := x.String()
+ exact := x.ExactString()
+ if short == exact {
+ w.emitf("const %s = %s", obj.Name(), short)
+ } else {
+ w.emitf("const %s = %s // %s", obj.Name(), short, exact)
+ }
case *types.Var:
w.emitf("var %s %s", obj.Name(), w.typeString(obj.Type()))
case *types.TypeName:
diff --git a/src/cmd/api/testdata/src/pkg/p1/golden.txt b/src/cmd/api/testdata/src/pkg/p1/golden.txt
index 3c43a226ff..0378a56870 100644
--- a/src/cmd/api/testdata/src/pkg/p1/golden.txt
+++ b/src/cmd/api/testdata/src/pkg/p1/golden.txt
@@ -10,7 +10,7 @@ pkg p1, const ConstChase2 = 11
pkg p1, const ConstChase2 ideal-int
pkg p1, const ConversionConst = 5
pkg p1, const ConversionConst MyInt
-pkg p1, const FloatConst = 3/2
+pkg p1, const FloatConst = 1.5 // 3/2
pkg p1, const FloatConst ideal-float
pkg p1, const StrConst = "foo"
pkg p1, const StrConst ideal-string