func (x *Float) uint64() uint64 {
u, acc := x.Uint64()
if acc != Exact {
- panic(fmt.Sprintf("%s is not a uint64", x.Format('g', 10)))
+ panic(fmt.Sprintf("%s is not a uint64", x.Text('g', 10)))
}
return u
}
func (x *Float) int64() int64 {
i, acc := x.Int64()
if acc != Exact {
- panic(fmt.Sprintf("%s is not an int64", x.Format('g', 10)))
+ panic(fmt.Sprintf("%s is not an int64", x.Text('g', 10)))
}
return i
}
func TestFloatZeroValue(t *testing.T) {
// zero (uninitialized) value is a ready-to-use 0.0
var x Float
- if s := x.Format('f', 1); s != "0.0" {
+ if s := x.Text('f', 1); s != "0.0" {
t.Errorf("zero value = %s; want 0.0", s)
}
m := new(Float)
e := x.MantExp(m)
if !alike(m, mant) || e != test.exp {
- t.Errorf("%s.MantExp() = %s, %d; want %s, %d", test.x, m.Format('g', 10), e, test.mant, test.exp)
+ t.Errorf("%s.MantExp() = %s, %d; want %s, %d", test.x, m.Text('g', 10), e, test.mant, test.exp)
}
}
}
t.Fatalf("Float.MantExp aliasing error: got %d; want 10", e)
}
if want := makeFloat("0.5"); !alike(x, want) {
- t.Fatalf("Float.MantExp aliasing error: got %s; want %s", x.Format('g', 10), want.Format('g', 10))
+ t.Fatalf("Float.MantExp aliasing error: got %s; want %s", x.Text('g', 10), want.Text('g', 10))
}
}
var z Float
z.SetMantExp(frac, test.exp)
if !alike(&z, want) {
- t.Errorf("SetMantExp(%s, %d) = %s; want %s", test.frac, test.exp, z.Format('g', 10), test.z)
+ t.Errorf("SetMantExp(%s, %d) = %s; want %s", test.frac, test.exp, z.Text('g', 10), test.z)
}
// test inverse property
mant := new(Float)
if z.SetMantExp(mant, want.MantExp(mant)).Cmp(want) != 0 {
- t.Errorf("Inverse property not satisfied: got %s; want %s", z.Format('g', 10), test.z)
+ t.Errorf("Inverse property not satisfied: got %s; want %s", z.Text('g', 10), test.z)
}
}
}
var f Float
f.SetUint64(want)
if got := f.uint64(); got != want {
- t.Errorf("got %#x (%s); want %#x", got, f.Format('p', 0), want)
+ t.Errorf("got %#x (%s); want %#x", got, f.Text('p', 0), want)
}
}
got := f.uint64()
want := x &^ (1<<(64-prec) - 1) // cut off (round to zero) low 64-prec bits
if got != want {
- t.Errorf("got %#x (%s); want %#x", got, f.Format('p', 0), want)
+ t.Errorf("got %#x (%s); want %#x", got, f.Text('p', 0), want)
}
}
}
var f Float
f.SetInt64(want)
if got := f.int64(); got != want {
- t.Errorf("got %#x (%s); want %#x", got, f.Format('p', 0), want)
+ t.Errorf("got %#x (%s); want %#x", got, f.Text('p', 0), want)
}
}
}
got := f.int64()
want := x &^ (1<<(63-prec) - 1) // cut off (round to zero) low 63-prec bits
if got != want {
- t.Errorf("got %#x (%s); want %#x", got, f.Format('p', 0), want)
+ t.Errorf("got %#x (%s); want %#x", got, f.Text('p', 0), want)
}
}
}
var f Float
f.SetFloat64(want)
if got, acc := f.Float64(); got != want || acc != Exact {
- t.Errorf("got %g (%s, %s); want %g (Exact)", got, f.Format('p', 0), acc, want)
+ t.Errorf("got %g (%s, %s); want %g (Exact)", got, f.Text('p', 0), acc, want)
}
}
}
got, _ := f.Float64()
want := float64(x &^ (1<<(52-prec) - 1)) // cut off (round to zero) low 53-prec bits
if got != want {
- t.Errorf("got %g (%s); want %g", got, f.Format('p', 0), want)
+ t.Errorf("got %g (%s); want %g", got, f.Text('p', 0), want)
}
}
var f Float
f.SetFloat64(math.NaN())
// should not reach here
- t.Errorf("got %s; want ErrNaN panic", f.Format('p', 0))
+ t.Errorf("got %s; want ErrNaN panic", f.Text('p', 0))
}
func TestFloatSetInt(t *testing.T) {
}
// check value
- got := f.Format('g', 100)
+ got := f.Text('g', 100)
if got != want {
- t.Errorf("got %s (%s); want %s", got, f.Format('p', 0), want)
+ t.Errorf("got %s (%s); want %s", got, f.Text('p', 0), want)
}
}
t.Errorf("got prec = %d; want %d", prec, n)
}
- got := f2.Format('g', 100)
+ got := f2.Text('g', 100)
if got != want {
- t.Errorf("got %s (%s); want %s", got, f2.Format('p', 0), want)
+ t.Errorf("got %s (%s); want %s", got, f2.Text('p', 0), want)
}
}
}
p := makeFloat(test)
a := new(Float).Abs(p)
if !alike(a, p) {
- t.Errorf("%s: got %s; want %s", test, a.Format('g', 10), test)
+ t.Errorf("%s: got %s; want %s", test, a.Text('g', 10), test)
}
n := makeFloat("-" + test)
a.Abs(n)
if !alike(a, p) {
- t.Errorf("-%s: got %s; want %s", test, a.Format('g', 10), test)
+ t.Errorf("-%s: got %s; want %s", test, a.Text('g', 10), test)
}
}
}
n2 := new(Float).Neg(p1)
p2 := new(Float).Neg(n2)
if !alike(n2, n1) {
- t.Errorf("%s: got %s; want %s", test, n2.Format('g', 10), n1.Format('g', 10))
+ t.Errorf("%s: got %s; want %s", test, n2.Text('g', 10), n1.Text('g', 10))
}
if !alike(p2, p1) {
- t.Errorf("%s: got %s; want %s", test, p2.Format('g', 10), p1.Format('g', 10))
+ t.Errorf("%s: got %s; want %s", test, p2.Text('g', 10), p1.Text('g', 10))
}
}
}
cc, acc := C.Float64()
if cc != c {
- t.Errorf("%g/%g = %s; want %.5g\n", a, b, C.Format('g', 5), c)
+ t.Errorf("%g/%g = %s; want %.5g\n", a, b, C.Text('g', 5), c)
continue
}
if acc != Exact {
default:
panic("unreachable")
}
- if got := z.Format('p', 0); got != test.want || z.Acc() != test.acc {
+ if got := z.Text('p', 0); got != test.want || z.Acc() != test.acc {
t.Errorf(
"prec = %d (%s): %s %c %s = %s (%s); want %s (%s)",
- test.prec, test.mode, x.Format('p', 0), test.op, y.Format('p', 0), got, z.Acc(), test.want, test.acc,
+ test.prec, test.mode, x.Text('p', 0), test.op, y.Text('p', 0), got, z.Acc(), test.want, test.acc,
)
}
}
"strings"
)
-// Format converts the floating-point number x to a string according
-// to the given format and precision prec. The format is one of:
+// Text converts the floating-point number x to a string according
+// to the given format and precision prec. The format must be one of:
//
// 'e' -d.dddde±dd, decimal exponent, at least two (possibly 0) exponent digits
// 'E' -d.ddddE±dd, decimal exponent, at least two (possibly 0) exponent digits
// The prec value is ignored for the 'b' or 'p' format.
//
// BUG(gri) Float.Format does not accept negative precisions.
-// BUG(gri) The Float.Format signature conflicts with Format(f fmt.State, c rune).
-// (https://github.com/golang/go/issues/10938)
-func (x *Float) Format(format byte, prec int) string {
+func (x *Float) Text(format byte, prec int) string {
const extra = 10 // TODO(gri) determine a good/better value here
return string(x.Append(make([]byte, 0, prec+extra), format, prec))
}
-// String formats x like x.Format('g', 10).
+// String formats x like x.Text('g', 10).
func (x *Float) String() string {
- return x.Format('g', 10)
+ return x.Text('g', 10)
}
// Append appends to buf the string form of the floating-point number x,
-// as generated by x.Format, and returns the extended buffer.
+// as generated by x.Text, and returns the extended buffer.
func (x *Float) Append(buf []byte, fmt byte, prec int) []byte {
// sign
if x.neg {