github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/cmd/compile/internal/big/floatconv_test.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package big
     6  
     7  import (
     8  	"fmt"
     9  	"math"
    10  	"strconv"
    11  	"testing"
    12  )
    13  
    14  func TestFloatSetFloat64String(t *testing.T) {
    15  	inf := math.Inf(0)
    16  	nan := math.NaN()
    17  
    18  	for _, test := range []struct {
    19  		s string
    20  		x float64 // NaNs represent invalid inputs
    21  	}{
    22  		// basics
    23  		{"0", 0},
    24  		{"-0", -0},
    25  		{"+0", 0},
    26  		{"1", 1},
    27  		{"-1", -1},
    28  		{"+1", 1},
    29  		{"1.234", 1.234},
    30  		{"-1.234", -1.234},
    31  		{"+1.234", 1.234},
    32  		{".1", 0.1},
    33  		{"1.", 1},
    34  		{"+1.", 1},
    35  
    36  		// various zeros
    37  		{"0e100", 0},
    38  		{"-0e+100", 0},
    39  		{"+0e-100", 0},
    40  		{"0E100", 0},
    41  		{"-0E+100", 0},
    42  		{"+0E-100", 0},
    43  
    44  		// various decimal exponent formats
    45  		{"1.e10", 1e10},
    46  		{"1e+10", 1e10},
    47  		{"+1e-10", 1e-10},
    48  		{"1E10", 1e10},
    49  		{"1.E+10", 1e10},
    50  		{"+1E-10", 1e-10},
    51  
    52  		// infinities
    53  		{"Inf", inf},
    54  		{"+Inf", inf},
    55  		{"-Inf", -inf},
    56  		{"inf", inf},
    57  		{"+inf", inf},
    58  		{"-inf", -inf},
    59  
    60  		// invalid numbers
    61  		{"", nan},
    62  		{"-", nan},
    63  		{"0x", nan},
    64  		{"0e", nan},
    65  		{"1.2ef", nan},
    66  		{"2..3", nan},
    67  		{"123..", nan},
    68  		{"infinity", nan},
    69  		{"foobar", nan},
    70  
    71  		// misc decimal values
    72  		{"3.14159265", 3.14159265},
    73  		{"-687436.79457e-245", -687436.79457e-245},
    74  		{"-687436.79457E245", -687436.79457e245},
    75  		{".0000000000000000000000000000000000000001", 1e-40},
    76  		{"+10000000000000000000000000000000000000000e-0", 1e40},
    77  
    78  		// decimal mantissa, binary exponent
    79  		{"0p0", 0},
    80  		{"-0p0", -0},
    81  		{"1p10", 1 << 10},
    82  		{"1p+10", 1 << 10},
    83  		{"+1p-10", 1.0 / (1 << 10)},
    84  		{"1024p-12", 0.25},
    85  		{"-1p10", -1024},
    86  		{"1.5p1", 3},
    87  
    88  		// binary mantissa, decimal exponent
    89  		{"0b0", 0},
    90  		{"-0b0", -0},
    91  		{"0b0e+10", 0},
    92  		{"-0b0e-10", -0},
    93  		{"0b1010", 10},
    94  		{"0B1010E2", 1000},
    95  		{"0b.1", 0.5},
    96  		{"0b.001", 0.125},
    97  		{"0b.001e3", 125},
    98  
    99  		// binary mantissa, binary exponent
   100  		{"0b0p+10", 0},
   101  		{"-0b0p-10", -0},
   102  		{"0b.1010p4", 10},
   103  		{"0b1p-1", 0.5},
   104  		{"0b001p-3", 0.125},
   105  		{"0b.001p3", 1},
   106  		{"0b0.01p2", 1},
   107  
   108  		// hexadecimal mantissa and exponent
   109  		{"0x0", 0},
   110  		{"-0x0", -0},
   111  		{"0x0p+10", 0},
   112  		{"-0x0p-10", -0},
   113  		{"0xff", 255},
   114  		{"0X.8p1", 1},
   115  		{"-0X0.00008p16", -0.5},
   116  		{"0x0.0000000000001p-1022", math.SmallestNonzeroFloat64},
   117  		{"0x1.fffffffffffffp1023", math.MaxFloat64},
   118  	} {
   119  		var x Float
   120  		x.SetPrec(53)
   121  		_, ok := x.SetString(test.s)
   122  		if math.IsNaN(test.x) {
   123  			// test.s is invalid
   124  			if ok {
   125  				t.Errorf("%s: want parse error", test.s)
   126  			}
   127  			continue
   128  		}
   129  		// test.s is valid
   130  		if !ok {
   131  			t.Errorf("%s: got parse error", test.s)
   132  			continue
   133  		}
   134  		f, _ := x.Float64()
   135  		want := new(Float).SetFloat64(test.x)
   136  		if x.Cmp(want) != 0 {
   137  			t.Errorf("%s: got %s (%v); want %v", test.s, &x, f, test.x)
   138  		}
   139  	}
   140  }
   141  
   142  const (
   143  	below1e23 = 99999999999999974834176
   144  	above1e23 = 100000000000000008388608
   145  )
   146  
   147  func TestFloat64Text(t *testing.T) {
   148  	for _, test := range []struct {
   149  		x      float64
   150  		format byte
   151  		prec   int
   152  		want   string
   153  	}{
   154  		{0, 'f', 0, "0"},
   155  		{math.Copysign(0, -1), 'f', 0, "-0"},
   156  		{1, 'f', 0, "1"},
   157  		{-1, 'f', 0, "-1"},
   158  
   159  		{0.001, 'e', 0, "1e-03"},
   160  		{0.459, 'e', 0, "5e-01"},
   161  		{1.459, 'e', 0, "1e+00"},
   162  		{2.459, 'e', 1, "2.5e+00"},
   163  		{3.459, 'e', 2, "3.46e+00"},
   164  		{4.459, 'e', 3, "4.459e+00"},
   165  		{5.459, 'e', 4, "5.4590e+00"},
   166  
   167  		{0.001, 'f', 0, "0"},
   168  		{0.459, 'f', 0, "0"},
   169  		{1.459, 'f', 0, "1"},
   170  		{2.459, 'f', 1, "2.5"},
   171  		{3.459, 'f', 2, "3.46"},
   172  		{4.459, 'f', 3, "4.459"},
   173  		{5.459, 'f', 4, "5.4590"},
   174  
   175  		{0, 'b', 0, "0"},
   176  		{math.Copysign(0, -1), 'b', 0, "-0"},
   177  		{1.0, 'b', 0, "4503599627370496p-52"},
   178  		{-1.0, 'b', 0, "-4503599627370496p-52"},
   179  		{4503599627370496, 'b', 0, "4503599627370496p+0"},
   180  
   181  		{0, 'p', 0, "0"},
   182  		{math.Copysign(0, -1), 'p', 0, "-0"},
   183  		{1024.0, 'p', 0, "0x.8p+11"},
   184  		{-1024.0, 'p', 0, "-0x.8p+11"},
   185  
   186  		// all test cases below from strconv/ftoa_test.go
   187  		{1, 'e', 5, "1.00000e+00"},
   188  		{1, 'f', 5, "1.00000"},
   189  		{1, 'g', 5, "1"},
   190  		// {1, 'g', -1, "1"},
   191  		// {20, 'g', -1, "20"},
   192  		// {1234567.8, 'g', -1, "1.2345678e+06"},
   193  		// {200000, 'g', -1, "200000"},
   194  		// {2000000, 'g', -1, "2e+06"},
   195  
   196  		// g conversion and zero suppression
   197  		{400, 'g', 2, "4e+02"},
   198  		{40, 'g', 2, "40"},
   199  		{4, 'g', 2, "4"},
   200  		{.4, 'g', 2, "0.4"},
   201  		{.04, 'g', 2, "0.04"},
   202  		{.004, 'g', 2, "0.004"},
   203  		{.0004, 'g', 2, "0.0004"},
   204  		{.00004, 'g', 2, "4e-05"},
   205  		{.000004, 'g', 2, "4e-06"},
   206  
   207  		{0, 'e', 5, "0.00000e+00"},
   208  		{0, 'f', 5, "0.00000"},
   209  		{0, 'g', 5, "0"},
   210  		// {0, 'g', -1, "0"},
   211  
   212  		{-1, 'e', 5, "-1.00000e+00"},
   213  		{-1, 'f', 5, "-1.00000"},
   214  		{-1, 'g', 5, "-1"},
   215  		// {-1, 'g', -1, "-1"},
   216  
   217  		{12, 'e', 5, "1.20000e+01"},
   218  		{12, 'f', 5, "12.00000"},
   219  		{12, 'g', 5, "12"},
   220  		// {12, 'g', -1, "12"},
   221  
   222  		{123456700, 'e', 5, "1.23457e+08"},
   223  		{123456700, 'f', 5, "123456700.00000"},
   224  		{123456700, 'g', 5, "1.2346e+08"},
   225  		// {123456700, 'g', -1, "1.234567e+08"},
   226  
   227  		{1.2345e6, 'e', 5, "1.23450e+06"},
   228  		{1.2345e6, 'f', 5, "1234500.00000"},
   229  		{1.2345e6, 'g', 5, "1.2345e+06"},
   230  
   231  		{1e23, 'e', 17, "9.99999999999999916e+22"},
   232  		{1e23, 'f', 17, "99999999999999991611392.00000000000000000"},
   233  		{1e23, 'g', 17, "9.9999999999999992e+22"},
   234  
   235  		// {1e23, 'e', -1, "1e+23"},
   236  		// {1e23, 'f', -1, "100000000000000000000000"},
   237  		// {1e23, 'g', -1, "1e+23"},
   238  
   239  		{below1e23, 'e', 17, "9.99999999999999748e+22"},
   240  		{below1e23, 'f', 17, "99999999999999974834176.00000000000000000"},
   241  		{below1e23, 'g', 17, "9.9999999999999975e+22"},
   242  
   243  		// {below1e23, 'e', -1, "9.999999999999997e+22"},
   244  		// {below1e23, 'f', -1, "99999999999999970000000"},
   245  		// {below1e23, 'g', -1, "9.999999999999997e+22"},
   246  
   247  		{above1e23, 'e', 17, "1.00000000000000008e+23"},
   248  		{above1e23, 'f', 17, "100000000000000008388608.00000000000000000"},
   249  		// {above1e23, 'g', 17, "1.0000000000000001e+23"},
   250  
   251  		// {above1e23, 'e', -1, "1.0000000000000001e+23"},
   252  		// {above1e23, 'f', -1, "100000000000000010000000"},
   253  		// {above1e23, 'g', -1, "1.0000000000000001e+23"},
   254  
   255  		// {fdiv(5e-304, 1e20), 'g', -1, "5e-324"},
   256  		// {fdiv(-5e-304, 1e20), 'g', -1, "-5e-324"},
   257  
   258  		// {32, 'g', -1, "32"},
   259  		// {32, 'g', 0, "3e+01"},
   260  
   261  		// {100, 'x', -1, "%x"},
   262  
   263  		// {math.NaN(), 'g', -1, "NaN"},
   264  		// {-math.NaN(), 'g', -1, "NaN"},
   265  		{math.Inf(0), 'g', -1, "+Inf"},
   266  		{math.Inf(-1), 'g', -1, "-Inf"},
   267  		{-math.Inf(0), 'g', -1, "-Inf"},
   268  
   269  		{-1, 'b', -1, "-4503599627370496p-52"},
   270  
   271  		// fixed bugs
   272  		{0.9, 'f', 1, "0.9"},
   273  		{0.09, 'f', 1, "0.1"},
   274  		{0.0999, 'f', 1, "0.1"},
   275  		{0.05, 'f', 1, "0.1"},
   276  		{0.05, 'f', 0, "0"},
   277  		{0.5, 'f', 1, "0.5"},
   278  		{0.5, 'f', 0, "0"},
   279  		{1.5, 'f', 0, "2"},
   280  
   281  		// http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
   282  		// {2.2250738585072012e-308, 'g', -1, "2.2250738585072014e-308"},
   283  		// http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
   284  		// {2.2250738585072011e-308, 'g', -1, "2.225073858507201e-308"},
   285  
   286  		// Issue 2625.
   287  		{383260575764816448, 'f', 0, "383260575764816448"},
   288  		// {383260575764816448, 'g', -1, "3.8326057576481645e+17"},
   289  	} {
   290  		f := new(Float).SetFloat64(test.x)
   291  		got := f.Text(test.format, test.prec)
   292  		if got != test.want {
   293  			t.Errorf("%v: got %s; want %s", test, got, test.want)
   294  		}
   295  
   296  		if test.format == 'b' && test.x == 0 {
   297  			continue // 'b' format in strconv.Float requires knowledge of bias for 0.0
   298  		}
   299  		if test.format == 'p' {
   300  			continue // 'p' format not supported in strconv.Format
   301  		}
   302  
   303  		// verify that Float format matches strconv format
   304  		want := strconv.FormatFloat(test.x, test.format, test.prec, 64)
   305  		if got != want {
   306  			t.Errorf("%v: got %s; want %s (strconv)", test, got, want)
   307  		}
   308  	}
   309  }
   310  
   311  func TestFloatText(t *testing.T) {
   312  	for _, test := range []struct {
   313  		x      string
   314  		prec   uint
   315  		format byte
   316  		digits int
   317  		want   string
   318  	}{
   319  		{"0", 10, 'f', 0, "0"},
   320  		{"-0", 10, 'f', 0, "-0"},
   321  		{"1", 10, 'f', 0, "1"},
   322  		{"-1", 10, 'f', 0, "-1"},
   323  
   324  		{"1.459", 100, 'e', 0, "1e+00"},
   325  		{"2.459", 100, 'e', 1, "2.5e+00"},
   326  		{"3.459", 100, 'e', 2, "3.46e+00"},
   327  		{"4.459", 100, 'e', 3, "4.459e+00"},
   328  		{"5.459", 100, 'e', 4, "5.4590e+00"},
   329  
   330  		{"1.459", 100, 'E', 0, "1E+00"},
   331  		{"2.459", 100, 'E', 1, "2.5E+00"},
   332  		{"3.459", 100, 'E', 2, "3.46E+00"},
   333  		{"4.459", 100, 'E', 3, "4.459E+00"},
   334  		{"5.459", 100, 'E', 4, "5.4590E+00"},
   335  
   336  		{"1.459", 100, 'f', 0, "1"},
   337  		{"2.459", 100, 'f', 1, "2.5"},
   338  		{"3.459", 100, 'f', 2, "3.46"},
   339  		{"4.459", 100, 'f', 3, "4.459"},
   340  		{"5.459", 100, 'f', 4, "5.4590"},
   341  
   342  		{"1.459", 100, 'g', 0, "1"},
   343  		{"2.459", 100, 'g', 1, "2"},
   344  		{"3.459", 100, 'g', 2, "3.5"},
   345  		{"4.459", 100, 'g', 3, "4.46"},
   346  		{"5.459", 100, 'g', 4, "5.459"},
   347  
   348  		{"1459", 53, 'g', 0, "1e+03"},
   349  		{"2459", 53, 'g', 1, "2e+03"},
   350  		{"3459", 53, 'g', 2, "3.5e+03"},
   351  		{"4459", 53, 'g', 3, "4.46e+03"},
   352  		{"5459", 53, 'g', 4, "5459"},
   353  
   354  		{"1459", 53, 'G', 0, "1E+03"},
   355  		{"2459", 53, 'G', 1, "2E+03"},
   356  		{"3459", 53, 'G', 2, "3.5E+03"},
   357  		{"4459", 53, 'G', 3, "4.46E+03"},
   358  		{"5459", 53, 'G', 4, "5459"},
   359  
   360  		{"3", 10, 'e', 40, "3.0000000000000000000000000000000000000000e+00"},
   361  		{"3", 10, 'f', 40, "3.0000000000000000000000000000000000000000"},
   362  		{"3", 10, 'g', 40, "3"},
   363  
   364  		{"3e40", 100, 'e', 40, "3.0000000000000000000000000000000000000000e+40"},
   365  		{"3e40", 100, 'f', 4, "30000000000000000000000000000000000000000.0000"},
   366  		{"3e40", 100, 'g', 40, "3e+40"},
   367  
   368  		// make sure "stupid" exponents don't stall the machine
   369  		{"1e1000000", 64, 'p', 0, "0x.88b3a28a05eade3ap+3321929"},
   370  		{"1e1000000000", 64, 'p', 0, "+Inf"},
   371  		{"1e-1000000", 64, 'p', 0, "0x.efb4542cc8ca418ap-3321928"},
   372  		{"1e-1000000000", 64, 'p', 0, "0"},
   373  
   374  		// TODO(gri) need tests for actual large Floats
   375  
   376  		{"0", 53, 'b', 0, "0"},
   377  		{"-0", 53, 'b', 0, "-0"},
   378  		{"1.0", 53, 'b', 0, "4503599627370496p-52"},
   379  		{"-1.0", 53, 'b', 0, "-4503599627370496p-52"},
   380  		{"4503599627370496", 53, 'b', 0, "4503599627370496p+0"},
   381  
   382  		// issue 9939
   383  		{"3", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
   384  		{"03", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
   385  		{"3.", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
   386  		{"3.0", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
   387  		{"3.00", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
   388  		{"3.000", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
   389  
   390  		{"3", 350, 'p', 0, "0x.cp+2"},
   391  		{"03", 350, 'p', 0, "0x.cp+2"},
   392  		{"3.", 350, 'p', 0, "0x.cp+2"},
   393  		{"3.0", 350, 'p', 0, "0x.cp+2"},
   394  		{"3.00", 350, 'p', 0, "0x.cp+2"},
   395  		{"3.000", 350, 'p', 0, "0x.cp+2"},
   396  
   397  		{"0", 64, 'p', 0, "0"},
   398  		{"-0", 64, 'p', 0, "-0"},
   399  		{"1024.0", 64, 'p', 0, "0x.8p+11"},
   400  		{"-1024.0", 64, 'p', 0, "-0x.8p+11"},
   401  
   402  		// unsupported format
   403  		{"3.14", 64, 'x', 0, "%x"},
   404  		{"-3.14", 64, 'x', 0, "%x"},
   405  	} {
   406  		f, _, err := ParseFloat(test.x, 0, test.prec, ToNearestEven)
   407  		if err != nil {
   408  			t.Errorf("%v: %s", test, err)
   409  			continue
   410  		}
   411  
   412  		got := f.Text(test.format, test.digits)
   413  		if got != test.want {
   414  			t.Errorf("%v: got %s; want %s", test, got, test.want)
   415  		}
   416  
   417  		// compare with strconv.FormatFloat output if possible
   418  		// ('p' format is not supported by strconv.FormatFloat,
   419  		// and its output for 0.0 prints a biased exponent value
   420  		// as in 0p-1074 which makes no sense to emulate here)
   421  		if test.prec == 53 && test.format != 'p' && f.Sign() != 0 {
   422  			f64, acc := f.Float64()
   423  			if acc != Exact {
   424  				t.Errorf("%v: expected exact conversion to float64", test)
   425  				continue
   426  			}
   427  			got := strconv.FormatFloat(f64, test.format, test.digits, 64)
   428  			if got != test.want {
   429  				t.Errorf("%v: got %s; want %s", test, got, test.want)
   430  			}
   431  		}
   432  	}
   433  }
   434  
   435  func TestFloatFormat(t *testing.T) {
   436  	for _, test := range []struct {
   437  		format string
   438  		value  interface{} // float32, float64, or string (== 512bit *Float)
   439  		want   string
   440  	}{
   441  		// TODO(gri) uncomment the disabled 'g'/'G' formats
   442  		// 	     below once (*Float).Text supports prec < 0
   443  
   444  		// from fmt/fmt_test.go
   445  		{"%+.3e", 0.0, "+0.000e+00"},
   446  		{"%+.3e", 1.0, "+1.000e+00"},
   447  		{"%+.3f", -1.0, "-1.000"},
   448  		{"%+.3F", -1.0, "-1.000"},
   449  		{"%+.3F", float32(-1.0), "-1.000"},
   450  		{"%+07.2f", 1.0, "+001.00"},
   451  		{"%+07.2f", -1.0, "-001.00"},
   452  		{"%+10.2f", +1.0, "     +1.00"},
   453  		{"%+10.2f", -1.0, "     -1.00"},
   454  		{"% .3E", -1.0, "-1.000E+00"},
   455  		{"% .3e", 1.0, " 1.000e+00"},
   456  		{"%+.3g", 0.0, "+0"},
   457  		{"%+.3g", 1.0, "+1"},
   458  		{"%+.3g", -1.0, "-1"},
   459  		{"% .3g", -1.0, "-1"},
   460  		{"% .3g", 1.0, " 1"},
   461  		{"%b", float32(1.0), "8388608p-23"},
   462  		{"%b", 1.0, "4503599627370496p-52"},
   463  
   464  		// from fmt/fmt_test.go: old test/fmt_test.go
   465  		{"%e", 1.0, "1.000000e+00"},
   466  		{"%e", 1234.5678e3, "1.234568e+06"},
   467  		{"%e", 1234.5678e-8, "1.234568e-05"},
   468  		{"%e", -7.0, "-7.000000e+00"},
   469  		{"%e", -1e-9, "-1.000000e-09"},
   470  		{"%f", 1234.5678e3, "1234567.800000"},
   471  		{"%f", 1234.5678e-8, "0.000012"},
   472  		{"%f", -7.0, "-7.000000"},
   473  		{"%f", -1e-9, "-0.000000"},
   474  		// {"%g", 1234.5678e3, "1.2345678e+06"},
   475  		// {"%g", float32(1234.5678e3), "1.2345678e+06"},
   476  		// {"%g", 1234.5678e-8, "1.2345678e-05"},
   477  		{"%g", -7.0, "-7"},
   478  		{"%g", -1e-9, "-1e-09"},
   479  		{"%g", float32(-1e-9), "-1e-09"},
   480  		{"%E", 1.0, "1.000000E+00"},
   481  		{"%E", 1234.5678e3, "1.234568E+06"},
   482  		{"%E", 1234.5678e-8, "1.234568E-05"},
   483  		{"%E", -7.0, "-7.000000E+00"},
   484  		{"%E", -1e-9, "-1.000000E-09"},
   485  		// {"%G", 1234.5678e3, "1.2345678E+06"},
   486  		// {"%G", float32(1234.5678e3), "1.2345678E+06"},
   487  		// {"%G", 1234.5678e-8, "1.2345678E-05"},
   488  		{"%G", -7.0, "-7"},
   489  		{"%G", -1e-9, "-1E-09"},
   490  		{"%G", float32(-1e-9), "-1E-09"},
   491  
   492  		{"%20.6e", 1.2345e3, "        1.234500e+03"},
   493  		{"%20.6e", 1.2345e-3, "        1.234500e-03"},
   494  		{"%20e", 1.2345e3, "        1.234500e+03"},
   495  		{"%20e", 1.2345e-3, "        1.234500e-03"},
   496  		{"%20.8e", 1.2345e3, "      1.23450000e+03"},
   497  		{"%20f", 1.23456789e3, "         1234.567890"},
   498  		{"%20f", 1.23456789e-3, "            0.001235"},
   499  		{"%20f", 12345678901.23456789, "  12345678901.234568"},
   500  		{"%-20f", 1.23456789e3, "1234.567890         "},
   501  		{"%20.8f", 1.23456789e3, "       1234.56789000"},
   502  		{"%20.8f", 1.23456789e-3, "          0.00123457"},
   503  		// {"%g", 1.23456789e3, "1234.56789"},
   504  		// {"%g", 1.23456789e-3, "0.00123456789"},
   505  		// {"%g", 1.23456789e20, "1.23456789e+20"},
   506  		{"%20e", math.Inf(1), "                +Inf"},
   507  		{"%-20f", math.Inf(-1), "-Inf                "},
   508  
   509  		// from fmt/fmt_test.go: comparison of padding rules with C printf
   510  		{"%.2f", 1.0, "1.00"},
   511  		{"%.2f", -1.0, "-1.00"},
   512  		{"% .2f", 1.0, " 1.00"},
   513  		{"% .2f", -1.0, "-1.00"},
   514  		{"%+.2f", 1.0, "+1.00"},
   515  		{"%+.2f", -1.0, "-1.00"},
   516  		{"%7.2f", 1.0, "   1.00"},
   517  		{"%7.2f", -1.0, "  -1.00"},
   518  		{"% 7.2f", 1.0, "   1.00"},
   519  		{"% 7.2f", -1.0, "  -1.00"},
   520  		{"%+7.2f", 1.0, "  +1.00"},
   521  		{"%+7.2f", -1.0, "  -1.00"},
   522  		{"%07.2f", 1.0, "0001.00"},
   523  		{"%07.2f", -1.0, "-001.00"},
   524  		{"% 07.2f", 1.0, " 001.00"},
   525  		{"% 07.2f", -1.0, "-001.00"},
   526  		{"%+07.2f", 1.0, "+001.00"},
   527  		{"%+07.2f", -1.0, "-001.00"},
   528  
   529  		// from fmt/fmt_test.go: zero padding does not apply to infinities
   530  		{"%020f", math.Inf(-1), "                -Inf"},
   531  		{"%020f", math.Inf(+1), "                +Inf"},
   532  		{"% 020f", math.Inf(-1), "                -Inf"},
   533  		{"% 020f", math.Inf(+1), "                 Inf"},
   534  		{"%+020f", math.Inf(-1), "                -Inf"},
   535  		{"%+020f", math.Inf(+1), "                +Inf"},
   536  		{"%20f", -1.0, "           -1.000000"},
   537  
   538  		// handle %v like %g
   539  		{"%v", 0.0, "0"},
   540  		{"%v", -7.0, "-7"},
   541  		{"%v", -1e-9, "-1e-09"},
   542  		{"%v", float32(-1e-9), "-1e-09"},
   543  		{"%010v", 0.0, "0000000000"},
   544  		{"%010v", 0.0, "0000000000"},
   545  
   546  		// *Float cases
   547  		{"%.20f", "1e-20", "0.00000000000000000001"},
   548  		{"%.20f", "-1e-20", "-0.00000000000000000001"},
   549  		{"%30.20f", "-1e-20", "       -0.00000000000000000001"},
   550  		{"%030.20f", "-1e-20", "-00000000.00000000000000000001"},
   551  		{"%030.20f", "+1e-20", "000000000.00000000000000000001"},
   552  		{"% 030.20f", "+1e-20", " 00000000.00000000000000000001"},
   553  
   554  		// erroneous formats
   555  		{"%s", 1.0, "%!s(*big.Float=1)"},
   556  	} {
   557  		value := new(Float)
   558  		switch v := test.value.(type) {
   559  		case float32:
   560  			value.SetPrec(24).SetFloat64(float64(v))
   561  		case float64:
   562  			value.SetPrec(53).SetFloat64(v)
   563  		case string:
   564  			value.SetPrec(512).Parse(v, 0)
   565  		default:
   566  			t.Fatalf("unsupported test value: %v (%T)", v, v)
   567  		}
   568  
   569  		if got := fmt.Sprintf(test.format, value); got != test.want {
   570  			t.Errorf("%v: got %q; want %q", test, got, test.want)
   571  		}
   572  	}
   573  }