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.
17 type atofTest
struct {
23 var atoftests
= []atofTest
{
27 {"1x", "0", ErrSyntax
},
28 {"1.1.", "0", ErrSyntax
},
29 {"1e23", "1e+23", nil},
30 {"1E23", "1e+23", nil},
31 {"100000000000000000000000", "1e+23", nil},
32 {"1e-100", "1e-100", nil},
33 {"123456700", "1.234567e+08", nil},
34 {"99999999999999974834176", "9.999999999999997e+22", nil},
35 {"100000000000000000000001", "1.0000000000000001e+23", nil},
36 {"100000000000000008388608", "1.0000000000000001e+23", nil},
37 {"100000000000000016777215", "1.0000000000000001e+23", nil},
38 {"100000000000000016777216", "1.0000000000000003e+23", nil},
40 {"-0.1", "-0.1", nil},
42 {"1e-20", "1e-20", nil},
43 {"625e-3", "0.625", nil},
52 {"-Inf", "-Inf", nil},
53 {"+INF", "+Inf", nil},
54 {"-Infinity", "-Inf", nil},
55 {"+INFINITY", "+Inf", nil},
56 {"Infinity", "+Inf", nil},
59 {"1.7976931348623157e308", "1.7976931348623157e+308", nil},
60 {"-1.7976931348623157e308", "-1.7976931348623157e+308", nil},
61 // next float64 - too large
62 {"1.7976931348623159e308", "+Inf", ErrRange
},
63 {"-1.7976931348623159e308", "-Inf", ErrRange
},
64 // the border is ...158079
66 {"1.7976931348623158e308", "1.7976931348623157e+308", nil},
67 {"-1.7976931348623158e308", "-1.7976931348623157e+308", nil},
68 // borderline - too large
69 {"1.797693134862315808e308", "+Inf", ErrRange
},
70 {"-1.797693134862315808e308", "-Inf", ErrRange
},
73 {"1e308", "1e+308", nil},
74 {"2e308", "+Inf", ErrRange
},
75 {"1e309", "+Inf", ErrRange
},
78 {"1e310", "+Inf", ErrRange
},
79 {"-1e310", "-Inf", ErrRange
},
80 {"1e400", "+Inf", ErrRange
},
81 {"-1e400", "-Inf", ErrRange
},
82 {"1e400000", "+Inf", ErrRange
},
83 {"-1e400000", "-Inf", ErrRange
},
86 {"1e-305", "1e-305", nil},
87 {"1e-306", "1e-306", nil},
88 {"1e-307", "1e-307", nil},
89 {"1e-308", "1e-308", nil},
90 {"1e-309", "1e-309", nil},
91 {"1e-310", "1e-310", nil},
92 {"1e-322", "1e-322", nil},
94 {"5e-324", "5e-324", nil},
95 {"4e-324", "5e-324", nil},
96 {"3e-324", "5e-324", nil},
100 {"1e-350", "0", nil},
101 {"1e-400000", "0", nil},
103 // try to overflow exponent
104 {"1e-4294967296", "0", nil},
105 {"1e+4294967296", "+Inf", ErrRange
},
106 {"1e-18446744073709551616", "0", nil},
107 {"1e+18446744073709551616", "+Inf", ErrRange
},
110 {"1e", "0", ErrSyntax
},
111 {"1e-", "0", ErrSyntax
},
112 {".e-1", "0", ErrSyntax
},
113 {"1\x00.2", "0", ErrSyntax
},
115 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
116 {"2.2250738585072012e-308", "2.2250738585072014e-308", nil},
117 // http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
118 {"2.2250738585072011e-308", "2.225073858507201e-308", nil},
120 // A very large number (initially wrongly parsed by the fast algorithm).
121 {"4.630813248087435e+307", "4.630813248087435e+307", nil},
123 // A different kind of very large number.
124 {"22.222222222222222", "22.22222222222222", nil},
125 {"2." + strings
.Repeat("2", 4000) + "e+1", "22.22222222222222", nil},
127 // Exactly halfway between 1 and math.Nextafter(1, 2).
128 // Round to even (down).
129 {"1.00000000000000011102230246251565404236316680908203125", "1", nil},
130 // Slightly lower; still round down.
131 {"1.00000000000000011102230246251565404236316680908203124", "1", nil},
132 // Slightly higher; round up.
133 {"1.00000000000000011102230246251565404236316680908203126", "1.0000000000000002", nil},
134 // Slightly higher, but you have to read all the way to the end.
135 {"1.00000000000000011102230246251565404236316680908203125" + strings
.Repeat("0", 10000) + "1", "1.0000000000000002", nil},
138 var atof32tests
= []atofTest
{
139 // Exactly halfway between 1 and the next float32.
140 // Round to even (down).
141 {"1.000000059604644775390625", "1", nil},
143 {"1.000000059604644775390624", "1", nil},
145 {"1.000000059604644775390626", "1.0000001", nil},
146 // Slightly higher, but you have to read all the way to the end.
147 {"1.000000059604644775390625" + strings
.Repeat("0", 10000) + "1", "1.0000001", nil},
149 // largest float32: (1<<128) * (1 - 2^-24)
150 {"340282346638528859811704183484516925440", "3.4028235e+38", nil},
151 {"-340282346638528859811704183484516925440", "-3.4028235e+38", nil},
152 // next float32 - too large
153 {"3.4028236e38", "+Inf", ErrRange
},
154 {"-3.4028236e38", "-Inf", ErrRange
},
155 // the border is 3.40282356779...e+38
157 {"3.402823567e38", "3.4028235e+38", nil},
158 {"-3.402823567e38", "-3.4028235e+38", nil},
159 // borderline - too large
160 {"3.4028235678e38", "+Inf", ErrRange
},
161 {"-3.4028235678e38", "-Inf", ErrRange
},
163 // Denormals: less than 2^-126
164 {"1e-38", "1e-38", nil},
165 {"1e-39", "1e-39", nil},
166 {"1e-40", "1e-40", nil},
167 {"1e-41", "1e-41", nil},
168 {"1e-42", "1e-42", nil},
169 {"1e-43", "1e-43", nil},
170 {"1e-44", "1e-44", nil},
171 {"6e-45", "6e-45", nil}, // 4p-149 = 5.6e-45
172 {"5e-45", "6e-45", nil},
174 {"1e-45", "1e-45", nil}, // 1p-149 = 1.4e-45
175 {"2e-45", "1e-45", nil},
177 // 2^92 = 8388608p+69 = 4951760157141521099596496896 (4.9517602e27)
178 // is an exact power of two that needs 8 decimal digits to be correctly
180 // The float32 before is 16777215p+68 = 4.95175986e+27
181 // The halfway is 4.951760009. A bad algorithm that thinks the previous
182 // float32 is 8388607p+69 will shorten incorrectly to 4.95176e+27.
183 {"4951760157141521099596496896", "4.9517602e+27", nil},
186 type atofSimpleTest
struct {
192 atofRandomTests
[]atofSimpleTest
193 benchmarksRandomBits
[1024]string
194 benchmarksRandomNormal
[1024]string
198 // The atof routines return NumErrors wrapping
199 // the error and the string. Convert the table above.
200 for i
:= range atoftests
{
201 test
:= &atoftests
[i
]
203 test
.err
= &NumError
{"ParseFloat", test
.in
, test
.err
}
206 for i
:= range atof32tests
{
207 test
:= &atof32tests
[i
]
209 test
.err
= &NumError
{"ParseFloat", test
.in
, test
.err
}
213 // Generate random inputs for tests and benchmarks
214 rand
.Seed(time
.Now().UnixNano())
216 atofRandomTests
= make([]atofSimpleTest
, 100)
218 atofRandomTests
= make([]atofSimpleTest
, 10000)
220 for i
:= range atofRandomTests
{
221 n
:= uint64(rand
.Uint32())<<32 |
uint64(rand
.Uint32())
222 x
:= math
.Float64frombits(n
)
223 s
:= FormatFloat(x
, 'g', -1, 64)
224 atofRandomTests
[i
] = atofSimpleTest
{x
, s
}
227 for i
:= range benchmarksRandomBits
{
228 bits
:= uint64(rand
.Uint32())<<32 |
uint64(rand
.Uint32())
229 x
:= math
.Float64frombits(bits
)
230 benchmarksRandomBits
[i
] = FormatFloat(x
, 'g', -1, 64)
233 for i
:= range benchmarksRandomNormal
{
234 x
:= rand
.NormFloat64()
235 benchmarksRandomNormal
[i
] = FormatFloat(x
, 'g', -1, 64)
239 func testAtof(t
*testing
.T
, opt
bool) {
240 oldopt
:= SetOptimize(opt
)
241 for i
:= 0; i
< len(atoftests
); i
++ {
242 test
:= &atoftests
[i
]
243 out
, err
:= ParseFloat(test
.in
, 64)
244 outs
:= FormatFloat(out
, 'g', -1, 64)
245 if outs
!= test
.out ||
!reflect
.DeepEqual(err
, test
.err
) {
246 t
.Errorf("ParseFloat(%v, 64) = %v, %v want %v, %v",
247 test
.in
, out
, err
, test
.out
, test
.err
)
250 if float64(float32(out
)) == out
{
251 out
, err
:= ParseFloat(test
.in
, 32)
252 out32
:= float32(out
)
253 if float64(out32
) != out
{
254 t
.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test
.in
, out
, float64(out32
))
257 outs
:= FormatFloat(float64(out32
), 'g', -1, 32)
258 if outs
!= test
.out ||
!reflect
.DeepEqual(err
, test
.err
) {
259 t
.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v # %v",
260 test
.in
, out32
, err
, test
.out
, test
.err
, out
)
264 for _
, test
:= range atof32tests
{
265 out
, err
:= ParseFloat(test
.in
, 32)
266 out32
:= float32(out
)
267 if float64(out32
) != out
{
268 t
.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test
.in
, out
, float64(out32
))
271 outs
:= FormatFloat(float64(out32
), 'g', -1, 32)
272 if outs
!= test
.out ||
!reflect
.DeepEqual(err
, test
.err
) {
273 t
.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v # %v",
274 test
.in
, out32
, err
, test
.out
, test
.err
, out
)
280 func TestAtof(t
*testing
.T
) { testAtof(t
, true) }
282 func TestAtofSlow(t
*testing
.T
) { testAtof(t
, false) }
284 func TestAtofRandom(t
*testing
.T
) {
285 for _
, test
:= range atofRandomTests
{
286 x
, _
:= ParseFloat(test
.s
, 64)
289 t
.Errorf("number %s badly parsed as %b (expected %b)", test
.s
, x
, test
.x
)
291 case math
.IsNaN(test
.x
) && math
.IsNaN(x
):
294 t
.Logf("tested %d random numbers", len(atofRandomTests
))
297 var roundTripCases
= []struct {
302 // This test will break the optimized conversion if the
303 // FPU is using 80-bit registers instead of 64-bit registers,
304 // usually because the operating system initialized the
305 // thread with 80-bit precision and the Go runtime didn't
306 // fix the FP control word.
307 {8865794286000691 << 39, "4.87402195346389e+27"},
308 {8865794286000692 << 39, "4.8740219534638903e+27"},
311 func TestRoundTrip(t
*testing
.T
) {
312 for _
, tt
:= range roundTripCases
{
313 old
:= SetOptimize(false)
314 s
:= FormatFloat(tt
.f
, 'g', -1, 64)
316 t
.Errorf("no-opt FormatFloat(%b) = %s, want %s", tt
.f
, s
, tt
.s
)
318 f
, err
:= ParseFloat(tt
.s
, 64)
319 if f
!= tt
.f || err
!= nil {
320 t
.Errorf("no-opt ParseFloat(%s) = %b, %v want %b, nil", tt
.s
, f
, err
, tt
.f
)
323 s
= FormatFloat(tt
.f
, 'g', -1, 64)
325 t
.Errorf("opt FormatFloat(%b) = %s, want %s", tt
.f
, s
, tt
.s
)
327 f
, err
= ParseFloat(tt
.s
, 64)
328 if f
!= tt
.f || err
!= nil {
329 t
.Errorf("opt ParseFloat(%s) = %b, %v want %b, nil", tt
.s
, f
, err
, tt
.f
)
335 // TestRoundTrip32 tries a fraction of all finite positive float32 values.
336 func TestRoundTrip32(t
*testing
.T
) {
342 for i
:= uint32(0); i
< 0xff<<23; i
+= step
{
343 f
:= math
.Float32frombits(i
)
347 s
:= FormatFloat(float64(f
), 'g', -1, 32)
349 parsed
, err
:= ParseFloat(s
, 32)
350 parsed32
:= float32(parsed
)
353 t
.Errorf("ParseFloat(%q, 32) gave error %s", s
, err
)
354 case float64(parsed32
) != parsed
:
355 t
.Errorf("ParseFloat(%q, 32) = %v, not a float32 (nearest is %v)", s
, parsed
, parsed32
)
357 t
.Errorf("ParseFloat(%q, 32) = %b (expected %b)", s
, parsed32
, f
)
361 t
.Logf("tested %d float32's", count
)
364 func BenchmarkAtof64Decimal(b
*testing
.B
) {
365 for i
:= 0; i
< b
.N
; i
++ {
366 ParseFloat("33909", 64)
370 func BenchmarkAtof64Float(b
*testing
.B
) {
371 for i
:= 0; i
< b
.N
; i
++ {
372 ParseFloat("339.7784", 64)
376 func BenchmarkAtof64FloatExp(b
*testing
.B
) {
377 for i
:= 0; i
< b
.N
; i
++ {
378 ParseFloat("-5.09e75", 64)
382 func BenchmarkAtof64Big(b
*testing
.B
) {
383 for i
:= 0; i
< b
.N
; i
++ {
384 ParseFloat("123456789123456789123456789", 64)
388 func BenchmarkAtof64RandomBits(b
*testing
.B
) {
389 for i
:= 0; i
< b
.N
; i
++ {
390 ParseFloat(benchmarksRandomBits
[i%1024
], 64)
394 func BenchmarkAtof64RandomFloats(b
*testing
.B
) {
395 for i
:= 0; i
< b
.N
; i
++ {
396 ParseFloat(benchmarksRandomNormal
[i%1024
], 64)
400 func BenchmarkAtof32Decimal(b
*testing
.B
) {
401 for i
:= 0; i
< b
.N
; i
++ {
402 ParseFloat("33909", 32)
406 func BenchmarkAtof32Float(b
*testing
.B
) {
407 for i
:= 0; i
< b
.N
; i
++ {
408 ParseFloat("339.778", 32)
412 func BenchmarkAtof32FloatExp(b
*testing
.B
) {
413 for i
:= 0; i
< b
.N
; i
++ {
414 ParseFloat("12.3456e32", 32)
418 var float32strings
[4096]string
420 func BenchmarkAtof32Random(b
*testing
.B
) {
422 for i
:= range float32strings
{
423 n
= (99991*n
+ 42) % (0xff << 23)
424 float32strings
[i
] = FormatFloat(float64(math
.Float32frombits(n
)), 'g', -1, 32)
427 for i
:= 0; i
< b
.N
; i
++ {
428 ParseFloat(float32strings
[i%4096
], 32)