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 5package strconv_test 6 7import ( 8 "math" 9 "math/rand" 10 "reflect" 11 . "strconv" 12 "strings" 13 "sync" 14 "testing" 15 "time" 16) 17 18type atofTest struct { 19 in string 20 out string 21 err error 22} 23 24var 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 // zeros 47 {"0", "0", nil}, 48 {"0e0", "0", nil}, 49 {"-0e0", "-0", nil}, 50 {"+0e0", "0", nil}, 51 {"0e-0", "0", nil}, 52 {"-0e-0", "-0", nil}, 53 {"+0e-0", "0", nil}, 54 {"0e+0", "0", nil}, 55 {"-0e+0", "-0", nil}, 56 {"+0e+0", "0", nil}, 57 {"0e+01234567890123456789", "0", nil}, 58 {"0.00e-01234567890123456789", "0", nil}, 59 {"-0e+01234567890123456789", "-0", nil}, 60 {"-0.00e-01234567890123456789", "-0", nil}, 61 {"0e291", "0", nil}, // issue 15364 62 {"0e292", "0", nil}, // issue 15364 63 {"0e347", "0", nil}, // issue 15364 64 {"0e348", "0", nil}, // issue 15364 65 {"-0e291", "-0", nil}, 66 {"-0e292", "-0", nil}, 67 {"-0e347", "-0", nil}, 68 {"-0e348", "-0", nil}, 69 70 // NaNs 71 {"nan", "NaN", nil}, 72 {"NaN", "NaN", nil}, 73 {"NAN", "NaN", nil}, 74 75 // Infs 76 {"inf", "+Inf", nil}, 77 {"-Inf", "-Inf", nil}, 78 {"+INF", "+Inf", nil}, 79 {"-Infinity", "-Inf", nil}, 80 {"+INFINITY", "+Inf", nil}, 81 {"Infinity", "+Inf", nil}, 82 83 // largest float64 84 {"1.7976931348623157e308", "1.7976931348623157e+308", nil}, 85 {"-1.7976931348623157e308", "-1.7976931348623157e+308", nil}, 86 // next float64 - too large 87 {"1.7976931348623159e308", "+Inf", ErrRange}, 88 {"-1.7976931348623159e308", "-Inf", ErrRange}, 89 // the border is ...158079 90 // borderline - okay 91 {"1.7976931348623158e308", "1.7976931348623157e+308", nil}, 92 {"-1.7976931348623158e308", "-1.7976931348623157e+308", nil}, 93 // borderline - too large 94 {"1.797693134862315808e308", "+Inf", ErrRange}, 95 {"-1.797693134862315808e308", "-Inf", ErrRange}, 96 97 // a little too large 98 {"1e308", "1e+308", nil}, 99 {"2e308", "+Inf", ErrRange}, 100 {"1e309", "+Inf", ErrRange}, 101 102 // way too large 103 {"1e310", "+Inf", ErrRange}, 104 {"-1e310", "-Inf", ErrRange}, 105 {"1e400", "+Inf", ErrRange}, 106 {"-1e400", "-Inf", ErrRange}, 107 {"1e400000", "+Inf", ErrRange}, 108 {"-1e400000", "-Inf", ErrRange}, 109 110 // denormalized 111 {"1e-305", "1e-305", nil}, 112 {"1e-306", "1e-306", nil}, 113 {"1e-307", "1e-307", nil}, 114 {"1e-308", "1e-308", nil}, 115 {"1e-309", "1e-309", nil}, 116 {"1e-310", "1e-310", nil}, 117 {"1e-322", "1e-322", nil}, 118 // smallest denormal 119 {"5e-324", "5e-324", nil}, 120 {"4e-324", "5e-324", nil}, 121 {"3e-324", "5e-324", nil}, 122 // too small 123 {"2e-324", "0", nil}, 124 // way too small 125 {"1e-350", "0", nil}, 126 {"1e-400000", "0", nil}, 127 128 // try to overflow exponent 129 {"1e-4294967296", "0", nil}, 130 {"1e+4294967296", "+Inf", ErrRange}, 131 {"1e-18446744073709551616", "0", nil}, 132 {"1e+18446744073709551616", "+Inf", ErrRange}, 133 134 // Parse errors 135 {"1e", "0", ErrSyntax}, 136 {"1e-", "0", ErrSyntax}, 137 {".e-1", "0", ErrSyntax}, 138 {"1\x00.2", "0", ErrSyntax}, 139 140 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/ 141 {"2.2250738585072012e-308", "2.2250738585072014e-308", nil}, 142 // http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/ 143 {"2.2250738585072011e-308", "2.225073858507201e-308", nil}, 144 145 // A very large number (initially wrongly parsed by the fast algorithm). 146 {"4.630813248087435e+307", "4.630813248087435e+307", nil}, 147 148 // A different kind of very large number. 149 {"22.222222222222222", "22.22222222222222", nil}, 150 {"2." + strings.Repeat("2", 4000) + "e+1", "22.22222222222222", nil}, 151 152 // Exactly halfway between 1 and math.Nextafter(1, 2). 153 // Round to even (down). 154 {"1.00000000000000011102230246251565404236316680908203125", "1", nil}, 155 // Slightly lower; still round down. 156 {"1.00000000000000011102230246251565404236316680908203124", "1", nil}, 157 // Slightly higher; round up. 158 {"1.00000000000000011102230246251565404236316680908203126", "1.0000000000000002", nil}, 159 // Slightly higher, but you have to read all the way to the end. 160 {"1.00000000000000011102230246251565404236316680908203125" + strings.Repeat("0", 10000) + "1", "1.0000000000000002", nil}, 161} 162 163var atof32tests = []atofTest{ 164 // Exactly halfway between 1 and the next float32. 165 // Round to even (down). 166 {"1.000000059604644775390625", "1", nil}, 167 // Slightly lower. 168 {"1.000000059604644775390624", "1", nil}, 169 // Slightly higher. 170 {"1.000000059604644775390626", "1.0000001", nil}, 171 // Slightly higher, but you have to read all the way to the end. 172 {"1.000000059604644775390625" + strings.Repeat("0", 10000) + "1", "1.0000001", nil}, 173 174 // largest float32: (1<<128) * (1 - 2^-24) 175 {"340282346638528859811704183484516925440", "3.4028235e+38", nil}, 176 {"-340282346638528859811704183484516925440", "-3.4028235e+38", nil}, 177 // next float32 - too large 178 {"3.4028236e38", "+Inf", ErrRange}, 179 {"-3.4028236e38", "-Inf", ErrRange}, 180 // the border is 3.40282356779...e+38 181 // borderline - okay 182 {"3.402823567e38", "3.4028235e+38", nil}, 183 {"-3.402823567e38", "-3.4028235e+38", nil}, 184 // borderline - too large 185 {"3.4028235678e38", "+Inf", ErrRange}, 186 {"-3.4028235678e38", "-Inf", ErrRange}, 187 188 // Denormals: less than 2^-126 189 {"1e-38", "1e-38", nil}, 190 {"1e-39", "1e-39", nil}, 191 {"1e-40", "1e-40", nil}, 192 {"1e-41", "1e-41", nil}, 193 {"1e-42", "1e-42", nil}, 194 {"1e-43", "1e-43", nil}, 195 {"1e-44", "1e-44", nil}, 196 {"6e-45", "6e-45", nil}, // 4p-149 = 5.6e-45 197 {"5e-45", "6e-45", nil}, 198 // Smallest denormal 199 {"1e-45", "1e-45", nil}, // 1p-149 = 1.4e-45 200 {"2e-45", "1e-45", nil}, 201 202 // 2^92 = 8388608p+69 = 4951760157141521099596496896 (4.9517602e27) 203 // is an exact power of two that needs 8 decimal digits to be correctly 204 // parsed back. 205 // The float32 before is 16777215p+68 = 4.95175986e+27 206 // The halfway is 4.951760009. A bad algorithm that thinks the previous 207 // float32 is 8388607p+69 will shorten incorrectly to 4.95176e+27. 208 {"4951760157141521099596496896", "4.9517602e+27", nil}, 209} 210 211type atofSimpleTest struct { 212 x float64 213 s string 214} 215 216var ( 217 atofOnce sync.Once 218 atofRandomTests []atofSimpleTest 219 benchmarksRandomBits [1024]string 220 benchmarksRandomNormal [1024]string 221) 222 223func initAtof() { 224 atofOnce.Do(initAtofOnce) 225} 226 227func initAtofOnce() { 228 // The atof routines return NumErrors wrapping 229 // the error and the string. Convert the table above. 230 for i := range atoftests { 231 test := &atoftests[i] 232 if test.err != nil { 233 test.err = &NumError{"ParseFloat", test.in, test.err} 234 } 235 } 236 for i := range atof32tests { 237 test := &atof32tests[i] 238 if test.err != nil { 239 test.err = &NumError{"ParseFloat", test.in, test.err} 240 } 241 } 242 243 // Generate random inputs for tests and benchmarks 244 rand.Seed(time.Now().UnixNano()) 245 if testing.Short() { 246 atofRandomTests = make([]atofSimpleTest, 100) 247 } else { 248 atofRandomTests = make([]atofSimpleTest, 10000) 249 } 250 for i := range atofRandomTests { 251 n := uint64(rand.Uint32())<<32 | uint64(rand.Uint32()) 252 x := math.Float64frombits(n) 253 s := FormatFloat(x, 'g', -1, 64) 254 atofRandomTests[i] = atofSimpleTest{x, s} 255 } 256 257 for i := range benchmarksRandomBits { 258 bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32()) 259 x := math.Float64frombits(bits) 260 benchmarksRandomBits[i] = FormatFloat(x, 'g', -1, 64) 261 } 262 263 for i := range benchmarksRandomNormal { 264 x := rand.NormFloat64() 265 benchmarksRandomNormal[i] = FormatFloat(x, 'g', -1, 64) 266 } 267} 268 269func testAtof(t *testing.T, opt bool) { 270 initAtof() 271 oldopt := SetOptimize(opt) 272 for i := 0; i < len(atoftests); i++ { 273 test := &atoftests[i] 274 out, err := ParseFloat(test.in, 64) 275 outs := FormatFloat(out, 'g', -1, 64) 276 if outs != test.out || !reflect.DeepEqual(err, test.err) { 277 t.Errorf("ParseFloat(%v, 64) = %v, %v want %v, %v", 278 test.in, out, err, test.out, test.err) 279 } 280 281 if float64(float32(out)) == out { 282 out, err := ParseFloat(test.in, 32) 283 out32 := float32(out) 284 if float64(out32) != out { 285 t.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test.in, out, float64(out32)) 286 continue 287 } 288 outs := FormatFloat(float64(out32), 'g', -1, 32) 289 if outs != test.out || !reflect.DeepEqual(err, test.err) { 290 t.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v # %v", 291 test.in, out32, err, test.out, test.err, out) 292 } 293 } 294 } 295 for _, test := range atof32tests { 296 out, err := ParseFloat(test.in, 32) 297 out32 := float32(out) 298 if float64(out32) != out { 299 t.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test.in, out, float64(out32)) 300 continue 301 } 302 outs := FormatFloat(float64(out32), 'g', -1, 32) 303 if outs != test.out || !reflect.DeepEqual(err, test.err) { 304 t.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v # %v", 305 test.in, out32, err, test.out, test.err, out) 306 } 307 } 308 SetOptimize(oldopt) 309} 310 311func TestAtof(t *testing.T) { testAtof(t, true) } 312 313func TestAtofSlow(t *testing.T) { testAtof(t, false) } 314 315func TestAtofRandom(t *testing.T) { 316 initAtof() 317 for _, test := range atofRandomTests { 318 x, _ := ParseFloat(test.s, 64) 319 switch { 320 default: 321 t.Errorf("number %s badly parsed as %b (expected %b)", test.s, x, test.x) 322 case x == test.x: 323 case math.IsNaN(test.x) && math.IsNaN(x): 324 } 325 } 326 t.Logf("tested %d random numbers", len(atofRandomTests)) 327} 328 329var roundTripCases = []struct { 330 f float64 331 s string 332}{ 333 // Issue 2917. 334 // This test will break the optimized conversion if the 335 // FPU is using 80-bit registers instead of 64-bit registers, 336 // usually because the operating system initialized the 337 // thread with 80-bit precision and the Go runtime didn't 338 // fix the FP control word. 339 {8865794286000691 << 39, "4.87402195346389e+27"}, 340 {8865794286000692 << 39, "4.8740219534638903e+27"}, 341} 342 343func TestRoundTrip(t *testing.T) { 344 for _, tt := range roundTripCases { 345 old := SetOptimize(false) 346 s := FormatFloat(tt.f, 'g', -1, 64) 347 if s != tt.s { 348 t.Errorf("no-opt FormatFloat(%b) = %s, want %s", tt.f, s, tt.s) 349 } 350 f, err := ParseFloat(tt.s, 64) 351 if f != tt.f || err != nil { 352 t.Errorf("no-opt ParseFloat(%s) = %b, %v want %b, nil", tt.s, f, err, tt.f) 353 } 354 SetOptimize(true) 355 s = FormatFloat(tt.f, 'g', -1, 64) 356 if s != tt.s { 357 t.Errorf("opt FormatFloat(%b) = %s, want %s", tt.f, s, tt.s) 358 } 359 f, err = ParseFloat(tt.s, 64) 360 if f != tt.f || err != nil { 361 t.Errorf("opt ParseFloat(%s) = %b, %v want %b, nil", tt.s, f, err, tt.f) 362 } 363 SetOptimize(old) 364 } 365} 366 367// TestRoundTrip32 tries a fraction of all finite positive float32 values. 368func TestRoundTrip32(t *testing.T) { 369 step := uint32(997) 370 if testing.Short() { 371 step = 99991 372 } 373 count := 0 374 for i := uint32(0); i < 0xff<<23; i += step { 375 f := math.Float32frombits(i) 376 if i&1 == 1 { 377 f = -f // negative 378 } 379 s := FormatFloat(float64(f), 'g', -1, 32) 380 381 parsed, err := ParseFloat(s, 32) 382 parsed32 := float32(parsed) 383 switch { 384 case err != nil: 385 t.Errorf("ParseFloat(%q, 32) gave error %s", s, err) 386 case float64(parsed32) != parsed: 387 t.Errorf("ParseFloat(%q, 32) = %v, not a float32 (nearest is %v)", s, parsed, parsed32) 388 case parsed32 != f: 389 t.Errorf("ParseFloat(%q, 32) = %b (expected %b)", s, parsed32, f) 390 } 391 count++ 392 } 393 t.Logf("tested %d float32's", count) 394} 395 396func BenchmarkAtof64Decimal(b *testing.B) { 397 for i := 0; i < b.N; i++ { 398 ParseFloat("33909", 64) 399 } 400} 401 402func BenchmarkAtof64Float(b *testing.B) { 403 for i := 0; i < b.N; i++ { 404 ParseFloat("339.7784", 64) 405 } 406} 407 408func BenchmarkAtof64FloatExp(b *testing.B) { 409 for i := 0; i < b.N; i++ { 410 ParseFloat("-5.09e75", 64) 411 } 412} 413 414func BenchmarkAtof64Big(b *testing.B) { 415 for i := 0; i < b.N; i++ { 416 ParseFloat("123456789123456789123456789", 64) 417 } 418} 419 420func BenchmarkAtof64RandomBits(b *testing.B) { 421 for i := 0; i < b.N; i++ { 422 ParseFloat(benchmarksRandomBits[i%1024], 64) 423 } 424} 425 426func BenchmarkAtof64RandomFloats(b *testing.B) { 427 for i := 0; i < b.N; i++ { 428 ParseFloat(benchmarksRandomNormal[i%1024], 64) 429 } 430} 431 432func BenchmarkAtof32Decimal(b *testing.B) { 433 for i := 0; i < b.N; i++ { 434 ParseFloat("33909", 32) 435 } 436} 437 438func BenchmarkAtof32Float(b *testing.B) { 439 for i := 0; i < b.N; i++ { 440 ParseFloat("339.778", 32) 441 } 442} 443 444func BenchmarkAtof32FloatExp(b *testing.B) { 445 for i := 0; i < b.N; i++ { 446 ParseFloat("12.3456e32", 32) 447 } 448} 449 450var float32strings [4096]string 451 452func BenchmarkAtof32Random(b *testing.B) { 453 n := uint32(997) 454 for i := range float32strings { 455 n = (99991*n + 42) % (0xff << 23) 456 float32strings[i] = FormatFloat(float64(math.Float32frombits(n)), 'g', -1, 32) 457 } 458 b.ResetTimer() 459 for i := 0; i < b.N; i++ { 460 ParseFloat(float32strings[i%4096], 32) 461 } 462} 463