]> Cypherpunks.ru repositories - gostls13.git/commit
go/constant: switch to floating-point representation when fractions become too large
authorRobert Griesemer <gri@golang.org>
Thu, 3 Dec 2015 01:41:39 +0000 (17:41 -0800)
committerRobert Griesemer <gri@golang.org>
Mon, 14 Dec 2015 23:42:01 +0000 (23:42 +0000)
commitd0c17461a785a03781e4cdbd18f7f3ba04d4b9db
treed5858b492505cbb2661f5558a660ffda43f7d16d
parente568a0180a8d7c296e254d84dc5cf485695cf570
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>
17 files changed:
api/go1.1.txt
src/cmd/api/goapi.go
src/cmd/api/testdata/src/pkg/p1/golden.txt
src/go/constant/value.go
src/go/constant/value_test.go
src/go/internal/gccgoimporter/importer_test.go
src/go/types/api_test.go
src/go/types/builtins.go
src/go/types/conversions.go
src/go/types/eval_test.go
src/go/types/expr.go
src/go/types/operand.go
src/go/types/stdlib_test.go
src/go/types/testdata/const0.src
src/go/types/testdata/decls0.src
src/go/types/testdata/errors.src
src/go/types/typexpr.go