• Robert Griesemer's avatar
    go/constant: switch to floating-point representation when fractions become too large · d0c17461
    Robert Griesemer authored
    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/17360Reviewed-by: 's avatarAlan Donovan <adonovan@google.com>
    Run-TryBot: Robert Griesemer <gri@golang.org>
    TryBot-Result: Gobot Gobot <gobot@golang.org>
    d0c17461
goapi.go 20.5 KB