Commit b8fcae02 authored by Robert Griesemer's avatar Robert Griesemer

math/big: fix %b format so it matches strconf %b format for non-zero values

(For zero values the strconv %b format prints the bias-adjusted exponent;
there's no bias in Float.)

Change-Id: I6f4dda9c3a50d02eac375cfe2c927c1540aae865
Reviewed-on: https://go-review.googlesource.com/3841Reviewed-by: 's avatarAlan Donovan <adonovan@google.com>
parent bbd67716
......@@ -205,12 +205,6 @@ func (x *Float) String() string {
return x.Format('p', 0)
}
// TODO(gri) The 'b' and 'p' formats have different meanings here than
// in strconv: in strconv, the printed exponent is the biased (hardware)
// exponent; here it is the unbiased exponent. Decide what to do.
// (a strconv 'p' formatted float value can only be interpreted correctly
// if the bias is known; i.e., we must know if it's a 32bit or 64bit number).
// bstring appends the string of x in the format ["-"] mantissa "p" exponent
// with a decimal mantissa and a binary exponent, or ["-"] "0" if x is zero,
// and returns the extended buffer.
......@@ -233,7 +227,11 @@ func (x *Float) bstring(buf []byte) []byte {
}
buf = append(buf, m.decimalString()...)
buf = append(buf, 'p')
return strconv.AppendInt(buf, int64(x.exp), 10)
e := int64(x.exp) - int64(x.prec)
if e >= 0 {
buf = append(buf, '+')
}
return strconv.AppendInt(buf, e, 10)
}
// pstring appends the string of x in the format ["-"] "0x." mantissa "p" exponent
......
......@@ -9,10 +9,11 @@ import (
"testing"
)
var floatSetFloat64StringTests = []struct {
func TestFloatSetFloat64String(t *testing.T) {
for _, test := range []struct {
s string
x float64
}{
}{
{"0", 0},
{"-0", -0},
{"+0", 0},
......@@ -54,10 +55,7 @@ var floatSetFloat64StringTests = []struct {
{".0000000000000000000000000000000000000001", 1e-40},
{"+10000000000000000000000000000000000000000e-0", 1e40},
}
func TestFloatSetFloat64String(t *testing.T) {
for _, test := range floatSetFloat64StringTests {
} {
var x Float
x.prec = 53 // TODO(gri) find better solution
_, ok := x.SetString(test.s)
......@@ -82,8 +80,9 @@ func TestFloatFormat(t *testing.T) {
}{
{"0", 'b', 0, "0"},
{"-0", 'b', 0, "-0"},
{"1.0", 'b', 0, "4503599627370496p1"},
{"-1.0", 'b', 0, "-4503599627370496p1"},
{"1.0", 'b', 0, "4503599627370496p-52"},
{"-1.0", 'b', 0, "-4503599627370496p-52"},
{"4503599627370496", 'b', 0, "4503599627370496p+0"},
{"0", 'p', 0, "0"},
{"-0", 'p', 0, "-0"},
......@@ -95,14 +94,21 @@ func TestFloatFormat(t *testing.T) {
t.Error(err)
continue
}
f := new(Float).SetFloat64(f64)
got := f.Format(test.format, test.prec)
if got != test.want {
t.Errorf("%v: got %s", test, got)
t.Errorf("%v: got %s; want %s", test, got, test.want)
}
if test.format == 'b' || test.format == 'p' {
continue // 'b', 'p' format not supported or different in strconv.Format
if test.format == 'b' && f64 == 0 {
continue // 'b' format in strconv.Float requires knowledge of bias for 0.0
}
if test.format == 'p' {
continue // 'p' format not supported in strconv.Format
}
// verify that Float format matches strconv format
want := strconv.FormatFloat(f64, test.format, test.prec, 64)
if got != want {
t.Errorf("%v: got %s; want %s", test, got, want)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment