Skip to content

Commit

Permalink
math/big: rename (*Float).Format to (*Float).Text
Browse files Browse the repository at this point in the history
This paves the way for a fmt-compatible (*Float).Format method.
A better name then Text is still desirable (suggestions welcome).

This is partly fixing issue golang#10938.

Change-Id: I59c20a8cee11f5dba059fe0f38b414fe75f2ab13
Reviewed-on: https://go-review.googlesource.com/10493
Reviewed-by: Alan Donovan <[email protected]>
  • Loading branch information
griesemer committed May 29, 2015
1 parent c413c45 commit 9b3d923
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/math/big/bits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func TestFromBits(t *testing.T) {
{append(Bits{2, 1, 0} /* 7 */, Bits{3, 1} /* 10 */ ...), "0x.88p+5" /* 17 */},
} {
f := test.bits.Float()
if got := f.Format('p', 0); got != test.want {
if got := f.Text('p', 0); got != test.want {
t.Errorf("setBits(%v) = %s; want %s", test.bits, got, test.want)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/math/big/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func (x *Float) validate() {
}
const msb = 1 << (_W - 1)
if x.mant[m-1]&msb == 0 {
panic(fmt.Sprintf("msb not set in last word %#x of %s", x.mant[m-1], x.Format('p', 0)))
panic(fmt.Sprintf("msb not set in last word %#x of %s", x.mant[m-1], x.Text('p', 0)))
}
if x.prec == 0 {
panic("zero precision finite number")
Expand Down
50 changes: 25 additions & 25 deletions src/math/big/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ var _ error = ErrNaN{}
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)
}

Expand Down Expand Up @@ -236,7 +236,7 @@ func TestFloatMantExp(t *testing.T) {
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)
}
}
}
Expand All @@ -247,7 +247,7 @@ func TestFloatMantExpAliasing(t *testing.T) {
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))
}
}

Expand Down Expand Up @@ -279,12 +279,12 @@ func TestFloatSetMantExp(t *testing.T) {
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)
}
}
}
Expand Down Expand Up @@ -562,7 +562,7 @@ func TestFloatSetUint64(t *testing.T) {
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)
}
}

Expand All @@ -573,7 +573,7 @@ func TestFloatSetUint64(t *testing.T) {
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)
}
}
}
Expand All @@ -596,7 +596,7 @@ func TestFloatSetInt64(t *testing.T) {
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)
}
}
}
Expand All @@ -608,7 +608,7 @@ func TestFloatSetInt64(t *testing.T) {
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)
}
}
}
Expand Down Expand Up @@ -639,7 +639,7 @@ func TestFloatSetFloat64(t *testing.T) {
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)
}
}
}
Expand All @@ -651,7 +651,7 @@ func TestFloatSetFloat64(t *testing.T) {
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)
}
}

Expand All @@ -664,7 +664,7 @@ func TestFloatSetFloat64(t *testing.T) {
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) {
Expand Down Expand Up @@ -696,9 +696,9 @@ 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)
}
}

Expand Down Expand Up @@ -738,9 +738,9 @@ func TestFloatSetRat(t *testing.T) {
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)
}
}
}
Expand Down Expand Up @@ -1096,13 +1096,13 @@ func TestFloatAbs(t *testing.T) {
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)
}
}
}
Expand All @@ -1122,10 +1122,10 @@ func TestFloatNeg(t *testing.T) {
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))
}
}
}
Expand Down Expand Up @@ -1467,7 +1467,7 @@ func TestFloatQuoSmoke(t *testing.T) {

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 {
Expand Down Expand Up @@ -1608,10 +1608,10 @@ func TestFloatArithmeticOverflow(t *testing.T) {
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,
)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/math/big/floatconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const (
above1e23 = 100000000000000008388608
)

func TestFloat64Format(t *testing.T) {
func TestFloat64Text(t *testing.T) {
for _, test := range []struct {
x float64
format byte
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestFloat64Format(t *testing.T) {
// {383260575764816448, 'g', -1, "3.8326057576481645e+17"},
} {
f := new(Float).SetFloat64(test.x)
got := f.Format(test.format, test.prec)
got := f.Text(test.format, test.prec)
if got != test.want {
t.Errorf("%v: got %s; want %s", test, got, test.want)
}
Expand All @@ -277,7 +277,7 @@ func TestFloat64Format(t *testing.T) {
}
}

func TestFloatFormat(t *testing.T) {
func TestFloatText(t *testing.T) {
for _, test := range []struct {
x string
prec uint
Expand Down Expand Up @@ -378,7 +378,7 @@ func TestFloatFormat(t *testing.T) {
continue
}

got := f.Format(test.format, test.digits)
got := f.Text(test.format, test.digits)
if got != test.want {
t.Errorf("%v: got %s; want %s", test, got, test.want)
}
Expand Down
6 changes: 3 additions & 3 deletions src/math/big/floatexample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func ExampleFloat_Add() {
y.SetFloat64(2.718281828) // y is automatically set to 53bit precision
z.SetPrec(32)
z.Add(&x, &y)
fmt.Printf("x = %s (%s, prec = %d, acc = %s)\n", &x, x.Format('p', 0), x.Prec(), x.Acc())
fmt.Printf("y = %s (%s, prec = %d, acc = %s)\n", &y, y.Format('p', 0), y.Prec(), y.Acc())
fmt.Printf("z = %s (%s, prec = %d, acc = %s)\n", &z, z.Format('p', 0), z.Prec(), z.Acc())
fmt.Printf("x = %s (%s, prec = %d, acc = %s)\n", &x, x.Text('p', 0), x.Prec(), x.Acc())
fmt.Printf("y = %s (%s, prec = %d, acc = %s)\n", &y, y.Text('p', 0), y.Prec(), y.Acc())
fmt.Printf("z = %s (%s, prec = %d, acc = %s)\n", &z, z.Text('p', 0), z.Prec(), z.Acc())
// Output:
// x = 1000 (0x.fap+10, prec = 64, acc = Exact)
// y = 2.718281828 (0x.adf85458248cd8p+2, prec = 53, acc = Exact)
Expand Down
14 changes: 6 additions & 8 deletions src/math/big/ftoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"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
Expand All @@ -37,20 +37,18 @@ import (
// 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 {
Expand Down

0 comments on commit 9b3d923

Please sign in to comment.