]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: fix formatting for 'b' format
authorRobert Griesemer <gri@golang.org>
Mon, 23 Feb 2015 21:15:10 +0000 (13:15 -0800)
committerRobert Griesemer <gri@golang.org>
Mon, 23 Feb 2015 21:23:50 +0000 (21:23 +0000)
Fixes #9939.

Change-Id: I9d60722b648fbc00650115da539a7466c6c86552
Reviewed-on: https://go-review.googlesource.com/5640
Reviewed-by: Alan Donovan <adonovan@google.com>
src/math/big/floatconv.go
src/math/big/floatconv_test.go

index e41d447db0ead3a0514ca6e01ef9c513a98ecce2..207e34a46c9917c05e298d91ec8f3a72923f389b 100644 (file)
@@ -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:
 //
-//     '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
 //
 // 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
 // with a decimal mantissa and a binary exponent, or ["-"] "0" if x is zero,
 // 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.
 func (x *Float) bstring(buf []byte) []byte {
        if x.neg {
@@ -231,12 +231,16 @@ func (x *Float) bstring(buf []byte) []byte {
                return append(buf, '0')
        }
        // x != 0
-       // normalize mantissa
+
+       // adjust mantissa to use exactly x.prec bits
        m := x.mant
-       t := uint(len(x.mant)*_W) - x.prec // 0 <= t < _W
-       if t > 0 {
-               m = nat(nil).shr(m, t)
+       switch w := uint(len(x.mant)) * _W; {
+       case w < x.prec:
+               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, 'p')
        e := int64(x.exp) - int64(x.prec)
@@ -258,7 +262,16 @@ func (x *Float) pstring(buf []byte) []byte {
                return append(buf, '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, strings.TrimRight(x.mant.hexString(), "0")...)
        buf = append(buf, 'p')
index a22a1f7ddf0830b8ab2b447d1a4253d2e22ae8af..4a91a63e64276888699175f258ab25d6eed352e3 100644 (file)
@@ -240,86 +240,112 @@ func TestFloat64Format(t *testing.T) {
 func TestFloatFormat(t *testing.T) {
        for _, test := range []struct {
                x      string
+               prec   uint
                format byte
-               prec   int
+               digits int
                want   string
        }{
-               {"0", 'f', 0, "0"},
-               {"-0", 'f', 0, "-0"},
-               {"1", 'f', 0, "1"},
-               {"-1", 'f', 0, "-1"},
-
-               {"1.459", 'e', 0, "1e+00"},
-               {"2.459", 'e', 1, "2.5e+00"},
-               {"3.459", 'e', 2, "3.46e+00"},
-               {"4.459", 'e', 3, "4.459e+00"},
-               {"5.459", 'e', 4, "5.4590e+00"},
-
-               {"1.459", 'E', 0, "1E+00"},
-               {"2.459", 'E', 1, "2.5E+00"},
-               {"3.459", 'E', 2, "3.46E+00"},
-               {"4.459", 'E', 3, "4.459E+00"},
-               {"5.459", 'E', 4, "5.4590E+00"},
-
-               {"1.459", 'f', 0, "1"},
-               {"2.459", 'f', 1, "2.5"},
-               {"3.459", 'f', 2, "3.46"},
-               {"4.459", 'f', 3, "4.459"},
-               {"5.459", 'f', 4, "5.4590"},
-
-               {"1.459", 'g', 0, "1"},
-               {"2.459", 'g', 1, "2"},
-               {"3.459", 'g', 2, "3.5"},
-               {"4.459", 'g', 3, "4.46"},
-               {"5.459", 'g', 4, "5.459"},
-
-               {"1459", 'g', 0, "1e+03"},
-               {"2459", 'g', 1, "2e+03"},
-               {"3459", 'g', 2, "3.5e+03"},
-               {"4459", 'g', 3, "4.46e+03"},
-               {"5459", 'g', 4, "5459"},
-
-               {"1459", 'G', 0, "1E+03"},
-               {"2459", 'G', 1, "2E+03"},
-               {"3459", 'G', 2, "3.5E+03"},
-               {"4459", 'G', 3, "4.46E+03"},
-               {"5459", 'G', 4, "5459"},
-
-               {"3", 'e', 40, "3.0000000000000000000000000000000000000000e+00"},
-               {"3", 'f', 40, "3.0000000000000000000000000000000000000000"},
-               {"3", 'g', 40, "3"},
-
-               {"3e40", 'e', 40, "3.0000000000000000000000000000000000000000e+40"},
-               {"3e40", 'f', 4, "30000000000000000000000000000000000000000.0000"},
-               {"3e40", 'g', 40, "3e+40"},
+               {"0", 10, 'f', 0, "0"},
+               {"-0", 10, 'f', 0, "-0"},
+               {"1", 10, 'f', 0, "1"},
+               {"-1", 10, 'f', 0, "-1"},
+
+               {"1.459", 100, 'e', 0, "1e+00"},
+               {"2.459", 100, 'e', 1, "2.5e+00"},
+               {"3.459", 100, 'e', 2, "3.46e+00"},
+               {"4.459", 100, 'e', 3, "4.459e+00"},
+               {"5.459", 100, 'e', 4, "5.4590e+00"},
+
+               {"1.459", 100, 'E', 0, "1E+00"},
+               {"2.459", 100, 'E', 1, "2.5E+00"},
+               {"3.459", 100, 'E', 2, "3.46E+00"},
+               {"4.459", 100, 'E', 3, "4.459E+00"},
+               {"5.459", 100, 'E', 4, "5.4590E+00"},
+
+               {"1.459", 100, 'f', 0, "1"},
+               {"2.459", 100, 'f', 1, "2.5"},
+               {"3.459", 100, 'f', 2, "3.46"},
+               {"4.459", 100, 'f', 3, "4.459"},
+               {"5.459", 100, 'f', 4, "5.4590"},
+
+               {"1.459", 100, 'g', 0, "1"},
+               {"2.459", 100, 'g', 1, "2"},
+               {"3.459", 100, 'g', 2, "3.5"},
+               {"4.459", 100, 'g', 3, "4.46"},
+               {"5.459", 100, 'g', 4, "5.459"},
+
+               {"1459", 53, 'g', 0, "1e+03"},
+               {"2459", 53, 'g', 1, "2e+03"},
+               {"3459", 53, 'g', 2, "3.5e+03"},
+               {"4459", 53, 'g', 3, "4.46e+03"},
+               {"5459", 53, 'g', 4, "5459"},
+
+               {"1459", 53, 'G', 0, "1E+03"},
+               {"2459", 53, 'G', 1, "2E+03"},
+               {"3459", 53, 'G', 2, "3.5E+03"},
+               {"4459", 53, 'G', 3, "4.46E+03"},
+               {"5459", 53, 'G', 4, "5459"},
+
+               {"3", 10, 'e', 40, "3.0000000000000000000000000000000000000000e+00"},
+               {"3", 10, 'f', 40, "3.0000000000000000000000000000000000000000"},
+               {"3", 10, 'g', 40, "3"},
+
+               {"3e40", 100, 'e', 40, "3.0000000000000000000000000000000000000000e+40"},
+               {"3e40", 100, 'f', 4, "30000000000000000000000000000000000000000.0000"},
+               {"3e40", 100, 'g', 40, "3e+40"},
 
                // TODO(gri) need tests for actual large Floats
 
-               // These depend on the selected mantissa length to match strconv.FormatFloat.
-               // Disabled for now.
-               // {"0", 'b', 0, "0"},
-               // {"-0", 'b', 0, "-0"},
-               // {"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"},
-               {"1024.0", 'p', 0, "0x.8p11"},
-               {"-1024.0", 'p', 0, "-0x.8p11"},
+               {"0", 53, 'b', 0, "0"},
+               {"-0", 53, 'b', 0, "-0"},
+               {"1.0", 53, 'b', 0, "4503599627370496p-52"},
+               {"-1.0", 53, 'b', 0, "-4503599627370496p-52"},
+               {"4503599627370496", 53, 'b', 0, "4503599627370496p+0"},
+
+               // issue 9939
+               {"3", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
+               {"03", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
+               {"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
-               {"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 {
                        t.Errorf("%v: %s", test, err)
                        continue
                }
 
-               got := f.Format(test.format, test.prec)
+               got := f.Format(test.format, test.digits)
                if 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)
+                       }
+               }
        }
 }