3 // Copyright 2021 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Excerpted from go/constant/value.go to capture a bug from there.
19 intVal struct{ val *big.Int } // Int values not representable as an int64
20 ratVal struct{ val *big.Rat } // Float values representable as a fraction
21 floatVal struct{ val *big.Float } // Float values not representable as a fraction
22 complexVal struct{ re, im Value }
27 func (unknownVal) String() string { return "unknown" }
29 func (x intVal) String() string { return x.val.String() }
30 func (x ratVal) String() string { return rtof(x).String() }
32 func (x floatVal) String() string {
35 // Use exact fmt formatting if in float64 range (common case):
36 // proceed if f doesn't underflow to 0 or overflow to inf.
37 if x, _ := f.Float64(); f.Sign() == 0 == (x == 0) && !math.IsInf(x, 0) {
38 return fmt.Sprintf("%.6g", x)
44 func (x complexVal) String() string { return fmt.Sprintf("(%s + %si)", x.re, x.im) }
46 func newFloat() *big.Float { return new(big.Float).SetPrec(prec) }
50 func itor(x intVal) ratVal { return ratVal{nil} }
54 func itof(x intVal) floatVal { return floatVal{nil} }
55 func rtof(x ratVal) floatVal { return floatVal{newFloat().SetRat(x.val)} }
57 type Value interface {
63 func ToFloat(x Value) Value {
64 switch x := x.(type) {
70 case ratVal, floatVal:
82 func smallInt(x *big.Int) bool {
88 func Sign(x Value) int {
94 v := ratVal{big.NewRat(22,7)}
95 s := ToFloat(v).String()
96 fmt.Printf("s=%s\n", s)