Source file src/internal/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  	. "internal/strconv"
     9  	"math"
    10  	"math/rand"
    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  	// f conversion basic cases
    46  	{12345, 'f', 2, "12345.00"},
    47  	{1234.5, 'f', 2, "1234.50"},
    48  	{123.45, 'f', 2, "123.45"},
    49  	{12.345, 'f', 2, "12.35"},
    50  	{1.2345, 'f', 2, "1.23"},
    51  	{0.12345, 'f', 2, "0.12"},
    52  	{0.12945, 'f', 2, "0.13"},
    53  	{0.012345, 'f', 2, "0.01"},
    54  	{0.015, 'f', 2, "0.01"},
    55  	{0.016, 'f', 2, "0.02"},
    56  	{0.0052345, 'f', 2, "0.01"},
    57  	{0.0012345, 'f', 2, "0.00"},
    58  	{0.00012345, 'f', 2, "0.00"},
    59  	{0.000012345, 'f', 2, "0.00"},
    60  
    61  	{0.996644984, 'f', 6, "0.996645"},
    62  	{0.996644984, 'f', 5, "0.99664"},
    63  	{0.996644984, 'f', 4, "0.9966"},
    64  	{0.996644984, 'f', 3, "0.997"},
    65  	{0.996644984, 'f', 2, "1.00"},
    66  	{0.996644984, 'f', 1, "1.0"},
    67  
    68  	// g conversion and zero suppression
    69  	{400, 'g', 2, "4e+02"},
    70  	{40, 'g', 2, "40"},
    71  	{4, 'g', 2, "4"},
    72  	{.4, 'g', 2, "0.4"},
    73  	{.04, 'g', 2, "0.04"},
    74  	{.004, 'g', 2, "0.004"},
    75  	{.0004, 'g', 2, "0.0004"},
    76  	{.00004, 'g', 2, "4e-05"},
    77  	{.000004, 'g', 2, "4e-06"},
    78  
    79  	{0, 'e', 5, "0.00000e+00"},
    80  	{0, 'f', 5, "0.00000"},
    81  	{0, 'g', 5, "0"},
    82  	{0, 'g', -1, "0"},
    83  	{0, 'x', 5, "0x0.00000p+00"},
    84  
    85  	{-1, 'e', 5, "-1.00000e+00"},
    86  	{-1, 'f', 5, "-1.00000"},
    87  	{-1, 'g', 5, "-1"},
    88  	{-1, 'g', -1, "-1"},
    89  
    90  	{12, 'e', 5, "1.20000e+01"},
    91  	{12, 'f', 5, "12.00000"},
    92  	{12, 'g', 5, "12"},
    93  	{12, 'g', -1, "12"},
    94  
    95  	{123456700, 'e', 5, "1.23457e+08"},
    96  	{123456700, 'f', 5, "123456700.00000"},
    97  	{123456700, 'g', 5, "1.2346e+08"},
    98  	{123456700, 'g', -1, "1.234567e+08"},
    99  
   100  	{1.2345e6, 'e', 5, "1.23450e+06"},
   101  	{1.2345e6, 'f', 5, "1234500.00000"},
   102  	{1.2345e6, 'g', 5, "1.2345e+06"},
   103  
   104  	// Round to even
   105  	{1.2345e6, 'e', 3, "1.234e+06"},
   106  	{1.2355e6, 'e', 3, "1.236e+06"},
   107  	{1.2345, 'f', 3, "1.234"},
   108  	{1.2355, 'f', 3, "1.236"},
   109  	{1234567890123456.5, 'e', 15, "1.234567890123456e+15"},
   110  	{1234567890123457.5, 'e', 15, "1.234567890123458e+15"},
   111  	{108678236358137.625, 'g', -1, "1.0867823635813762e+14"},
   112  
   113  	{1e23, 'e', 17, "9.99999999999999916e+22"},
   114  	{1e23, 'f', 17, "99999999999999991611392.00000000000000000"},
   115  	{1e23, 'g', 17, "9.9999999999999992e+22"},
   116  
   117  	{1e23, 'e', -1, "1e+23"},
   118  	{1e23, 'f', -1, "100000000000000000000000"},
   119  	{1e23, 'g', -1, "1e+23"},
   120  
   121  	{below1e23, 'e', 17, "9.99999999999999748e+22"},
   122  	{below1e23, 'f', 17, "99999999999999974834176.00000000000000000"},
   123  	{below1e23, 'g', 17, "9.9999999999999975e+22"},
   124  
   125  	{below1e23, 'e', -1, "9.999999999999997e+22"},
   126  	{below1e23, 'f', -1, "99999999999999970000000"},
   127  	{below1e23, 'g', -1, "9.999999999999997e+22"},
   128  
   129  	{above1e23, 'e', 17, "1.00000000000000008e+23"},
   130  	{above1e23, 'f', 17, "100000000000000008388608.00000000000000000"},
   131  	{above1e23, 'g', 17, "1.0000000000000001e+23"},
   132  
   133  	{above1e23, 'e', -1, "1.0000000000000001e+23"},
   134  	{above1e23, 'f', -1, "100000000000000010000000"},
   135  	{above1e23, 'g', -1, "1.0000000000000001e+23"},
   136  
   137  	{fdiv(5e-304, 1e20), 'g', -1, "5e-324"},   // avoid constant arithmetic
   138  	{fdiv(-5e-304, 1e20), 'g', -1, "-5e-324"}, // avoid constant arithmetic
   139  	{fdiv(5e-304, 1e20), 'e', -1, "5e-324"},
   140  
   141  	{32, 'g', -1, "32"},
   142  	{32, 'g', 0, "3e+01"},
   143  
   144  	{100, 'x', -1, "0x1.9p+06"},
   145  	{100, 'y', -1, "%y"},
   146  
   147  	{math.NaN(), 'g', -1, "NaN"},
   148  	{-math.NaN(), 'g', -1, "NaN"},
   149  	{math.Inf(0), 'g', -1, "+Inf"},
   150  	{math.Inf(-1), 'g', -1, "-Inf"},
   151  	{-math.Inf(0), 'g', -1, "-Inf"},
   152  
   153  	{-1, 'b', -1, "-4503599627370496p-52"},
   154  
   155  	// fixed bugs
   156  	{0.9, 'f', 1, "0.9"},
   157  	{0.09, 'f', 1, "0.1"},
   158  	{0.0999, 'f', 1, "0.1"},
   159  	{0.05, 'f', 1, "0.1"},
   160  	{0.05, 'f', 0, "0"},
   161  	{0.5, 'f', 1, "0.5"},
   162  	{0.5, 'f', 0, "0"},
   163  	{1.5, 'f', 0, "2"},
   164  
   165  	// https://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
   166  	{2.2250738585072012e-308, 'g', -1, "2.2250738585072014e-308"},
   167  	// https://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
   168  	{2.2250738585072011e-308, 'g', -1, "2.225073858507201e-308"},
   169  
   170  	// Issue 2625.
   171  	{383260575764816448, 'f', 0, "383260575764816448"},
   172  	{383260575764816448, 'g', -1, "3.8326057576481645e+17"},
   173  
   174  	// Issue 29491.
   175  	{498484681984085570, 'f', -1, "498484681984085570"},
   176  	{-5.8339553793802237e+23, 'g', -1, "-5.8339553793802237e+23"},
   177  
   178  	// Issue 52187
   179  	{123.45, '?', 0, "%?"},
   180  	{123.45, '?', 1, "%?"},
   181  	{123.45, '?', -1, "%?"},
   182  
   183  	// rounding
   184  	{2.275555555555555, 'x', -1, "0x1.23456789abcdep+01"},
   185  	{2.275555555555555, 'x', 0, "0x1p+01"},
   186  	{2.275555555555555, 'x', 2, "0x1.23p+01"},
   187  	{2.275555555555555, 'x', 16, "0x1.23456789abcde000p+01"},
   188  	{2.275555555555555, 'x', 21, "0x1.23456789abcde00000000p+01"},
   189  	{2.2755555510520935, 'x', -1, "0x1.2345678p+01"},
   190  	{2.2755555510520935, 'x', 6, "0x1.234568p+01"},
   191  	{2.275555431842804, 'x', -1, "0x1.2345668p+01"},
   192  	{2.275555431842804, 'x', 6, "0x1.234566p+01"},
   193  	{3.999969482421875, 'x', -1, "0x1.ffffp+01"},
   194  	{3.999969482421875, 'x', 4, "0x1.ffffp+01"},
   195  	{3.999969482421875, 'x', 3, "0x1.000p+02"},
   196  	{3.999969482421875, 'x', 2, "0x1.00p+02"},
   197  	{3.999969482421875, 'x', 1, "0x1.0p+02"},
   198  	{3.999969482421875, 'x', 0, "0x1p+02"},
   199  
   200  	// Cases that Java once mishandled, from David Chase.
   201  	{1.801439850948199e+16, 'g', -1, "1.801439850948199e+16"},
   202  	{5.960464477539063e-08, 'g', -1, "5.960464477539063e-08"},
   203  	{1.012e-320, 'g', -1, "1.012e-320"},
   204  
   205  	// Cases from TestFtoaRandom that caught bugs in fixedFtoa.
   206  	{8177880169308380. * (1 << 1), 'e', 14, "1.63557603386168e+16"},
   207  	{8393378656576888. * (1 << 1), 'e', 15, "1.678675731315378e+16"},
   208  	{8738676561280626. * (1 << 4), 'e', 16, "1.3981882498049002e+17"},
   209  	{8291032395191335. / (1 << 30), 'e', 5, "7.72163e+06"},
   210  	{8880392441509914. / (1 << 80), 'e', 16, "7.3456884594794477e-09"},
   211  
   212  	// Exercise divisiblePow5 case in fixedFtoa
   213  	{2384185791015625. * (1 << 12), 'e', 5, "9.76562e+18"},
   214  	{2384185791015625. * (1 << 13), 'e', 5, "1.95312e+19"},
   215  
   216  	// Exercise potential mistakes in fixedFtoa.
   217  	// Found by introducing mistakes and running 'go test -testbase'.
   218  	{0x1.000000000005p+71, 'e', 16, "2.3611832414348645e+21"},
   219  	{0x1.0000p-27, 'e', 17, "7.45058059692382812e-09"},
   220  	{0x1.0000p-41, 'e', 17, "4.54747350886464119e-13"},
   221  
   222  	// go.dev/issue/79591; used NULs instead of trailing zeros
   223  	{0.00000000000000000093564868367555, 'f', 150, "0.000000000000000000935648683675550014820074270847655141588693848715273682775661612254225474316626787185668945312500000000000000000000000000000000000000"},
   224  }
   225  
   226  func TestFtoa(t *testing.T) {
   227  	for i := 0; i < len(ftoatests); i++ {
   228  		test := &ftoatests[i]
   229  		s := FormatFloat(test.f, test.fmt, test.prec, 64)
   230  		if s != test.s {
   231  			t.Error("testN=64", test.f, string(test.fmt), test.prec, "want", test.s, "got", s)
   232  		}
   233  		x := AppendFloat([]byte("abc"), test.f, test.fmt, test.prec, 64)
   234  		if string(x) != "abc"+test.s {
   235  			t.Error("AppendFloat testN=64", test.f, string(test.fmt), test.prec, "want", "abc"+test.s, "got", string(x))
   236  		}
   237  		if float64(float32(test.f)) == test.f && test.fmt != 'b' {
   238  			test_s := test.s
   239  			if test.f == 5.960464477539063e-08 {
   240  				// This test is an exact float32 but asking for float64 precision in the string.
   241  				// (All our other float64-only tests fail to exactness check above.)
   242  				test_s = "5.9604645e-08"
   243  				continue
   244  			}
   245  			s := FormatFloat(test.f, test.fmt, test.prec, 32)
   246  			if s != test.s {
   247  				t.Error("testN=32", test.f, string(test.fmt), test.prec, "want", test_s, "got", s)
   248  			}
   249  			x := AppendFloat([]byte("abc"), test.f, test.fmt, test.prec, 32)
   250  			if string(x) != "abc"+test_s {
   251  				t.Error("AppendFloat testN=32", test.f, string(test.fmt), test.prec, "want", "abc"+test_s, "got", string(x))
   252  			}
   253  		}
   254  	}
   255  }
   256  
   257  func TestFtoaPowersOfTwo(t *testing.T) {
   258  	for exp := -2048; exp <= 2048; exp++ {
   259  		f := math.Ldexp(1, exp)
   260  		if !math.IsInf(f, 0) {
   261  			s := FormatFloat(f, 'e', -1, 64)
   262  			if x, _ := ParseFloat(s, 64); x != f {
   263  				t.Errorf("failed roundtrip %v => %s => %v", f, s, x)
   264  			}
   265  		}
   266  		f32 := float32(f)
   267  		if !math.IsInf(float64(f32), 0) {
   268  			s := FormatFloat(float64(f32), 'e', -1, 32)
   269  			if x, _ := ParseFloat(s, 32); float32(x) != f32 {
   270  				t.Errorf("failed roundtrip %v => %s => %v", f32, s, float32(x))
   271  			}
   272  		}
   273  	}
   274  }
   275  
   276  func TestFtoaRandom(t *testing.T) {
   277  	N := int(1e4)
   278  	if testing.Short() {
   279  		N = 100
   280  	}
   281  	t.Logf("testing %d random numbers with fast and slow FormatFloat", N)
   282  	for i := 0; i < N; i++ {
   283  		bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
   284  		x := math.Float64frombits(bits)
   285  
   286  		shortFast := FormatFloat(x, 'g', -1, 64)
   287  		SetOptimize(false)
   288  		shortSlow := FormatFloat(x, 'g', -1, 64)
   289  		SetOptimize(true)
   290  		if shortSlow != shortFast {
   291  			t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow)
   292  		}
   293  
   294  		prec := rand.Intn(12) + 5
   295  		shortFast = FormatFloat(x, 'e', prec, 64)
   296  		SetOptimize(false)
   297  		shortSlow = FormatFloat(x, 'e', prec, 64)
   298  		SetOptimize(true)
   299  		if shortSlow != shortFast {
   300  			t.Errorf("%b printed with %%.%de as %s, want %s", x, prec, shortFast, shortSlow)
   301  		}
   302  	}
   303  }
   304  
   305  func TestFormatFloatInvalidBitSize(t *testing.T) {
   306  	defer func() {
   307  		if r := recover(); r == nil {
   308  			t.Fatalf("expected panic due to invalid bitSize")
   309  		}
   310  	}()
   311  	_ = FormatFloat(3.14, 'g', -1, 100)
   312  }
   313  
   314  var ftoaBenches = []struct {
   315  	name    string
   316  	float   float64
   317  	fmt     byte
   318  	prec    int
   319  	bitSize int
   320  }{
   321  	{"Decimal", 33909, 'g', -1, 64},
   322  	{"Float", 339.7784, 'g', -1, 64},
   323  	{"Exp", -5.09e75, 'g', -1, 64},
   324  	{"NegExp", -5.11e-95, 'g', -1, 64},
   325  	{"LongExp", 1.234567890123456e-78, 'g', -1, 64},
   326  
   327  	{"Big", 123456789123456789123456789, 'g', -1, 64},
   328  	{"BinaryExp", -1, 'b', -1, 64},
   329  
   330  	{"32Integer", 33909, 'g', -1, 32},
   331  	{"32ExactFraction", 3.375, 'g', -1, 32},
   332  	{"32Point", 339.7784, 'g', -1, 32},
   333  	{"32Exp", -5.09e25, 'g', -1, 32},
   334  	{"32NegExp", -5.11e-25, 'g', -1, 32},
   335  	{"32Shortest", 1.234567e-8, 'g', -1, 32},
   336  	{"32Fixed8Hard", math.Ldexp(15961084, -125), 'e', 8, 32},
   337  	{"32Fixed9Hard", math.Ldexp(14855922, -83), 'e', 9, 32},
   338  
   339  	{"64Fixed1", 123456, 'e', 3, 64},
   340  	{"64Fixed2", 123.456, 'e', 3, 64},
   341  	{"64Fixed2.5", 1.2345e+06, 'e', 3, 64},
   342  	{"64Fixed3", 1.23456e+78, 'e', 3, 64},
   343  	{"64Fixed4", 1.23456e-78, 'e', 3, 64},
   344  	{"64Fixed5Hard", 4.096e+25, 'e', 5, 64}, // needs divisiblePow5(..., 20)
   345  	{"64Fixed12", 1.23456e-78, 'e', 12, 64},
   346  	{"64Fixed16", 1.23456e-78, 'e', 16, 64},
   347  	// From testdata/testfp.txt
   348  	{"64Fixed12Hard", math.Ldexp(6965949469487146, -249), 'e', 12, 64},
   349  	{"64Fixed17Hard", math.Ldexp(8887055249355788, 665), 'e', 17, 64},
   350  	{"64Fixed18Hard", math.Ldexp(6994187472632449, 690), 'e', 18, 64},
   351  
   352  	{"64FixedF1", 123.456, 'f', 6, 64},
   353  	{"64FixedF2", 0.0123, 'f', 6, 64},
   354  	{"64FixedF3", 12.3456, 'f', 2, 64},
   355  
   356  	// Trigger slow path (see issue #15672).
   357  	// The shortest is: 8.034137530808823e+43
   358  	{"Slowpath64", 8.03413753080882349e+43, 'e', -1, 64},
   359  	// This denormal is pathological because the lower/upper
   360  	// halfways to neighboring floats are:
   361  	// 622666234635.321003e-320 ~= 622666234635.321e-320
   362  	// 622666234635.321497e-320 ~= 622666234635.3215e-320
   363  	// making it hard to find the 3rd digit
   364  	{"SlowpathDenormal64", 622666234635.3213e-320, 'e', -1, 64},
   365  
   366  	// Trigger the shorter interval case (3.90625e-3 = 1/256).
   367  	{"ShorterIntervalCase32", 3.90625e-3, 'e', -1, 32},
   368  	{"ShorterIntervalCase64", 3.90625e-3, 'e', -1, 64},
   369  }
   370  
   371  func BenchmarkFormatFloat(b *testing.B) {
   372  	for _, c := range ftoaBenches {
   373  		b.Run(c.name, func(b *testing.B) {
   374  			for i := 0; i < b.N; i++ {
   375  				FormatFloat(c.float, c.fmt, c.prec, c.bitSize)
   376  			}
   377  		})
   378  	}
   379  }
   380  
   381  func BenchmarkAppendFloat(b *testing.B) {
   382  	dst := make([]byte, 30)
   383  	for _, c := range ftoaBenches {
   384  		b.Run(c.name, func(b *testing.B) {
   385  			for i := 0; i < b.N; i++ {
   386  				AppendFloat(dst[:0], c.float, c.fmt, c.prec, c.bitSize)
   387  			}
   388  		})
   389  	}
   390  }
   391  

View as plain text