Source file src/strconv/ftoa_test.go

     1  // Copyright 2009 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 strconv_test
     6  
     7  import (
     8  	"math"
     9  	"math/rand"
    10  	. "strconv"
    11  	"testing"
    12  )
    13  
    14  type ftoaTest struct {
    15  	f    float64
    16  	fmt  byte
    17  	prec int
    18  	s    string
    19  }
    20  
    21  func fdiv(a, b float64) float64 { return a / b }
    22  
    23  const (
    24  	below1e23 = 99999999999999974834176
    25  	above1e23 = 100000000000000008388608
    26  )
    27  
    28  var ftoatests = []ftoaTest{
    29  	{1, 'e', 5, "1.00000e+00"},
    30  	{1, 'f', 5, "1.00000"},
    31  	{1, 'g', 5, "1"},
    32  	{1, 'g', -1, "1"},
    33  	{1, 'x', -1, "0x1p+00"},
    34  	{1, 'x', 5, "0x1.00000p+00"},
    35  	{20, 'g', -1, "20"},
    36  	{20, 'x', -1, "0x1.4p+04"},
    37  	{1234567.8, 'g', -1, "1.2345678e+06"},
    38  	{1234567.8, 'x', -1, "0x1.2d687cccccccdp+20"},
    39  	{200000, 'g', -1, "200000"},
    40  	{200000, 'x', -1, "0x1.86ap+17"},
    41  	{200000, 'X', -1, "0X1.86AP+17"},
    42  	{2000000, 'g', -1, "2e+06"},
    43  	{1e10, 'g', -1, "1e+10"},
    44  
    45  	// g conversion and zero suppression
    46  	{400, 'g', 2, "4e+02"},
    47  	{40, 'g', 2, "40"},
    48  	{4, 'g', 2, "4"},
    49  	{.4, 'g', 2, "0.4"},
    50  	{.04, 'g', 2, "0.04"},
    51  	{.004, 'g', 2, "0.004"},
    52  	{.0004, 'g', 2, "0.0004"},
    53  	{.00004, 'g', 2, "4e-05"},
    54  	{.000004, 'g', 2, "4e-06"},
    55  
    56  	{0, 'e', 5, "0.00000e+00"},
    57  	{0, 'f', 5, "0.00000"},
    58  	{0, 'g', 5, "0"},
    59  	{0, 'g', -1, "0"},
    60  	{0, 'x', 5, "0x0.00000p+00"},
    61  
    62  	{-1, 'e', 5, "-1.00000e+00"},
    63  	{-1, 'f', 5, "-1.00000"},
    64  	{-1, 'g', 5, "-1"},
    65  	{-1, 'g', -1, "-1"},
    66  
    67  	{12, 'e', 5, "1.20000e+01"},
    68  	{12, 'f', 5, "12.00000"},
    69  	{12, 'g', 5, "12"},
    70  	{12, 'g', -1, "12"},
    71  
    72  	{123456700, 'e', 5, "1.23457e+08"},
    73  	{123456700, 'f', 5, "123456700.00000"},
    74  	{123456700, 'g', 5, "1.2346e+08"},
    75  	{123456700, 'g', -1, "1.234567e+08"},
    76  
    77  	{1.2345e6, 'e', 5, "1.23450e+06"},
    78  	{1.2345e6, 'f', 5, "1234500.00000"},
    79  	{1.2345e6, 'g', 5, "1.2345e+06"},
    80  
    81  	// Round to even
    82  	{1.2345e6, 'e', 3, "1.234e+06"},
    83  	{1.2355e6, 'e', 3, "1.236e+06"},
    84  	{1.2345, 'f', 3, "1.234"},
    85  	{1.2355, 'f', 3, "1.236"},
    86  	{1234567890123456.5, 'e', 15, "1.234567890123456e+15"},
    87  	{1234567890123457.5, 'e', 15, "1.234567890123458e+15"},
    88  	{108678236358137.625, 'g', -1, "1.0867823635813762e+14"},
    89  
    90  	{1e23, 'e', 17, "9.99999999999999916e+22"},
    91  	{1e23, 'f', 17, "99999999999999991611392.00000000000000000"},
    92  	{1e23, 'g', 17, "9.9999999999999992e+22"},
    93  
    94  	{1e23, 'e', -1, "1e+23"},
    95  	{1e23, 'f', -1, "100000000000000000000000"},
    96  	{1e23, 'g', -1, "1e+23"},
    97  
    98  	{below1e23, 'e', 17, "9.99999999999999748e+22"},
    99  	{below1e23, 'f', 17, "99999999999999974834176.00000000000000000"},
   100  	{below1e23, 'g', 17, "9.9999999999999975e+22"},
   101  
   102  	{below1e23, 'e', -1, "9.999999999999997e+22"},
   103  	{below1e23, 'f', -1, "99999999999999970000000"},
   104  	{below1e23, 'g', -1, "9.999999999999997e+22"},
   105  
   106  	{above1e23, 'e', 17, "1.00000000000000008e+23"},
   107  	{above1e23, 'f', 17, "100000000000000008388608.00000000000000000"},
   108  	{above1e23, 'g', 17, "1.0000000000000001e+23"},
   109  
   110  	{above1e23, 'e', -1, "1.0000000000000001e+23"},
   111  	{above1e23, 'f', -1, "100000000000000010000000"},
   112  	{above1e23, 'g', -1, "1.0000000000000001e+23"},
   113  
   114  	{fdiv(5e-304, 1e20), 'g', -1, "5e-324"},   // avoid constant arithmetic
   115  	{fdiv(-5e-304, 1e20), 'g', -1, "-5e-324"}, // avoid constant arithmetic
   116  
   117  	{32, 'g', -1, "32"},
   118  	{32, 'g', 0, "3e+01"},
   119  
   120  	{100, 'x', -1, "0x1.9p+06"},
   121  	{100, 'y', -1, "%y"},
   122  
   123  	{math.NaN(), 'g', -1, "NaN"},
   124  	{-math.NaN(), 'g', -1, "NaN"},
   125  	{math.Inf(0), 'g', -1, "+Inf"},
   126  	{math.Inf(-1), 'g', -1, "-Inf"},
   127  	{-math.Inf(0), 'g', -1, "-Inf"},
   128  
   129  	{-1, 'b', -1, "-4503599627370496p-52"},
   130  
   131  	// fixed bugs
   132  	{0.9, 'f', 1, "0.9"},
   133  	{0.09, 'f', 1, "0.1"},
   134  	{0.0999, 'f', 1, "0.1"},
   135  	{0.05, 'f', 1, "0.1"},
   136  	{0.05, 'f', 0, "0"},
   137  	{0.5, 'f', 1, "0.5"},
   138  	{0.5, 'f', 0, "0"},
   139  	{1.5, 'f', 0, "2"},
   140  
   141  	// https://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
   142  	{2.2250738585072012e-308, 'g', -1, "2.2250738585072014e-308"},
   143  	// https://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
   144  	{2.2250738585072011e-308, 'g', -1, "2.225073858507201e-308"},
   145  
   146  	// Issue 2625.
   147  	{383260575764816448, 'f', 0, "383260575764816448"},
   148  	{383260575764816448, 'g', -1, "3.8326057576481645e+17"},
   149  
   150  	// Issue 29491.
   151  	{498484681984085570, 'f', -1, "498484681984085570"},
   152  	{-5.8339553793802237e+23, 'g', -1, "-5.8339553793802237e+23"},
   153  
   154  	// Issue 52187
   155  	{123.45, '?', 0, "%?"},
   156  	{123.45, '?', 1, "%?"},
   157  	{123.45, '?', -1, "%?"},
   158  
   159  	// rounding
   160  	{2.275555555555555, 'x', -1, "0x1.23456789abcdep+01"},
   161  	{2.275555555555555, 'x', 0, "0x1p+01"},
   162  	{2.275555555555555, 'x', 2, "0x1.23p+01"},
   163  	{2.275555555555555, 'x', 16, "0x1.23456789abcde000p+01"},
   164  	{2.275555555555555, 'x', 21, "0x1.23456789abcde00000000p+01"},
   165  	{2.2755555510520935, 'x', -1, "0x1.2345678p+01"},
   166  	{2.2755555510520935, 'x', 6, "0x1.234568p+01"},
   167  	{2.275555431842804, 'x', -1, "0x1.2345668p+01"},
   168  	{2.275555431842804, 'x', 6, "0x1.234566p+01"},
   169  	{3.999969482421875, 'x', -1, "0x1.ffffp+01"},
   170  	{3.999969482421875, 'x', 4, "0x1.ffffp+01"},
   171  	{3.999969482421875, 'x', 3, "0x1.000p+02"},
   172  	{3.999969482421875, 'x', 2, "0x1.00p+02"},
   173  	{3.999969482421875, 'x', 1, "0x1.0p+02"},
   174  	{3.999969482421875, 'x', 0, "0x1p+02"},
   175  
   176  	// Cases that Java once mishandled, from David Chase.
   177  	{1.801439850948199e+16, 'g', -1, "1.801439850948199e+16"},
   178  	{5.960464477539063e-08, 'g', -1, "5.960464477539063e-08"},
   179  	{1.012e-320, 'g', -1, "1.012e-320"},
   180  }
   181  
   182  func TestFtoa(t *testing.T) {
   183  	for i := 0; i < len(ftoatests); i++ {
   184  		test := &ftoatests[i]
   185  		s := FormatFloat(test.f, test.fmt, test.prec, 64)
   186  		if s != test.s {
   187  			t.Error("testN=64", test.f, string(test.fmt), test.prec, "want", test.s, "got", s)
   188  		}
   189  		x := AppendFloat([]byte("abc"), test.f, test.fmt, test.prec, 64)
   190  		if string(x) != "abc"+test.s {
   191  			t.Error("AppendFloat testN=64", test.f, string(test.fmt), test.prec, "want", "abc"+test.s, "got", string(x))
   192  		}
   193  		if float64(float32(test.f)) == test.f && test.fmt != 'b' {
   194  			test_s := test.s
   195  			if test.f == 5.960464477539063e-08 {
   196  				// This test is an exact float32 but asking for float64 precision in the string.
   197  				// (All our other float64-only tests fail to exactness check above.)
   198  				test_s = "5.9604645e-08"
   199  				continue
   200  			}
   201  			s := FormatFloat(test.f, test.fmt, test.prec, 32)
   202  			if s != test.s {
   203  				t.Error("testN=32", test.f, string(test.fmt), test.prec, "want", test_s, "got", s)
   204  			}
   205  			x := AppendFloat([]byte("abc"), test.f, test.fmt, test.prec, 32)
   206  			if string(x) != "abc"+test_s {
   207  				t.Error("AppendFloat testN=32", test.f, string(test.fmt), test.prec, "want", "abc"+test_s, "got", string(x))
   208  			}
   209  		}
   210  	}
   211  }
   212  
   213  func TestFtoaPowersOfTwo(t *testing.T) {
   214  	for exp := -2048; exp <= 2048; exp++ {
   215  		f := math.Ldexp(1, exp)
   216  		if !math.IsInf(f, 0) {
   217  			s := FormatFloat(f, 'e', -1, 64)
   218  			if x, _ := ParseFloat(s, 64); x != f {
   219  				t.Errorf("failed roundtrip %v => %s => %v", f, s, x)
   220  			}
   221  		}
   222  		f32 := float32(f)
   223  		if !math.IsInf(float64(f32), 0) {
   224  			s := FormatFloat(float64(f32), 'e', -1, 32)
   225  			if x, _ := ParseFloat(s, 32); float32(x) != f32 {
   226  				t.Errorf("failed roundtrip %v => %s => %v", f32, s, float32(x))
   227  			}
   228  		}
   229  	}
   230  }
   231  
   232  func TestFtoaRandom(t *testing.T) {
   233  	N := int(1e4)
   234  	if testing.Short() {
   235  		N = 100
   236  	}
   237  	t.Logf("testing %d random numbers with fast and slow FormatFloat", N)
   238  	for i := 0; i < N; i++ {
   239  		bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
   240  		x := math.Float64frombits(bits)
   241  
   242  		shortFast := FormatFloat(x, 'g', -1, 64)
   243  		SetOptimize(false)
   244  		shortSlow := FormatFloat(x, 'g', -1, 64)
   245  		SetOptimize(true)
   246  		if shortSlow != shortFast {
   247  			t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow)
   248  		}
   249  
   250  		prec := rand.Intn(12) + 5
   251  		shortFast = FormatFloat(x, 'e', prec, 64)
   252  		SetOptimize(false)
   253  		shortSlow = FormatFloat(x, 'e', prec, 64)
   254  		SetOptimize(true)
   255  		if shortSlow != shortFast {
   256  			t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow)
   257  		}
   258  	}
   259  }
   260  
   261  func TestFormatFloatInvalidBitSize(t *testing.T) {
   262  	defer func() {
   263  		if r := recover(); r == nil {
   264  			t.Fatalf("expected panic due to invalid bitSize")
   265  		}
   266  	}()
   267  	_ = FormatFloat(3.14, 'g', -1, 100)
   268  }
   269  
   270  var ftoaBenches = []struct {
   271  	name    string
   272  	float   float64
   273  	fmt     byte
   274  	prec    int
   275  	bitSize int
   276  }{
   277  	{"Decimal", 33909, 'g', -1, 64},
   278  	{"Float", 339.7784, 'g', -1, 64},
   279  	{"Exp", -5.09e75, 'g', -1, 64},
   280  	{"NegExp", -5.11e-95, 'g', -1, 64},
   281  	{"LongExp", 1.234567890123456e-78, 'g', -1, 64},
   282  
   283  	{"Big", 123456789123456789123456789, 'g', -1, 64},
   284  	{"BinaryExp", -1, 'b', -1, 64},
   285  
   286  	{"32Integer", 33909, 'g', -1, 32},
   287  	{"32ExactFraction", 3.375, 'g', -1, 32},
   288  	{"32Point", 339.7784, 'g', -1, 32},
   289  	{"32Exp", -5.09e25, 'g', -1, 32},
   290  	{"32NegExp", -5.11e-25, 'g', -1, 32},
   291  	{"32Shortest", 1.234567e-8, 'g', -1, 32},
   292  	{"32Fixed8Hard", math.Ldexp(15961084, -125), 'e', 8, 32},
   293  	{"32Fixed9Hard", math.Ldexp(14855922, -83), 'e', 9, 32},
   294  
   295  	{"64Fixed1", 123456, 'e', 3, 64},
   296  	{"64Fixed2", 123.456, 'e', 3, 64},
   297  	{"64Fixed3", 1.23456e+78, 'e', 3, 64},
   298  	{"64Fixed4", 1.23456e-78, 'e', 3, 64},
   299  	{"64Fixed12", 1.23456e-78, 'e', 12, 64},
   300  	{"64Fixed16", 1.23456e-78, 'e', 16, 64},
   301  	// From testdata/testfp.txt
   302  	{"64Fixed12Hard", math.Ldexp(6965949469487146, -249), 'e', 12, 64},
   303  	{"64Fixed17Hard", math.Ldexp(8887055249355788, 665), 'e', 17, 64},
   304  	{"64Fixed18Hard", math.Ldexp(6994187472632449, 690), 'e', 18, 64},
   305  
   306  	// Trigger slow path (see issue #15672).
   307  	// The shortest is: 8.034137530808823e+43
   308  	{"Slowpath64", 8.03413753080882349e+43, 'e', -1, 64},
   309  	// This denormal is pathological because the lower/upper
   310  	// halfways to neighboring floats are:
   311  	// 622666234635.321003e-320 ~= 622666234635.321e-320
   312  	// 622666234635.321497e-320 ~= 622666234635.3215e-320
   313  	// making it hard to find the 3rd digit
   314  	{"SlowpathDenormal64", 622666234635.3213e-320, 'e', -1, 64},
   315  }
   316  
   317  func BenchmarkFormatFloat(b *testing.B) {
   318  	for _, c := range ftoaBenches {
   319  		b.Run(c.name, func(b *testing.B) {
   320  			for i := 0; i < b.N; i++ {
   321  				FormatFloat(c.float, c.fmt, c.prec, c.bitSize)
   322  			}
   323  		})
   324  	}
   325  }
   326  
   327  func BenchmarkAppendFloat(b *testing.B) {
   328  	dst := make([]byte, 30)
   329  	for _, c := range ftoaBenches {
   330  		b.Run(c.name, func(b *testing.B) {
   331  			for i := 0; i < b.N; i++ {
   332  				AppendFloat(dst[:0], c.float, c.fmt, c.prec, c.bitSize)
   333  			}
   334  		})
   335  	}
   336  }
   337  

View as plain text