Esempio n. 1
0
// 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 *14.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()
}