// valString returns the string representation for the value v. // Setting floatFmt forces an integer value to be formatted in // normalized floating-point format. // TODO(gri) Move this code into package exact. func valString(v exact.Value, floatFmt bool) string { switch v.Kind() { case exact.Int: if floatFmt { return floatString(v) } case exact.Float: return floatString(v) case exact.Complex: re := exact.Real(v) im := exact.Imag(v) var s string if exact.Sign(re) != 0 { s = floatString(re) if exact.Sign(im) >= 0 { s += " + " } else { s += " - " im = exact.UnaryOp(token.SUB, im, 0) // negate im } } // im != 0, otherwise v would be exact.Int or exact.Float return s + floatString(im) + "i" } return v.String() }
// constValString emulates Go 1.6's go/constant.ExactString well enough // to make the tests pass. This is just a stopgap until we throw away // all the *15.go files. func constValString(v exact.Value) string { if v.Kind() == exact.Float { f, _ := exact.Float64Val(v) return fmt.Sprintf("%g", f) } return v.String() }
// Helper function to adjust go1.5 numeric go/constant formatting. // Can be removed once we give up compatibility with go1.5. func constValString(v exact.Value) string { if v.Kind() == exact.Float { // In go1.5, go/constant floating-point values are printed // as fractions. Make them appear as floating-point numbers. f, _ := exact.Float64Val(v) return fmt.Sprintf("%g", f) } return v.String() }
func (c *converter) convertConstantValue(v goconstant.Value) constant.Value { if v == nil { return nil } if v, ok := c.converted[v]; ok { return v.(constant.Value) } var ret constant.Value switch v.Kind() { case goconstant.Bool: ret = constant.MakeBool(goconstant.BoolVal(v)) case goconstant.String: ret = constant.MakeString(goconstant.StringVal(v)) case goconstant.Int: ret = constant.MakeFromLiteral(v.String(), token.INT, 0) case goconstant.Float: ret = constant.MakeFromLiteral(v.String(), token.FLOAT, 0) case goconstant.Complex: ret = constant.MakeFromLiteral(v.String(), token.IMAG, 0) } c.converted[v] = ret return ret }