mirror of
https://github.com/golang/go
synced 2024-11-23 18:00:06 -07:00
math/big: fix formatting for 'b' format
Fixes #9939. Change-Id: I9d60722b648fbc00650115da539a7466c6c86552 Reviewed-on: https://go-review.googlesource.com/5640 Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
parent
f0bbb5c450
commit
ce137592c0
@ -171,7 +171,7 @@ func ParseFloat(s string, base int, prec uint, mode RoundingMode) (f *Float, b i
|
|||||||
//
|
//
|
||||||
// For the binary exponent formats, the mantissa is printed in normalized form:
|
// For the binary exponent formats, the mantissa is printed in normalized form:
|
||||||
//
|
//
|
||||||
// 'b' decimal integer mantissa using x.Precision() bits, or -0
|
// 'b' decimal integer mantissa using x.Prec() bits, or -0
|
||||||
// 'p' hexadecimal fraction with 0.5 <= 0.mantissa < 1.0, or -0
|
// 'p' hexadecimal fraction with 0.5 <= 0.mantissa < 1.0, or -0
|
||||||
//
|
//
|
||||||
// The precision prec controls the number of digits (excluding the exponent)
|
// The precision prec controls the number of digits (excluding the exponent)
|
||||||
@ -221,7 +221,7 @@ func (x *Float) String() string {
|
|||||||
// bstring appends the string of x in the format ["-"] mantissa "p" exponent
|
// 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,
|
// with a decimal mantissa and a binary exponent, or ["-"] "0" if x is zero,
|
||||||
// and returns the extended buffer.
|
// and returns the extended buffer.
|
||||||
// The mantissa is normalized such that is uses x.Precision() bits in binary
|
// The mantissa is normalized such that is uses x.Prec() bits in binary
|
||||||
// representation.
|
// representation.
|
||||||
func (x *Float) bstring(buf []byte) []byte {
|
func (x *Float) bstring(buf []byte) []byte {
|
||||||
if x.neg {
|
if x.neg {
|
||||||
@ -231,12 +231,16 @@ func (x *Float) bstring(buf []byte) []byte {
|
|||||||
return append(buf, '0')
|
return append(buf, '0')
|
||||||
}
|
}
|
||||||
// x != 0
|
// x != 0
|
||||||
// normalize mantissa
|
|
||||||
|
// adjust mantissa to use exactly x.prec bits
|
||||||
m := x.mant
|
m := x.mant
|
||||||
t := uint(len(x.mant)*_W) - x.prec // 0 <= t < _W
|
switch w := uint(len(x.mant)) * _W; {
|
||||||
if t > 0 {
|
case w < x.prec:
|
||||||
m = nat(nil).shr(m, t)
|
m = nat(nil).shl(m, x.prec-w)
|
||||||
|
case w > x.prec:
|
||||||
|
m = nat(nil).shr(m, w-x.prec)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = append(buf, m.decimalString()...)
|
buf = append(buf, m.decimalString()...)
|
||||||
buf = append(buf, 'p')
|
buf = append(buf, 'p')
|
||||||
e := int64(x.exp) - int64(x.prec)
|
e := int64(x.exp) - int64(x.prec)
|
||||||
@ -258,7 +262,16 @@ func (x *Float) pstring(buf []byte) []byte {
|
|||||||
return append(buf, '0')
|
return append(buf, '0')
|
||||||
}
|
}
|
||||||
// x != 0
|
// x != 0
|
||||||
// mantissa is stored in normalized form
|
|
||||||
|
// remove trailing 0 words early
|
||||||
|
// (no need to convert to hex 0's and trim later)
|
||||||
|
m := x.mant
|
||||||
|
i := 0
|
||||||
|
for i < len(m) && m[i] == 0 {
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
m = m[i:]
|
||||||
|
|
||||||
buf = append(buf, "0x."...)
|
buf = append(buf, "0x."...)
|
||||||
buf = append(buf, strings.TrimRight(x.mant.hexString(), "0")...)
|
buf = append(buf, strings.TrimRight(x.mant.hexString(), "0")...)
|
||||||
buf = append(buf, 'p')
|
buf = append(buf, 'p')
|
||||||
|
@ -240,86 +240,112 @@ func TestFloat64Format(t *testing.T) {
|
|||||||
func TestFloatFormat(t *testing.T) {
|
func TestFloatFormat(t *testing.T) {
|
||||||
for _, test := range []struct {
|
for _, test := range []struct {
|
||||||
x string
|
x string
|
||||||
|
prec uint
|
||||||
format byte
|
format byte
|
||||||
prec int
|
digits int
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{"0", 'f', 0, "0"},
|
{"0", 10, 'f', 0, "0"},
|
||||||
{"-0", 'f', 0, "-0"},
|
{"-0", 10, 'f', 0, "-0"},
|
||||||
{"1", 'f', 0, "1"},
|
{"1", 10, 'f', 0, "1"},
|
||||||
{"-1", 'f', 0, "-1"},
|
{"-1", 10, 'f', 0, "-1"},
|
||||||
|
|
||||||
{"1.459", 'e', 0, "1e+00"},
|
{"1.459", 100, 'e', 0, "1e+00"},
|
||||||
{"2.459", 'e', 1, "2.5e+00"},
|
{"2.459", 100, 'e', 1, "2.5e+00"},
|
||||||
{"3.459", 'e', 2, "3.46e+00"},
|
{"3.459", 100, 'e', 2, "3.46e+00"},
|
||||||
{"4.459", 'e', 3, "4.459e+00"},
|
{"4.459", 100, 'e', 3, "4.459e+00"},
|
||||||
{"5.459", 'e', 4, "5.4590e+00"},
|
{"5.459", 100, 'e', 4, "5.4590e+00"},
|
||||||
|
|
||||||
{"1.459", 'E', 0, "1E+00"},
|
{"1.459", 100, 'E', 0, "1E+00"},
|
||||||
{"2.459", 'E', 1, "2.5E+00"},
|
{"2.459", 100, 'E', 1, "2.5E+00"},
|
||||||
{"3.459", 'E', 2, "3.46E+00"},
|
{"3.459", 100, 'E', 2, "3.46E+00"},
|
||||||
{"4.459", 'E', 3, "4.459E+00"},
|
{"4.459", 100, 'E', 3, "4.459E+00"},
|
||||||
{"5.459", 'E', 4, "5.4590E+00"},
|
{"5.459", 100, 'E', 4, "5.4590E+00"},
|
||||||
|
|
||||||
{"1.459", 'f', 0, "1"},
|
{"1.459", 100, 'f', 0, "1"},
|
||||||
{"2.459", 'f', 1, "2.5"},
|
{"2.459", 100, 'f', 1, "2.5"},
|
||||||
{"3.459", 'f', 2, "3.46"},
|
{"3.459", 100, 'f', 2, "3.46"},
|
||||||
{"4.459", 'f', 3, "4.459"},
|
{"4.459", 100, 'f', 3, "4.459"},
|
||||||
{"5.459", 'f', 4, "5.4590"},
|
{"5.459", 100, 'f', 4, "5.4590"},
|
||||||
|
|
||||||
{"1.459", 'g', 0, "1"},
|
{"1.459", 100, 'g', 0, "1"},
|
||||||
{"2.459", 'g', 1, "2"},
|
{"2.459", 100, 'g', 1, "2"},
|
||||||
{"3.459", 'g', 2, "3.5"},
|
{"3.459", 100, 'g', 2, "3.5"},
|
||||||
{"4.459", 'g', 3, "4.46"},
|
{"4.459", 100, 'g', 3, "4.46"},
|
||||||
{"5.459", 'g', 4, "5.459"},
|
{"5.459", 100, 'g', 4, "5.459"},
|
||||||
|
|
||||||
{"1459", 'g', 0, "1e+03"},
|
{"1459", 53, 'g', 0, "1e+03"},
|
||||||
{"2459", 'g', 1, "2e+03"},
|
{"2459", 53, 'g', 1, "2e+03"},
|
||||||
{"3459", 'g', 2, "3.5e+03"},
|
{"3459", 53, 'g', 2, "3.5e+03"},
|
||||||
{"4459", 'g', 3, "4.46e+03"},
|
{"4459", 53, 'g', 3, "4.46e+03"},
|
||||||
{"5459", 'g', 4, "5459"},
|
{"5459", 53, 'g', 4, "5459"},
|
||||||
|
|
||||||
{"1459", 'G', 0, "1E+03"},
|
{"1459", 53, 'G', 0, "1E+03"},
|
||||||
{"2459", 'G', 1, "2E+03"},
|
{"2459", 53, 'G', 1, "2E+03"},
|
||||||
{"3459", 'G', 2, "3.5E+03"},
|
{"3459", 53, 'G', 2, "3.5E+03"},
|
||||||
{"4459", 'G', 3, "4.46E+03"},
|
{"4459", 53, 'G', 3, "4.46E+03"},
|
||||||
{"5459", 'G', 4, "5459"},
|
{"5459", 53, 'G', 4, "5459"},
|
||||||
|
|
||||||
{"3", 'e', 40, "3.0000000000000000000000000000000000000000e+00"},
|
{"3", 10, 'e', 40, "3.0000000000000000000000000000000000000000e+00"},
|
||||||
{"3", 'f', 40, "3.0000000000000000000000000000000000000000"},
|
{"3", 10, 'f', 40, "3.0000000000000000000000000000000000000000"},
|
||||||
{"3", 'g', 40, "3"},
|
{"3", 10, 'g', 40, "3"},
|
||||||
|
|
||||||
{"3e40", 'e', 40, "3.0000000000000000000000000000000000000000e+40"},
|
{"3e40", 100, 'e', 40, "3.0000000000000000000000000000000000000000e+40"},
|
||||||
{"3e40", 'f', 4, "30000000000000000000000000000000000000000.0000"},
|
{"3e40", 100, 'f', 4, "30000000000000000000000000000000000000000.0000"},
|
||||||
{"3e40", 'g', 40, "3e+40"},
|
{"3e40", 100, 'g', 40, "3e+40"},
|
||||||
|
|
||||||
// TODO(gri) need tests for actual large Floats
|
// TODO(gri) need tests for actual large Floats
|
||||||
|
|
||||||
// These depend on the selected mantissa length to match strconv.FormatFloat.
|
{"0", 53, 'b', 0, "0"},
|
||||||
// Disabled for now.
|
{"-0", 53, 'b', 0, "-0"},
|
||||||
// {"0", 'b', 0, "0"},
|
{"1.0", 53, 'b', 0, "4503599627370496p-52"},
|
||||||
// {"-0", 'b', 0, "-0"},
|
{"-1.0", 53, 'b', 0, "-4503599627370496p-52"},
|
||||||
// {"1.0", 'b', 0, "4503599627370496p-52"},
|
{"4503599627370496", 53, 'b', 0, "4503599627370496p+0"},
|
||||||
// {"-1.0", 'b', 0, "-4503599627370496p-52"},
|
|
||||||
// {"4503599627370496", 'b', 0, "4503599627370496p+0"},
|
|
||||||
|
|
||||||
{"0", 'p', 0, "0"},
|
// issue 9939
|
||||||
{"-0", 'p', 0, "-0"},
|
{"3", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
|
||||||
{"1024.0", 'p', 0, "0x.8p11"},
|
{"03", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
|
||||||
{"-1024.0", 'p', 0, "-0x.8p11"},
|
{"3.", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
|
||||||
|
{"3.0", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
|
||||||
|
{"3.00", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
|
||||||
|
{"3.000", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
|
||||||
|
|
||||||
|
{"3", 350, 'p', 0, "0x.cp2"},
|
||||||
|
{"03", 350, 'p', 0, "0x.cp2"},
|
||||||
|
{"3.", 350, 'p', 0, "0x.cp2"},
|
||||||
|
{"3.0", 350, 'p', 0, "0x.cp2"},
|
||||||
|
{"3.00", 350, 'p', 0, "0x.cp2"},
|
||||||
|
{"3.000", 350, 'p', 0, "0x.cp2"},
|
||||||
|
|
||||||
|
{"0", 64, 'p', 0, "0"},
|
||||||
|
{"-0", 64, 'p', 0, "-0"},
|
||||||
|
{"1024.0", 64, 'p', 0, "0x.8p11"},
|
||||||
|
{"-1024.0", 64, 'p', 0, "-0x.8p11"},
|
||||||
|
|
||||||
// unsupported format
|
// unsupported format
|
||||||
{"3.14", 'x', 0, "%x"},
|
{"3.14", 64, 'x', 0, "%x"},
|
||||||
} {
|
} {
|
||||||
f, _, err := ParseFloat(test.x, 0, 1000, ToNearestEven)
|
f, _, err := ParseFloat(test.x, 0, test.prec, ToNearestEven)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("%v: %s", test, err)
|
t.Errorf("%v: %s", test, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
got := f.Format(test.format, test.prec)
|
got := f.Format(test.format, test.digits)
|
||||||
if got != test.want {
|
if got != test.want {
|
||||||
t.Errorf("%v: got %s; want %s", test, got, test.want)
|
t.Errorf("%v: got %s; want %s", test, got, test.want)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// compare with strconv.FormatFloat output if possible
|
||||||
|
// ('p' format is not supported by strconv.FormatFloat,
|
||||||
|
// and its output for 0.0 prints a biased exponent value
|
||||||
|
// as in 0p-1074 which makes no sense to emulate here)
|
||||||
|
if test.prec == 53 && test.format != 'p' && f.Sign() != 0 {
|
||||||
|
f64, _ := f.Float64()
|
||||||
|
got := strconv.FormatFloat(f64, test.format, test.digits, 64)
|
||||||
|
if got != test.want {
|
||||||
|
t.Errorf("%v: got %s; want %s", test, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user