Black Lives Matter. Support the Equal Justice Initiative.

Source file src/strconv/atof_test.go

Documentation: strconv

     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  	"reflect"
    11  	. "strconv"
    12  	"strings"
    13  	"sync"
    14  	"testing"
    15  	"time"
    16  )
    17  
    18  type atofTest struct {
    19  	in  string
    20  	out string
    21  	err error
    22  }
    23  
    24  var atoftests = []atofTest{
    25  	{"", "0", ErrSyntax},
    26  	{"1", "1", nil},
    27  	{"+1", "1", nil},
    28  	{"1x", "0", ErrSyntax},
    29  	{"1.1.", "0", ErrSyntax},
    30  	{"1e23", "1e+23", nil},
    31  	{"1E23", "1e+23", nil},
    32  	{"100000000000000000000000", "1e+23", nil},
    33  	{"1e-100", "1e-100", nil},
    34  	{"123456700", "1.234567e+08", nil},
    35  	{"99999999999999974834176", "9.999999999999997e+22", nil},
    36  	{"100000000000000000000001", "1.0000000000000001e+23", nil},
    37  	{"100000000000000008388608", "1.0000000000000001e+23", nil},
    38  	{"100000000000000016777215", "1.0000000000000001e+23", nil},
    39  	{"100000000000000016777216", "1.0000000000000003e+23", nil},
    40  	{"-1", "-1", nil},
    41  	{"-0.1", "-0.1", nil},
    42  	{"-0", "-0", nil},
    43  	{"1e-20", "1e-20", nil},
    44  	{"625e-3", "0.625", nil},
    45  
    46  	// Hexadecimal floating-point.
    47  	{"0x1p0", "1", nil},
    48  	{"0x1p1", "2", nil},
    49  	{"0x1p-1", "0.5", nil},
    50  	{"0x1ep-1", "15", nil},
    51  	{"-0x1ep-1", "-15", nil},
    52  	{"-0x1_ep-1", "-15", nil},
    53  	{"0x1p-200", "6.223015277861142e-61", nil},
    54  	{"0x1p200", "1.6069380442589903e+60", nil},
    55  	{"0x1fFe2.p0", "131042", nil},
    56  	{"0x1fFe2.P0", "131042", nil},
    57  	{"-0x2p3", "-16", nil},
    58  	{"0x0.fp4", "15", nil},
    59  	{"0x0.fp0", "0.9375", nil},
    60  	{"0x1e2", "0", ErrSyntax},
    61  	{"1p2", "0", ErrSyntax},
    62  
    63  	// zeros
    64  	{"0", "0", nil},
    65  	{"0e0", "0", nil},
    66  	{"-0e0", "-0", nil},
    67  	{"+0e0", "0", nil},
    68  	{"0e-0", "0", nil},
    69  	{"-0e-0", "-0", nil},
    70  	{"+0e-0", "0", nil},
    71  	{"0e+0", "0", nil},
    72  	{"-0e+0", "-0", nil},
    73  	{"+0e+0", "0", nil},
    74  	{"0e+01234567890123456789", "0", nil},
    75  	{"0.00e-01234567890123456789", "0", nil},
    76  	{"-0e+01234567890123456789", "-0", nil},
    77  	{"-0.00e-01234567890123456789", "-0", nil},
    78  	{"0x0p+01234567890123456789", "0", nil},
    79  	{"0x0.00p-01234567890123456789", "0", nil},
    80  	{"-0x0p+01234567890123456789", "-0", nil},
    81  	{"-0x0.00p-01234567890123456789", "-0", nil},
    82  
    83  	{"0e291", "0", nil}, // issue 15364
    84  	{"0e292", "0", nil}, // issue 15364
    85  	{"0e347", "0", nil}, // issue 15364
    86  	{"0e348", "0", nil}, // issue 15364
    87  	{"-0e291", "-0", nil},
    88  	{"-0e292", "-0", nil},
    89  	{"-0e347", "-0", nil},
    90  	{"-0e348", "-0", nil},
    91  	{"0x0p126", "0", nil},
    92  	{"0x0p127", "0", nil},
    93  	{"0x0p128", "0", nil},
    94  	{"0x0p129", "0", nil},
    95  	{"0x0p130", "0", nil},
    96  	{"0x0p1022", "0", nil},
    97  	{"0x0p1023", "0", nil},
    98  	{"0x0p1024", "0", nil},
    99  	{"0x0p1025", "0", nil},
   100  	{"0x0p1026", "0", nil},
   101  	{"-0x0p126", "-0", nil},
   102  	{"-0x0p127", "-0", nil},
   103  	{"-0x0p128", "-0", nil},
   104  	{"-0x0p129", "-0", nil},
   105  	{"-0x0p130", "-0", nil},
   106  	{"-0x0p1022", "-0", nil},
   107  	{"-0x0p1023", "-0", nil},
   108  	{"-0x0p1024", "-0", nil},
   109  	{"-0x0p1025", "-0", nil},
   110  	{"-0x0p1026", "-0", nil},
   111  
   112  	// NaNs
   113  	{"nan", "NaN", nil},
   114  	{"NaN", "NaN", nil},
   115  	{"NAN", "NaN", nil},
   116  
   117  	// Infs
   118  	{"inf", "+Inf", nil},
   119  	{"-Inf", "-Inf", nil},
   120  	{"+INF", "+Inf", nil},
   121  	{"-Infinity", "-Inf", nil},
   122  	{"+INFINITY", "+Inf", nil},
   123  	{"Infinity", "+Inf", nil},
   124  
   125  	// largest float64
   126  	{"1.7976931348623157e308", "1.7976931348623157e+308", nil},
   127  	{"-1.7976931348623157e308", "-1.7976931348623157e+308", nil},
   128  	{"0x1.fffffffffffffp1023", "1.7976931348623157e+308", nil},
   129  	{"-0x1.fffffffffffffp1023", "-1.7976931348623157e+308", nil},
   130  	{"0x1fffffffffffffp+971", "1.7976931348623157e+308", nil},
   131  	{"-0x1fffffffffffffp+971", "-1.7976931348623157e+308", nil},
   132  	{"0x.1fffffffffffffp1027", "1.7976931348623157e+308", nil},
   133  	{"-0x.1fffffffffffffp1027", "-1.7976931348623157e+308", nil},
   134  
   135  	// next float64 - too large
   136  	{"1.7976931348623159e308", "+Inf", ErrRange},
   137  	{"-1.7976931348623159e308", "-Inf", ErrRange},
   138  	{"0x1p1024", "+Inf", ErrRange},
   139  	{"-0x1p1024", "-Inf", ErrRange},
   140  	{"0x2p1023", "+Inf", ErrRange},
   141  	{"-0x2p1023", "-Inf", ErrRange},
   142  	{"0x.1p1028", "+Inf", ErrRange},
   143  	{"-0x.1p1028", "-Inf", ErrRange},
   144  	{"0x.2p1027", "+Inf", ErrRange},
   145  	{"-0x.2p1027", "-Inf", ErrRange},
   146  
   147  	// the border is ...158079
   148  	// borderline - okay
   149  	{"1.7976931348623158e308", "1.7976931348623157e+308", nil},
   150  	{"-1.7976931348623158e308", "-1.7976931348623157e+308", nil},
   151  	{"0x1.fffffffffffff7fffp1023", "1.7976931348623157e+308", nil},
   152  	{"-0x1.fffffffffffff7fffp1023", "-1.7976931348623157e+308", nil},
   153  	// borderline - too large
   154  	{"1.797693134862315808e308", "+Inf", ErrRange},
   155  	{"-1.797693134862315808e308", "-Inf", ErrRange},
   156  	{"0x1.fffffffffffff8p1023", "+Inf", ErrRange},
   157  	{"-0x1.fffffffffffff8p1023", "-Inf", ErrRange},
   158  	{"0x1fffffffffffff.8p+971", "+Inf", ErrRange},
   159  	{"-0x1fffffffffffff8p+967", "-Inf", ErrRange},
   160  	{"0x.1fffffffffffff8p1027", "+Inf", ErrRange},
   161  	{"-0x.1fffffffffffff9p1027", "-Inf", ErrRange},
   162  
   163  	// a little too large
   164  	{"1e308", "1e+308", nil},
   165  	{"2e308", "+Inf", ErrRange},
   166  	{"1e309", "+Inf", ErrRange},
   167  	{"0x1p1025", "+Inf", ErrRange},
   168  
   169  	// way too large
   170  	{"1e310", "+Inf", ErrRange},
   171  	{"-1e310", "-Inf", ErrRange},
   172  	{"1e400", "+Inf", ErrRange},
   173  	{"-1e400", "-Inf", ErrRange},
   174  	{"1e400000", "+Inf", ErrRange},
   175  	{"-1e400000", "-Inf", ErrRange},
   176  	{"0x1p1030", "+Inf", ErrRange},
   177  	{"0x1p2000", "+Inf", ErrRange},
   178  	{"0x1p2000000000", "+Inf", ErrRange},
   179  	{"-0x1p1030", "-Inf", ErrRange},
   180  	{"-0x1p2000", "-Inf", ErrRange},
   181  	{"-0x1p2000000000", "-Inf", ErrRange},
   182  
   183  	// denormalized
   184  	{"1e-305", "1e-305", nil},
   185  	{"1e-306", "1e-306", nil},
   186  	{"1e-307", "1e-307", nil},
   187  	{"1e-308", "1e-308", nil},
   188  	{"1e-309", "1e-309", nil},
   189  	{"1e-310", "1e-310", nil},
   190  	{"1e-322", "1e-322", nil},
   191  	// smallest denormal
   192  	{"5e-324", "5e-324", nil},
   193  	{"4e-324", "5e-324", nil},
   194  	{"3e-324", "5e-324", nil},
   195  	// too small
   196  	{"2e-324", "0", nil},
   197  	// way too small
   198  	{"1e-350", "0", nil},
   199  	{"1e-400000", "0", nil},
   200  
   201  	// Near denormals and denormals.
   202  	{"0x2.00000000000000p-1010", "1.8227805048890994e-304", nil}, // 0x00e0000000000000
   203  	{"0x1.fffffffffffff0p-1010", "1.8227805048890992e-304", nil}, // 0x00dfffffffffffff
   204  	{"0x1.fffffffffffff7p-1010", "1.8227805048890992e-304", nil}, // rounded down
   205  	{"0x1.fffffffffffff8p-1010", "1.8227805048890994e-304", nil}, // rounded up
   206  	{"0x1.fffffffffffff9p-1010", "1.8227805048890994e-304", nil}, // rounded up
   207  
   208  	{"0x2.00000000000000p-1022", "4.450147717014403e-308", nil},  // 0x0020000000000000
   209  	{"0x1.fffffffffffff0p-1022", "4.4501477170144023e-308", nil}, // 0x001fffffffffffff
   210  	{"0x1.fffffffffffff7p-1022", "4.4501477170144023e-308", nil}, // rounded down
   211  	{"0x1.fffffffffffff8p-1022", "4.450147717014403e-308", nil},  // rounded up
   212  	{"0x1.fffffffffffff9p-1022", "4.450147717014403e-308", nil},  // rounded up
   213  
   214  	{"0x1.00000000000000p-1022", "2.2250738585072014e-308", nil}, // 0x0010000000000000
   215  	{"0x0.fffffffffffff0p-1022", "2.225073858507201e-308", nil},  // 0x000fffffffffffff
   216  	{"0x0.ffffffffffffe0p-1022", "2.2250738585072004e-308", nil}, // 0x000ffffffffffffe
   217  	{"0x0.ffffffffffffe7p-1022", "2.2250738585072004e-308", nil}, // rounded down
   218  	{"0x1.ffffffffffffe8p-1023", "2.225073858507201e-308", nil},  // rounded up
   219  	{"0x1.ffffffffffffe9p-1023", "2.225073858507201e-308", nil},  // rounded up
   220  
   221  	{"0x0.00000003fffff0p-1022", "2.072261e-317", nil},  // 0x00000000003fffff
   222  	{"0x0.00000003456780p-1022", "1.694649e-317", nil},  // 0x0000000000345678
   223  	{"0x0.00000003456787p-1022", "1.694649e-317", nil},  // rounded down
   224  	{"0x0.00000003456788p-1022", "1.694649e-317", nil},  // rounded down (half to even)
   225  	{"0x0.00000003456790p-1022", "1.6946496e-317", nil}, // 0x0000000000345679
   226  	{"0x0.00000003456789p-1022", "1.6946496e-317", nil}, // rounded up
   227  
   228  	{"0x0.0000000345678800000000000000000000000001p-1022", "1.6946496e-317", nil}, // rounded up
   229  
   230  	{"0x0.000000000000f0p-1022", "7.4e-323", nil}, // 0x000000000000000f
   231  	{"0x0.00000000000060p-1022", "3e-323", nil},   // 0x0000000000000006
   232  	{"0x0.00000000000058p-1022", "3e-323", nil},   // rounded up
   233  	{"0x0.00000000000057p-1022", "2.5e-323", nil}, // rounded down
   234  	{"0x0.00000000000050p-1022", "2.5e-323", nil}, // 0x0000000000000005
   235  
   236  	{"0x0.00000000000010p-1022", "5e-324", nil},  // 0x0000000000000001
   237  	{"0x0.000000000000081p-1022", "5e-324", nil}, // rounded up
   238  	{"0x0.00000000000008p-1022", "0", nil},       // rounded down
   239  	{"0x0.00000000000007fp-1022", "0", nil},      // rounded down
   240  
   241  	// try to overflow exponent
   242  	{"1e-4294967296", "0", nil},
   243  	{"1e+4294967296", "+Inf", ErrRange},
   244  	{"1e-18446744073709551616", "0", nil},
   245  	{"1e+18446744073709551616", "+Inf", ErrRange},
   246  	{"0x1p-4294967296", "0", nil},
   247  	{"0x1p+4294967296", "+Inf", ErrRange},
   248  	{"0x1p-18446744073709551616", "0", nil},
   249  	{"0x1p+18446744073709551616", "+Inf", ErrRange},
   250  
   251  	// Parse errors
   252  	{"1e", "0", ErrSyntax},
   253  	{"1e-", "0", ErrSyntax},
   254  	{".e-1", "0", ErrSyntax},
   255  	{"1\x00.2", "0", ErrSyntax},
   256  	{"0x", "0", ErrSyntax},
   257  	{"0x.", "0", ErrSyntax},
   258  	{"0x1", "0", ErrSyntax},
   259  	{"0x.1", "0", ErrSyntax},
   260  	{"0x1p", "0", ErrSyntax},
   261  	{"0x.1p", "0", ErrSyntax},
   262  	{"0x1p+", "0", ErrSyntax},
   263  	{"0x.1p+", "0", ErrSyntax},
   264  	{"0x1p-", "0", ErrSyntax},
   265  	{"0x.1p-", "0", ErrSyntax},
   266  	{"0x1p+2", "4", nil},
   267  	{"0x.1p+2", "0.25", nil},
   268  	{"0x1p-2", "0.25", nil},
   269  	{"0x.1p-2", "0.015625", nil},
   270  
   271  	// https://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
   272  	{"2.2250738585072012e-308", "2.2250738585072014e-308", nil},
   273  	// https://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
   274  	{"2.2250738585072011e-308", "2.225073858507201e-308", nil},
   275  
   276  	// A very large number (initially wrongly parsed by the fast algorithm).
   277  	{"4.630813248087435e+307", "4.630813248087435e+307", nil},
   278  
   279  	// A different kind of very large number.
   280  	{"22.222222222222222", "22.22222222222222", nil},
   281  	{"2." + strings.Repeat("2", 4000) + "e+1", "22.22222222222222", nil},
   282  	{"0x1.1111111111111p222", "7.18931911124017e+66", nil},
   283  	{"0x2.2222222222222p221", "7.18931911124017e+66", nil},
   284  	{"0x2." + strings.Repeat("2", 4000) + "p221", "7.18931911124017e+66", nil},
   285  
   286  	// Exactly halfway between 1 and math.Nextafter(1, 2).
   287  	// Round to even (down).
   288  	{"1.00000000000000011102230246251565404236316680908203125", "1", nil},
   289  	{"0x1.00000000000008p0", "1", nil},
   290  	// Slightly lower; still round down.
   291  	{"1.00000000000000011102230246251565404236316680908203124", "1", nil},
   292  	{"0x1.00000000000007Fp0", "1", nil},
   293  	// Slightly higher; round up.
   294  	{"1.00000000000000011102230246251565404236316680908203126", "1.0000000000000002", nil},
   295  	{"0x1.000000000000081p0", "1.0000000000000002", nil},
   296  	{"0x1.00000000000009p0", "1.0000000000000002", nil},
   297  	// Slightly higher, but you have to read all the way to the end.
   298  	{"1.00000000000000011102230246251565404236316680908203125" + strings.Repeat("0", 10000) + "1", "1.0000000000000002", nil},
   299  	{"0x1.00000000000008" + strings.Repeat("0", 10000) + "1p0", "1.0000000000000002", nil},
   300  
   301  	// Halfway between x := math.Nextafter(1, 2) and math.Nextafter(x, 2)
   302  	// Round to even (up).
   303  	{"1.00000000000000033306690738754696212708950042724609375", "1.0000000000000004", nil},
   304  	{"0x1.00000000000018p0", "1.0000000000000004", nil},
   305  
   306  	// Halfway between 1090544144181609278303144771584 and 1090544144181609419040633126912
   307  	// (15497564393479157p+46, should round to even 15497564393479156p+46, issue 36657)
   308  	{"1090544144181609348671888949248", "1.0905441441816093e+30", nil},
   309  	// slightly above, rounds up
   310  	{"1090544144181609348835077142190", "1.0905441441816094e+30", nil},
   311  
   312  	// Underscores.
   313  	{"1_23.50_0_0e+1_2", "1.235e+14", nil},
   314  	{"-_123.5e+12", "0", ErrSyntax},
   315  	{"+_123.5e+12", "0", ErrSyntax},
   316  	{"_123.5e+12", "0", ErrSyntax},
   317  	{"1__23.5e+12", "0", ErrSyntax},
   318  	{"123_.5e+12", "0", ErrSyntax},
   319  	{"123._5e+12", "0", ErrSyntax},
   320  	{"123.5_e+12", "0", ErrSyntax},
   321  	{"123.5__0e+12", "0", ErrSyntax},
   322  	{"123.5e_+12", "0", ErrSyntax},
   323  	{"123.5e+_12", "0", ErrSyntax},
   324  	{"123.5e_-12", "0", ErrSyntax},
   325  	{"123.5e-_12", "0", ErrSyntax},
   326  	{"123.5e+1__2", "0", ErrSyntax},
   327  	{"123.5e+12_", "0", ErrSyntax},
   328  
   329  	{"0x_1_2.3_4_5p+1_2", "74565", nil},
   330  	{"-_0x12.345p+12", "0", ErrSyntax},
   331  	{"+_0x12.345p+12", "0", ErrSyntax},
   332  	{"_0x12.345p+12", "0", ErrSyntax},
   333  	{"0x__12.345p+12", "0", ErrSyntax},
   334  	{"0x1__2.345p+12", "0", ErrSyntax},
   335  	{"0x12_.345p+12", "0", ErrSyntax},
   336  	{"0x12._345p+12", "0", ErrSyntax},
   337  	{"0x12.3__45p+12", "0", ErrSyntax},
   338  	{"0x12.345_p+12", "0", ErrSyntax},
   339  	{"0x12.345p_+12", "0", ErrSyntax},
   340  	{"0x12.345p+_12", "0", ErrSyntax},
   341  	{"0x12.345p_-12", "0", ErrSyntax},
   342  	{"0x12.345p-_12", "0", ErrSyntax},
   343  	{"0x12.345p+1__2", "0", ErrSyntax},
   344  	{"0x12.345p+12_", "0", ErrSyntax},
   345  
   346  	{"1e100x", "0", ErrSyntax},
   347  	{"1e1000x", "0", ErrSyntax},
   348  }
   349  
   350  var atof32tests = []atofTest{
   351  	// Hex
   352  	{"0x1p-100", "7.888609e-31", nil},
   353  	{"0x1p100", "1.2676506e+30", nil},
   354  
   355  	// Exactly halfway between 1 and the next float32.
   356  	// Round to even (down).
   357  	{"1.000000059604644775390625", "1", nil},
   358  	{"0x1.000001p0", "1", nil},
   359  	// Slightly lower.
   360  	{"1.000000059604644775390624", "1", nil},
   361  	{"0x1.0000008p0", "1", nil},
   362  	{"0x1.000000fp0", "1", nil},
   363  	// Slightly higher.
   364  	{"1.000000059604644775390626", "1.0000001", nil},
   365  	{"0x1.000002p0", "1.0000001", nil},
   366  	{"0x1.0000018p0", "1.0000001", nil},
   367  	{"0x1.0000011p0", "1.0000001", nil},
   368  	// Slightly higher, but you have to read all the way to the end.
   369  	{"1.000000059604644775390625" + strings.Repeat("0", 10000) + "1", "1.0000001", nil},
   370  	{"0x1.000001" + strings.Repeat("0", 10000) + "1p0", "1.0000001", nil},
   371  
   372  	// largest float32: (1<<128) * (1 - 2^-24)
   373  	{"340282346638528859811704183484516925440", "3.4028235e+38", nil},
   374  	{"-340282346638528859811704183484516925440", "-3.4028235e+38", nil},
   375  	{"0x.ffffffp128", "3.4028235e+38", nil},
   376  	{"-340282346638528859811704183484516925440", "-3.4028235e+38", nil},
   377  	{"-0x.ffffffp128", "-3.4028235e+38", nil},
   378  	// next float32 - too large
   379  	{"3.4028236e38", "+Inf", ErrRange},
   380  	{"-3.4028236e38", "-Inf", ErrRange},
   381  	{"0x1.0p128", "+Inf", ErrRange},
   382  	{"-0x1.0p128", "-Inf", ErrRange},
   383  	// the border is 3.40282356779...e+38
   384  	// borderline - okay
   385  	{"3.402823567e38", "3.4028235e+38", nil},
   386  	{"-3.402823567e38", "-3.4028235e+38", nil},
   387  	{"0x.ffffff7fp128", "3.4028235e+38", nil},
   388  	{"-0x.ffffff7fp128", "-3.4028235e+38", nil},
   389  	// borderline - too large
   390  	{"3.4028235678e38", "+Inf", ErrRange},
   391  	{"-3.4028235678e38", "-Inf", ErrRange},
   392  	{"0x.ffffff8p128", "+Inf", ErrRange},
   393  	{"-0x.ffffff8p128", "-Inf", ErrRange},
   394  
   395  	// Denormals: less than 2^-126
   396  	{"1e-38", "1e-38", nil},
   397  	{"1e-39", "1e-39", nil},
   398  	{"1e-40", "1e-40", nil},
   399  	{"1e-41", "1e-41", nil},
   400  	{"1e-42", "1e-42", nil},
   401  	{"1e-43", "1e-43", nil},
   402  	{"1e-44", "1e-44", nil},
   403  	{"6e-45", "6e-45", nil}, // 4p-149 = 5.6e-45
   404  	{"5e-45", "6e-45", nil},
   405  
   406  	// Smallest denormal
   407  	{"1e-45", "1e-45", nil}, // 1p-149 = 1.4e-45
   408  	{"2e-45", "1e-45", nil},
   409  	{"3e-45", "3e-45", nil},
   410  
   411  	// Near denormals and denormals.
   412  	{"0x0.89aBcDp-125", "1.2643093e-38", nil},  // 0x0089abcd
   413  	{"0x0.8000000p-125", "1.1754944e-38", nil}, // 0x00800000
   414  	{"0x0.1234560p-125", "1.671814e-39", nil},  // 0x00123456
   415  	{"0x0.1234567p-125", "1.671814e-39", nil},  // rounded down
   416  	{"0x0.1234568p-125", "1.671814e-39", nil},  // rounded down
   417  	{"0x0.1234569p-125", "1.671815e-39", nil},  // rounded up
   418  	{"0x0.1234570p-125", "1.671815e-39", nil},  // 0x00123457
   419  	{"0x0.0000010p-125", "1e-45", nil},         // 0x00000001
   420  	{"0x0.00000081p-125", "1e-45", nil},        // rounded up
   421  	{"0x0.0000008p-125", "0", nil},             // rounded down
   422  	{"0x0.0000007p-125", "0", nil},             // rounded down
   423  
   424  	// 2^92 = 8388608p+69 = 4951760157141521099596496896 (4.9517602e27)
   425  	// is an exact power of two that needs 8 decimal digits to be correctly
   426  	// parsed back.
   427  	// The float32 before is 16777215p+68 = 4.95175986e+27
   428  	// The halfway is 4.951760009. A bad algorithm that thinks the previous
   429  	// float32 is 8388607p+69 will shorten incorrectly to 4.95176e+27.
   430  	{"4951760157141521099596496896", "4.9517602e+27", nil},
   431  }
   432  
   433  type atofSimpleTest struct {
   434  	x float64
   435  	s string
   436  }
   437  
   438  var (
   439  	atofOnce               sync.Once
   440  	atofRandomTests        []atofSimpleTest
   441  	benchmarksRandomBits   [1024]string
   442  	benchmarksRandomNormal [1024]string
   443  )
   444  
   445  func initAtof() {
   446  	atofOnce.Do(initAtofOnce)
   447  }
   448  
   449  func initAtofOnce() {
   450  	// The atof routines return NumErrors wrapping
   451  	// the error and the string. Convert the table above.
   452  	for i := range atoftests {
   453  		test := &atoftests[i]
   454  		if test.err != nil {
   455  			test.err = &NumError{"ParseFloat", test.in, test.err}
   456  		}
   457  	}
   458  	for i := range atof32tests {
   459  		test := &atof32tests[i]
   460  		if test.err != nil {
   461  			test.err = &NumError{"ParseFloat", test.in, test.err}
   462  		}
   463  	}
   464  
   465  	// Generate random inputs for tests and benchmarks
   466  	rand.Seed(time.Now().UnixNano())
   467  	if testing.Short() {
   468  		atofRandomTests = make([]atofSimpleTest, 100)
   469  	} else {
   470  		atofRandomTests = make([]atofSimpleTest, 10000)
   471  	}
   472  	for i := range atofRandomTests {
   473  		n := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
   474  		x := math.Float64frombits(n)
   475  		s := FormatFloat(x, 'g', -1, 64)
   476  		atofRandomTests[i] = atofSimpleTest{x, s}
   477  	}
   478  
   479  	for i := range benchmarksRandomBits {
   480  		bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
   481  		x := math.Float64frombits(bits)
   482  		benchmarksRandomBits[i] = FormatFloat(x, 'g', -1, 64)
   483  	}
   484  
   485  	for i := range benchmarksRandomNormal {
   486  		x := rand.NormFloat64()
   487  		benchmarksRandomNormal[i] = FormatFloat(x, 'g', -1, 64)
   488  	}
   489  }
   490  
   491  func TestParseFloatPrefix(t *testing.T) {
   492  	for i := range atoftests {
   493  		test := &atoftests[i]
   494  		if test.err != nil {
   495  			continue
   496  		}
   497  		// Adding characters that do not extend a number should not invalidate it.
   498  		// Test a few. The "i" and "init" cases test that we accept "infi", "infinit"
   499  		// correctly as "inf" with suffix.
   500  		for _, suffix := range []string{" ", "q", "+", "-", "<", "=", ">", "(", ")", "i", "init"} {
   501  			in := test.in + suffix
   502  			_, n, err := ParseFloatPrefix(in, 64)
   503  			if err != nil {
   504  				t.Errorf("ParseFloatPrefix(%q, 64): err = %v; want no error", in, err)
   505  			}
   506  			if n != len(test.in) {
   507  				t.Errorf("ParseFloatPrefix(%q, 64): n = %d; want %d", in, n, len(test.in))
   508  			}
   509  		}
   510  	}
   511  }
   512  
   513  func testAtof(t *testing.T, opt bool) {
   514  	initAtof()
   515  	oldopt := SetOptimize(opt)
   516  	for i := 0; i < len(atoftests); i++ {
   517  		test := &atoftests[i]
   518  		out, err := ParseFloat(test.in, 64)
   519  		outs := FormatFloat(out, 'g', -1, 64)
   520  		if outs != test.out || !reflect.DeepEqual(err, test.err) {
   521  			t.Errorf("ParseFloat(%v, 64) = %v, %v want %v, %v",
   522  				test.in, out, err, test.out, test.err)
   523  		}
   524  
   525  		if float64(float32(out)) == out {
   526  			out, err := ParseFloat(test.in, 32)
   527  			out32 := float32(out)
   528  			if float64(out32) != out {
   529  				t.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test.in, out, float64(out32))
   530  				continue
   531  			}
   532  			outs := FormatFloat(float64(out32), 'g', -1, 32)
   533  			if outs != test.out || !reflect.DeepEqual(err, test.err) {
   534  				t.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v  # %v",
   535  					test.in, out32, err, test.out, test.err, out)
   536  			}
   537  		}
   538  	}
   539  	for _, test := range atof32tests {
   540  		out, err := ParseFloat(test.in, 32)
   541  		out32 := float32(out)
   542  		if float64(out32) != out {
   543  			t.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test.in, out, float64(out32))
   544  			continue
   545  		}
   546  		outs := FormatFloat(float64(out32), 'g', -1, 32)
   547  		if outs != test.out || !reflect.DeepEqual(err, test.err) {
   548  			t.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v  # %v",
   549  				test.in, out32, err, test.out, test.err, out)
   550  		}
   551  	}
   552  	SetOptimize(oldopt)
   553  }
   554  
   555  func TestAtof(t *testing.T) { testAtof(t, true) }
   556  
   557  func TestAtofSlow(t *testing.T) { testAtof(t, false) }
   558  
   559  func TestAtofRandom(t *testing.T) {
   560  	initAtof()
   561  	for _, test := range atofRandomTests {
   562  		x, _ := ParseFloat(test.s, 64)
   563  		switch {
   564  		default:
   565  			t.Errorf("number %s badly parsed as %b (expected %b)", test.s, x, test.x)
   566  		case x == test.x:
   567  		case math.IsNaN(test.x) && math.IsNaN(x):
   568  		}
   569  	}
   570  	t.Logf("tested %d random numbers", len(atofRandomTests))
   571  }
   572  
   573  var roundTripCases = []struct {
   574  	f float64
   575  	s string
   576  }{
   577  	// Issue 2917.
   578  	// This test will break the optimized conversion if the
   579  	// FPU is using 80-bit registers instead of 64-bit registers,
   580  	// usually because the operating system initialized the
   581  	// thread with 80-bit precision and the Go runtime didn't
   582  	// fix the FP control word.
   583  	{8865794286000691 << 39, "4.87402195346389e+27"},
   584  	{8865794286000692 << 39, "4.8740219534638903e+27"},
   585  }
   586  
   587  func TestRoundTrip(t *testing.T) {
   588  	for _, tt := range roundTripCases {
   589  		old := SetOptimize(false)
   590  		s := FormatFloat(tt.f, 'g', -1, 64)
   591  		if s != tt.s {
   592  			t.Errorf("no-opt FormatFloat(%b) = %s, want %s", tt.f, s, tt.s)
   593  		}
   594  		f, err := ParseFloat(tt.s, 64)
   595  		if f != tt.f || err != nil {
   596  			t.Errorf("no-opt ParseFloat(%s) = %b, %v want %b, nil", tt.s, f, err, tt.f)
   597  		}
   598  		SetOptimize(true)
   599  		s = FormatFloat(tt.f, 'g', -1, 64)
   600  		if s != tt.s {
   601  			t.Errorf("opt FormatFloat(%b) = %s, want %s", tt.f, s, tt.s)
   602  		}
   603  		f, err = ParseFloat(tt.s, 64)
   604  		if f != tt.f || err != nil {
   605  			t.Errorf("opt ParseFloat(%s) = %b, %v want %b, nil", tt.s, f, err, tt.f)
   606  		}
   607  		SetOptimize(old)
   608  	}
   609  }
   610  
   611  // TestRoundTrip32 tries a fraction of all finite positive float32 values.
   612  func TestRoundTrip32(t *testing.T) {
   613  	step := uint32(997)
   614  	if testing.Short() {
   615  		step = 99991
   616  	}
   617  	count := 0
   618  	for i := uint32(0); i < 0xff<<23; i += step {
   619  		f := math.Float32frombits(i)
   620  		if i&1 == 1 {
   621  			f = -f // negative
   622  		}
   623  		s := FormatFloat(float64(f), 'g', -1, 32)
   624  
   625  		parsed, err := ParseFloat(s, 32)
   626  		parsed32 := float32(parsed)
   627  		switch {
   628  		case err != nil:
   629  			t.Errorf("ParseFloat(%q, 32) gave error %s", s, err)
   630  		case float64(parsed32) != parsed:
   631  			t.Errorf("ParseFloat(%q, 32) = %v, not a float32 (nearest is %v)", s, parsed, parsed32)
   632  		case parsed32 != f:
   633  			t.Errorf("ParseFloat(%q, 32) = %b (expected %b)", s, parsed32, f)
   634  		}
   635  		count++
   636  	}
   637  	t.Logf("tested %d float32's", count)
   638  }
   639  
   640  // Issue 42297: a lot of code in the wild accidentally calls ParseFloat(s, 10)
   641  // or ParseFloat(s, 0), so allow bitSize values other than 32 and 64.
   642  func TestParseFloatIncorrectBitSize(t *testing.T) {
   643  	const s = "1.5e308"
   644  	const want = 1.5e308
   645  
   646  	for _, bitSize := range []int{0, 10, 100, 128} {
   647  		f, err := ParseFloat(s, bitSize)
   648  		if err != nil {
   649  			t.Fatalf("ParseFloat(%q, %d) gave error %s", s, bitSize, err)
   650  		}
   651  		if f != want {
   652  			t.Fatalf("ParseFloat(%q, %d) = %g (expected %g)", s, bitSize, f, want)
   653  		}
   654  	}
   655  }
   656  
   657  func BenchmarkAtof64Decimal(b *testing.B) {
   658  	for i := 0; i < b.N; i++ {
   659  		ParseFloat("33909", 64)
   660  	}
   661  }
   662  
   663  func BenchmarkAtof64Float(b *testing.B) {
   664  	for i := 0; i < b.N; i++ {
   665  		ParseFloat("339.7784", 64)
   666  	}
   667  }
   668  
   669  func BenchmarkAtof64FloatExp(b *testing.B) {
   670  	for i := 0; i < b.N; i++ {
   671  		ParseFloat("-5.09e75", 64)
   672  	}
   673  }
   674  
   675  func BenchmarkAtof64Big(b *testing.B) {
   676  	for i := 0; i < b.N; i++ {
   677  		ParseFloat("123456789123456789123456789", 64)
   678  	}
   679  }
   680  
   681  func BenchmarkAtof64RandomBits(b *testing.B) {
   682  	initAtof()
   683  	b.ResetTimer()
   684  	for i := 0; i < b.N; i++ {
   685  		ParseFloat(benchmarksRandomBits[i%1024], 64)
   686  	}
   687  }
   688  
   689  func BenchmarkAtof64RandomFloats(b *testing.B) {
   690  	initAtof()
   691  	b.ResetTimer()
   692  	for i := 0; i < b.N; i++ {
   693  		ParseFloat(benchmarksRandomNormal[i%1024], 64)
   694  	}
   695  }
   696  
   697  func BenchmarkAtof64RandomLongFloats(b *testing.B) {
   698  	initAtof()
   699  	samples := make([]string, len(atofRandomTests))
   700  	for i, t := range atofRandomTests {
   701  		samples[i] = FormatFloat(t.x, 'g', 20, 64)
   702  	}
   703  	b.ResetTimer()
   704  	idx := 0
   705  	for i := 0; i < b.N; i++ {
   706  		ParseFloat(samples[idx], 64)
   707  		idx++
   708  		if idx == len(samples) {
   709  			idx = 0
   710  		}
   711  	}
   712  }
   713  
   714  func BenchmarkAtof32Decimal(b *testing.B) {
   715  	for i := 0; i < b.N; i++ {
   716  		ParseFloat("33909", 32)
   717  	}
   718  }
   719  
   720  func BenchmarkAtof32Float(b *testing.B) {
   721  	for i := 0; i < b.N; i++ {
   722  		ParseFloat("339.778", 32)
   723  	}
   724  }
   725  
   726  func BenchmarkAtof32FloatExp(b *testing.B) {
   727  	for i := 0; i < b.N; i++ {
   728  		ParseFloat("12.3456e32", 32)
   729  	}
   730  }
   731  
   732  func BenchmarkAtof32Random(b *testing.B) {
   733  	n := uint32(997)
   734  	var float32strings [4096]string
   735  	for i := range float32strings {
   736  		n = (99991*n + 42) % (0xff << 23)
   737  		float32strings[i] = FormatFloat(float64(math.Float32frombits(n)), 'g', -1, 32)
   738  	}
   739  	b.ResetTimer()
   740  	for i := 0; i < b.N; i++ {
   741  		ParseFloat(float32strings[i%4096], 32)
   742  	}
   743  }
   744  
   745  func BenchmarkAtof32RandomLong(b *testing.B) {
   746  	n := uint32(997)
   747  	var float32strings [4096]string
   748  	for i := range float32strings {
   749  		n = (99991*n + 42) % (0xff << 23)
   750  		float32strings[i] = FormatFloat(float64(math.Float32frombits(n)), 'g', 20, 32)
   751  	}
   752  	b.ResetTimer()
   753  	for i := 0; i < b.N; i++ {
   754  		ParseFloat(float32strings[i%4096], 32)
   755  	}
   756  }
   757  

View as plain text